summaryrefslogtreecommitdiffstats
path: root/src/main/java/net/sf/antcontrib/antclipse/ClassPathTask.java
blob: 6c32181129f7196d11487c10ad1790a5663c200d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 * Copyright (c) 2001-2004 Ant-Contrib project.  All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.sf.antcontrib.antclipse;
import java.io.File;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
import org.apache.tools.ant.taskdefs.Property;
import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.Path;
import org.apache.tools.ant.types.Path.PathElement;
import org.apache.tools.ant.util.RegexpPatternMapper;
import org.xml.sax.AttributeList;
import org.xml.sax.HandlerBase;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 * Support class for the Antclipse task. Basically, it takes the .classpath Eclipse file
 * and feeds a SAX parser. The handler is slightly different according to what we want to
 * obtain (a classpath or a fileset)
 * @author Adrian Spinei aspinei@myrealbox.com
 * @version $Revision: 1.2 $
  * @since Ant 1.5
 */
public class ClassPathTask extends Task
{
	private String project;
	private String idContainer = "antclipse";
	private boolean includeSource = false; //default, do not include source
	private boolean includeOutput = false; //default, do not include output directory
	private boolean includeLibs = true; //default, include all libraries
	private boolean verbose = false; //default quiet
	RegexpPatternMapper irpm = null;
	RegexpPatternMapper erpm = null;
	public static final String TARGET_CLASSPATH = "classpath";
	public static final String TARGET_FILESET = "fileset";
	private String produce = null; //classpath by default

	/**
	 * Setter for task parameter
	 * @param includeLibs Boolean, whether to include or not the project libraries. Default is true.
	 */
	public void setIncludeLibs(boolean includeLibs)
	{
		this.includeLibs = includeLibs;
	}

	/**
	 * Setter for task parameter
	 * @param produce This parameter tells the task wether to produce a "classpath" or a "fileset" (multiple filesets, as a matter of fact).
	 */
	public void setproduce(String produce)
	{
		this.produce = produce;
	}

	/**
	 * Setter for task parameter
	 * @param verbose Boolean, telling the app to throw some info during each step. Default is false.
	 */
	public void setVerbose(boolean verbose)
	{
		this.verbose = verbose;
	}

	/**
	 * Setter for task parameter
	 * @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.
	 */
	public void setExcludes(String excludes)
	{
		if (excludes != null)
		{
			erpm = new RegexpPatternMapper();
			erpm.setFrom(excludes);
			erpm.setTo("."); //mandatory
		}
		else
			erpm = null;
	}

	/**
	 * Setter for task parameter
	 * @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.
	 */
	public void setIncludes(String includes)
	{
		if (includes != null)
		{
			irpm = new RegexpPatternMapper();
			irpm.setFrom(includes);
			irpm.setTo("."); //mandatory
		}
		else
			irpm = null;
	}

	/**
	 * Setter for task parameter
	 * @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" 
	 */
	public void setIdContainer(String idContainer)
	{
		this.idContainer = idContainer;
	}

	/**
	 * Setter for task parameter
	 * @param includeOutput Boolean, whether to include or not the project output directories. Default is false.
	 */
	public void setIncludeOutput(boolean includeOutput)
	{
		this.includeOutput = includeOutput;
	}

	/**
	 * Setter for task parameter
	 * @param includeSource Boolean, whether to include or not the project source directories. Default is false.
	 */
	public void setIncludeSource(boolean includeSource)
	{
		this.includeSource = includeSource;
	}

	/**
	 * Setter for task parameter
	 * @param project project name
	 */
	public void setProject(String project)
	{
		this.project = project;
	}

	/**
	 * @see org.apache.tools.ant.Task#execute()
	 */
	public void execute() throws BuildException
	{
		if (!TARGET_CLASSPATH.equalsIgnoreCase(this.produce) && !TARGET_FILESET.equals(this.produce))
			throw new BuildException(
				"Mandatory target must be either '" + TARGET_CLASSPATH + "' or '" + TARGET_FILESET + "'");
		ClassPathParser parser = new ClassPathParser();
		AbstractCustomHandler handler;
		if (TARGET_CLASSPATH.equalsIgnoreCase(this.produce))
		{
			Path path = new Path(this.getProject());
			this.getProject().addReference(this.idContainer, path);
			handler = new PathCustomHandler(path);
		}
		else
		{
			FileSet fileSet = new FileSet();
			this.getProject().addReference(this.idContainer, fileSet);
			fileSet.setDir(new File(this.getProject().getBaseDir().getAbsolutePath().toString()));
			handler = new FileSetCustomHandler(fileSet);
		}
		parser.parse(new File(this.getProject().getBaseDir().getAbsolutePath(), ".classpath"), handler);
	}

	abstract class AbstractCustomHandler extends HandlerBase
	{
		protected String projDir;
		protected static final String ATTRNAME_PATH = "path";
		protected static final String ATTRNAME_KIND = "kind";
		protected static final String ATTR_LIB = "lib";
		protected static final String ATTR_SRC = "src";
		protected static final String ATTR_OUTPUT = "output";
		protected static final String EMPTY = "";
	}

	class FileSetCustomHandler extends AbstractCustomHandler
	{
		private FileSet fileSet = null;

		/**
		 * nazi style, forbid default constructor
		 */
		private FileSetCustomHandler()
		{
		}

		/**
		 * @param fileSet
		 */
		public FileSetCustomHandler(FileSet fileSet)
		{
			super();
			this.fileSet = fileSet;
			projDir = getProject().getBaseDir().getAbsolutePath().toString();
		}

		/**
		 * @see org.xml.sax.DocumentHandler#endDocument()
		 */
		public void endDocument() throws SAXException
		{
			super.endDocument();
			if (fileSet != null && !fileSet.hasPatterns())
				fileSet.setExcludes("**/*");
			//exclude everything or we'll take all the project dirs
		}

		public void startElement(String tag, AttributeList attrs) throws SAXParseException
		{
			if (tag.equalsIgnoreCase("classpathentry"))
			{
				//start by checking if the classpath is coherent at all
				String kind = attrs.getValue(ATTRNAME_KIND);
				if (kind == null)
					throw new BuildException("classpathentry 'kind' attribute is mandatory");
				String path = attrs.getValue(ATTRNAME_PATH);
				if (path == null)
					throw new BuildException("classpathentry 'path' attribute is mandatory");

				//put the outputdirectory in a property
				if (kind.equalsIgnoreCase(ATTR_OUTPUT))
				{
					String propName = idContainer + "outpath";
					Property property = new Property();
					property.setName(propName);
					property.setValue(path);
					property.setProject(getProject());
					property.execute();
					if (verbose)
						System.out.println("Setting property " + propName + " to value " + path);
				}

				//let's put the last source directory in a property
				if (kind.equalsIgnoreCase(ATTR_SRC))
				{
					String propName = idContainer + "srcpath";
					Property property = new Property();
					property.setName(propName);
					property.setValue(path);
					property.setProject(getProject());
					property.execute();
					if (verbose)
						System.out.println("Setting property " + propName + " to value " + path);
				}

				if ((kind.equalsIgnoreCase(ATTR_SRC) && includeSource)
					|| (kind.equalsIgnoreCase(ATTR_OUTPUT) && includeOutput)
					|| (kind.equalsIgnoreCase(ATTR_LIB) && includeLibs))
				{
					//all seem fine
					//	check the includes
					String[] inclResult = new String[] { "all included" };
					if (irpm != null)
					{
						inclResult = irpm.mapFileName(path);
					}
					String[] exclResult = null;
					if (erpm != null)
					{
						exclResult = erpm.mapFileName(path);
					}
					if (inclResult != null && exclResult == null)
					{
						//THIS is the specific code
						if (kind.equalsIgnoreCase(ATTR_OUTPUT))
						{
							//we have included output so let's build a new fileset
							FileSet outFileSet = new FileSet();
							String newReference = idContainer + "-" + path.replace(File.separatorChar, '-');
							getProject().addReference(newReference, outFileSet);
							if (verbose)
								System.out.println(
									"Created new fileset "
										+ newReference
										+ " containing all the files from the output dir "
										+ projDir
										+ File.separator
										+ path);
							outFileSet.setDefaultexcludes(false);
							outFileSet.setDir(new File(projDir + File.separator + path));
							outFileSet.setIncludes("**/*"); //get everything
						}
						else
							if (kind.equalsIgnoreCase(ATTR_SRC))
							{
								//we have included source so let's build a new fileset
								FileSet srcFileSet = new FileSet();
								String newReference = idContainer + "-" + path.replace(File.separatorChar, '-');
								getProject().addReference(newReference, srcFileSet);
								if (verbose)
									System.out.println(
										"Created new fileset "
											+ newReference
											+ " containing all the files from the source dir "
											+ projDir
											+ File.separator
											+ path);
								srcFileSet.setDefaultexcludes(false);
								srcFileSet.setDir(new File(projDir + File.separator + path));
								srcFileSet.setIncludes("**/*"); //get everything
							}
							else
							{
								//not otuptut, just add file after file to the fileset
								File file = new File(fileSet.getDir(getProject()) + "/" + path);
								if (file.isDirectory())
									path += "/**/*";
								if (verbose)
									System.out.println(
										"Adding  "
											+ path
											+ " to fileset "
											+ idContainer
											+ " at "
											+ fileSet.getDir(getProject()));
								fileSet.setIncludes(path);
							}
					}
				}
			}
		}
	}

	class PathCustomHandler extends AbstractCustomHandler
	{
		private Path path = null;

		/**
		 * @param path the path to add files
		 */
		public PathCustomHandler(Path path)
		{
			super();
			this.path = path;
		}

		/**
		 * nazi style, forbid default constructor
		 */
		private PathCustomHandler()
		{
		}

		public void startElement(String tag, AttributeList attrs) throws SAXParseException
		{
			if (tag.equalsIgnoreCase("classpathentry"))
			{
				//start by checking if the classpath is coherent at all
				String kind = attrs.getValue(ATTRNAME_KIND);
				if (kind == null)
					throw new BuildException("classpathentry 'kind' attribute is mandatory");
				String path = attrs.getValue(ATTRNAME_PATH);
				if (path == null)
					throw new BuildException("classpathentry 'path' attribute is mandatory");

				//put the outputdirectory in a property
				if (kind.equalsIgnoreCase(ATTR_OUTPUT))
				{
					String propName = idContainer + "outpath";
					Property property = new Property();
					property.setName(propName);
					property.setValue(path);
					property.setProject(getProject());
					property.execute();
					if (verbose)
						System.out.println("Setting property " + propName + " to value " + path);
				}

				//let's put the last source directory in a property
				if (kind.equalsIgnoreCase(ATTR_SRC))
				{
					String propName = idContainer + "srcpath";
					Property property = new Property();
					property.setName(propName);
					property.setValue(path);
					property.setProject(getProject());
					property.execute();
					if (verbose)
						System.out.println("Setting property " + propName + " to value " + path);
				}

				if ((kind.equalsIgnoreCase(ATTR_SRC) && includeSource)
					|| (kind.equalsIgnoreCase(ATTR_OUTPUT) && includeOutput)
					|| (kind.equalsIgnoreCase(ATTR_LIB) && includeLibs))
				{
					//all seem fine
					//	check the includes
					String[] inclResult = new String[] { "all included" };
					if (irpm != null)
					{
						inclResult = irpm.mapFileName(path);
					}
					String[] exclResult = null;
					if (erpm != null)
					{
						exclResult = erpm.mapFileName(path);
					}
					if (inclResult != null && exclResult == null)
					{
						//THIS is the only specific code
						if (verbose)
							System.out.println("Adding  " + path + " to classpath " + idContainer);
						PathElement element = this.path.createPathElement();
						element.setLocation(new File(path));
					}
				}
			}
		}
	}
}