Author: jhm Date: Wed Jul 25 03:12:58 2007 New Revision: 559386 URL: http://svn.apache.org/viewvc?view=rev&rev=559386 Log: <property> now supports xml-based property definition. Bug 42946.
Modified: ant/core/trunk/WHATSNEW ant/core/trunk/docs/manual/CoreTasks/property.html ant/core/trunk/src/etc/testcases/taskdefs/property.xml ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java Modified: ant/core/trunk/WHATSNEW URL: http://svn.apache.org/viewvc/ant/core/trunk/WHATSNEW?view=diff&rev=559386&r1=559385&r2=559386 ============================================================================== --- ant/core/trunk/WHATSNEW (original) +++ ant/core/trunk/WHATSNEW Wed Jul 25 03:12:58 2007 @@ -127,6 +127,10 @@ * Regression: Path subclasses that overrode list() stopped working in resourceCollection contexts in Ant 1.7.0. Bugzilla 42967. +* <property> supports loading from xml based property definition. + Bugzilla 42946 + + Other changes: -------------- * <script> now has basic support for JavaFX scripts Modified: ant/core/trunk/docs/manual/CoreTasks/property.html URL: http://svn.apache.org/viewvc/ant/core/trunk/docs/manual/CoreTasks/property.html?view=diff&rev=559386&r1=559385&r2=559386 ============================================================================== --- ant/core/trunk/docs/manual/CoreTasks/property.html (original) +++ ant/core/trunk/docs/manual/CoreTasks/property.html Wed Jul 25 03:12:58 2007 @@ -57,6 +57,10 @@ This also holds for properties loaded from a property file.</p> <p>A list of predefined properties can be found <a href="../using.html#built-in-props">here</a>.</p> +<p>Since Ant 1.7.1 it is possible to load properties defined in xml +according to <a href="http://java.sun.com/dtd/properties.dtd">Suns DTD</a>, +if Java5+ is present. For this the name of the file, resource or url has +to end with <tt>.xml</tt>.</p> <h4>OpenVMS Users</h4> <p>With the <code>environment</code> attribute this task will load all defined Modified: ant/core/trunk/src/etc/testcases/taskdefs/property.xml URL: http://svn.apache.org/viewvc/ant/core/trunk/src/etc/testcases/taskdefs/property.xml?view=diff&rev=559386&r1=559385&r2=559386 ============================================================================== --- ant/core/trunk/src/etc/testcases/taskdefs/property.xml (original) +++ ant/core/trunk/src/etc/testcases/taskdefs/property.xml Wed Jul 25 03:12:58 2007 @@ -2,6 +2,13 @@ <project name="property-test" basedir="." default="test1"> + <property name="tmp.dir" value="_tmpdir_"/> + <available property="java5+" classname="java.lang.Iterable"/> + + <target name="tearDown"> + <delete dir="${tmp.dir}"/> + </target> + <target name="test1"> <property environment="testenv"/> </target> @@ -45,5 +52,25 @@ <property file="property5.properties"/> <echo>b is ${b}</echo> </target> + + <target name="genXmlPropFile"> + <mkdir dir="${tmp.dir}"/> + <echo file="${tmp.dir}/props.xml"><?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> +<properties version="1.0"> + <comment> + Example of property definition according to Suns DTD as + specified in the Java5 docs and http://java.sun.com/dtd/properties.dtd. + </comment> + <entry key="xml.one">ONE</entry> + <entry key="xml.two">TWO</entry> +</properties> + </echo> + </target> + + <target name="testXmlProperty.internal" depends="genXmlPropFile" if="java5+"> + <property file="${tmp.dir}/props.xml"/> + </target> + <target name="testXmlProperty" depends="testXmlProperty.internal"/> </project> Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java?view=diff&rev=559386&r1=559385&r2=559386 ============================================================================== --- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java (original) +++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Property.java Wed Jul 25 03:12:58 2007 @@ -21,6 +21,8 @@ import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.net.URL; import java.util.Enumeration; import java.util.HashMap; @@ -36,6 +38,7 @@ import org.apache.tools.ant.Task; import org.apache.tools.ant.types.Path; import org.apache.tools.ant.types.Reference; +import org.apache.tools.ant.util.ReflectUtil; /** * Sets a property by name, or set of properties (from file or @@ -499,7 +502,7 @@ try { InputStream is = url.openStream(); try { - props.load(is); + loadProperties(props, is, url.getFile().endsWith(".xml")); } finally { if (is != null) { is.close(); @@ -511,6 +514,39 @@ } } + /** + * Loads the properties defined in the InputStream into the given + * property. On Java5+ it supports reading from XML based property + * definition. + * @param props The property object to load into + * @param is The input stream from where to load + * @param isXml <tt>true</tt> if we should try to load from xml + * @throws IOException if something goes wrong + * @since 1.7.1 + * @see http://java.sun.com/dtd/properties.dtd + * @see java.util.Properties#loadFromXML(InputStream) + */ + private void loadProperties(Properties props, InputStream is, boolean isXml) throws IOException { + if (isXml) { + // load the xml based property definition + // use reflection because of bwc to Java 1.3 + try { + Method loadXmlMethod = props.getClass().getMethod("loadFromXML", new Class[]{InputStream.class}); + loadXmlMethod.invoke(props, new Object[]{is}); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + log("Can not load xml based property definition on Java < 5"); + return; + } catch (Exception e) { + // no-op + e.printStackTrace(); + } + } else { + // load ".properties" format + props.load(is); + } + } + /** * load properties from a file @@ -525,7 +561,7 @@ FileInputStream fis = null; try { fis = new FileInputStream(file); - props.load(fis); + loadProperties(props, fis, file.getName().endsWith(".xml")); } finally { if (fis != null) { fis.close(); @@ -565,7 +601,7 @@ } if (is != null) { - props.load(is); + loadProperties(props, is, name.endsWith(".xml")); addProperties(props); } else { log("Unable to find resource " + name, Project.MSG_WARN); @@ -581,7 +617,6 @@ } } } - } /** Modified: ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java?view=diff&rev=559386&r1=559385&r2=559386 ============================================================================== --- ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java (original) +++ ant/core/trunk/src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java Wed Jul 25 03:12:58 2007 @@ -107,5 +107,16 @@ public void testThisIsNotACircularReference() { expectLog("thisIsNotACircularReference", "b is A/A/A"); } + + public void testXmlProperty() { + try { + Class.forName("java.lang.Iterable"); + executeTarget("testXmlProperty"); + assertEquals("ONE", project.getProperty("xml.one")); + assertEquals("TWO", project.getProperty("xml.two")); + } catch (ClassNotFoundException e) { + // Xml-Loading only on Java5+ + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]