KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > savant > ant > taskdefs > MacroForeachTask


1 /*
2  * Copyright (C) 2002-2004, Inversoft Corporation, All Rights Reserved.
3  */

4 package com.inversoft.savant.ant.taskdefs;
5
6
7 import java.io.File JavaDoc;
8 import java.util.ArrayList JavaDoc;
9 import java.util.Arrays JavaDoc;
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.StringTokenizer JavaDoc;
13
14 import org.apache.tools.ant.BuildException;
15 import org.apache.tools.ant.Project;
16 import org.apache.tools.ant.Task;
17 import org.apache.tools.ant.taskdefs.MacroInstance;
18 import org.apache.tools.ant.types.FileSet;
19 import org.apache.tools.ant.types.Mapper;
20 import org.apache.tools.ant.util.FileNameMapper;
21 import org.apache.tools.ant.util.IdentityMapper;
22
23
24 /**
25  * Run an Ant task several times, setting a param from a tokenized list
26  * or fileset each time.
27  *
28  * target - target to be called, like antcall. Required.
29  * property - name of property to set to value on each call. Required.
30  * mappedproperty - name of property to be set to mapped value on each call.
31  * delims - delimiters for tokenizing values. Defaults to "\t\n, ".
32  * list - a delimited list of values. target is called once with each of
33  * these values setting property.
34  * fileset - target is called once for each file, with property set
35  * to the full path to the file.
36  * dirset - target is called once for each file, with property set
37  * to the full path to the directory.
38  *
39  * An optional mapper is allowed, the default is Identity.
40  * If specified, then target is only called for those values or files which
41  * match the mapper, and mappedproperty is set to the result "to" value on
42  * each call. Works for both filesets and values.
43  *
44  *
45  * See the AntCall task.
46  */

47 public class MacroForeachTask extends Task {
48
49     private String JavaDoc macroName;
50     private String JavaDoc delims = ", \t\n";
51     private String JavaDoc property = null;
52     private String JavaDoc mappedProperty = null;
53     private String JavaDoc list = null;
54     private final List JavaDoc filesets = new ArrayList JavaDoc();
55     private final List JavaDoc dirsets = new ArrayList JavaDoc();
56     private final List JavaDoc params = new ArrayList JavaDoc();
57     private Mapper mapperElement;
58
59
60     public void setMacro(String JavaDoc macroName) {
61         this.macroName = macroName;
62     }
63
64     /**
65      * Sets the property to be set with each call.
66      */

67     public void setProperty(String JavaDoc property) {
68         this.property = property;
69     }
70
71     /**
72      * Sets the property to be set with each call.
73      */

74     public void setMappedproperty(String JavaDoc property) {
75         this.mappedProperty = property;
76     }
77
78     /**
79      * Sets the list of values to iterate over
80      */

81     public void setList(String JavaDoc list) {
82         this.list = list;
83     }
84
85     /**
86      * Sets tokens.
87      */

88     public void setDelims(String JavaDoc delims) {
89         this.delims = delims;
90     }
91
92     /**
93      * Adds a set of files (nested fileset attribute).
94      */

95     public void addFileset(FileSet set) {
96         filesets.add(set);
97     }
98
99     /**
100      * Adds a new nested dirset
101      */

102     public void addDirset(FileSet set) {
103         dirsets.add(set);
104     }
105
106     /**
107      * Adds a new parameter that is passed to the macro.
108      */

109     public void addParam(Param param) {
110         params.add(param);
111     }
112
113     /**
114      * Creates a new nested mapper
115      */

116     public Mapper createMapper() throws BuildException {
117         if (mapperElement != null) {
118             throw new BuildException("Cannot define more than one mapper",
119                 getLocation());
120         } else {
121             mapperElement = new Mapper(getProject());
122             return mapperElement;
123         }
124     }
125
126     /**
127      * Do the looping
128      */

129     public void execute() throws BuildException {
130         validate();
131
132         // Set up mapper
133
FileNameMapper mapper = null;
134         if (mapperElement != null) {
135             mapper = mapperElement.getImplementation();
136         } else {
137             mapper = new IdentityMapper();
138         }
139
140         // Loop for tokenized values
141
if (list != null) {
142             for (StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(list, delims);
143                     st.hasMoreTokens(); ) {
144                 String JavaDoc val = st.nextToken();
145                 String JavaDoc[] val2 = mapper.mapFileName(val);
146
147                 callTarget(val, val2);
148             }
149         }
150
151         // loop for fileset
152
if (filesets.size() != 0) {
153
154             // Walk thru the filesets
155
for (Iterator JavaDoc sets = filesets.iterator(); sets.hasNext(); ) {
156                 FileSet fs = (FileSet) sets.next();
157                 File JavaDoc fromDir = fs.getDir(getProject());
158                 ArrayList JavaDoc fromFiles = new ArrayList JavaDoc();
159
160                 fromFiles.addAll(
161                     Arrays.asList(fs.getDirectoryScanner(getProject()).getIncludedFiles()));
162
163                 for (Iterator JavaDoc files = fromFiles.iterator(); files.hasNext(); ) {
164                     String JavaDoc f = (String JavaDoc) files.next();
165                     String JavaDoc val = new File JavaDoc(fromDir, f).getPath();
166                     String JavaDoc[] val2 = mapper.mapFileName(val);
167
168                     callTarget(val, val2);
169                 }
170             }
171         }
172
173         // loop for dirset
174
if (dirsets.size() != 0) {
175
176             // Walk thru the dirsets
177
for (Iterator JavaDoc sets = dirsets.iterator(); sets.hasNext(); ) {
178                 FileSet fs = (FileSet) sets.next();
179                 File JavaDoc fromDir = fs.getDir(getProject());
180                 ArrayList JavaDoc fromFiles = new ArrayList JavaDoc();
181
182                 fromFiles.addAll(
183                     Arrays.asList(fs.getDirectoryScanner(getProject()).getIncludedDirectories()));
184
185                 for (Iterator JavaDoc files = fromFiles.iterator(); files.hasNext(); ) {
186                     String JavaDoc f = (String JavaDoc) files.next();
187                     String JavaDoc val = new File JavaDoc(fromDir, f).getPath();
188                     String JavaDoc[] val2 = mapper.mapFileName(val);
189
190                     callTarget(val, val2);
191                 }
192             }
193         }
194     }
195
196     private void validate() {
197         if (property == null && mappedProperty == null) {
198             throw new BuildException( "Specify a property or mappedproperty name.",
199                 getLocation());
200         }
201
202         if (!(list == null ^ (filesets.size() == 0 && dirsets.size() == 0))) {
203             throw new BuildException("Specify either list or a fileset.", getLocation());
204         }
205     }
206
207     /**
208      * Calls the ant target that the loop is setup to call
209      */

210     private void callTarget(String JavaDoc val, String JavaDoc[] val2) {
211         if (val2 == null || val2.length == 0) {
212             log("Skipping because no mapped property", Project.MSG_DEBUG);
213             return;
214         }
215
216         Task task = getProject().createTask(macroName);
217         if (!(task instanceof MacroInstance)) {
218             throw new BuildException("Only macros can be used with the marcoforeach task");
219         }
220
221         for (int j = 0; j < val2.length; ++j) {
222             MacroInstance macro = (MacroInstance) task;
223             if (property != null) {
224                 macro.setDynamicAttribute(property, val);
225             }
226
227             if (mappedProperty != null) {
228                 macro.setDynamicAttribute(mappedProperty, val2[j]);
229             }
230
231             // Add the extra parameters
232
for (Iterator JavaDoc iter = params.iterator(); iter.hasNext(); ) {
233                 Param param = (Param) iter.next();
234                 macro.setDynamicAttribute(param.getName(), param.getValue());
235             }
236
237             macro.perform();
238         }
239     }
240
241
242     /**
243      * Cheesy inner class for supporting parameters to marcos.
244      */

245     public static class Param {
246         private String JavaDoc name;
247         private String JavaDoc value;
248
249         public String JavaDoc getName() {
250             return name;
251         }
252
253         public void setName(String JavaDoc name) {
254             this.name = name;
255         }
256
257         public String JavaDoc getValue() {
258             return value;
259         }
260
261         public void setValue(String JavaDoc value) {
262             this.value = value;
263         }
264     }
265 }
Popular Tags