Debian Codegraph @

src/main/java/net/sf/antcontrib/antclipse/ClassPathTask.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package net.sf.antcontrib.antclipse;
17 import java.io.File;
18
19 import org.apache.tools.ant.BuildException;
20 import org.apache.tools.ant.Task;
21 import org.apache.tools.ant.taskdefs.Property;
22 import org.apache.tools.ant.types.FileSet;
23 import org.apache.tools.ant.types.Path;
24 import org.apache.tools.ant.types.Path.PathElement;
25 import org.apache.tools.ant.util.RegexpPatternMapper;
26 import org.xml.sax.AttributeList;
27 import org.xml.sax.HandlerBase;
28 import org.xml.sax.SAXException;
29 import org.xml.sax.SAXParseException;
30
31 /**
32 * Support class for the Antclipse task. Basically, it takes the .classpath Eclipse file
33 * and feeds a SAX parser. The handler is slightly different according to what we want to
34 * obtain (a classpath or a fileset)
35 * @author Adrian Spinei [email protected]
36 * @version $Revision: 1.2 $
37 * @since Ant 1.5
38 */
39 public class ClassPathTask extends Task
40 {
41 private String project;
42 private String idContainer = "antclipse";
43 private boolean includeSource = false; //default, do not include source
44 private boolean includeOutput = false; //default, do not include output directory
45 private boolean includeLibs = true; //default, include all libraries
46 private boolean verbose = false; //default quiet
47 RegexpPatternMapper irpm = null;
48 RegexpPatternMapper erpm = null;
49 public static final String TARGET_CLASSPATH = "classpath";
50 public static final String TARGET_FILESET = "fileset";
51 private String produce = null; //classpath by default
52
53 /**
54 * Setter for task parameter
55 * @param includeLibs Boolean, whether to include or not the project libraries. Default is true.
56 */
57 public void setIncludeLibs(boolean includeLibs)
58 {
59 this.includeLibs = includeLibs;
60 }
61
62 /**
63 * Setter for task parameter
64 * @param produce This parameter tells the task wether to produce a "classpath" or a "fileset" (multiple filesets, as a matter of fact).
65 */
66 public void setproduce(String produce)
67 {
68 this.produce = produce;
69 }
70
71 /**
72 * Setter for task parameter
73 * @param verbose Boolean, telling the app to throw some info during each step. Default is false.
74 */
75 public void setVerbose(boolean verbose)
76 {
77 this.verbose = verbose;
78 }
79
80 /**
81 * Setter for task parameter
82 * @param excludes A regexp for files to exclude. It is taken into account only when producing a classpath, doesn't work on source or output files. It is a real regexp, not a "*" expression.
83 */
84 public void setExcludes(String excludes)
85 {
86 if (excludes != null)
87 {
88 erpm = new RegexpPatternMapper();
89 erpm.setFrom(excludes);
90 erpm.setTo("."); //mandatory
91 }
92 else
93 erpm = null;
94 }
95
96 /**
97 * Setter for task parameter
98 * @param includes A regexp for files to include. It is taken into account only when producing a classpath, doesn't work on source or output files. It is a real regexp, not a "*" expression.
99 */
100 public void setIncludes(String includes)
101 {
102 if (includes != null)
103 {
104 irpm = new RegexpPatternMapper();
105 irpm.setFrom(includes);
106 irpm.setTo("."); //mandatory
107 }
108 else
109 irpm = null;
110 }
111
112 /**
113 * Setter for task parameter
114 * @param idContainer The refid which will serve to identify the deliverables. When multiple filesets are produces, their refid is a concatenation between this value and something else (usually obtained from a path). Default "antclipse"
115 */
116 public void setIdContainer(String idContainer)
117 {
118 this.idContainer = idContainer;
119 }
120
121 /**
122 * Setter for task parameter
123 * @param includeOutput Boolean, whether to include or not the project output directories. Default is false.
124 */
125 public void setIncludeOutput(boolean includeOutput)
126 {
127 this.includeOutput = includeOutput;
128 }
129
130 /**
131 * Setter for task parameter
132 * @param includeSource Boolean, whether to include or not the project source directories. Default is false.
133 */
134 public void setIncludeSource(boolean includeSource)
135 {
136 this.includeSource = includeSource;
137 }
138
139 /**
140 * Setter for task parameter
141 * @param project project name
142 */
143 public void setProject(String project)
144 {
145 this.project = project;
146 }
147
148 /**
149 * @see org.apache.tools.ant.Task#execute()
150 */
151 public void execute() throws BuildException
152 {
153 if (!TARGET_CLASSPATH.equalsIgnoreCase(this.produce) && !TARGET_FILESET.equals(this.produce))
154 throw new BuildException(
155 "Mandatory target must be either '" + TARGET_CLASSPATH + "' or '" + TARGET_FILESET + "'");
156 ClassPathParser parser = new ClassPathParser();
157 AbstractCustomHandler handler;
158 if (TARGET_CLASSPATH.equalsIgnoreCase(this.produce))
159 {
160 Path path = new Path(this.getProject());
161 this.getProject().addReference(this.idContainer, path);
162 handler = new PathCustomHandler(path);
163 }
164 else
165 {
166 FileSet fileSet = new FileSet();
167 this.getProject().addReference(this.idContainer, fileSet);
168 fileSet.setDir(new File(this.getProject().getBaseDir().getAbsolutePath().toString()));
169 handler = new FileSetCustomHandler(fileSet);
170 }
171 parser.parse(new File(this.getProject().getBaseDir().getAbsolutePath(), ".classpath"), handler);
172 }
173
174 abstract class AbstractCustomHandler extends HandlerBase
175 {
176 protected String projDir;
177 protected static final String ATTRNAME_PATH = "path";
178 protected static final String ATTRNAME_KIND = "kind";
179 protected static final String ATTR_LIB = "lib";
180 protected static final String ATTR_SRC = "src";
181 protected static final String ATTR_OUTPUT = "output";
182 protected static final String EMPTY = "";
183 }
184
185 class FileSetCustomHandler extends AbstractCustomHandler
186 {
187 private FileSet fileSet = null;
188
189 /**
190 * nazi style, forbid default constructor
191 */
192 private FileSetCustomHandler()
193 {
194 }
195
196 /**
197 * @param fileSet
198 */
199 public FileSetCustomHandler(FileSet fileSet)
200 {
201 super();
202 this.fileSet = fileSet;
203 projDir = getProject().getBaseDir().getAbsolutePath().toString();
204 }
205
206 /**
207 * @see org.xml.sax.DocumentHandler#endDocument()
208 */
209 public void endDocument() throws SAXException
210 {
211 super.endDocument();
212 if (fileSet != null && !fileSet.hasPatterns())
213 fileSet.setExcludes("**/*");
214 //exclude everything or we'll take all the project dirs
215 }
216
217 public void startElement(String tag, AttributeList attrs) throws SAXParseException
218 {
219 if (tag.equalsIgnoreCase("classpathentry"))
220 {
221 //start by checking if the classpath is coherent at all
222 String kind = attrs.getValue(ATTRNAME_KIND);
223 if (kind == null)
224 throw new BuildException("classpathentry 'kind' attribute is mandatory");
225 String path = attrs.getValue(ATTRNAME_PATH);
226 if (path == null)
227 throw new BuildException("classpathentry 'path' attribute is mandatory");
228
229 //put the outputdirectory in a property
230 if (kind.equalsIgnoreCase(ATTR_OUTPUT))
231 {
232 String propName = idContainer + "outpath";
233 Property property = new Property();
234 property.setName(propName);
235 property.setValue(path);
236 property.setProject(getProject());
237 property.execute();
238 if (verbose)
239 System.out.println("Setting property " + propName + " to value " + path);
240 }
241
242 //let's put the last source directory in a property
243 if (kind.equalsIgnoreCase(ATTR_SRC))
244 {
245 String propName = idContainer + "srcpath";
246 Property property = new Property();
247 property.setName(propName);
248 property.setValue(path);
249 property.setProject(getProject());
250 property.execute();
251 if (verbose)
252 System.out.println("Setting property " + propName + " to value " + path);
253 }
254
255 if ((kind.equalsIgnoreCase(ATTR_SRC) && includeSource)
256 || (kind.equalsIgnoreCase(ATTR_OUTPUT) && includeOutput)
257 || (kind.equalsIgnoreCase(ATTR_LIB) && includeLibs))
258 {
259 //all seem fine
260 // check the includes
261 String[] inclResult = new String[] { "all included" };
262 if (irpm != null)
263 {
264 inclResult = irpm.mapFileName(path);
265 }
266 String[] exclResult = null;
267 if (erpm != null)
268 {
269 exclResult = erpm.mapFileName(path);
270 }
271 if (inclResult != null && exclResult == null)
272 {
273 //THIS is the specific code
274 if (kind.equalsIgnoreCase(ATTR_OUTPUT))
275 {
276 //we have included output so let's build a new fileset
277 FileSet outFileSet = new FileSet();
278 String newReference = idContainer + "-" + path.replace(File.separatorChar, '-');
279 getProject().addReference(newReference, outFileSet);
280 if (verbose)
281 System.out.println(
282 "Created new fileset "
283 + newReference
284 + " containing all the files from the output dir "
285 + projDir
286 + File.separator
287 + path);
288 outFileSet.setDefaultexcludes(false);
289 outFileSet.setDir(new File(projDir + File.separator + path));
290 outFileSet.setIncludes("**/*"); //get everything
291 }
292 else
293 if (kind.equalsIgnoreCase(ATTR_SRC))
294 {
295 //we have included source so let's build a new fileset
296 FileSet srcFileSet = new FileSet();
297 String newReference = idContainer + "-" + path.replace(File.separatorChar, '-');
298 getProject().addReference(newReference, srcFileSet);
299 if (verbose)
300 System.out.println(
301 "Created new fileset "
302 + newReference
303 + " containing all the files from the source dir "
304 + projDir
305 + File.separator
306 + path);
307 srcFileSet.setDefaultexcludes(false);
308 srcFileSet.setDir(new File(projDir + File.separator + path));
309 srcFileSet.setIncludes("**/*"); //get everything
310 }
311 else
312 {
313 //not otuptut, just add file after file to the fileset
314 File file = new File(fileSet.getDir(getProject()) + "/" + path);
315 if (file.isDirectory())
316 path += "/**/*";
317 if (verbose)
318 System.out.println(
319 "Adding "
320 + path
321 + " to fileset "
322 + idContainer
323 + " at "
324 + fileSet.getDir(getProject()));
325 fileSet.setIncludes(path);
326 }
327 }
328 }
329 }
330 }
331 }
332
333 class PathCustomHandler extends AbstractCustomHandler
334 {
335 private Path path = null;
336
337 /**
338 * @param path the path to add files
339 */
340 public PathCustomHandler(Path path)
341 {
342 super();
343 this.path = path;
344 }
345
346 /**
347 * nazi style, forbid default constructor
348 */
349 private PathCustomHandler()
350 {
351 }
352
353 public void startElement(String tag, AttributeList attrs) throws SAXParseException
354 {
355 if (tag.equalsIgnoreCase("classpathentry"))
356 {
357 //start by checking if the classpath is coherent at all
358 String kind = attrs.getValue(ATTRNAME_KIND);
359 if (kind == null)
360 throw new BuildException("classpathentry 'kind' attribute is mandatory");
361 String path = attrs.getValue(ATTRNAME_PATH);
362 if (path == null)
363 throw new BuildException("classpathentry 'path' attribute is mandatory");
364
365 //put the outputdirectory in a property
366 if (kind.equalsIgnoreCase(ATTR_OUTPUT))
367 {
368 String propName = idContainer + "outpath";
369 Property property = new Property();
370 property.setName(propName);
371 property.setValue(path);
372 property.setProject(getProject());
373 property.execute();
374 if (verbose)
375 System.out.println("Setting property " + propName + " to value " + path);
376 }
377
378 //let's put the last source directory in a property
379 if (kind.equalsIgnoreCase(ATTR_SRC))
380 {
381 String propName = idContainer + "srcpath";
382 Property property = new Property();
383 property.setName(propName);
384 property.setValue(path);
385 property.setProject(getProject());
386 property.execute();
387 if (verbose)
388 System.out.println("Setting property " + propName + " to value " + path);
389 }
390
391 if ((kind.equalsIgnoreCase(ATTR_SRC) && includeSource)
392 || (kind.equalsIgnoreCase(ATTR_OUTPUT) && includeOutput)
393 || (kind.equalsIgnoreCase(ATTR_LIB) && includeLibs))
394 {
395 //all seem fine
396 // check the includes
397 String[] inclResult = new String[] { "all included" };
398 if (irpm != null)
399 {
400 inclResult = irpm.mapFileName(path);
401 }
402 String[] exclResult = null;
403 if (erpm != null)
404 {
405 exclResult = erpm.mapFileName(path);
406 }
407 if (inclResult != null && exclResult == null)
408 {
409 //THIS is the only specific code
410 if (verbose)
411 System.out.println("Adding " + path + " to classpath " + idContainer);
412 PathElement element = this.path.createPathElement();
413 element.setLocation(new File(path));
414 }
415 }
416 }
417 }
418 }
419 }