From 1ddab2c141c45161d8006a705ed52100f9f318f9 Mon Sep 17 00:00:00 2001 From: Peter Reilly Date: Fri, 10 Sep 2004 08:50:25 +0000 Subject: [PATCH] add xmlcatalog nested element to the xmlproperty task PR: 27053 Obtained from: David Crossley and Dave Brondsema git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@276840 13f79535-47bb-0310-9956-ffa450edef68 --- CONTRIBUTORS | 1 + WHATSNEW | 2 + docs/manual/CoreTasks/xmlproperty.html | 7 +- src/etc/testcases/taskdefs/skinconfig.dtd | 3 + src/etc/testcases/taskdefs/xmlproperty.xml | 10 +++ .../taskdefs/xmlproperty_needscat.xml | 6 ++ .../tools/ant/taskdefs/XmlProperty.java | 70 +++++++++++++++++-- .../tools/ant/taskdefs/XmlPropertyTest.java | 5 ++ 8 files changed, 99 insertions(+), 5 deletions(-) create mode 100644 src/etc/testcases/taskdefs/skinconfig.dtd create mode 100644 src/etc/testcases/taskdefs/xmlproperty_needscat.xml diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 813f79026..55a96589e 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -39,6 +39,7 @@ Danno Ferrin Davanum Srinivas Dave Brondsema David A. Herman +David Crossley David Kavanagh David Maclean David Rees diff --git a/WHATSNEW b/WHATSNEW index 57a3ba43f..5a18291c0 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -69,6 +69,8 @@ Other changes: * Allow file attribute of to rename a directory. Bugzilla Report 22863. +* Add xmlcatalog nested element to XmlProperty. Bugzilla report 27053. + Fixed bugs: ----------- diff --git a/docs/manual/CoreTasks/xmlproperty.html b/docs/manual/CoreTasks/xmlproperty.html index 7d2cf01cb..618902c8e 100644 --- a/docs/manual/CoreTasks/xmlproperty.html +++ b/docs/manual/CoreTasks/xmlproperty.html @@ -131,6 +131,11 @@ is roughly equivalent to the following fragments in a build.xml file: +

Nested Elements

+

xmlcatalog

+

The <xmlcatalog> +element is used to perform entity resolution.

+

Examples

@@ -257,4 +262,4 @@ is equivalent to the following entries in a build file: Reserved.

- \ No newline at end of file + diff --git a/src/etc/testcases/taskdefs/skinconfig.dtd b/src/etc/testcases/taskdefs/skinconfig.dtd new file mode 100644 index 000000000..86fa9ddd8 --- /dev/null +++ b/src/etc/testcases/taskdefs/skinconfig.dtd @@ -0,0 +1,3 @@ + + + diff --git a/src/etc/testcases/taskdefs/xmlproperty.xml b/src/etc/testcases/taskdefs/xmlproperty.xml index 6436a73a5..10c260fa3 100644 --- a/src/etc/testcases/taskdefs/xmlproperty.xml +++ b/src/etc/testcases/taskdefs/xmlproperty.xml @@ -7,4 +7,14 @@ + + + + + + + + + diff --git a/src/etc/testcases/taskdefs/xmlproperty_needscat.xml b/src/etc/testcases/taskdefs/xmlproperty_needscat.xml new file mode 100644 index 000000000..0ca2b3e8f --- /dev/null +++ b/src/etc/testcases/taskdefs/xmlproperty_needscat.xml @@ -0,0 +1,6 @@ + + + + true + false + diff --git a/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java b/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java index c7107b4e4..1d9f959c6 100644 --- a/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java +++ b/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java @@ -21,10 +21,12 @@ import java.io.File; import java.io.IOException; import java.util.Hashtable; import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.ParserConfigurationException; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.types.Path; +import org.apache.tools.ant.types.XMLCatalog; import org.apache.tools.ant.util.FileUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -32,6 +34,7 @@ import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; +import org.xml.sax.EntityResolver; /** * Loads property values from a valid XML file, generating the @@ -178,6 +181,7 @@ public class XmlProperty extends org.apache.tools.ant.Task { private File rootDirectory = null; private FileUtils fileUtils = FileUtils.newFileUtils(); private Hashtable addedAttributes = new Hashtable(); + private XMLCatalog xmlCatalog = new XMLCatalog(); private static final String ID = "id"; private static final String REF_ID = "refid"; @@ -202,6 +206,15 @@ public class XmlProperty extends org.apache.tools.ant.Task { public void init() { super.init(); + xmlCatalog.setProject(getProject()); + } + + + /** + * @return the xmlCatalog as the entityresolver. + */ + protected EntityResolver getEntityResolver() { + return xmlCatalog; } /** @@ -226,7 +239,9 @@ public class XmlProperty extends org.apache.tools.ant.Task { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validate); factory.setNamespaceAware(false); - Document document = factory.newDocumentBuilder().parse(src); + DocumentBuilder builder = factory.newDocumentBuilder(); + builder.setEntityResolver(getEntityResolver()); + Document document = builder.parse(src); Element topElement = document.getDocumentElement(); // Keep a hashtable of attributes added by this task. @@ -571,48 +586,95 @@ public class XmlProperty extends org.apache.tools.ant.Task { this.collapseAttributes = collapseAttributes; } - public void setSemanticAttributes (boolean semanticAttributes) { + /** + * Attribute to enable special handling of attributes - see ant manual. + * @param semanticAttributes if true enable the special handling. + */ + public void setSemanticAttributes(boolean semanticAttributes) { this.semanticAttributes = semanticAttributes; } - public void setRootDirectory (File rootDirectory) { + /** + * The directory to use for resolving file references. + * Ignored if semanticAttributes is not set to true. + * @param rootDirectory the directory. + */ + public void setRootDirectory(File rootDirectory) { this.rootDirectory = rootDirectory; } - public void setIncludeSemanticAttribute (boolean includeSemanticAttribute) { + /** + * Include the semantic attribute name as part of the property name. + * Ignored if semanticAttributes is not set to true. + * @param includeSemanticAttribute if true include the sematic attribute + * name. + */ + public void setIncludeSemanticAttribute(boolean includeSemanticAttribute) { this.includeSemanticAttribute = includeSemanticAttribute; } + /** + * add an XMLCatalog as a nested element; optional. + * @param catalog the XMLCatalog to use + */ + public void addConfiguredXMLCatalog(XMLCatalog catalog) { + xmlCatalog.addConfiguredXMLCatalog(catalog); + } + /* Expose members for extensibility */ + /** + * @return the file attribute. + */ protected File getFile () { return this.src; } + /** + * @return the prefix attribute. + */ protected String getPrefix () { return this.prefix; } + /** + * @return the keeproot attribute. + */ protected boolean getKeeproot () { return this.keepRoot; } + /** + * @return the validate attribute. + */ protected boolean getValidate () { return this.validate; } + /** + * @return the collapse attributes attribute. + */ protected boolean getCollapseAttributes () { return this.collapseAttributes; } + /** + * @return the semantic attributes attribute. + */ protected boolean getSemanticAttributes () { return this.semanticAttributes; } + /** + * @return the root directory attribute. + */ protected File getRootDirectory () { return this.rootDirectory; } + /** + * @return the include semantic attribute. + */ protected boolean getIncludeSementicAttribute () { return this.includeSemanticAttribute; } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java b/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java index 5145e8ef8..942a48c1a 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java @@ -104,6 +104,11 @@ public class XmlPropertyTest extends BuildFileTest { doTest("testSemanticInclude", false, false, true, false, true); } + public void testNeedsCatalog() { + executeTarget("testneedscat"); + assertEquals("true", getProject().getProperty("skinconfig.foo")); + } + /** * Actually run a test, finding all input files (and corresponding * goldfile)