| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 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 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
public class ClassPathTask extends Task |
| 40 |
{ |
| 41 |
private String project; |
| 42 |
private String idContainer = "antclipse"; |
| 43 |
private boolean includeSource = false; |
| 44 |
private boolean includeOutput = false; |
| 45 |
private boolean includeLibs = true; |
| 46 |
private boolean verbose = false; |
| 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; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public void setIncludeLibs(boolean includeLibs) |
| 58 |
{ |
| 59 |
this.includeLibs = includeLibs; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public void setproduce(String produce) |
| 67 |
{ |
| 68 |
this.produce = produce; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
public void setVerbose(boolean verbose) |
| 76 |
{ |
| 77 |
this.verbose = verbose; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public void setExcludes(String excludes) |
| 85 |
{ |
| 86 |
if (excludes != null) |
| 87 |
{ |
| 88 |
erpm = new RegexpPatternMapper(); |
| 89 |
erpm.setFrom(excludes); |
| 90 |
erpm.setTo("."); |
| 91 |
} |
| 92 |
else |
| 93 |
erpm = null; |
| 94 |
} |
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
public void setIncludes(String includes) |
| 101 |
{ |
| 102 |
if (includes != null) |
| 103 |
{ |
| 104 |
irpm = new RegexpPatternMapper(); |
| 105 |
irpm.setFrom(includes); |
| 106 |
irpm.setTo("."); |
| 107 |
} |
| 108 |
else |
| 109 |
irpm = null; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public void setIdContainer(String idContainer) |
| 117 |
{ |
| 118 |
this.idContainer = idContainer; |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
public void setIncludeOutput(boolean includeOutput) |
| 126 |
{ |
| 127 |
this.includeOutput = includeOutput; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
public void setIncludeSource(boolean includeSource) |
| 135 |
{ |
| 136 |
this.includeSource = includeSource; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
public void setProject(String project) |
| 144 |
{ |
| 145 |
this.project = project; |
| 146 |
} |
| 147 |
|
| 148 |
|
| 149 |
|
| 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 |
|
| 191 |
|
| 192 |
private FileSetCustomHandler() |
| 193 |
{ |
| 194 |
} |
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public FileSetCustomHandler(FileSet fileSet) |
| 200 |
{ |
| 201 |
super(); |
| 202 |
this.fileSet = fileSet; |
| 203 |
projDir = getProject().getBaseDir().getAbsolutePath().toString(); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
public void endDocument() throws SAXException |
| 210 |
{ |
| 211 |
super.endDocument(); |
| 212 |
if (fileSet != null && !fileSet.hasPatterns()) |
| 213 |
fileSet.setExcludes("**/*"); |
| 214 |
|
| 215 |
} |
| 216 |
|
| 217 |
public void startElement(String tag, AttributeList attrs) throws SAXParseException |
| 218 |
{ |
| 219 |
if (tag.equalsIgnoreCase("classpathentry")) |
| 220 |
{ |
| 221 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 260 |
|
| 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 |
|
| 274 |
if (kind.equalsIgnoreCase(ATTR_OUTPUT)) |
| 275 |
{ |
| 276 |
|
| 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("**/*"); |
| 291 |
} |
| 292 |
else |
| 293 |
if (kind.equalsIgnoreCase(ATTR_SRC)) |
| 294 |
{ |
| 295 |
|
| 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("**/*"); |
| 310 |
} |
| 311 |
else |
| 312 |
{ |
| 313 |
|
| 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 |
|
| 339 |
|
| 340 |
public PathCustomHandler(Path path) |
| 341 |
{ |
| 342 |
super(); |
| 343 |
this.path = path; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 396 |
|
| 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 |
|
| 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 |
} |