ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r349636 - in /ant/core/trunk: docs/manual/CoreTasks/ src/etc/testcases/taskdefs/ src/main/org/apache/tools/ant/taskdefs/ src/testcases/org/apache/tools/ant/taskdefs/
Date Tue, 29 Nov 2005 04:41:39 GMT
Author: bodewig
Date: Mon Nov 28 20:41:31 2005
New Revision: 349636

URL: http://svn.apache.org/viewcvs?rev=349636&view=rev
Log:
resource support for <loadproperties>

Modified:
    ant/core/trunk/docs/manual/CoreTasks/loadproperties.html
    ant/core/trunk/src/etc/testcases/taskdefs/loadproperties.xml
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java
    ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java

Modified: ant/core/trunk/docs/manual/CoreTasks/loadproperties.html
URL: http://svn.apache.org/viewcvs/ant/core/trunk/docs/manual/CoreTasks/loadproperties.html?rev=349636&r1=349635&r2=349636&view=diff
==============================================================================
--- ant/core/trunk/docs/manual/CoreTasks/loadproperties.html (original)
+++ ant/core/trunk/docs/manual/CoreTasks/loadproperties.html Mon Nov 28 20:41:31 2005
@@ -32,7 +32,8 @@
   <tr>
     <td valign="top">srcFile</td>
     <td valign="top">source file</td>
-    <td valign="top" rowspan="2" align="center">One of these</td>
+    <td valign="top" rowspan="2" align="center">One of these or a
+          nested resource</td>
   </tr>
   <tr>
     <td valign="top">resource</td>
@@ -56,14 +57,29 @@
     <td align="center" valign="top">No</td>
   </tr>
 </table>
-<p>
-The LoadProperties task supports nested <a href="../CoreTypes/filterchain.html">
-FilterChain</a>s, as well as a nested <code>&lt;classpath&gt;</code>
-element for use with the <i>resource</i> attribute.
+
+<h3>Parameters specified as nested elements</h3>
+
+<h4>any <a href="../CoreTypes/resources.html">resource</a> or single element
+resource collection</h4>
+
+<p>The specified resource will be used as src. <em>Since Ant 1.7</em></p>
+
+<h4><a href="../CoreTypes/filterchain.html">FilterChain</a></h4>
+
+<h4>classpath</h4>
+
+<p>for use with the <i>resource</i> attribute.</p>
 
 <h3>Examples</h3>
 <pre>    &lt;loadproperties srcFile="file.properties"/&gt;
 </pre>
+or
+<pre>
+    &lt;loadproperties&gt;
+      &lt;file file="file.properties"/&gt;
+    &lt;/loadproperties&gt;
+</pre>
 Load contents of file.properties as Ant properties.
 
 <pre>    &lt;loadproperties srcFile="file.properties"&gt;
@@ -77,6 +93,17 @@
 Read the lines that contain the string &quot;import.&quot;
 from the file &quot;file.properties&quot; and load them as
 Ant properties.
+
+<pre>
+    &lt;loadproperties&gt;
+      &lt;<a href="../CoreTypes/resources.html#gzipresource">gzipresource</a>&gt;
+        &lt;<a href="../CoreTypes/resources.html#url">url</a> url="http://example.org/url.properties.gz"/&gt;
+      &lt;/gzipresource&gt;
+    &lt;/loadproperties&gt;
+</pre>
+Load contents of http://example.org/url.properties.gz, uncompress it
+on the fly and load the contents as Ant properties.
+
 <hr>
 
 <p align="center">Copyright &copy; 2002-2005 The Apache Software Foundation. All
rights

Modified: ant/core/trunk/src/etc/testcases/taskdefs/loadproperties.xml
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/etc/testcases/taskdefs/loadproperties.xml?rev=349636&r1=349635&r2=349636&view=diff
==============================================================================
--- ant/core/trunk/src/etc/testcases/taskdefs/loadproperties.xml (original)
+++ ant/core/trunk/src/etc/testcases/taskdefs/loadproperties.xml Mon Nov 28 20:41:31 2005
@@ -30,7 +30,7 @@
       value="http://${server1.http.server}:${server1.http.port}"/>
   </target>
 
-  <target name="testPropertiesFromResource" depends="init">
+  <target name="write properties.tmp" depends="init">
     <echo file="properties.tmp">
 #tpfr.a=a
 tpfr.a=A
@@ -38,6 +38,9 @@
        e
 tpfr.c=@C@
     </echo>
+  </target>
+
+  <target name="testPropertiesFromResource" depends="write properties.tmp">
     <loadproperties resource="properties.tmp" classpath="${basedir}">
       <filterchain>
         <replacetokens>
@@ -45,6 +48,22 @@
         </replacetokens>
       </filterchain>
     </loadproperties>
+  </target>
+
+  <target name="testPropertiesFromFileSet" depends="write properties.tmp">
+    <loadproperties>
+      <fileset dir="${basedir}">
+        <include name="properties.tmp"/>
+      </fileset>
+      <filterchain>
+        <replacetokens>
+          <token key="C" value="sea"/>
+        </replacetokens>
+      </filterchain>
+    </loadproperties>
+  </target>
+
+  <target name="loadPropertiesCheck">
     <condition property="testPropertiesFromResource.ok">
         <equals arg1="Abesea" arg2="${tpfr.a}${tpfr.b}${tpfr.c}" />
     </condition>

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java?rev=349636&r1=349635&r2=349636&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/LoadProperties.java Mon Nov 28 20:41:31
2005
@@ -32,7 +32,12 @@
 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.Resource;
+import org.apache.tools.ant.types.ResourceCollection;
 import org.apache.tools.ant.types.FilterChain;
+import org.apache.tools.ant.types.resources.FileResource;
+import org.apache.tools.ant.types.resources.JavaResource;
+import org.apache.tools.ant.util.FileUtils;
 
 /**
  * Load a file's contents as Ant properties.
@@ -43,19 +48,9 @@
 public class LoadProperties extends Task {
 
     /**
-     * Source file
+     * Source resource.
      */
-    private File srcFile = null;
-
-    /**
-     * Resource
-     */
-    private String resource = null;
-
-    /**
-     * Classpath
-     */
-    private Path classpath = null;
+    private Resource src = null;
 
     /**
      * Holds filterchains
@@ -73,7 +68,7 @@
      * @param srcFile The new SrcFile value
      */
     public final void setSrcFile(final File srcFile) {
-        this.srcFile = srcFile;
+        addConfigured(new FileResource(srcFile));
     }
 
     /**
@@ -82,7 +77,8 @@
      * @param resource resource on classpath
      */
     public void setResource(String resource) {
-        this.resource = resource;
+        assertSrcIsJavaResource();
+        ((JavaResource) src).setName(resource);
     }
 
     /**
@@ -105,11 +101,8 @@
      * @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);
-        }
+        assertSrcIsJavaResource();
+        ((JavaResource) src).setClasspath(classpath);
     }
 
     /**
@@ -117,10 +110,8 @@
      * @return The classpath to be configured
      */
     public Path createClasspath() {
-        if (this.classpath == null) {
-            this.classpath = new Path(getProject());
-        }
-        return this.classpath.createPath();
+        assertSrcIsJavaResource();
+        return ((JavaResource) src).createClasspath();
     }
 
     /**
@@ -129,7 +120,8 @@
      * @param r The reference value
      */
     public void setClasspathRef(Reference r) {
-        createClasspath().setRefid(r);
+        assertSrcIsJavaResource();
+        ((JavaResource) src).setClasspathRef(r);
     }
 
     /**
@@ -137,7 +129,8 @@
      * @return The classpath
      */
     public Path getClasspath() {
-        return classpath;
+        assertSrcIsJavaResource();
+        return ((JavaResource) src).getClasspath();
     }
 
     /**
@@ -147,48 +140,24 @@
      */
     public final void execute() throws BuildException {
         //validation
-        if (srcFile == null && resource == null) {
-            throw new BuildException(
-                "One of \"srcfile\" or \"resource\" is required.");
+        if (src == null) {
+            throw new BuildException("A source resource is required.");
         }
-
-        BufferedInputStream bis = null;
-
-        if (srcFile != null) {
-            if (!srcFile.exists()) {
-                throw new BuildException("Source file does not exist :"+srcFile);
-            }
-
-            if (!srcFile.isFile()) {
-                throw new BuildException("Source file is not a file :"+srcFile);
-            }
-
-            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);
+        if (!src.isExists()) {
+            if (src instanceof JavaResource) {
+                // dreaded backwards compatibility
+                log("Unable to find resource " + src, Project.MSG_WARN);
                 return;
             }
+            throw new BuildException("Source resource does not exist: " + src);
         }
 
+        BufferedInputStream bis = null;
         Reader instream = null;
         ByteArrayInputStream tis = null;
 
         try {
+            bis = new BufferedInputStream(src.getInputStream());
             if (encoding == null) {
                 instream = new InputStreamReader(bis);
             } else {
@@ -224,23 +193,9 @@
         } catch (final IOException ioe) {
             final String message = "Unable to load file: " + ioe.toString();
             throw new BuildException(message, ioe, getLocation());
-        } catch (final BuildException be) {
-            throw be;
         } finally {
-            try {
-                if (bis != null) {
-                    bis.close();
-                }
-            } catch (IOException ioex) {
-                //ignore
-            }
-            try {
-                if (tis != null) {
-                    tis.close();
-                }
-            } catch (IOException ioex) {
-                //ignore
-            }
+            FileUtils.close(bis);
+            FileUtils.close(tis);
         }
     }
 
@@ -252,4 +207,28 @@
         filterChains.addElement(filter);
     }
 
+    /**
+     * Set the source resource.
+     * @param a the resource to load as a single element Resource collection.
+     * @since Ant 1.7
+     */
+    public void addConfigured(ResourceCollection a) {
+        if (src != null) {
+            throw new BuildException("only a single source is supported");
+        }
+        if (a.size() != 1) {
+            throw new BuildException("only single argument resource collections"
+                                     + " are supported");
+        }
+        src = (Resource) a.iterator().next();
+    }
+
+    private void assertSrcIsJavaResource() {
+        if (src == null) {
+            src = new JavaResource();
+            src.setProject(getProject());
+        } else if (!(src instanceof JavaResource)) {
+            throw new BuildException("expected a java resource as source");
+        }
+    }
 }

Modified: ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java?rev=349636&r1=349635&r2=349636&view=diff
==============================================================================
--- ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java (original)
+++ ant/core/trunk/src/testcases/org/apache/tools/ant/taskdefs/LoadPropertiesTest.java Mon
Nov 28 20:41:31 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright  2002,2004 The Apache Software Foundation
+ * Copyright  2002,2004-2005 The Apache Software Foundation
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -50,5 +50,11 @@
 
     public void testPropertiesFromResource() {
         executeTarget("testPropertiesFromResource");
+        executeTarget("loadPropertiesCheck");
+    }
+
+    public void testPropertiesFromFileSet() {
+        executeTarget("testPropertiesFromFileSet");
+        executeTarget("loadPropertiesCheck");
     }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
For additional commands, e-mail: dev-help@ant.apache.org


Mime
View raw message