Browse Source

<property> now supports xml-based property definition. Bug 42946.

git-svn-id: https://svn.apache.org/repos/asf/ant/core/trunk@559386 13f79535-47bb-0310-9956-ffa450edef68
master
Jan Materne 18 years ago
parent
commit
b1eddca62b
5 changed files with 85 additions and 4 deletions
  1. +4
    -0
      WHATSNEW
  2. +4
    -0
      docs/manual/CoreTasks/property.html
  3. +27
    -0
      src/etc/testcases/taskdefs/property.xml
  4. +39
    -4
      src/main/org/apache/tools/ant/taskdefs/Property.java
  5. +11
    -0
      src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java

+ 4
- 0
WHATSNEW View File

@@ -127,6 +127,10 @@ Fixed bugs:
* 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


+ 4
- 0
docs/manual/CoreTasks/property.html View File

@@ -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>
<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


+ 27
- 0
src/etc/testcases/taskdefs/property.xml View File

@@ -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>
@@ -46,4 +53,24 @@
<echo>b is ${b}</echo>
</target>

<target name="genXmlPropFile">
<mkdir dir="${tmp.dir}"/>
<echo file="${tmp.dir}/props.xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;!DOCTYPE properties SYSTEM &quot;http://java.sun.com/dtd/properties.dtd&quot;&gt;
&lt;properties version=&quot;1.0&quot;&gt;
&lt;comment&gt;
Example of property definition according to Suns DTD as
specified in the Java5 docs and http://java.sun.com/dtd/properties.dtd.
&lt;/comment&gt;
&lt;entry key=&quot;xml.one&quot;&gt;ONE&lt;/entry&gt;
&lt;entry key=&quot;xml.two&quot;&gt;TWO&lt;/entry&gt;
&lt;/properties&gt;
</echo>
</target>

<target name="testXmlProperty.internal" depends="genXmlPropFile" if="java5+">
<property file="${tmp.dir}/props.xml"/>
</target>
<target name="testXmlProperty" depends="testXmlProperty.internal"/>

</project>

+ 39
- 4
src/main/org/apache/tools/ant/taskdefs/Property.java View File

@@ -21,6 +21,8 @@ import java.io.File;
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.PropertyHelper;
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 @@ public class Property extends Task {
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 @@ 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
@@ -525,7 +561,7 @@ public class Property extends Task {
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 @@ public class Property extends Task {
}

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 @@ public class Property extends Task {
}
}
}

}

/**


+ 11
- 0
src/tests/junit/org/apache/tools/ant/taskdefs/PropertyTest.java View File

@@ -107,5 +107,16 @@ public class PropertyTest extends BuildFileTest {
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+
}
}

}

Loading…
Cancel
Save