KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > antcontrib > logic > ForEach


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  * Copyright (c) 2001-2002 Ant-Contrib project. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution, if
19  * any, must include the following acknowlegement:
20  * "This product includes software developed by the
21  * Ant-Contrib project (http://sourceforge.net/projects/ant-contrib)."
22  * Alternately, this acknowlegement may appear in the software itself,
23  * if and wherever such third-party acknowlegements normally appear.
24  *
25  * 4. The name Ant-Contrib must not be used to endorse or promote products
26  * derived from this software without prior written permission. For
27  * written permission, please contact
28  * ant-contrib-developers@lists.sourceforge.net.
29  *
30  * 5. Products derived from this software may not be called "Ant-Contrib"
31  * nor may "Ant-Contrib" appear in their names without prior written
32  * permission of the Ant-Contrib project.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
35  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
36  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
37  * DISCLAIMED. IN NO EVENT SHALL THE ANT-CONTRIB PROJECT OR ITS
38  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
39  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
40  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
41  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
42  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
43  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
44  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45  * SUCH DAMAGE.
46  * ====================================================================
47  */

48 package net.sf.antcontrib.logic;
49
50 import org.apache.tools.ant.BuildException;
51 import org.apache.tools.ant.DirectoryScanner;
52 import org.apache.tools.ant.Project;
53 import org.apache.tools.ant.Target;
54 import org.apache.tools.ant.Task;
55 import org.apache.tools.ant.taskdefs.Ant;
56 import org.apache.tools.ant.taskdefs.CallTarget;
57 import org.apache.tools.ant.taskdefs.Property;
58 import org.apache.tools.ant.types.FileSet;
59 import java.io.File;
60 import java.util.Enumeration;
61 import java.util.StringTokenizer;
62 import java.util.Vector;
63
64 /***
65  * Task definition for the foreach task. The foreach task iterates
66  * over a list, a list of filesets, or both.
67  *
68  * <pre>
69  *
70  * Usage:
71  *
72  * Task declaration in the project:
73  * <code>
74  * &lt;taskdef name="foreach" classname="net.sf.antcontrib.logic.ForEach" /&gt;
75  * </code>
76  *
77  * Call Syntax:
78  * <code>
79  * &lt;foreach list="values" target="targ" param="name"
80  * [delimiter="delim"] /&gt;
81  * </code>
82  *
83  * Attributes:
84  * list --> The list of values to process, with the delimiter character,
85  * indicated by the "delim" attribute, separating each value
86  * target --> The target to call for each token, passing the token as the
87  * parameter with the name indicated by the "param" attribute
88  * param --> The name of the parameter to pass the tokens in as to the
89  * target
90  * delimiter --> The delimiter string that separates the values in the "list"
91  * parameter. The default is ","
92  *
93  * </pre>
94  * @author <a HREF="mailto:mattinger@mindless.com">Matthew Inger</a>
95  */

96 public class ForEach extends Task
97 {
98     private String list;
99     private String param;
100     private String delimiter;
101     private String target;
102     private boolean inheritAll;
103     private boolean inheritRefs;
104     private Vector filesets;
105     private Vector params;
106     private Vector references;
107
108     /***
109      * Default Constructor
110      */

111     public ForEach()
112     {
113         super();
114         this.list = null;
115         this.param = null;
116         this.delimiter = ",";
117         this.target = null;
118         this.inheritAll = false;
119         this.inheritRefs = false;
120         this.filesets = new Vector();
121         this.params = new Vector();
122         this.references = new Vector();
123     }
124
125     public void execute()
126         throws BuildException
127     {
128         if (list == null && filesets.size() == 0)
129             throw new BuildException("You must have a list or fileset to iterate through");
130         if (param == null)
131             throw new BuildException("You must supply a property name to set on each iteration");
132         if (target == null)
133             throw new BuildException("You must supply an action to perform");
134
135         // Take Care of the list attribute
136
if (list != null)
137         {
138             StringTokenizer st = new StringTokenizer(list, delimiter);
139             String toks[] = new String[st.countTokens()];
140             int i = 0;
141             
142             while (st.hasMoreTokens())
143             {
144                 String tok = st.nextToken();
145                 CallTarget ct = createCallTarget();
146                 Property p = ct.createParam();
147                 p.setName(param);
148                 p.setValue(tok);
149                 ct.execute();
150             }
151         }
152
153         int sz = filesets.size();
154         for (int i=0;i<sz;i++)
155         {
156             FileSet fs = (FileSet)(filesets.elementAt(i));
157             DirectoryScanner ds = fs.getDirectoryScanner(getProject());
158
159             String files[] = ds.getIncludedFiles();
160             for (int j=0;j<files.length;j++)
161             {
162                 CallTarget ct = createCallTarget();
163                 Property p = ct.createParam();
164                 p.setName(param);
165                 p.setLocation(new File(fs.getDir(getProject()), files[j]));
166                 ct.execute();
167             }
168
169             String dirs[] = ds.getIncludedDirectories();
170             for (int j=0;j<dirs.length;j++)
171             {
172                 CallTarget ct = createCallTarget();
173                 Property p = ct.createParam();
174                 p.setName(param);
175                 p.setLocation(new File(fs.getDir(getProject()), dirs[j]));
176                 ct.execute();
177             }
178         }
179     }
180
181     public void setList(String list)
182     {
183         this.list = list;
184     }
185
186     public void setDelimiter(String delimiter)
187     {
188         this.delimiter = delimiter;
189     }
190
191     public void setParam(String param)
192     {
193         this.param = param;
194     }
195
196     public void setTarget(String target)
197     {
198         this.target = target;
199     }
200
201     /**
202      * Corresponds to <code>&lt;antcall&gt;</code>'s <code>inheritall</code>
203      * attribute.
204      */

205     public void setInheritall(boolean b) {
206         this.inheritAll = b;
207     }
208
209     /**
210      * Corresponds to <code>&lt;antcall&gt;</code>'s <code>inheritrefs</code>
211      * attribute.
212      */

213     public void setInheritrefs(boolean b) {
214         this.inheritRefs = b;
215     }
216
217     /**
218      * Corresponds to <code>&lt;antcall&gt;</code>'s nested
219      * <code>&lt;param&gt;</code> element.
220      */

221     public void addParam(Property p) {
222         params.addElement(p);
223     }
224
225     /**
226      * Corresponds to <code>&lt;antcall&gt;</code>'s nested
227      * <code>&lt;reference&gt;</code> element.
228      */

229     public void addReference(Ant.Reference r) {
230         references.addElement(r);
231     }
232
233     public void addFileset(FileSet set)
234     {
235         filesets.addElement(set);
236     }
237
238     private CallTarget createCallTarget() {
239         CallTarget ct = (CallTarget) getProject().createTask("antcall");
240         ct.setOwningTarget(getOwningTarget());
241         ct.init();
242         ct.setTarget(target);
243         ct.setInheritAll(inheritAll);
244         ct.setInheritRefs(inheritRefs);
245         Enumeration enum = params.elements();
246         while (enum.hasMoreElements()) {
247             Property param = (Property) enum.nextElement();
248             Property toSet = ct.createParam();
249             toSet.setName(param.getName());
250             if (param.getValue() != null) {
251                 toSet.setValue(param.getValue());
252             }
253             if (param.getFile() != null) {
254                 toSet.setFile(param.getFile());
255             }
256             if (param.getResource() != null) {
257                 toSet.setResource(param.getResource());
258             }
259             if (param.getPrefix() != null) {
260                 toSet.setPrefix(param.getPrefix());
261             }
262             if (param.getRefid() != null) {
263                 toSet.setRefid(param.getRefid());
264             }
265             if (param.getEnvironment() != null) {
266                 toSet.setEnvironment(param.getEnvironment());
267             }
268             if (param.getClasspath() != null) {
269                 toSet.setClasspath(param.getClasspath());
270             }
271         }
272         
273         enum = references.elements();
274         while (enum.hasMoreElements()) {
275             ct.addReference((Ant.Reference) enum.nextElement());
276         }
277
278         return ct;
279     }
280
281 }
282
283
284
Popular Tags