diff --git a/docs/manual/CoreTasks/xmlproperty.html b/docs/manual/CoreTasks/xmlproperty.html index 6fde443c3..545eea676 100644 --- a/docs/manual/CoreTasks/xmlproperty.html +++ b/docs/manual/CoreTasks/xmlproperty.html @@ -85,7 +85,7 @@ is roughly equivalent to the following fragments in a build.xml file: file The XML file to parse. - Yes + Yes, or a nested resource collection. prefix @@ -137,6 +137,11 @@ is roughly equivalent to the following fragments in a build.xml file:

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

+

any resource or single element +resource collection

+ +

The specified resource will be used as input.

+

Examples

diff --git a/src/etc/testcases/taskdefs/xmlproperty.xml b/src/etc/testcases/taskdefs/xmlproperty.xml index 10c260fa3..9ffb66b1e 100644 --- a/src/etc/testcases/taskdefs/xmlproperty.xml +++ b/src/etc/testcases/taskdefs/xmlproperty.xml @@ -8,6 +8,13 @@ + + + + + + + diff --git a/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java b/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java index 2b87d693e..83894edc1 100644 --- a/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java +++ b/src/main/org/apache/tools/ant/taskdefs/XmlProperty.java @@ -26,7 +26,10 @@ 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.Resource; +import org.apache.tools.ant.types.ResourceCollection; import org.apache.tools.ant.types.XMLCatalog; +import org.apache.tools.ant.types.resources.FileResource; import org.apache.tools.ant.util.FileUtils; import org.w3c.dom.Document; import org.w3c.dom.Element; @@ -171,7 +174,7 @@ import org.xml.sax.EntityResolver; public class XmlProperty extends org.apache.tools.ant.Task { - private File src; + private Resource src; private String prefix = ""; private boolean keepRoot = true; private boolean validate = false; @@ -226,22 +229,29 @@ public class XmlProperty extends org.apache.tools.ant.Task { public void execute() throws BuildException { - if (getFile() == null) { - String msg = "XmlProperty task requires a file attribute"; + Resource r = getResource(); + + if (r == null) { + String msg = "XmlProperty task requires a source resource"; throw new BuildException(msg); } try { - log("Loading " + src.getAbsolutePath(), Project.MSG_VERBOSE); + log("Loading " + src, Project.MSG_VERBOSE); - if (src.exists()) { + if (r.isExists()) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validate); factory.setNamespaceAware(false); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(getEntityResolver()); - Document document = builder.parse(src); + 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. @@ -261,7 +271,7 @@ public class XmlProperty extends org.apache.tools.ant.Task { } } else { - log("Unable to find property file: " + src.getAbsolutePath(), + log("Unable to find property resource: " + r, Project.MSG_VERBOSE); } @@ -547,9 +557,36 @@ public class XmlProperty extends org.apache.tools.ant.Task { * @param src the file to parse */ public void setFile(File src) { + setSrcResource(new FileResource(src)); + } + + /** + * The resource to pack; required. + * @param src resource to expand + */ + public void setSrcResource(Resource src) { + 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; } + /** + * Set the source resource. + * @param a the resource to pack as a single element Resource collection. + */ + public void addConfigured(ResourceCollection a) { + if (a.size() != 1) { + throw new BuildException("only single argument resource collections" + + " are supported as archives"); + } + setSrcResource((Resource) a.iterator().next()); + } + /** * the prefix to prepend to each property * @param prefix the prefix to prepend to each property @@ -626,7 +663,25 @@ public class XmlProperty extends org.apache.tools.ant.Task { * @return the file attribute. */ protected File getFile () { - return this.src; + if (src instanceof FileResource) { + return ((FileResource) src).getFile(); + } else { + return null; + } + } + + /** + * @return the resource. + */ + protected 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; + } } /** @@ -689,4 +744,18 @@ public class XmlProperty extends org.apache.tools.ant.Task { return FILE_UTILS.resolveFile(rootDirectory, fileName); } + /** + * Whether this task can deal with non-file resources. + * + *

This implementation returns true only if this task is + * <gzip>. 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.

+ * + * @since Ant 1.7 + */ + protected boolean supportsNonFileResources() { + return getClass().equals(XmlProperty.class); + } } diff --git a/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java b/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java index ba431ecf4..02b1e0041 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/XmlPropertyTest.java @@ -44,8 +44,16 @@ public class XmlPropertyTest extends BuildFileTest { configureProject("src/etc/testcases/taskdefs/xmlproperty.xml"); } - public void testProperties() { - executeTarget("test"); + public void testFile() { + testProperties("test"); + } + + public void testResource() { + testProperties("testResource"); + } + + private void testProperties(String target) { + executeTarget(target); assertEquals("true", getProject().getProperty("root-tag(myattr)")); assertEquals("Text", getProject().getProperty("root-tag.inner-tag")); assertEquals("val",