KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > spoon > SpoonTask


1 package spoon;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Vector JavaDoc;
7
8 import org.apache.tools.ant.BuildException;
9 import org.apache.tools.ant.DirectoryScanner;
10 import org.apache.tools.ant.taskdefs.Java;
11 import org.apache.tools.ant.types.FileSet;
12
13 /**
14  * This class implements an Ant task for Spoon that encapsulates
15  * {@link spoon.Launcher}.
16  */

17 public class SpoonTask extends Java {
18
19     /**
20      * Nested element to define a processor type.
21      */

22     public static class ProcessorType {
23         String JavaDoc type;
24
25         /**
26          * Ant-required empty constructor.
27          */

28         public ProcessorType() {
29         }
30
31         /**
32          * Constructs a new processor type.
33          *
34          * @param type
35          * the type's fully qualified name
36          */

37         public ProcessorType(String JavaDoc type) {
38             setType(type);
39         }
40
41         /**
42          * Gets the processor type.
43          */

44         public String JavaDoc getType() {
45             return type;
46         }
47
48         /**
49          * Sets the processor's type as a string representing the Java qualified
50          * name.
51          */

52         public void setType(String JavaDoc type) {
53             this.type = type;
54         }
55     }
56
57     String JavaDoc classname;
58
59     boolean compile = false;
60
61     File JavaDoc input;
62
63     int javaCompliance = 5;
64
65     boolean nooutput = false;
66
67     File JavaDoc output;
68
69     File JavaDoc build;
70
71     File JavaDoc spoonlet;
72
73     Vector JavaDoc<FileSet> spoonletfileset = new Vector JavaDoc<FileSet>();
74
75     List JavaDoc<ProcessorType> processorTypes = new ArrayList JavaDoc<ProcessorType>();
76
77     File JavaDoc properties;
78
79     Vector JavaDoc<FileSet> sourcefilesets = new Vector JavaDoc<FileSet>();
80
81     boolean stats = false;
82
83     File JavaDoc template;
84
85     Vector JavaDoc<FileSet> templatefilesets = new Vector JavaDoc<FileSet>();
86
87     boolean verbose = false;
88
89     /**
90      * Constructor.
91      */

92     public SpoonTask() {
93         setClassname("spoon.Launcher");
94     }
95
96     /**
97      * Adds a new processor type to be instantiated and used by Spoon when
98      * processing the code.
99      */

100     public void addProcessor(ProcessorType processorType) {
101         this.processorTypes.add(processorType);
102     }
103
104     /**
105      * Adds a source set.
106      */

107     public void addSourceSet(FileSet set) {
108         sourcefilesets.addElement(set);
109     }
110
111     /**
112      * Adds a template source set.
113      */

114     public void addTemplateSet(FileSet set) {
115         templatefilesets.addElement(set);
116     }
117
118     /**
119      * Executes the task.
120      */

121     @Override JavaDoc
122     public void execute() throws BuildException {
123
124         setFork(true);
125
126         // Verbose
127
if (verbose)
128             createArg().setValue("-v");
129
130         if (nooutput) {
131             createArg().setValue("--no");
132         } else {
133             if (compile) {
134                 createArg().setValue("--compile");
135
136                 createArg().setValue("--build");
137
138                 createArg().setFile(build);
139
140             }
141         }
142
143         createArg().setValue("--compliance");
144         createArg().setValue("" + javaCompliance);
145
146         // Input directories
147
if (spoonlet != null || spoonletfileset.size() > 0) {
148             createArg().setValue("-s");
149             String JavaDoc f = "";
150             if (spoonlet != null) {
151                 f += spoonlet.getAbsolutePath() + File.pathSeparator;
152             }
153             for (int i = 0; i < spoonletfileset.size(); i++) {
154                 FileSet fs = (FileSet) spoonletfileset.elementAt(i);
155                 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
156                 File JavaDoc dir = fs.getDir(getProject());
157                 String JavaDoc[] srcs = ds.getIncludedFiles();
158                 for (int j = 0; j < srcs.length; j++) {
159                     f += dir.getAbsolutePath() + File.separatorChar + srcs[j]
160                             + File.pathSeparator;
161                 }
162             }
163             createArg().setValue(f);
164         }
165
166         // output directory
167
if (output != null) {
168             if (output.exists() && !output.isDirectory())
169                 throw new BuildException("Output must be a directory");
170             createArg().setValue("-o");
171             createArg().setValue(output.getAbsolutePath());
172         }
173         // Input directories
174
if (input != null || sourcefilesets.size() > 0) {
175             createArg().setValue("-i");
176             String JavaDoc f = "";
177             if (input != null) {
178                 f += input.getAbsolutePath() + File.pathSeparator;
179             }
180             for (int i = 0; i < sourcefilesets.size(); i++) {
181                 FileSet fs = (FileSet) sourcefilesets.elementAt(i);
182                 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
183                 File JavaDoc dir = fs.getDir(getProject());
184                 String JavaDoc[] srcs = ds.getIncludedFiles();
185                 for (int j = 0; j < srcs.length; j++) {
186                     f += dir.getAbsolutePath() + File.separatorChar + srcs[j]
187                             + File.pathSeparator;
188                 }
189             }
190             createArg().setValue(f);
191         }
192
193         // Template directories
194
if (template != null || templatefilesets.size() > 0) {
195             createArg().setValue("-t");
196             String JavaDoc f = "";
197             if (template != null) {
198                 f += template.getAbsolutePath() + File.pathSeparator;
199             }
200             for (int i = 0; i < templatefilesets.size(); i++) {
201                 FileSet fs = (FileSet) templatefilesets.elementAt(i);
202                 DirectoryScanner ds = fs.getDirectoryScanner(getProject());
203                 File JavaDoc dir = fs.getDir(getProject());
204                 String JavaDoc[] srcs = ds.getIncludedFiles();
205                 for (int j = 0; j < srcs.length; j++) {
206                     f += dir.getAbsolutePath() + File.separatorChar + srcs[j]
207                             + File.pathSeparator;
208                 }
209             }
210             createArg().setValue(f);
211         }
212
213         // Properties directories
214
if (properties != null) {
215             createArg().setValue("--properties");
216             createArg().setValue(properties.getAbsolutePath());
217         }
218
219         // processors
220
if (processorTypes != null && processorTypes.size() > 0) {
221             createArg().setValue("-p");
222             String JavaDoc process = "";
223             for (ProcessorType t : processorTypes)
224                 process += t.type + File.pathSeparator;
225             createArg().setValue(process);
226         }
227
228         if (classname != null)
229             createArg().setValue(classname);
230
231         super.execute();
232
233     }
234
235     /**
236      * Sets the name of the laucher to be used.
237      */

238     public void setClassName(String JavaDoc classname) {
239         this.classname = classname;
240     }
241
242     /**
243      * Sets Spoon to be in verbose mode.
244      */

245     public void setCompile(boolean compile) {
246         this.compile = compile;
247     }
248
249     /**
250      * Sets a file or a directory to be processed (no templates, see
251      * {@link #setTemplate(File)}).
252      */

253     public void setInput(File JavaDoc input) {
254         this.input = input;
255     }
256
257     /**
258      * Sets a Spoolet to be deployed.
259      * @param spoonlet the deployment descriptor file (usually spoon.xml)
260      */

261     public void setSpoonlet(File JavaDoc spoonlet) {
262         this.spoonlet = spoonlet;
263     }
264
265     /**
266      * Sets the java14 property (to be able to parse java 1.4 source files).
267      */

268     public void setJavaCompliance(int javaCompliance) {
269         this.javaCompliance = javaCompliance;
270     }
271
272     /**
273      * Tells Spoon not to generate any files.
274      */

275     public void setNoOutput(boolean nooutput) {
276         this.nooutput = nooutput;
277     }
278
279     /**
280      * Sets the output directory for generated sources.
281      */

282     public void setOutput(File JavaDoc output) {
283         this.output = output;
284     }
285
286     /**
287      * Sets the output directory for generated sources.
288      */

289     public void setBuild(File JavaDoc build) {
290         this.build = build;
291     }
292
293     /**
294      * Sets the root directory where the processors' properties XML
295      * configuration files are located.
296      */

297     public void setProperties(File JavaDoc properties) {
298         this.properties = properties;
299     }
300
301     /**
302      * Enables/disable printing out statistics on Spoon execution time.
303      */

304     public void setStats(boolean stats) {
305         this.stats = stats;
306     }
307
308     /**
309      * Sets a file or a directory to be processed (only templates, see
310      * {@link #setInput(File)}).
311      */

312     public void setTemplate(File JavaDoc template) {
313         this.template = template;
314     }
315
316     /**
317      * Sets Spoon to be in verbose mode.
318      */

319     public void setVerbose(boolean verbose) {
320         this.verbose = verbose;
321     }
322
323 }
324
Popular Tags