3

Within the following document:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="toc">section</string>
<string name="id">id17</string>
</resources>

How do I return the value: id17

When I run the following target in my Ant file:

  <target name="print"
    description="print the contents of the config.xml file in various ways" >

  <xmlproperty file="$config.xml" prefix="build"/>
  <echo message="name = ${build.resources.string}"/>
  </target>

I get -

print:
        [echo] name = section,id17

Is there a way to specify that I only want the resource "id"?

1 Answer 1

4

I had a good news and a bad news for you. A bad news is that there is not out-of-the-box solution. The good news is that xmlproperty task is quite extendable thanks for exposing processNode() method as protected. Here's what you can do:

1. Create and compile with ant.jar (you can find one in lib subdirectory in your ant distribution or get it from Maven) on classpath the following code:

package pl.sobczyk.piotr;

import org.apache.tools.ant.taskdefs.XmlProperty;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class MyXmlProp extends XmlProperty{

@Override
public Object processNode(Node node, String prefix, Object container) {
    if(node.hasAttributes()){
        NamedNodeMap nodeAttributes = node.getAttributes();
        Node nameNode = nodeAttributes.getNamedItem("name");
        if(nameNode != null){
           String name = nameNode.getNodeValue();

           String value = node.getTextContent();
           if(!value.trim().isEmpty()){
               String propName = prefix + "[" + name + "]";
               getProject().setProperty(propName, value);
           }
       }
    }

    return super.processNode(node, prefix, container);
}

}

2. Now you only need to make this task visible to ant. The simpliest way: create task subdirectory in directory where you have your ant script -> copy compiled MyXmlProp class with it's directory structure to task directory so you should end up with something like: task/pl/sobczyk/peter/MyXmlProp.class.

3. Import task to your ant script, you should end up with something like:

<target name="print">
  <taskdef name="myxmlproperty" classname="pl.sobczyk.piotr.MyXmlProp">
    <classpath>
      <pathelement location="task"/>
    </classpath>
  </taskdef>
  <myxmlproperty file="config.xml" prefix="build"/>
  <echo message="name = ${build.resources.string[id]}"/>
</target>

4. Run ant, ant voila, you should see: [echo] name = id17

What we did here is defining a special fancy square brackets syntax for your specific case :-). For some more general solution task extension may be a little more complex, but everything is possible :). Good luck.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for the detailed response. I'll post back results.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.