Class XmlProperty

All Implemented Interfaces:

public class XmlProperty extends Task
Loads property values from a valid XML file, generating the property names from the file's element and attribute names.


   <root-tag myattr="true">
     <inner-tag someattr="val">Text</inner-tag>

this generates the following properties:


The collapseAttributes property of this task can be set to true (the default is false) which will instead result in the following properties (note the difference in names of properties corresponding to XML attributes):


Optionally, to more closely mirror the abilities of the Property task, a selected set of attributes can be treated specially. To enable this behavior, the "semanticAttributes" property of this task must be set to true (it defaults to false). If this attribute is specified, the following attributes take on special meaning (setting this to true implicitly sets collapseAttributes to true as well):

  • value: Identifies a text value for a property.
  • location: Identifies a file location for a property.
  • id: Sets an id for a property
  • refid: Sets a property to the value of another property based upon the provided id
  • pathid: Defines a path rather than a property with the given id.

For example, with keepRoot = false, the following properties file:

   <build folder="build">
     <classes id="build.classes" location="${build.folder}/classes"/>
     <reference refid="build.classes"/>
     <classpath pathid="compile.classpath">
       <pathelement location="${build.classes}"/>
     <classpath pathid="run-time.classpath">
       <path refid="compile.classpath"/>
       <pathelement path="${run-time.jars}"/>

is equivalent to the following entries in a build file:

 <property name="build" location="build"/>
 <property name="build.classes" location="${build.location}/classes"/>
 <property name="build.reference" refid="build.classes"/>

 <property name="run-time.jars" value="*.jar/>

 <classpath id="compile.classpath">
   <pathelement location="${build.classes}"/>

 <classpath id="run-time.classpath">
   <path refid="compile.classpath"/>
   <pathelement path="${run-time.jars}"/>

This task requires the following attributes:

  • file: The name of the file to load.

This task supports the following attributes:

  • prefix: Optionally specify a prefix applied to all properties loaded. Defaults to an empty string.
  • keepRoot: Indicate whether the root xml element is kept as part of property name. Defaults to true.
  • validate: Indicate whether the xml file is validated. Defaults to false.
  • collapseAttributes: Indicate whether attributes are stored in property names with parens or with period delimiters. Defaults to false, meaning properties are stored with parens (i.e., foo(attr)).
  • semanticAttributes: Indicate whether attributes named "location", "value", "refid" and "path" are interpreted as ant properties. Defaults to false.
  • rootDirectory: Indicate the directory to use as the root directory for resolving location properties. Defaults to the directory of the project using the task.
  • includeSemanticAttribute: Indicate whether to include the semantic attribute ("location" or "value") as part of the property name. Defaults to false.
  • Constructor Details

    • XmlProperty

      public XmlProperty()
  • Method Details

    • init

      public void init()
      Initializes the task.
      init in class Task
    • getEntityResolver

      protected EntityResolver getEntityResolver()
      the xmlCatalog as the EntityResolver.
    • execute

      public void execute() throws BuildException
      Run the task.
      execute in class Task
      BuildException - The exception raised during task execution.
      To do:
      validate the source file is valid before opening, print a better error message, add a verbose level log message listing the name of the file being loaded
    • processNode

      public Object processNode(Node node, String prefix, Object container)
      Process the given node, adding any required attributes from this child node alone -- but not processing any children.
      node - the XML Node to parse
      prefix - A string to prepend to any properties that get added by this node.
      container - Optionally, an object that a parent node generated that this node might belong to. For example, this node could be within a node that generated a Path.
      the Object created by this node. Generally, this is either a String if this node resulted in setting an attribute, or a Path.
    • setFile

      public void setFile(File src)
      The XML file to parse; required.
      src - the file to parse
    • setSrcResource

      public void setSrcResource(Resource src)
      The resource to pack; required.
      src - resource to expand
    • addConfigured

      public void addConfigured(ResourceCollection a)
      Set the source resource.
      a - the resource to pack as a single element Resource collection.
    • setPrefix

      public void setPrefix(String prefix)
      the prefix to prepend to each property
      prefix - the prefix to prepend to each property
    • setKeeproot

      public void setKeeproot(boolean keepRoot)
      flag to include the xml root tag as a first value in the property name; optional, default is true
      keepRoot - if true (default), include the xml root tag
    • setValidate

      public void setValidate(boolean validate)
      flag to validate the XML file; optional, default false
      validate - if true validate the XML file, default false
    • setCollapseAttributes

      public void setCollapseAttributes(boolean collapseAttributes)
      flag to treat attributes as nested elements; optional, default false
      collapseAttributes - if true treat attributes as nested elements
    • setSemanticAttributes

      public void setSemanticAttributes(boolean semanticAttributes)
      Attribute to enable special handling of attributes - see ant manual.
      semanticAttributes - if true enable the special handling.
    • setRootDirectory

      public void setRootDirectory(File rootDirectory)
      The directory to use for resolving file references. Ignored if semanticAttributes is not set to true.
      rootDirectory - the directory.
    • setIncludeSemanticAttribute

      public void setIncludeSemanticAttribute(boolean includeSemanticAttribute)
      Include the semantic attribute name as part of the property name. Ignored if semanticAttributes is not set to true.
      includeSemanticAttribute - if true include the semantic attribute name.
    • addConfiguredXMLCatalog

      public void addConfiguredXMLCatalog(XMLCatalog catalog)
      add an XMLCatalog as a nested element; optional.
      catalog - the XMLCatalog to use
    • getFile

      protected File getFile()
      the file attribute.
    • getResource

      protected Resource getResource()
      the resource.
    • getPrefix

      protected String getPrefix()
      the prefix attribute.
    • getKeeproot

      protected boolean getKeeproot()
      the keeproot attribute.
    • getValidate

      protected boolean getValidate()
      the validate attribute.
    • getCollapseAttributes

      protected boolean getCollapseAttributes()
      the collapse attributes attribute.
    • getSemanticAttributes

      protected boolean getSemanticAttributes()
      the semantic attributes attribute.
    • getRootDirectory

      protected File getRootDirectory()
      the root directory attribute.
    • getIncludeSementicAttribute

      @Deprecated protected boolean getIncludeSementicAttribute()
    • getIncludeSemanticAttribute

      protected boolean getIncludeSemanticAttribute()
      the include semantic attribute.
    • supportsNonFileResources

      protected boolean supportsNonFileResources()
      Whether this task can deal with non-file resources.

      This implementation returns true only if this task is <xmlproperty>. Any subclass of this class that also wants to support non-file resources needs to override this method. We need to do so for backwards compatibility reasons since we can't expect subclasses to support resources.

      true for this task.
      Ant 1.7
    • getDelimiter

      public String getDelimiter()
      Get the current delimiter.
    • setDelimiter

      public void setDelimiter(String delimiter)
      Sets a new delimiter.
      delimiter - new value
      Ant 1.7.1