diff --git a/WHATSNEW b/WHATSNEW index 31aea7673..b6ca4e9b7 100644 --- a/WHATSNEW +++ b/WHATSNEW @@ -55,6 +55,8 @@ Other changes: * New type introduced to provide extreme I/O flexibility. Initial support for , , and tasks. +* supports loading from a resource. + Changes from Ant 1.6.1 to current Ant 1.6 CVS version ============================================= diff --git a/docs/manual/CoreTasks/loadproperties.html b/docs/manual/CoreTasks/loadproperties.html index 6569f3538..3e32f8ce0 100644 --- a/docs/manual/CoreTasks/loadproperties.html +++ b/docs/manual/CoreTasks/loadproperties.html @@ -10,7 +10,7 @@

Description

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.

@@ -29,17 +29,34 @@ filter.

srcFile source file - Yes + One of these + + + resource + the resource name of the property file encoding encoding to use when loading the file No + + classpath + the classpath to use when looking up a resource. + No + + + classpathref + the classpath to use when looking up a resource, + given as reference + to a <path> defined elsewhere.. + No +

The LoadProperties task supports nested -FilterChains. +FilterChains, as well as a nested <classpath> +element for use with the resource attribute.

Examples

    <loadproperties srcFile="file.properties"/>
diff --git a/src/etc/testcases/taskdefs/loadproperties.xml b/src/etc/testcases/taskdefs/loadproperties.xml
index 7f1706dc5..8259f7092 100644
--- a/src/etc/testcases/taskdefs/loadproperties.xml
+++ b/src/etc/testcases/taskdefs/loadproperties.xml
@@ -30,6 +30,29 @@ http.@SERVER@ = ${server}
       value="http://${server1.http.server}:${server1.http.port}"/>
   
 
+  
+    
+#tpfr.a=a
+tpfr.a=A
+tpfr.b=b\
+       e
+tpfr.c=@C@
+    
+    
+      
+        
+          
+        
+      
+    
+    
+        
+    
+    
+$${tpfr.a}$${tpfr.b}$${tpfr.c}="${tpfr.a}${tpfr.b}${tpfr.c}"
+    
+  
+
   
     
   
diff --git a/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java b/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java
index 3f57e4329..df455328c 100644
--- a/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java
+++ b/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java
@@ -19,6 +19,7 @@ package org.apache.tools.ant.taskdefs;
 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.io.Reader;
 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;
 
 /**
@@ -44,19 +48,28 @@ public final class LoadProperties extends Task {
      */
     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
      */
@@ -64,6 +77,15 @@ public final class LoadProperties extends Task {
         this.srcFile = srcFile;
     }
 
+    /**
+     * 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. 

@@ -75,41 +97,96 @@ public final class LoadProperties extends Task { * * @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 LoadProperties. + */ + 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 @@ public final class LoadProperties extends Task { throw be; } finally { try { - if (fis != null) { - fis.close(); + if (bis != null) { + bis.close(); } } catch (IOException ioex) { //ignore diff --git a/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java b/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java index 144879719..be56f55af 100644 --- a/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java +++ b/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java @@ -47,4 +47,8 @@ public class LoadPropertiesTest extends BuildFileTest { String url = project.getProperty("server1.http.url"); assertEquals("http://localhost:80", url); } + + public void testPropertiesFromResource() { + executeTarget("testPropertiesFromResource"); + } }