ant-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From bode...@apache.org
Subject svn commit: r724400 - in /ant/core/trunk/src: main/org/apache/tools/ant/taskdefs/Replace.java tests/antunit/taskdefs/replace-test.xml
Date Mon, 08 Dec 2008 16:48:39 GMT
Author: bodewig
Date: Mon Dec  8 08:48:38 2008
New Revision: 724400

URL: http://svn.apache.org/viewvc?rev=724400&view=rev
Log:
Resource-enable replacefilterfile and propertyfile attributes f <replace>.  Somewhat
related to PR 42702.

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java
    ant/core/trunk/src/tests/antunit/taskdefs/replace-test.xml

Modified: ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java?rev=724400&r1=724399&r2=724400&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/taskdefs/Replace.java Mon Dec  8 08:48:38
2008
@@ -27,6 +27,7 @@
 import java.io.FileReader;
 import java.io.FileWriter;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.OutputStreamWriter;
 import java.io.Reader;
@@ -40,6 +41,7 @@
 import org.apache.tools.ant.types.Resource;
 import org.apache.tools.ant.types.ResourceCollection;
 import org.apache.tools.ant.types.resources.FileProvider;
+import org.apache.tools.ant.types.resources.FileResource;
 import org.apache.tools.ant.types.resources.Union;
 import org.apache.tools.ant.util.FileUtils;
 import org.apache.tools.ant.util.StringUtils;
@@ -63,8 +65,8 @@
     private NestedString token = null;
     private NestedString value = new NestedString();
 
-    private File propertyFile = null;
-    private File replaceFilterFile = null;
+    private Resource propertyResource = null;
+    private Resource replaceFilterResource = null;
     private Properties properties = null;
     private ArrayList replacefilters = new ArrayList();
 
@@ -161,10 +163,10 @@
 
             if ((property != null)) {
                 //the property attribute must have access to a property file
-                if (propertyFile == null) {
+                if (propertyResource == null) {
                     String message = "The replacefilter's property attribute "
                         + "can only be used with the replacetask's "
-                        + "propertyFile attribute.";
+                        + "propertyFile/Resource attribute.";
                     throw new BuildException(message);
                 }
 
@@ -172,7 +174,7 @@
                 if (properties == null
                     || properties.getProperty(property) == null) {
                     String message = "property \"" + property
-                        + "\" was not found in " + propertyFile.getPath();
+                        + "\" was not found in " + propertyResource.getName();
                     throw new BuildException(message);
                 }
             }
@@ -512,8 +514,8 @@
         }
 
         try {
-            if (replaceFilterFile != null) {
-                Properties props = getProperties(replaceFilterFile);
+            if (replaceFilterResource != null) {
+                Properties props = getProperties(replaceFilterResource);
                 Iterator e = props.keySet().iterator();
                 while (e.hasNext()) {
                     String tok =  e.next().toString();
@@ -525,8 +527,8 @@
 
             validateAttributes();
 
-            if (propertyFile != null) {
-                properties = getProperties(propertyFile);
+            if (propertyResource != null) {
+                properties = getProperties(propertyResource);
             }
 
             validateReplacefilters();
@@ -582,8 +584,8 @@
                 + "or nested resources must be specified";
             throw new BuildException(message, getLocation());
         }
-        if (propertyFile != null && !propertyFile.exists()) {
-            String message = "Property file " + propertyFile.getPath()
+        if (propertyResource != null && !propertyResource.isExists()) {
+            String message = "Property file " + propertyResource.getName()
                 + " does not exist.";
             throw new BuildException(message, getLocation());
         }
@@ -620,18 +622,26 @@
      * @throws BuildException if the file could not be found or read.
      */
     public Properties getProperties(File propertyFile) throws BuildException {
+        return getProperties(new FileResource(getProject(), propertyFile));
+    }
+
+    /**
+     * Load a properties resource.
+     * @param propertyResource the resource to load the properties from.
+     * @return loaded <code>Properties</code> object.
+     * @throws BuildException if the resource could not be found or read.
+     * @since Ant 1.8.0
+     */
+    public Properties getProperties(Resource propertyResource)
+        throws BuildException {
         Properties props = new Properties();
 
-        FileInputStream in = null;
+        InputStream in = null;
         try {
-            in = new FileInputStream(propertyFile);
+            in = propertyResource.getInputStream();
             props.load(in);
-        } catch (FileNotFoundException e) {
-            String message = "Property file (" + propertyFile.getPath()
-                + ") not found.";
-            throw new BuildException(message);
         } catch (IOException e) {
-            String message = "Property file (" + propertyFile.getPath()
+            String message = "Property resource (" + propertyResource.getName()
                 + ") cannot be loaded.";
             throw new BuildException(message);
         } finally {
@@ -793,7 +803,19 @@
      * @param replaceFilterFile <code>File</code> to load.
      */
     public void setReplaceFilterFile(File replaceFilterFile) {
-        this.replaceFilterFile = replaceFilterFile;
+        setReplaceFilterResource(new FileResource(getProject(),
+                                                  replaceFilterFile));
+    }
+
+    /**
+     * Sets the name of a resource containing filters; optional.
+     * Each property will be treated as a replacefilter where token is the name
+     * of the property and value is the value of the property.
+     * @param replaceFilterFile <code>File</code> to load.
+     * @since Ant 1.8.0
+     */
+    public void setReplaceFilterResource(Resource replaceFilter) {
+        this.replaceFilterResource = replaceFilter;
     }
 
     /**
@@ -807,8 +829,8 @@
 
     /**
      * Set the string token to replace; required unless a nested
-     * <code>replacetoken</code> element or the <code>replacefilterfile</code>
-     * attribute is used.
+     * <code>replacetoken</code> element or the
+     * <code>replacefilterresource</code> attribute is used.
      * @param token token <code>String</code>.
      */
     public void setToken(String token) {
@@ -860,7 +882,20 @@
      * @param propertyFile <code>File</code> to load.
      */
     public void setPropertyFile(File propertyFile) {
-        this.propertyFile = propertyFile;
+        setPropertyResource(new FileResource(propertyFile));
+    }
+
+    /**
+     * A resource from which properties specified using nested
+     * <code>&lt;replacefilter&gt;</code> elements are drawn; required
+     * only if the <i>property</i> attribute of
+     * <code>&lt;replacefilter&gt;</code> is used.
+     * @param propertyResource <code>Resource</code> to load.
+     *
+     * @since Ant 1.8.0
+     */
+    public void setPropertyResource(Resource propertyResource) {
+        this.propertyResource = propertyResource;
     }
 
     /**

Modified: ant/core/trunk/src/tests/antunit/taskdefs/replace-test.xml
URL: http://svn.apache.org/viewvc/ant/core/trunk/src/tests/antunit/taskdefs/replace-test.xml?rev=724400&r1=724399&r2=724400&view=diff
==============================================================================
--- ant/core/trunk/src/tests/antunit/taskdefs/replace-test.xml (original)
+++ ant/core/trunk/src/tests/antunit/taskdefs/replace-test.xml Mon Dec  8 08:48:38 2008
@@ -18,6 +18,12 @@
 <project default="antunit" xmlns:au="antlib:org.apache.ant.antunit">
   <import file="../antunit-base.xml" />
 
+  <import file="../propertyhelpers.xml" as="ph"/>
+
+  <target name="tearDown" depends="antunit-base.tearDown,ph.tearDown">
+    <delete dir="foo"/>
+  </target>
+
   <target name="setUp">
     <mkdir dir="${output}"/>
     <echo file="${output}/text.txt"><![CDATA[
@@ -82,4 +88,16 @@
     </au:expectfailure>
   </target>
 
+  <target name="testPropertyFilterResource" depends="setUp,ph.defineHelpers">
+    <mkdir dir="foo"/>
+    <echo file="foo/foo.properties"><![CDATA[
+world=Ant
+]]></echo>
+    <replace replacefilterresource="${java:foo!foo.properties}">
+      <file file="${output}/text.txt"/>
+    </replace>
+    <au:assertResourceContains
+       resource="${output}/text.txt" value="Hello, Ant!"/>
+  </target>
+
 </project>



Mime
View raw message