Methods Summary |
---|
public void | addConfigured(org.apache.tools.ant.types.ResourceCollection a)Set the source resource.
if (a.size() != 1) {
throw new BuildException("only single argument resource collections"
+ " are supported as archives");
}
setSrcResource((Resource) a.iterator().next());
|
public void | addConfiguredXMLCatalog(org.apache.tools.ant.types.XMLCatalog catalog)add an XMLCatalog as a nested element; optional.
xmlCatalog.addConfiguredXMLCatalog(catalog);
|
private void | addNodeRecursively(org.w3c.dom.Node node, java.lang.String prefix, java.lang.Object container)Iterate through all nodes in the tree.
// Set the prefix for this node to include its tag name.
String nodePrefix = prefix;
if (node.getNodeType() != Node.TEXT_NODE) {
if (prefix.trim().length() > 0) {
nodePrefix += ".";
}
nodePrefix += node.getNodeName();
}
// Pass the container to the processing of this node,
Object nodeObject = processNode(node, nodePrefix, container);
// now, iterate through children.
if (node.hasChildNodes()) {
NodeList nodeChildren = node.getChildNodes();
int numChildren = nodeChildren.getLength();
for (int i = 0; i < numChildren; i++) {
// For each child, pass the object added by
// processNode to its children -- in other word, each
// object can pass information along to its children.
addNodeRecursively(nodeChildren.item(i), nodePrefix,
nodeObject);
}
}
|
void | addNodeRecursively(org.w3c.dom.Node node, java.lang.String prefix)
addNodeRecursively(node, prefix, null);
|
private void | addProperty(java.lang.String name, java.lang.String value, java.lang.String id)Actually add the given property/value to the project
after writing a log message.
String msg = name + ":" + value;
if (id != null) {
msg += ("(id=" + id + ")");
}
log(msg, Project.MSG_DEBUG);
if (addedAttributes.containsKey(name)) {
// If this attribute was added by this task, then
// we append this value to the existing value.
// We use the setProperty method which will
// forcibly override the property if it already exists.
// We need to put these properties into the project
// when we read them, though (instead of keeping them
// outside of the project and batch adding them at the end)
// to allow other properties to reference them.
value = (String) addedAttributes.get(name) + "," + value;
getProject().setProperty(name, value);
addedAttributes.put(name, value);
} else if (getProject().getProperty(name) == null) {
getProject().setNewProperty(name, value);
addedAttributes.put(name, value);
} else {
log("Override ignored for property " + name, Project.MSG_VERBOSE);
}
if (id != null) {
getProject().addReference(id, value);
}
|
public void | execute()Run the task.
Resource r = getResource();
if (r == null) {
String msg = "XmlProperty task requires a source resource";
throw new BuildException(msg);
}
try {
log("Loading " + src, Project.MSG_VERBOSE);
if (r.isExists()) {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validate);
factory.setNamespaceAware(false);
DocumentBuilder builder = factory.newDocumentBuilder();
builder.setEntityResolver(getEntityResolver());
Document document = null;
if (src instanceof FileResource) {
document = builder.parse(((FileResource) src).getFile());
} else {
document = builder.parse(src.getInputStream());
}
Element topElement = document.getDocumentElement();
// Keep a hashtable of attributes added by this task.
// This task is allow to override its own properties
// but not other properties. So we need to keep track
// of which properties we've added.
addedAttributes = new Hashtable();
if (keepRoot) {
addNodeRecursively(topElement, prefix, null);
} else {
NodeList topChildren = topElement.getChildNodes();
int numChildren = topChildren.getLength();
for (int i = 0; i < numChildren; i++) {
addNodeRecursively(topChildren.item(i), prefix, null);
}
}
} else {
log("Unable to find property resource: " + r,
Project.MSG_VERBOSE);
}
} catch (SAXException sxe) {
// Error generated during parsing
Exception x = sxe;
if (sxe.getException() != null) {
x = sxe.getException();
}
throw new BuildException("Failed to load " + src, x);
} catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
throw new BuildException(pce);
} catch (IOException ioe) {
// I/O error
throw new BuildException("Failed to load " + src, ioe);
}
|
private java.lang.String | getAttributeName(org.w3c.dom.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.
String attributeName = attributeNode.getNodeName();
if (semanticAttributes) {
// Never include the "refid" attribute as part of the
// attribute name.
if (attributeName.equals(REF_ID)) {
return "";
// Otherwise, return it appended unless property to hide it is set.
} else if (!isSemanticAttribute(attributeName)
|| includeSemanticAttribute) {
return "." + attributeName;
} else {
return "";
}
} else if (collapseAttributes) {
return "." + attributeName;
} else {
return "(" + attributeName + ")";
}
|
private java.lang.String | getAttributeValue(org.w3c.dom.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.
String nodeValue = attributeNode.getNodeValue().trim();
if (semanticAttributes) {
String attributeName = attributeNode.getNodeName();
nodeValue = getProject().replaceProperties(nodeValue);
if (attributeName.equals(LOCATION)) {
File f = resolveFile(nodeValue);
return f.getPath();
} else if (attributeName.equals(REF_ID)) {
Object ref = getProject().getReference(nodeValue);
if (ref != null) {
return ref.toString();
}
}
}
return nodeValue;
|
protected boolean | getCollapseAttributes()
return this.collapseAttributes;
|
protected org.xml.sax.EntityResolver | getEntityResolver()
return xmlCatalog;
|
protected java.io.File | getFile()
if (src instanceof FileResource) {
return ((FileResource) src).getFile();
} else {
return null;
}
|
protected boolean | getIncludeSementicAttribute()
return this.includeSemanticAttribute;
|
protected boolean | getKeeproot()
return this.keepRoot;
|
protected java.lang.String | getPrefix()
return this.prefix;
|
protected org.apache.tools.ant.types.Resource | getResource()
// delegate this way around to support subclasses that
// overwrite getFile
File f = getFile();
if (f != null) {
return new FileResource(f);
} else {
return src;
}
|
protected java.io.File | getRootDirectory()
return this.rootDirectory;
|
protected boolean | getSemanticAttributes()
return this.semanticAttributes;
|
protected boolean | getValidate()
return this.validate;
|
public void | init()Initializes the task.
super.init();
xmlCatalog.setProject(getProject());
|
private static boolean | isSemanticAttribute(java.lang.String attributeName)Return whether the provided attribute name is recognized or not.
for (int i = 0; i < ATTRIBUTES.length; i++) {
if (attributeName.equals(ATTRIBUTES[i])) {
return true;
}
}
return false;
|
public java.lang.Object | processNode(org.w3c.dom.Node node, java.lang.String prefix, java.lang.Object container)Process the given node, adding any required attributes from
this child node alone -- but not processing any
children.
// Parse the attribute(s) and text of this node, adding
// properties for each.
// if the "path" attribute is specified, then return the created path
// which will be passed to the children of this node.
Object addedPath = null;
// The value of an id attribute of this node.
String id = null;
if (node.hasAttributes()) {
NamedNodeMap nodeAttributes = node.getAttributes();
// Is there an id attribute?
Node idNode = nodeAttributes.getNamedItem(ID);
id = (semanticAttributes && idNode != null
? idNode.getNodeValue() : null);
// Now, iterate through the attributes adding them.
for (int i = 0; i < nodeAttributes.getLength(); i++) {
Node attributeNode = nodeAttributes.item(i);
if (!semanticAttributes) {
String attributeName = getAttributeName(attributeNode);
String attributeValue = getAttributeValue(attributeNode);
addProperty(prefix + attributeName, attributeValue, null);
} else {
String nodeName = attributeNode.getNodeName();
String attributeValue = getAttributeValue(attributeNode);
Path containingPath = (container != null
&& container instanceof Path ? (Path) container : null);
/*
* The main conditional logic -- if the attribute
* is somehow "special" (i.e., it has known
* semantic meaning) then deal with it
* appropriately.
*/
if (nodeName.equals(ID)) {
// ID has already been found above.
continue;
} else if (containingPath != null
&& nodeName.equals(PATH)) {
// A "path" attribute for a node within a Path object.
containingPath.setPath(attributeValue);
} else if (container instanceof Path
&& nodeName.equals(REF_ID)) {
// A "refid" attribute for a node within a Path object.
containingPath.setPath(attributeValue);
} else if (container instanceof Path
&& nodeName.equals(LOCATION)) {
// A "location" attribute for a node within a
// Path object.
containingPath.setLocation(resolveFile(attributeValue));
} else if (nodeName.equals(PATHID)) {
// A node identifying a new path
if (container != null) {
throw new BuildException("XmlProperty does not "
+ "support nested paths");
}
addedPath = new Path(getProject());
getProject().addReference(attributeValue, addedPath);
} else {
// An arbitrary attribute.
String attributeName = getAttributeName(attributeNode);
addProperty(prefix + attributeName, attributeValue, id);
}
}
}
}
String nodeText = null;
boolean emptyNode = false;
boolean semanticEmptyOverride = false;
if (node.getNodeType() == Node.ELEMENT_NODE
&& semanticAttributes
&& node.hasAttributes()
&& (node.getAttributes().getNamedItem(VALUE) != null
|| node.getAttributes().getNamedItem(LOCATION) != null
|| node.getAttributes().getNamedItem(REF_ID) != null
|| node.getAttributes().getNamedItem(PATH) != null
|| node.getAttributes().getNamedItem(PATHID) != null)) {
semanticEmptyOverride = true;
}
if (node.getNodeType() == Node.TEXT_NODE) {
// For the text node, add a property.
nodeText = getAttributeValue(node);
} else if ((node.getNodeType() == Node.ELEMENT_NODE)
&& (node.getChildNodes().getLength() == 1)
&& (node.getFirstChild().getNodeType() == Node.CDATA_SECTION_NODE)) {
nodeText = node.getFirstChild().getNodeValue();
if ("".equals(nodeText) && !semanticEmptyOverride) {
emptyNode = true;
}
} else if ((node.getNodeType() == Node.ELEMENT_NODE)
&& (node.getChildNodes().getLength() == 0)
&& !semanticEmptyOverride) {
nodeText = "";
emptyNode = true;
} else if ((node.getNodeType() == Node.ELEMENT_NODE)
&& (node.getChildNodes().getLength() == 1)
&& (node.getFirstChild().getNodeType() == Node.TEXT_NODE)
&& ("".equals(node.getFirstChild().getNodeValue()))
&& !semanticEmptyOverride) {
nodeText = "";
emptyNode = true;
}
if (nodeText != null) {
// If the containing object was a String, then use it as the ID.
if (semanticAttributes && id == null
&& container instanceof String) {
id = (String) container;
}
if (nodeText.trim().length() != 0 || emptyNode) {
addProperty(prefix, nodeText, id);
}
}
// Return the Path we added or the ID of this node for
// children to reference if needed. Path objects are
// definitely used by child path elements, and ID may be used
// for a child text node.
return (addedPath != null ? addedPath : id);
|
private java.io.File | resolveFile(java.lang.String fileName)Let project resolve the file - or do it ourselves if
rootDirectory has been set.
if (rootDirectory == null) {
return FILE_UTILS.resolveFile(getProject().getBaseDir(), fileName);
}
return FILE_UTILS.resolveFile(rootDirectory, fileName);
|
public void | setCollapseAttributes(boolean collapseAttributes)flag to treat attributes as nested elements;
optional, default false
this.collapseAttributes = collapseAttributes;
|
public void | setFile(java.io.File src)The XML file to parse; required.
setSrcResource(new FileResource(src));
|
public void | setIncludeSemanticAttribute(boolean includeSemanticAttribute)Include the semantic attribute name as part of the property name.
Ignored if semanticAttributes is not set to true.
this.includeSemanticAttribute = includeSemanticAttribute;
|
public void | setKeeproot(boolean keepRoot)flag to include the xml root tag as a
first value in the property name; optional,
default is true
this.keepRoot = keepRoot;
|
public void | setPrefix(java.lang.String prefix)the prefix to prepend to each property
this.prefix = prefix.trim();
|
public void | setRootDirectory(java.io.File rootDirectory)The directory to use for resolving file references.
Ignored if semanticAttributes is not set to true.
this.rootDirectory = rootDirectory;
|
public void | setSemanticAttributes(boolean semanticAttributes)Attribute to enable special handling of attributes - see ant manual.
this.semanticAttributes = semanticAttributes;
|
public void | setSrcResource(org.apache.tools.ant.types.Resource src)The resource to pack; required.
if (src.isDirectory()) {
throw new BuildException("the source can't be a directory");
}
if (src instanceof FileResource && !supportsNonFileResources()) {
throw new BuildException("Only FileSystem resources are"
+ " supported.");
}
this.src = src;
|
public void | setValidate(boolean validate)flag to validate the XML file; optional, default false
this.validate = validate;
|
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.
return getClass().equals(XmlProperty.class);
|