1 18 19 package org.apache.tools.ant.taskdefs.optional.jsp; 20 21 import java.io.File ; 22 import java.util.Date ; 23 import java.util.Enumeration ; 24 import java.util.Vector ; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.DirectoryScanner; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.taskdefs.MatchingTask; 29 import org.apache.tools.ant.taskdefs.optional.jsp.compilers.JspCompilerAdapter; 30 import org.apache.tools.ant.taskdefs.optional.jsp.compilers.JspCompilerAdapterFactory; 31 import org.apache.tools.ant.types.Path; 32 import org.apache.tools.ant.types.Reference; 33 34 65 public class JspC extends MatchingTask { 66 private Path classpath; 67 private Path compilerClasspath; 68 private Path src; 69 private File destDir; 70 private String packageName; 71 72 private String compilerName = "jasper"; 73 74 77 private String iepluginid; 78 private boolean mapped; 79 private int verbose = 0; 80 protected Vector compileList = new Vector (); 82 Vector javaFiles = new Vector (); 83 84 87 protected boolean failOnError = true; 88 89 93 private File uriroot; 94 95 98 private File webinc; 99 100 103 104 private File webxml; 105 106 109 protected WebAppParameter webApp; 110 111 112 113 private static final String FAIL_MSG 114 = "Compile failed, messages should have been provided."; 115 116 118 122 public void setSrcDir(Path srcDir) { 123 if (src == null) { 124 src = srcDir; 125 } else { 126 src.append(srcDir); 127 } 128 } 129 130 134 public Path getSrcDir() { 135 return src; 136 } 137 138 143 public void setDestdir(File destDir) { 144 this.destDir = destDir; 145 } 146 147 151 public File getDestdir() { 152 return destDir; 153 } 154 155 159 public void setPackage(String pkg) { 160 this.packageName = pkg; 161 } 162 163 167 public String getPackage() { 168 return packageName; 169 } 170 171 175 public void setVerbose(int i) { 176 verbose = i; 177 } 178 179 183 public int getVerbose() { 184 return verbose; 185 } 186 187 192 public void setFailonerror(boolean fail) { 193 failOnError = fail; 194 } 195 199 public boolean getFailonerror() { 200 return failOnError; 201 } 202 203 207 public String getIeplugin() { 208 return iepluginid; 209 } 210 214 public void setIeplugin(String iepluginid) { 215 this.iepluginid = iepluginid; 216 } 217 218 223 public boolean isMapped() { 224 return mapped; 225 } 226 227 232 public void setMapped(boolean mapped) { 233 this.mapped = mapped; 234 } 235 236 243 public void setUribase(File uribase) { 244 log("Uribase is currently an unused parameter", Project.MSG_WARN); 245 } 246 247 251 public File getUribase() { 252 return uriroot; 253 } 254 255 261 public void setUriroot(File uriroot) { 262 this.uriroot = uriroot; 263 } 264 265 269 public File getUriroot() { 270 return uriroot; 271 } 272 273 274 278 public void setClasspath(Path cp) { 279 if (classpath == null) { 280 classpath = cp; 281 } else { 282 classpath.append(cp); 283 } 284 } 285 286 290 public Path createClasspath() { 291 if (classpath == null) { 292 classpath = new Path(getProject()); 293 } 294 return classpath.createPath(); 295 } 296 297 301 public void setClasspathRef(Reference r) { 302 createClasspath().setRefid(r); 303 } 304 305 309 public Path getClasspath() { 310 return classpath; 311 } 312 313 317 public void setCompilerclasspath(Path cp) { 318 if (compilerClasspath == null) { 319 compilerClasspath = cp; 320 } else { 321 compilerClasspath.append(cp); 322 } 323 } 324 325 329 public Path getCompilerclasspath() { 330 return compilerClasspath; 331 } 332 333 337 public Path createCompilerclasspath() { 338 if (compilerClasspath == null) { 339 compilerClasspath = new Path(getProject()); 340 } 341 return compilerClasspath.createPath(); 342 } 343 344 349 public void setWebxml(File webxml) { 350 this.webxml = webxml; 351 } 352 353 357 public File getWebxml() { 358 return this.webxml; 359 } 360 361 366 public void setWebinc(File webinc) { 367 this.webinc = webinc; 368 } 369 370 374 public File getWebinc() { 375 return this.webinc; 376 } 377 378 384 public void addWebApp(WebAppParameter webappParam) 385 throws BuildException { 386 if (webApp == null) { 388 webApp = webappParam; 389 } else { 390 throw new BuildException("Only one webapp can be specified"); 391 } 392 } 393 394 398 public WebAppParameter getWebApp() { 399 return webApp; 400 } 401 402 406 public void setCompiler(String compiler) { 407 this.compilerName = compiler; 408 } 409 410 414 public Vector getCompileList() { 415 return compileList; 416 } 417 418 423 public void execute() 424 throws BuildException { 425 426 if (destDir == null) { 428 throw new BuildException("destdir attribute must be set!", 429 getLocation()); 430 } 431 432 if (!destDir.isDirectory()) { 433 throw new BuildException("destination directory \"" + destDir 434 + "\" does not exist or is not a directory", getLocation()); 435 } 436 437 File dest = getActualDestDir(); 438 439 JspCompilerAdapter compiler = 441 JspCompilerAdapterFactory.getCompiler(compilerName, this, 442 getProject().createClassLoader(compilerClasspath)); 443 444 if (webApp != null) { 446 doCompilation(compiler); 447 return; 448 } 449 450 if (src == null) { 452 throw new BuildException("srcdir attribute must be set!", 453 getLocation()); 454 } 455 String [] list = src.list(); 456 if (list.length == 0) { 457 throw new BuildException("srcdir attribute must be set!", 458 getLocation()); 459 } 460 461 462 if (compiler.implementsOwnDependencyChecking()) { 464 doCompilation(compiler); 465 return; 466 } 467 468 JspMangler mangler = compiler.createMangler(); 470 471 resetFileLists(); 474 int filecount = 0; 475 for (int i = 0; i < list.length; i++) { 476 File srcDir = getProject().resolveFile(list[i]); 477 if (!srcDir.exists()) { 478 throw new BuildException("srcdir \"" + srcDir.getPath() 479 + "\" does not exist!", getLocation()); 480 } 481 DirectoryScanner ds = this.getDirectoryScanner(srcDir); 482 String [] files = ds.getIncludedFiles(); 483 filecount = files.length; 484 scanDir(srcDir, dest, mangler, files); 485 } 486 487 489 log("compiling " + compileList.size() + " files", Project.MSG_VERBOSE); 490 491 if (compileList.size() > 0) { 492 493 log("Compiling " + compileList.size() + " source file" 494 + (compileList.size() == 1 ? "" : "s") 495 + " to " 496 + dest); 497 doCompilation(compiler); 498 499 } else { 500 if (filecount == 0) { 501 log("there were no files to compile", Project.MSG_INFO); 502 } else { 503 log("all files are up to date", Project.MSG_VERBOSE); 504 } 505 } 506 } 507 508 512 private File getActualDestDir() { 513 File dest = null; 514 if (packageName == null) { 515 dest = destDir; 516 } else { 517 String path = destDir.getPath() + File.separatorChar 518 + packageName.replace('.', File.separatorChar); 519 dest = new File (path); 520 } 521 return dest; 522 } 523 524 527 private void doCompilation(JspCompilerAdapter compiler) 528 throws BuildException { 529 compiler.setJspc(this); 531 532 if (!compiler.execute()) { 534 if (failOnError) { 535 throw new BuildException(FAIL_MSG, getLocation()); 536 } else { 537 log(FAIL_MSG, Project.MSG_ERR); 538 } 539 } 540 } 541 542 545 protected void resetFileLists() { 546 compileList.removeAllElements(); 547 } 548 549 557 protected void scanDir( 558 File srcDir, File dest, JspMangler mangler, String [] files) { 559 560 long now = (new Date ()).getTime(); 561 562 for (int i = 0; i < files.length; i++) { 563 String filename = files[i]; 564 File srcFile = new File (srcDir, filename); 565 File javaFile = mapToJavaFile(mangler, srcFile, srcDir, dest); 566 if (javaFile == null) { 567 continue; 568 } 569 570 if (srcFile.lastModified() > now) { 571 log("Warning: file modified in the future: " + filename, 572 Project.MSG_WARN); 573 } 574 boolean shouldCompile = false; 575 shouldCompile = isCompileNeeded(srcFile, javaFile); 576 if (shouldCompile) { 577 compileList.addElement(srcFile.getAbsolutePath()); 578 javaFiles.addElement(javaFile); 579 } 580 } 581 } 582 583 599 private boolean isCompileNeeded(File srcFile, File javaFile) { 600 boolean shouldCompile = false; 601 if (!javaFile.exists()) { 602 shouldCompile = true; 603 log("Compiling " + srcFile.getPath() 604 + " because java file " + javaFile.getPath() 605 + " does not exist", Project.MSG_VERBOSE); 606 } else { 607 if (srcFile.lastModified() > javaFile.lastModified()) { 608 shouldCompile = true; 609 log("Compiling " + srcFile.getPath() 610 + " because it is out of date with respect to " 611 + javaFile.getPath(), 612 Project.MSG_VERBOSE); 613 } else { 614 if (javaFile.length() == 0) { 615 shouldCompile = true; 616 log("Compiling " + srcFile.getPath() 617 + " because java file " + javaFile.getPath() 618 + " is empty", Project.MSG_VERBOSE); 619 } 620 } 621 } 622 return shouldCompile; 623 } 624 625 626 635 protected File mapToJavaFile(JspMangler mangler, File srcFile, File srcDir, File dest) { 636 if (!srcFile.getName().endsWith(".jsp")) { 637 return null; 638 } 639 String javaFileName = mangler.mapJspToJavaName(srcFile); 640 return new File (dest, javaFileName); 642 } 643 644 649 public void deleteEmptyJavaFiles() { 650 if (javaFiles != null) { 651 Enumeration e = javaFiles.elements(); 652 while (e.hasMoreElements()) { 653 File file = (File ) e.nextElement(); 654 if (file.exists() && file.length() == 0) { 655 log("deleting empty output file " + file); 656 file.delete(); 657 } 658 } 659 } 660 } 661 662 665 public static class WebAppParameter { 666 667 670 private File directory; 671 672 676 public File getDirectory() { 677 return directory; 678 } 679 680 684 public void setBaseDir(File directory) { 685 this.directory = directory; 686 } 687 } 689 690 691 } 693 | Popular Tags |