mbenson 2004/04/20 12:29:56
Modified: . WHATSNEW
docs/manual/CoreTasks loadproperties.html
src/etc/testcases/taskdefs loadproperties.xml
src/main/org/apache/tools/ant/taskdefs LoadProperties.java
src/testcases/org/apache/tools/ant/taskdefs
LoadPropertiesTest.java
Log:
Add resource support to <loadproperties>.
PR: 28340
Revision Changes Path
1.591 +2 -0 ant/WHATSNEW
Index: WHATSNEW
===================================================================
RCS file: /home/cvs/ant/WHATSNEW,v
retrieving revision 1.590
retrieving revision 1.591
diff -u -r1.590 -r1.591
--- WHATSNEW 20 Apr 2004 13:25:08 -0000 1.590
+++ WHATSNEW 20 Apr 2004 19:29:55 -0000 1.591
@@ -55,6 +55,8 @@
* New <redirector> type introduced to provide extreme I/O flexibility.
Initial support for <exec>, <apply>, and <java> tasks.
+* <loadproperties> supports loading from a resource.
+
Changes from Ant 1.6.1 to current Ant 1.6 CVS version
=============================================
1.8 +20 -3 ant/docs/manual/CoreTasks/loadproperties.html
Index: loadproperties.html
===================================================================
RCS file: /home/cvs/ant/docs/manual/CoreTasks/loadproperties.html,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- loadproperties.html 9 Feb 2004 21:50:05 -0000 1.7
+++ loadproperties.html 20 Apr 2004 19:29:56 -0000 1.8
@@ -10,7 +10,7 @@
<h3>Description</h3>
<p>
Load a file's contents as Ant properties. This is equivalent
-to <property file="..."/> except that it
+to <property file|resource="..."/> except that it
supports nested <filterchain> elements.
</p>
@@ -29,17 +29,34 @@
<tr>
<td valign="top">srcFile</td>
<td valign="top">source file</td>
- <td valign="top" align="center">Yes</td>
+ <td valign="top" rowspan="2" align="center">One of these</td>
+ </tr>
+ <tr>
+ <td valign="top">resource</td>
+ <td valign="top">the resource name of the property file</td>
</tr>
<tr>
<td valign="top">encoding</td>
<td valign="top">encoding to use when loading the file</td>
<td align="center" valign="top">No</td>
</tr>
+ <tr>
+ <td valign="top">classpath</td>
+ <td valign="top">the classpath to use when looking up a resource.</td>
+ <td align="center" valign="top">No</td>
+ </tr>
+ <tr>
+ <td valign="top">classpathref</td>
+ <td valign="top">the classpath to use when looking up a resource,
+ given as <a href="../using.html#references">reference</a>
+ to a <path> defined elsewhere..</td>
+ <td align="center" valign="top">No</td>
+ </tr>
</table>
<p>
The LoadProperties task supports nested <a
href="../CoreTypes/filterchain.html">
-FilterChain</a>s.
+FilterChain</a>s, as well as a nested <code><classpath></code>
+element for use with the <i>resource</i> attribute.
<h3>Examples</h3>
<pre> <loadproperties srcFile="file.properties"/>
1.2 +23 -0 ant/src/etc/testcases/taskdefs/loadproperties.xml
Index: loadproperties.xml
===================================================================
RCS file: /home/cvs/ant/src/etc/testcases/taskdefs/loadproperties.xml,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- loadproperties.xml 8 Mar 2002 02:02:20 -0000 1.1
+++ loadproperties.xml 20 Apr 2004 19:29:56 -0000 1.2
@@ -30,6 +30,29 @@
value="http://${server1.http.server}:${server1.http.port}"/>
</target>
+ <target name="testPropertiesFromResource" depends="init">
+ <echo file="properties.tmp">
+#tpfr.a=a
+tpfr.a=A
+tpfr.b=b\
+ e
[EMAIL PROTECTED]@
+ </echo>
+ <loadproperties resource="properties.tmp" classpath="${basedir}">
+ <filterchain>
+ <replacetokens>
+ <token key="C" value="sea"/>
+ </replacetokens>
+ </filterchain>
+ </loadproperties>
+ <condition property="testPropertiesFromResource.ok">
+ <equals arg1="Abesea" arg2="${tpfr.a}${tpfr.b}${tpfr.c}" />
+ </condition>
+ <fail unless="testPropertiesFromResource.ok">
+$${tpfr.a}$${tpfr.b}$${tpfr.c}="${tpfr.a}${tpfr.b}${tpfr.c}"
+ </fail>
+ </target>
+
<target name="cleanup">
<delete file="properties.tmp"/>
</target>
1.25 +98 -21
ant/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java
Index: LoadProperties.java
===================================================================
RCS file:
/home/cvs/ant/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -r1.24 -r1.25
--- LoadProperties.java 9 Mar 2004 16:48:06 -0000 1.24
+++ LoadProperties.java 20 Apr 2004 19:29:56 -0000 1.25
@@ -19,6 +19,7 @@
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
+import java.io.InputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
@@ -26,9 +27,12 @@
import java.util.Enumeration;
import java.util.Properties;
import java.util.Vector;
+import org.apache.tools.ant.Project;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.filters.util.ChainReaderHelper;
+import org.apache.tools.ant.types.Path;
+import org.apache.tools.ant.types.Reference;
import org.apache.tools.ant.types.FilterChain;
/**
@@ -45,18 +49,27 @@
private File srcFile = null;
/**
+ * Resource
+ */
+ private String resource = null;
+
+ /**
+ * Classpath
+ */
+ private Path classpath = null;
+
+ /**
* Holds filterchains
*/
private final Vector filterChains = new Vector();
/**
- * Encoding to use for filenames, defaults to the platform's default
- * encoding.
+ * Encoding to use for input; defaults to the platform's default
encoding.
*/
private String encoding = null;
/**
- * Sets the file to load.
+ * Set the file to load.
*
* @param srcFile The new SrcFile value
*/
@@ -65,6 +78,15 @@
}
/**
+ * Set the resource name of a property file to load.
+ *
+ * @param resource resource on classpath
+ */
+ public void setResource(String resource) {
+ this.resource = resource;
+ }
+
+ /**
* Encoding to use for input, defaults to the platform's default
* encoding. <p>
*
@@ -75,41 +97,96 @@
*
* @param encoding The new Encoding value
*/
-
public final void setEncoding(final String encoding) {
this.encoding = encoding;
}
/**
- * read in a source file's contents and load them up as Ant properties
+ * Set the classpath to use when looking up a resource.
+ * @param classpath to add to any existing classpath
+ */
+ public void setClasspath(Path classpath) {
+ if (this.classpath == null) {
+ this.classpath = classpath;
+ } else {
+ this.classpath.append(classpath);
+ }
+ }
+
+ /**
+ * Add a classpath to use when looking up a resource.
+ */
+ public Path createClasspath() {
+ if (this.classpath == null) {
+ this.classpath = new Path(getProject());
+ }
+ return this.classpath.createPath();
+ }
+
+ /**
+ * Set the classpath to use when looking up a resource,
+ * given as reference to a <path> defined elsewhere
+ */
+ public void setClasspathRef(Reference r) {
+ createClasspath().setRefid(r);
+ }
+
+ /**
+ * get the classpath used by this <CODE>LoadProperties</CODE>.
+ */
+ public Path getClasspath() {
+ return classpath;
+ }
+
+ /**
+ * load Ant properties from the source file or resource
*
* @exception BuildException if something goes wrong with the build
*/
public final void execute() throws BuildException {
//validation
- if (srcFile == null) {
- throw new BuildException("Source file not defined.");
+ if (srcFile == null && resource == null) {
+ throw new BuildException(
+ "One of \"srcfile\" or \"resource\" is required.");
}
- if (!srcFile.exists()) {
- throw new BuildException("Source file does not exist.");
- }
+ BufferedInputStream bis = null;
+
+ if (srcFile != null ) {
+ if (!srcFile.exists()) {
+ throw new BuildException("Source file does not exist.");
+ }
+
+ if (!srcFile.isFile()) {
+ throw new BuildException("Source file is not a file.");
+ }
- if (!srcFile.isFile()) {
- throw new BuildException("Source file is not a file.");
+ try {
+ bis = new BufferedInputStream(new FileInputStream(srcFile));
+ } catch (IOException eyeOhEx) {
+ throw new BuildException(eyeOhEx);
+ }
+ } else {
+ ClassLoader cL = (classpath != null)
+ ? getProject().createClassLoader(classpath)
+ : LoadProperties.class.getClassLoader();
+
+ InputStream is = (cL == null)
+ ? ClassLoader.getSystemResourceAsStream(resource)
+ : cL.getResourceAsStream(resource);
+
+ if (is != null) {
+ bis = new BufferedInputStream(is);
+ } else { // do it like Property
+ log("Unable to find resource " + resource, Project.MSG_WARN);
+ return;
+ }
}
- FileInputStream fis = null;
- BufferedInputStream bis = null;
Reader instream = null;
ByteArrayInputStream tis = null;
try {
- final long len = srcFile.length();
-
- //open up the file
- fis = new FileInputStream(srcFile);
- bis = new BufferedInputStream(fis);
if (encoding == null) {
instream = new InputStreamReader(bis);
} else {
@@ -150,8 +227,8 @@
throw be;
} finally {
try {
- if (fis != null) {
- fis.close();
+ if (bis != null) {
+ bis.close();
}
} catch (IOException ioex) {
//ignore
1.8 +4 -0
ant/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java
Index: LoadPropertiesTest.java
===================================================================
RCS file:
/home/cvs/ant/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- LoadPropertiesTest.java 9 Mar 2004 16:48:57 -0000 1.7
+++ LoadPropertiesTest.java 20 Apr 2004 19:29:56 -0000 1.8
@@ -47,4 +47,8 @@
String url = project.getProperty("server1.http.url");
assertEquals("http://localhost:80", url);
}
+
+ public void testPropertiesFromResource() {
+ executeTarget("testPropertiesFromResource");
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]