KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sync4j > test > tools > ant > IterateTask


1 /**
2  * Copyright (C) 2003-2005 Funambol
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package sync4j.test.tools.ant;
19
20 import java.util.StringTokenizer JavaDoc;
21
22 import org.apache.tools.ant.Task;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.taskdefs.Ant;
25 import org.apache.tools.ant.taskdefs.Property;
26
27
28 /**
29  * This class is a Jakarta Ant custom task that repeatedly calls a target
30  * passing different values from a delimited string.
31  *
32  * <p>For example:
33  * <pre>
34  * &lt;project name="Test" default="main" basedir="."&gt;
35  * &lt;taskdef name="iterate" classname="sync4j.test.tools.ant.IterateTask"/&gt;
36  *
37  * &lt;target name="main"&gt;
38  * &lt;iterate target="hello" itemList="1,2,3" property="i" &gt;
39  * &lt;/iterate&gt;
40  * &lt;/target&gt;
41  *
42  * &lt;target name="hello"&gt;
43  * &lt;echo message="${i}" /&gt;
44  * &lt;/target&gt;
45  * &lt;/project&gt;
46  * </pre>
47  *
48  * <p>The above example will call the hello target three times, each time
49  * passing a value from the item list. In this case the hello target will
50  * echo 1, then 2 and then 3.
51  *
52  * <p>A more useful example is the ability to compile multiple source
53  * directories into multiple jar files. For example:
54  *
55  * <pre>
56  * &lt;target name="build" depends="init"&gt;
57  *
58  * &lt;!-- iterate through the ${build.modules} variable, compiling each module specified --&gt;
59  * &lt;iterate target="javac" itemList="myModule,myModule/mySubModule" property="iterate.module"/&gt;
60  * &lt;/target&gt;
61  *
62  * &lt;target name="javac" depends="checkSource, cleanBuild, prepareBuild" if="compile.source.exist"&gt;
63  *
64  * &lt;javac srcdir="${iterate.module}/src" destdir="${iterate.module}/build"&gt;
65  * &lt;include name="**\/*.java"/&gt;
66  * &lt;/javac&gt;
67  *
68  * &lt;!-- create a jar file for each module--&gt;
69  * &lt;mkdir dir="${iterate.module}/lib"/&gt;
70  * &lt;jar jarfile="${iterate.module}/lib/classes.jar"&gt;
71  * &lt;fileset dir="${iterate.module}/build"/&gt;
72  * &lt;/jar&gt;
73  * &lt;/target&gt;
74  * </pre>
75  *
76  * <p>The about example does the following:
77  * <ul>
78  * <li>compiles the myModule/src directory into myModule/lib/classes.jar
79  * <li>compiles the myModule/mySubModule/src directory into myModule/mySubModule/lib/classes.jar
80  * <ul>
81  *
82  * <p>List of attributes:
83  * <table border>
84  * <tr><th>Attribute</th><th>Description</th><th>Required</th></tr>
85  * <tr><td>target</td><td>This is the name of the target to call.</td><td>Yes</td></tr>
86  * <tr><td>property</td><td>The name of the property in which each value from the item list will be stored for each iteration. This allows the called target to access each item from the item list.</td><td>Yes</td></tr>
87  * <tr><td>items</td><td>A delimited list of items strings.</td><td>Yes</td></tr>
88  * <tr><td>iheritAll</td><td>Boolean to enable/disable the called target from inheriting all the properties from the environment.</td><td>No</td></tr>
89  * <tr><td>delimiter</td><td>The delimiter that is used to delimited the strings in the item list.</td><td>No</td></tr>
90  * </table>
91
92  * @author Stefano Fornari
93  * @version $Id: IterateTask.java,v 1.5 2005/03/02 20:57:40 harrie Exp $
94  */

95 public class IterateTask extends Task {
96
97   /** Default constructor.
98    */

99   public IterateTask() {}
100
101   /** Set the item list string. The item list can contain one or more
102    * delimited strings. The specified target will be called once for each
103    * string in the item list.
104    *
105    * <p>The delimiter can be changed by specifying the
106    * delimiter attribute.
107    *
108    * @param items Delimited string of items.
109    */

110   public void setItems(String JavaDoc items) {
111       this.items = items;
112   }
113
114   /** Set the Ant target name that will be called repeatedly, once for each
115    * item in the item list.
116    *
117    * @param targetName The name of the target to call repeatedly.
118    */

119   public void setTarget(String JavaDoc targetName) {
120     this.targetName = targetName;
121   }
122
123   /** Sets the inherit all flag. If the value is true, then the target that
124    * is called will inherit all the properties. Default is true.
125    *
126    * @param inheritAll Inherit flag.
127    */

128   public void setInheritAll(boolean inheritAll) {
129     this.inheritAll = inheritAll;
130   }
131
132   /** Set the Property. The property attribute is the name of the property
133    * that will contain each item in the item list.
134    *
135    * @param property Property Name
136    */

137   public void setProperty(String JavaDoc property) {
138     this.property = property;
139   }
140
141   /** Set the delimiter that will be used to delimit the strings in the item
142    * list, the default is comma ",".
143    *
144    * @param delimiter Delimiter charater.
145    */

146   public void setDelimiter(String JavaDoc delimiter) {
147     this.delimiter = delimiter;
148   }
149
150   /** Ant execute service method, called when the task is executed.
151    *
152    * @exception BuildException When required attributes are not set
153    */

154   public void execute() throws BuildException {
155
156     validateAttributes();
157
158     // initialise the target
159
task.setDir(getProject().getBaseDir());
160     task.setAntfile(getProject().getProperty("ant.file"));
161     task.setTarget(targetName);
162     task.setInheritAll(inheritAll);
163
164     // call the target for each item in the item list
165
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(items, delimiter);
166     while (st.hasMoreTokens()) {
167       getProject().setProperty(property, (st.nextToken().trim()));
168       task.execute();
169     }
170   }
171
172   /** Ant init service method, to initialise the task.
173    *
174    * @exception BuildException Build exception.
175    */

176   public void init() throws BuildException {
177     super.init();
178
179     // initialise the called target
180
task = (Ant) getProject().createTask("ant");
181     task.setOwningTarget(getOwningTarget());
182     task.setTaskName(targetName);
183     task.setLocation(getLocation());
184     task.init();
185   }
186
187   /** Ant create param service method, which is called for each embedded param
188    * element.
189    *
190    * @return Property object.
191    */

192   public Property createParam() {
193     return task.createProperty();
194   }
195
196   
197   // ----------------------------------------------------------- Private methods
198

199   private void validateAttributes() throws BuildException {
200       if (isEmpty(targetName)) {
201           throw new BuildException("Attribute target is required.", getLocation());
202       }
203
204       if (isEmpty(property)) {
205           throw new BuildException("Attribute property is required.", getLocation());
206       }
207
208       if (items == null) {
209           throw new BuildException("Attribute items is required.", getLocation());
210       }
211   }
212   
213   private boolean isEmpty(String JavaDoc s) {
214       return ((s == null) || (s.length() == 0));
215   }
216   
217   // -------------------------------------------------------------- Private data
218

219   private String JavaDoc items;
220   private String JavaDoc targetName;
221   private boolean inheritAll = true;
222   private Ant task;
223   private String JavaDoc property;
224   private String JavaDoc delimiter = ",";
225
226 }
Popular Tags