Sorry, forgot to include Apache file header.


On Fri, 2001-09-21 at 14:06, Matthew Inger wrote:
> 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);
>     }
> 
> }
> 
> 
-- 
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 
/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights 
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:  
 *       "This product includes software developed by the 
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written 
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */
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:
 * <pre>
 *
 *   Task declaration in the project:
 *     &lt;taskdef name="foreach" classname="org.apache.tools.ant.taskdefs.optional" />
 *
 *   Call Syntax:
 *     &lt;foreach list="values" target="targ" param="name"
 *              [delimiter="delim"] /&gt;
 *
 *   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
 * </pre>
 *
 * @author <a href="mailto:[EMAIL PROTECTED]">Matthew Inger</a>
 */
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);
    }

}


/*
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999 The Apache Software Foundation.  All rights 
 * reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution, if
 *    any, must include the following acknowlegement:  
 *       "This product includes software developed by the 
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Ant", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written 
 *    permission, please contact [EMAIL PROTECTED]
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 */
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:
 *     &lt;taskdef name="propertycopy" classname="org.apache.tools.ant.taskdefs.optional.PropertyCopy" /&gt;
 *
 *   Call Syntax:
 *     &lt;propertycopy name="propname" from="copyfrom" (silent="true|false")? /&gt;
 *
 *   Attributes:
 *     name      --&gt; The name of the property you wish to set with the value
 *     from      --&gt; The name of the property you wish to copy the value from
 *     silent    --&gt; Do you want to suppress the error if the "from" property
 *                   does not exist, and just not set the property "name".
 *
 *   Example:
 *     &lt;property name="org" value="MyOrg" /&gt;
 *     &lt;property name="org.MyOrg.DisplayName" value="My Organiziation" /&gt;
 *     &lt;propertycopy name="displayName" from="org.${org}.DisplayName" /&gt;
 *     &lt;echo message="${displayName}" /&gt;
 * </pre>
 *
 * @author <a href="mailto:[EMAIL PROTECTED]">Matthew Inger</a>
 */
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);
        }
    }

}


Reply via email to