git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@559386 13f79535-47bb-0310-9956-ffa450edef68master
| @@ -127,6 +127,10 @@ Fixed bugs: | |||||
| * Regression: Path subclasses that overrode list() stopped working in | * Regression: Path subclasses that overrode list() stopped working in | ||||
| resourceCollection contexts in Ant 1.7.0. Bugzilla 42967. | resourceCollection contexts in Ant 1.7.0. Bugzilla 42967. | ||||
| * <property> supports loading from xml based property definition. | |||||
| Bugzilla 42946 | |||||
| Other changes: | Other changes: | ||||
| -------------- | -------------- | ||||
| * <script> now has basic support for JavaFX scripts | * <script> now has basic support for JavaFX scripts | ||||
| @@ -57,6 +57,10 @@ properties. These references are resolved at the time these properties are set. | |||||
| This also holds for properties loaded from a property file.</p> | This also holds for properties loaded from a property file.</p> | ||||
| <p>A list of predefined properties can be found <a | <p>A list of predefined properties can be found <a | ||||
| href="../using.html#built-in-props">here</a>.</p> | 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> | <h4>OpenVMS Users</h4> | ||||
| <p>With the <code>environment</code> attribute this task will load all defined | <p>With the <code>environment</code> attribute this task will load all defined | ||||
| @@ -2,6 +2,13 @@ | |||||
| <project name="property-test" basedir="." default="test1"> | <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"> | <target name="test1"> | ||||
| <property environment="testenv"/> | <property environment="testenv"/> | ||||
| </target> | </target> | ||||
| @@ -46,4 +53,24 @@ | |||||
| <echo>b is ${b}</echo> | <echo>b is ${b}</echo> | ||||
| </target> | </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> | </project> | ||||
| @@ -21,6 +21,8 @@ import java.io.File; | |||||
| import java.io.FileInputStream; | import java.io.FileInputStream; | ||||
| import java.io.IOException; | import java.io.IOException; | ||||
| import java.io.InputStream; | import java.io.InputStream; | ||||
| import java.lang.reflect.InvocationTargetException; | |||||
| import java.lang.reflect.Method; | |||||
| import java.net.URL; | import java.net.URL; | ||||
| import java.util.Enumeration; | import java.util.Enumeration; | ||||
| import java.util.HashMap; | import java.util.HashMap; | ||||
| @@ -36,6 +38,7 @@ import org.apache.tools.ant.PropertyHelper; | |||||
| import org.apache.tools.ant.Task; | import org.apache.tools.ant.Task; | ||||
| import org.apache.tools.ant.types.Path; | import org.apache.tools.ant.types.Path; | ||||
| import org.apache.tools.ant.types.Reference; | 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 | * Sets a property by name, or set of properties (from file or | ||||
| @@ -499,7 +502,7 @@ public class Property extends Task { | |||||
| try { | try { | ||||
| InputStream is = url.openStream(); | InputStream is = url.openStream(); | ||||
| try { | try { | ||||
| props.load(is); | |||||
| loadProperties(props, is, url.getFile().endsWith(".xml")); | |||||
| } finally { | } finally { | ||||
| if (is != null) { | if (is != null) { | ||||
| is.close(); | is.close(); | ||||
| @@ -511,6 +514,39 @@ public class Property extends Task { | |||||
| } | } | ||||
| } | } | ||||
| /** | |||||
| * 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 | * load properties from a file | ||||
| @@ -525,7 +561,7 @@ public class Property extends Task { | |||||
| FileInputStream fis = null; | FileInputStream fis = null; | ||||
| try { | try { | ||||
| fis = new FileInputStream(file); | fis = new FileInputStream(file); | ||||
| props.load(fis); | |||||
| loadProperties(props, fis, file.getName().endsWith(".xml")); | |||||
| } finally { | } finally { | ||||
| if (fis != null) { | if (fis != null) { | ||||
| fis.close(); | fis.close(); | ||||
| @@ -565,7 +601,7 @@ public class Property extends Task { | |||||
| } | } | ||||
| if (is != null) { | if (is != null) { | ||||
| props.load(is); | |||||
| loadProperties(props, is, name.endsWith(".xml")); | |||||
| addProperties(props); | addProperties(props); | ||||
| } else { | } else { | ||||
| log("Unable to find resource " + name, Project.MSG_WARN); | log("Unable to find resource " + name, Project.MSG_WARN); | ||||
| @@ -581,7 +617,6 @@ public class Property extends Task { | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| } | } | ||||
| /** | /** | ||||
| @@ -107,5 +107,16 @@ public class PropertyTest extends BuildFileTest { | |||||
| public void testThisIsNotACircularReference() { | public void testThisIsNotACircularReference() { | ||||
| expectLog("thisIsNotACircularReference", "b is A/A/A"); | 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+ | |||||
| } | |||||
| } | |||||
| } | } | ||||