You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by co...@locus.apache.org on 2000/09/24 13:19:30 UTC

cvs commit: jakarta-ant/src/main/org/apache/tools/ant/taskdefs Filter.java

conor       00/09/24 04:19:29

  Modified:    src/main/org/apache/tools/ant/taskdefs Filter.java
  Log:
  Allow filters to be set from a file.
  
  Submitted by:	Gero Vermaas <Ge...@sun.com>
  
  Revision  Changes    Path
  1.4       +47 -7     jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Filter.java
  
  Index: Filter.java
  ===================================================================
  RCS file: /home/cvs/jakarta-ant/src/main/org/apache/tools/ant/taskdefs/Filter.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- Filter.java	2000/09/18 07:55:00	1.3
  +++ Filter.java	2000/09/24 11:19:28	1.4
  @@ -54,19 +54,27 @@
   
   package org.apache.tools.ant.taskdefs;
   
  +import java.util.Enumeration;
  +import java.util.Properties;
  +import java.io.File;
  +import java.io.FileInputStream;
  +
   import org.apache.tools.ant.*;
   
   /**
  - * This task set a token filter that is used by the file copy methods
  - * of the project to do token substitution.
  + * This task sets a token filter that is used by the file copy methods
  + * of the project to do token substitution, or sets mutiple tokens by
  + * reading these from a file.
    *
    * @author Stefano Mazzocchi <a href="mailto:stefano@apache.org">stefano@apache.org</a>
  + * @author Gero Vermaas <a href="mailto:gero@xs4all.nl">gero@xs4all.nl</a>
    */
   public class Filter extends Task {
   
       private String token;
       private String value;
  -
  +    private File filtersFile;
  +    
       public void setToken(String token) {
           this.token = token;
       }
  @@ -75,11 +83,43 @@
           this.value = value;
       }
   
  +    public void setFiltersfile(File filterFile) {
  +        this.filtersFile = filtersFile;
  +    }
  +
       public void execute() throws BuildException {
  -        if (token == null || value == null) {
  -            throw new BuildException("token and value are required", location);
  +        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);
           }
  -
  -        project.addFilter(token, value);
  +        
  +        if (isSingleFilter) {
  +            project.addFilter(token, value);
  +        }
  +        
  +        if (isFiltersFromFile) {
  +            readFilters();
  +        }
  +    }
  +    
  +    protected void readFilters() throws BuildException {
  +        log("Reading filters from " + filtersFile, Project.MSG_VERBOSE);
  +        try {
  +            Properties props = new Properties();
  +            props.load(new FileInputStream(filtersFile));
  +
  +            Project proj = getProject();
  +
  +            Enumeration enum = props.propertyNames();		
  +            while (enum.hasMoreElements()) {
  +                String strPropName = (String)enum.nextElement();
  +                String strValue = props.getProperty(strPropName);
  +                proj.addFilter(strPropName, strValue);
  +            }
  +        } catch (Exception e) {
  +            throw new BuildException("Could not read filters from file: " + filtersFile);
  +        }
       }
   }