attached are two useful tasks i have written, that would
be helpful to be put into the next release:
PropertyCopy - double dereferencing (ie. ${a.${b}.c})
this essentially takes the "from" attribute and treats
it as a property name. It gets the value of that property
and puts it in the property given by the "name" attribute.
It's useful when you have property names that follow a certain
pattern with some identifier in it (ie. org.TEST.server)
ex. <propertycopy name="NewVar" from="a.${b}.c" />
Foreach - relatively self explanatory
ex. <foreach list="${items}" target="callMe"
param="item" delimiter="," />
I'm sure there are foreach tasks floating around, but
while i'm posting propertycopy i might as well post
the foreach task as well.
--
Matt Inger ([EMAIL PROTECTED])
Sedona Corporation
455 S. Gulph Road, Suite 300
King of Prussia, PA 19406
(484) 679-2213
"Self-respect - the secure feeling that no one,
as yet, is suspicious." -H.L. Mencken
package org.apache.tools.ant.taskdefs.optional;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.CallTarget;
import org.apache.tools.ant.taskdefs.Property;
import org.apache.tools.ant.types.FileSet;
import java.io.File;
/***
* Task definition for the propertycopy task, which copies the value of a
* named property to another property. This is useful when you need to
* plug in the value of another property in order to get a property name
* and then want to get the value of that property name.
* <pre>
* Usage:
*
* Task declaration in the project:
* <taskdef name="propertycopy" classname="org.apache.tools.ant.taskdefs.optional.PropertyCopy" />
*
* Call Syntax:
* <propertycopy name="propname" from="copyfrom" (silent="true|false")? />
*
* Attributes:
* name --> The name of the property you wish to set with the value
* from --> The name of the property you wish to copy the value from
* silent --> Do you want to suppress the error if the "from" property
* does not exist, and just not set the property "name".
*
* Example:
* <property name="org" value="MyOrg" />
* <property name="org.MyOrg.DisplayName" value="My Organiziation" />
* <propertycopy name="displayName" from="org.${org}.DisplayName" />
* <echo message="${displayName}" />
* </pre>
*/
public class PropertyCopy extends Task
{
private String name;
private String from;
private boolean silent;
/***
* Default Constructor
*/
public PropertyCopy()
{
super();
this.name = null;
this.from = null;
this.silent = false;
}
public void setName(String name)
{
this.name = name;
}
public void setFrom(String from)
{
this.from = from;
}
public void setSilent(String silent)
{
Boolean val = Boolean.valueOf(silent);
if (val == null)
val = Boolean.FALSE;
this.silent = val.booleanValue();
}
public void execute()
throws BuildException
{
if (name == null)
throw new BuildException("Missing the 'name' attribute.");
if (from == null)
throw new BuildException("Missing the 'from' attribute.");
String value = project.getProperty(from);
if (value == null && ! silent)
throw new BuildException("Property '" + from + "' is not defined.");
if (value != null)
{
if (project.getUserProperty(name) == null)
project.setProperty(name, value);
else
project.setUserProperty(name, value);
}
}
}
package org.apache.tools.ant.taskdefs.optional;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.DirectoryScanner;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.Target;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.CallTarget;
import org.apache.tools.ant.taskdefs.Property;
import org.apache.tools.ant.types.FileSet;
import java.io.File;
import java.util.Vector;
import java.util.StringTokenizer;
/***
* Task definition for the foreach task
* Usage:
*
* Task declaration in the project:
* <taskdef name="foreach" classname="org.apache.tools.ant.taskdefs.optional" />
*
* Call Syntax:
* <foreach list="values" target="targ" param="name"
* [delimiter="delim"] />
*
* Attributes:
* list --> The list of values to process, with the delimiter character,
* indicated by the "delim" attribute, separating each value
* target --> The target to call for each token, passing the token as the
* parameter with the name indicated by the "param" attribute
* param --> The name of the parameter to pass the tokens in as to the
* target
* delimiter --> The delimiter string that separates the values in the "list"
* parameter
*
*/
public class ForEach extends Task
{
private String list;
private String param;
private String delimiter;
private String target;
private Vector filesets;
/***
* Default Constructor
*/
public ForEach()
{
super();
this.list = null;
this.param = null;
this.delimiter = ",";
this.target = null;
this.filesets = new Vector();
}
public void execute()
throws BuildException
{
if (list == null && filesets.size() == 0)
throw new BuildException("You must have a list or fileset to iterate through");
if (param == null)
throw new BuildException("You must supply a property name to set on each iteration");
if (target == null)
throw new BuildException("You must supply an action to perform");
// Take Care of the list attribute
if (list != null)
{
StringTokenizer st = new StringTokenizer(list, delimiter);
String toks[] = new String[st.countTokens()];
int i = 0;
while (st.hasMoreTokens())
{
String tok = st.nextToken();
CallTarget ct = (CallTarget)(project.createTask("antcall"));
ct.getProject().setBaseDir(getProject().getBaseDir());
ct.setOwningTarget(getOwningTarget());
ct.init();
ct.setTarget(target);
Property p = ct.createParam();
p.setName(param);
p.setValue(tok);
ct.execute();
}
}
int sz = filesets.size();
for (int i=0;i<sz;i++)
{
FileSet fs = (FileSet)(filesets.elementAt(i));
DirectoryScanner ds = fs.getDirectoryScanner(getProject());
String files[] = ds.getIncludedFiles();
for (int j=0;j<files.length;j++)
{
CallTarget ct = (CallTarget)(project.createTask("antcall"));
ct.getProject().setBaseDir(getProject().getBaseDir());
ct.setOwningTarget(getOwningTarget());
ct.init();
ct.setTarget(target);
Property p = ct.createParam();
p.setName(param);
p.setValue(new File(files[j]).getAbsolutePath());
ct.execute();
}
String dirs[] = ds.getIncludedDirectories();
for (int j=0;j<dirs.length;j++)
{
CallTarget ct = (CallTarget)(project.createTask("antcall"));
ct.getProject().setBaseDir(getProject().getBaseDir());
ct.setOwningTarget(getOwningTarget());
ct.init();
ct.setTarget(target);
Property p = ct.createParam();
p.setName(param);
p.setValue(new File(dirs[j]).getAbsolutePath());
ct.execute();
}
}
}
public void setList(String list)
{
this.list = list;
}
public void setDelimiter(String delimiter)
{
this.delimiter = delimiter;
}
public void setParam(String param)
{
this.param = param;
}
public void setTarget(String target)
{
this.target = target;
}
public void addFileset(FileSet set)
{
filesets.addElement(set);
}
}