@@ -54,19 +54,27 @@
package org.apache.tools.ant.taskdefs;
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.*;
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 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 {
public class Filter extends Task {
private String token;
private String token;
private String value;
private String value;
private File filtersFile;
public void setToken(String token) {
public void setToken(String token) {
this.token = token;
this.token = token;
}
}
@@ -75,11 +83,43 @@ public class Filter extends Task {
this.value = value;
this.value = value;
}
}
public void setFiltersfile(File filterFile) {
this.filtersFile = filtersFile;
}
public void execute() throws BuildException {
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);
}
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.addFilter(token, value);
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);
}
}
}
}
}