Most of the copy-filters I set are based on properties, so I end up doing a
lot of this:

    <filter token="mail.debug"              value="${mail.debug}" />
    <filter token="mail.from"               value="${mail.from}" />
    <filter token="mail.host"               value="${mail.host}" />
    <filter token="mail.transport.protocol" value="${mail.transport.protocol}" 
/>

This patch adds a short-cut for deriving filters from properties:

    <filter property="mail.debug" />
    <filter property="mail.from" />
    <filter property="mail.host" />
    <filter property="mail.transport.protocol" />

And also allows you to publish all properties as filters:

    <filter property="*" />

-- 
Mike
Index: src/main/org/apache/tools/ant/taskdefs/Filter.java
===================================================================
RCS file: /home/cvspublic/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Filter.java,v
retrieving revision 1.8
diff -u -r1.8 Filter.java
--- src/main/org/apache/tools/ant/taskdefs/Filter.java	2001/01/03 14:18:30	1.8
+++ src/main/org/apache/tools/ant/taskdefs/Filter.java	2001/08/03 00:51:58
@@ -55,6 +55,7 @@
 package org.apache.tools.ant.taskdefs;
 
 import java.util.Enumeration;
+import java.util.Hashtable;
 import java.util.Properties;
 import java.io.File;
 import java.io.FileInputStream;
@@ -73,6 +74,7 @@
 
     private String token;
     private String value;
+    private String property;
     private File filtersFile;
     
     public void setToken(String token) {
@@ -83,16 +85,22 @@
         this.value = value;
     }
 
+    public void setProperty(String property) {
+        this.property = property;
+    }
+
     public void setFiltersfile(File filtersFile) {
         this.filtersFile = filtersFile;
     }
 
     public void execute() throws BuildException {
-        boolean isFiltersFromFile = filtersFile != null && token == null && value == null;
-        boolean isSingleFilter = filtersFile == null && token != null && value != null;
         
-        if (!isFiltersFromFile && !isSingleFilter) {
-            throw new BuildException("both token and value parameters, or only a filtersFile parameter is required", location);
+        boolean isFiltersFromProperty = filtersFile == null && property != null && token == null && value == null;
+        boolean isFiltersFromFile = filtersFile != null && property == null && token == null && value == null;
+        boolean isSingleFilter = filtersFile == null && property == null && token != null && value != null;
+        
+        if (!isFiltersFromProperty && !isFiltersFromFile && !isSingleFilter) {
+            throw new BuildException("bad parameter combination: please specify token+value OR filtersFile OR property", location);
         }
         
         if (isSingleFilter) {
@@ -102,6 +110,10 @@
         if (isFiltersFromFile) {
             readFilters();
         }
+
+        if (isFiltersFromProperty) {
+            resolveProperty();
+        }
     }
     
     protected void readFilters() throws BuildException {
@@ -111,15 +123,7 @@
             Properties props = new Properties();
             in = new FileInputStream(filtersFile);
             props.load(in);
-
-            Project proj = getProject();
-
-            Enumeration enum = props.propertyNames();
-            while (enum.hasMoreElements()) {
-                String strPropName = (String)enum.nextElement();
-                String strValue = props.getProperty(strPropName);
-                proj.addFilter(strPropName, strValue);
-            }
+            addFilters( props );
         } catch (Exception e) {
             throw new BuildException("Could not read filters from file: " + filtersFile);
         } finally {
@@ -130,4 +134,27 @@
             }
         }
     }
+
+    protected void resolveProperty() throws BuildException {
+        if ("*".equals( property )) {
+            log("Setting filters for all properties", Project.MSG_VERBOSE);
+            addFilters( getProject().getProperties() );
+        } else {
+            String value = getProject().getProperty( property );
+            if (value != null) {
+                project.addFilter( property, value );
+            }
+        }
+    }
+
+    protected void addFilters( Hashtable properties ) {
+        Project proj = getProject();
+        Enumeration propEnum = properties.keys();
+        while (propEnum.hasMoreElements()) {
+            String property = (String) propEnum.nextElement();
+            String value = (String) properties.get(property);
+            proj.addFilter( property, value );
+        }
+    }
+
 }
Index: docs/manual/CoreTasks/filter.html
===================================================================
RCS file: /home/cvspublic/jakarta-ant/docs/manual/CoreTasks/filter.html,v
retrieving revision 1.4
diff -u -r1.4 filter.html
--- docs/manual/CoreTasks/filter.html	2001/03/02 15:56:59	1.4
+++ docs/manual/CoreTasks/filter.html	2001/08/03 00:51:58
@@ -40,6 +40,13 @@
     <td valign="top">The file from which the filters must be read. This file must be a formatted as a property file. </td>
     <td align="center" valign="top">Yes*</td>
   </tr>
+  <tr>
+    <td valign="top">property</td>
+    <td valign="top">The name of a property that should be made available
+        as a filter.  If the wildcard "*" is specified, filters are created
+        for all available properties. </td>
+    <td align="center" valign="top">Yes*</td>
+  </tr>
 </table>
 <p>* see notes 1 and 2 above parameters table.</p>
 <h3>Examples</h3>
@@ -53,6 +60,10 @@
 <pre>  &lt;filter filtersfile=&quot;deploy_env.properties&quot;/&gt;</pre>
 will read all property entries from the <i>deploy_env.properties</i> file
 and set these as filters.
+<p>
+<pre>  &lt;filter property=&quot;user.home&quot;/&gt;</pre>
+is equivalent to
+<pre>  &lt;filter token=&quot;user.home&quot; value=&quot;${user.home}&quot; /&gt;</pre>
 
 <hr>
 <p align="center">Copyright &copy; 2000,2001 Apache Software Foundation. All rights

Reply via email to