Top Description Fields Constructors Methods
org.apache.tools.ant.taskdefs

public Class XmlProperty

extends Task
Class Inheritance
Imports
java.io.File, .IOException, java.util.Arrays, .Hashtable, .Map, javax.xml.parsers.DocumentBuilder, .DocumentBuilderFactory, .ParserConfigurationException, org.apache.tools.ant.BuildException, .Project, .Task, org.apache.tools.ant.types.Path, .Resource, .ResourceCollection, .XMLCatalog, org.apache.tools.ant.types.resources.FileProvider, .FileResource, org.apache.tools.ant.util.FileUtils, org.w3c.dom.Document, .Element, .NamedNodeMap, .Node, .NodeList, org.xml.sax.EntityResolver, .SAXException

Loads property values from a valid XML file, generating the property names from the file's element and attribute names.

Example:

   <root-tag myattr="true">
     <inner-tag someattr="val">Text</inner-tag>
     <a2><a3><a4>false</a4></a3></a2>
     <x>x1</x>
     <x>x2</x>
   </root-tag>

this generates the following properties:

  root-tag(myattr)=true
  root-tag.inner-tag=Text
  root-tag.inner-tag(someattr)=val
  root-tag.a2.a3.a4=false
  root-tag.x=x1,x2
 

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):

  root-tag.myattr=true
  root-tag.inner-tag=Text
  root-tag.inner-tag.someattr=val
  root-tag.a2.a3.a4=false
  root-tag.x=x1,x2
 

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):

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

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

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>

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

This task requires the following attributes:

This task supports the following attributes:

Field Summary

Modifier and TypeField and Description
private Map<String, String>
private static final String[]
private boolean
private String
private static final FileUtils
private static final String
private boolean
private boolean
private static final String
private static final String
private static final String
private String
private static final String
private File
private boolean
private Resource
private boolean
private static final String
private XMLCatalog
Inherited from org.apache.tools.ant.Task:
targettaskNametaskTypewrapper

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public void
addConfigured(ResourceCollection
the resource to pack as a single element Resource collection.
a
)

Set the source resource.

public void
addConfiguredXMLCatalog(XMLCatalog
the XMLCatalog to use
catalog
)

add an XMLCatalog as a nested element; optional.

private void
addNodeRecursively(Node node, String prefix, Object container)

Iterate through all nodes in the tree.

pack-priv void
private void
addProperty(String name, String value, String id)

Actually add the given property/value to the project after writing a log message.

public void
execute()

Overrides org.apache.tools.ant.Task.execute.

Run the task.
private String
getAttributeName(Node attributeNode)

Return a reasonable attribute name for the given node.

private String
getAttributeValue(Node attributeNode)

Return the value for the given attribute.

protected boolean

Returns:

the collapse attributes attribute.
getCollapseAttributes
()

public String

Returns:

delimiter
getDelimiter
()

Get the current delimiter.

protected EntityResolver

Returns:

the xmlCatalog as the EntityResolver.
getEntityResolver
()

protected File

Returns:

the file attribute.
getFile
()

protected boolean

Returns:

the include semantic attribute.
getIncludeSemanticAttribute
()

protected boolean
protected boolean

Returns:

the keeproot attribute.
getKeeproot
()

protected String

Returns:

the prefix attribute.
getPrefix
()

protected Resource

Returns:

the resource.
getResource
()

protected File

Returns:

the root directory attribute.
getRootDirectory
()

protected boolean

Returns:

the semantic attributes attribute.
getSemanticAttributes
()

protected boolean

Returns:

the validate attribute.
getValidate
()

public void
init()

Overrides org.apache.tools.ant.Task.init.

Initializes the task.
private static boolean
isSemanticAttribute(String attributeName)

Return whether the provided attribute name is recognized or not.

public Object

Returns:

the Object created by this node. Generally, this is either a String if this node resulted in setting an attribute, or a Path.
processNode
(Node
the XML Node to parse
node
,
String
A string to prepend to any properties that get added by this node.
prefix
,
Object
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.
container
)

Process the given node, adding any required attributes from this child node alone -- but not processing any children.

private File
resolveFile(String fileName)

Let project resolve the file - or do it ourselves if rootDirectory has been set.

public void
setCollapseAttributes(boolean
if true treat attributes as nested elements
collapseAttributes
)

flag to treat attributes as nested elements; optional, default false

public void
setDelimiter(String
new value
delimiter
)

Sets a new delimiter.

public void
setFile(File
the file to parse
src
)

The XML file to parse; required.

public void
setIncludeSemanticAttribute(boolean
if true include the semantic attribute name.
includeSemanticAttribute
)

Include the semantic attribute name as part of the property name.

public void
setKeeproot(boolean
if true (default), include the xml root tag
keepRoot
)

flag to include the xml root tag as a first value in the property name; optional, default is true

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

the prefix to prepend to each property

public void
setRootDirectory(File
the directory.
rootDirectory
)

The directory to use for resolving file references.

public void
setSemanticAttributes(boolean
if true enable the special handling.
semanticAttributes
)

Attribute to enable special handling of attributes - see ant manual.

public void
setSrcResource(Resource
resource to expand
src
)

The resource to pack; required.

public void
setValidate(boolean
if true validate the XML file, default false
validate
)

flag to validate the XML file; optional, default false

protected boolean

Returns:

true for this task.
supportsNonFileResources
()

Whether this task can deal with non-file resources.

Inherited from org.apache.tools.ant.Task:
bindToOwnergetOwningTargetgetRuntimeConfigurableWrappergetTaskNamegetTaskTypegetWrapperhandleErrorFlushhandleErrorOutputhandleFlushhandleInputhandleOutputisInvalidloglogloglogmaybeConfigureperformreconfiguresetOwningTargetsetRuntimeConfigurableWrappersetTaskNamesetTaskType

Field Detail

addedAttributesback to summary
private Map<String, String> addedAttributes
ATTRIBUTESback to summary
private static final String[] ATTRIBUTES
collapseAttributesback to summary
private boolean collapseAttributes
delimiterback to summary
private String delimiter
FILE_UTILSback to summary
private static final FileUtils FILE_UTILS
IDback to summary
private static final String ID
includeSemanticAttributeback to summary
private boolean includeSemanticAttribute
keepRootback to summary
private boolean keepRoot
LOCATIONback to summary
private static final String LOCATION
PATHback to summary
private static final String PATH
PATHIDback to summary
private static final String PATHID
prefixback to summary
private String prefix
REF_IDback to summary
private static final String REF_ID
rootDirectoryback to summary
private File rootDirectory
semanticAttributesback to summary
private boolean semanticAttributes
srcback to summary
private Resource src
validateback to summary
private boolean validate
VALUEback to summary
private static final String VALUE
xmlCatalogback to summary
private XMLCatalog xmlCatalog

Constructor Detail

XmlPropertyback to summary
public XmlProperty()

Method Detail

addConfiguredback to summary
public void addConfigured(ResourceCollection a)

Set the source resource.

Parameters
a:ResourceCollection

the resource to pack as a single element Resource collection.

addConfiguredXMLCatalogback to summary
public void addConfiguredXMLCatalog(XMLCatalog catalog)

add an XMLCatalog as a nested element; optional.

Parameters
catalog:XMLCatalog

the XMLCatalog to use

addNodeRecursivelyback to summary
private void addNodeRecursively(Node node, String prefix, Object container)

Iterate through all nodes in the tree.

addNodeRecursivelyback to summary
pack-priv void addNodeRecursively(Node node, String prefix)
addPropertyback to summary
private void addProperty(String name, String value, String id)

Actually add the given property/value to the project after writing a log message.

executeback to summary
public void execute() throws BuildException

Overrides org.apache.tools.ant.Task.execute.

Run the task.

Annotations
@Override
Exceptions
BuildException:
The exception raised during task execution.
getAttributeNameback to summary
private String getAttributeName(Node attributeNode)

Return a reasonable attribute name for the given node. If we are using semantic attributes or collapsing attributes, the returned name is ".nodename". Otherwise, we return "(nodename)". This is long-standing (and default) <xmlproperty> behavior.

getAttributeValueback to summary
private String getAttributeValue(Node attributeNode)

Return the value for the given attribute. If we are not using semantic attributes, its just the literal string value of the attribute.

If we are using semantic attributes, then first dependent properties are resolved (i.e., ${foo} is resolved based on the foo property value), and then an appropriate data type is used. In particular, location-based properties are resolved to absolute file names. Also for refid values, look up the referenced object from the project.

getCollapseAttributesback to summary
protected boolean getCollapseAttributes()
Returns:boolean

the collapse attributes attribute.

getDelimiterback to summary
public String getDelimiter()

Get the current delimiter.

Returns:String

delimiter

getEntityResolverback to summary
protected EntityResolver getEntityResolver()
Returns:EntityResolver

the xmlCatalog as the EntityResolver.

getFileback to summary
protected File getFile()
Returns:File

the file attribute.

getIncludeSemanticAttributeback to summary
protected boolean getIncludeSemanticAttribute()
Returns:boolean

the include semantic attribute.

getIncludeSementicAttributeback to summary
protected boolean getIncludeSementicAttribute()

Deprecated

Annotations
@Deprecated
getKeeprootback to summary
protected boolean getKeeproot()
Returns:boolean

the keeproot attribute.

getPrefixback to summary
protected String getPrefix()
Returns:String

the prefix attribute.

getResourceback to summary
protected Resource getResource()
Returns:Resource

the resource.

getRootDirectoryback to summary
protected File getRootDirectory()
Returns:File

the root directory attribute.

getSemanticAttributesback to summary
protected boolean getSemanticAttributes()
Returns:boolean

the semantic attributes attribute.

getValidateback to summary
protected boolean getValidate()
Returns:boolean

the validate attribute.

initback to summary
public void init()

Overrides org.apache.tools.ant.Task.init.

Initializes the task.

Annotations
@Override
isSemanticAttributeback to summary
private static boolean isSemanticAttribute(String attributeName)

Return whether the provided attribute name is recognized or not.

processNodeback to summary
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.

Parameters
node:Node

the XML Node to parse

prefix:String

A string to prepend to any properties that get added by this node.

container:Object

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.

Returns:Object

the Object created by this node. Generally, this is either a String if this node resulted in setting an attribute, or a Path.

resolveFileback to summary
private File resolveFile(String fileName)

Let project resolve the file - or do it ourselves if rootDirectory has been set.

setCollapseAttributesback to summary
public void setCollapseAttributes(boolean collapseAttributes)

flag to treat attributes as nested elements; optional, default false

Parameters
collapseAttributes:boolean

if true treat attributes as nested elements

setDelimiterback to summary
public void setDelimiter(String delimiter)

Sets a new delimiter.

Parameters
delimiter:String

new value

Since
Ant 1.7.1
setFileback to summary
public void setFile(File src)

The XML file to parse; required.

Parameters
src:File

the file to parse

setIncludeSemanticAttributeback to summary
public void setIncludeSemanticAttribute(boolean includeSemanticAttribute)

Include the semantic attribute name as part of the property name. Ignored if semanticAttributes is not set to true.

Parameters
includeSemanticAttribute:boolean

if true include the semantic attribute name.

setKeeprootback to summary
public void setKeeproot(boolean keepRoot)

flag to include the xml root tag as a first value in the property name; optional, default is true

Parameters
keepRoot:boolean

if true (default), include the xml root tag

setPrefixback to summary
public void setPrefix(String prefix)

the prefix to prepend to each property

Parameters
prefix:String

the prefix to prepend to each property

setRootDirectoryback to summary
public void setRootDirectory(File rootDirectory)

The directory to use for resolving file references. Ignored if semanticAttributes is not set to true.

Parameters
rootDirectory:File

the directory.

setSemanticAttributesback to summary
public void setSemanticAttributes(boolean semanticAttributes)

Attribute to enable special handling of attributes - see ant manual.

Parameters
semanticAttributes:boolean

if true enable the special handling.

setSrcResourceback to summary
public void setSrcResource(Resource src)

The resource to pack; required.

Parameters
src:Resource

resource to expand

setValidateback to summary
public void setValidate(boolean validate)

flag to validate the XML file; optional, default false

Parameters
validate:boolean

if true validate the XML file, default false

supportsNonFileResourcesback to summary
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.

Returns:boolean

true for this task.

Since
Ant 1.7