1 32 package org.enhydra.xml.xmlc.taskdef; 33 34 import java.io.File ; 35 import java.lang.reflect.InvocationTargetException ; 36 import java.lang.reflect.Method ; 37 import java.util.ArrayList ; 38 39 import org.apache.tools.ant.BuildException; 40 import org.apache.tools.ant.DirectoryScanner; 41 import org.apache.tools.ant.Project; 42 import org.apache.tools.ant.types.Commandline; 43 import org.apache.tools.ant.types.EnumeratedAttribute; 44 45 46 195 public class Xmlc extends org.apache.tools.ant.taskdefs.MatchingTask { 196 197 protected static String logHdr = " [xmlc] "; 198 protected File srcDir, destDir, srcOutDir; 199 protected String classPath, packageDir, packageName, srcDirName, srcOutDirName; 200 protected String options = null; 201 protected String xmlcArgs = null; 202 protected String performExec = null; 203 protected boolean verbose, forceBuild, compile; 204 protected boolean keep = true; 205 protected int logLevel = Project.MSG_VERBOSE; 206 protected int upToDate = 0; 207 protected String ML = "HTML"; 211 protected Commandline cmdl = new Commandline(); 212 213 218 public void setSrcdir(String srcdirname) { 219 srcDirName = srcdirname; 220 srcDir = getProject().resolveFile(srcDirName); 221 } 222 223 229 public void setDestdir(String destDirName) { 230 destDir = getProject().resolveFile(destDirName); 231 } 232 233 240 public void setArgs(String args) { 241 this.xmlcArgs = args; 242 getProject().log(logHdr+"DEPRECATED, The 'args' attribute. Use <arg> tags instead.", Project.MSG_WARN); 243 } 244 245 251 public void setClasspath(String classpath) { 252 this.classPath = classpath; 253 getProject().log(logHdr+"DEPRECATED, The 'classpath' attribute. Use <arg> tags instead.", Project.MSG_WARN); 254 } 255 256 265 public void setSourceout(String sourceout) { 266 this.srcOutDirName = sourceout; 267 this.srcOutDir = getProject().resolveFile(sourceout); 268 } 269 270 275 public void setForce(BooleanAttribute force) { 276 this.forceBuild = force.booleanValue(); 277 } 278 279 285 public void setVerbose(BooleanAttribute verbose) { 286 this.verbose = verbose.booleanValue(); 287 if (this.verbose) { 290 logLevel = Project.MSG_INFO; 291 } else { 292 logLevel = Project.MSG_VERBOSE; 293 } 294 } 295 296 301 public void setKeep(BooleanAttribute keep) { 302 this.keep = keep.booleanValue(); 303 } 304 305 310 public void setCompile(BooleanAttribute compile) { 311 this.compile = compile.booleanValue(); 312 } 313 314 321 public void setOptions(String options) { 322 this.options = options; 323 } 324 325 338 public void setPackagedir(String packagedir) { 339 packageDir = packagedir; 340 } 341 342 362 public void setPackagename(String packagename) { 363 this.packageName = packagename; 364 } 365 366 375 public void setMarkup(String markup) { 376 this.ML = markup; 377 } 378 379 387 public void setPerformexec(String performExec) { 388 this.performExec = performExec; 389 } 390 391 396 private boolean isNewerThanFiles(File target, File [] files) { 397 boolean newer = true; 398 for (int i = 0 ; i < files.length ; i++) { 399 if (files[i].lastModified() > target.lastModified()) { 400 newer = false; 401 getProject().log(logHdr+" " + files[i].getPath() + " is newer then " + target.getPath(), logLevel); 402 break; 403 } 404 } 405 return newer; 406 } 407 408 417 public void execute() throws org.apache.tools.ant.BuildException { 418 getProject().log(logHdr+"Xmlc srcdir=" + srcDir, logLevel); 419 getProject().log(logHdr+" destdir=" + destDir, logLevel); 420 getProject().log(logHdr+" sourceout=" + srcOutDir, logLevel); 421 getProject().log(logHdr+" packagedir=" + packageDir, logLevel); 422 getProject().log(logHdr+" packagename=" + packageName, logLevel); 423 425 if (srcDir == null) { 426 throw new BuildException("srcdir attribute must be set!"); 427 } 428 if (!srcDir.exists()) { 429 throw new BuildException("srcdir does not exist!"); 430 } 431 if (!compile && !keep) { 432 throw new BuildException("If you do not keep the source, you must have XMLC compile them!"); 433 } 434 435 XmlcUtils xmlcUtils = XmlcUtils.create(); 436 437 DirectoryScanner ds = this.getDirectoryScanner(srcDir); 440 String [] files = ds.getIncludedFiles(); 441 442 getProject().log(logHdr+"XMLC processing " + files.length + " files: ", logLevel); 443 for (int i=0; i<files.length; i++) { 444 String file = files[i]; 445 try { 446 String fileName = xmlcUtils.buildFullBaseFileName(packageName, packageDir, file); 447 448 String className = xmlcUtils.buildClassName(fileName, ML); 449 String javaName = className.replace('.', '/'); 450 451 if (srcOutDir == null) { 455 srcOutDir = srcDir; 456 srcOutDirName = srcDirName; 457 } 458 459 File mlFile = new File (srcDir, file); 460 File javaFile = new File (srcOutDir, javaName + ".java"); File classFile = new File ((destDir != null) ? destDir : srcOutDir, fileName + ".class"); 463 464 java.util.ArrayList args = new java.util.ArrayList (); 465 466 File target = null; 469 470 474 ArrayList al = new ArrayList (); 477 File f = javaFile; 478 while ((f = f.getParentFile()) != null) { 479 al.add(f); 480 } 481 f = mlFile; 482 while ((f = f.getParentFile()) != null) { 483 al.add(f); 484 } 485 File [] possibleOptionFilesDirs = new File [al.size()]; 486 for (int ii=0; ii<al.size(); ii++) { 487 possibleOptionFilesDirs[al.size() - ii - 1] = (File )al.get(ii); 488 getProject().log("possibleOptionFilesDirs :" + possibleOptionFilesDirs[al.size() - ii - 1].getAbsolutePath(), logLevel); 489 } 490 493 String [] possibleOptionFileNames = { fileName, options}; 494 String [] foundOptionsFiles = 495 xmlcUtils.getOptionFiles(possibleOptionFilesDirs, possibleOptionFileNames); 496 497 File [] foundOptions = new File [foundOptionsFiles.length]; 498 for (int j = 0 ; j < foundOptions.length ; j++) { 499 foundOptions[j] = new File (foundOptionsFiles[j]); 500 } 501 502 508 File optionCheck = null; 509 if (keep) { 510 optionCheck = new File (javaFile.getPath() + ".optionCheck"); 511 } else { 512 optionCheck = new File (classFile.getPath() + ".optionCheck"); 513 } 514 515 getProject().log(logHdr+" option check to created " + optionCheck.getPath(), logLevel); 516 if (keep) { 517 if (!javaFile.exists()) { 518 target = javaFile; 519 getProject().log(logHdr+" " + javaFile + " does not exist, invoking XMLC", logLevel); 521 } else if (compile) { 522 target = classFile; 523 if (mlFile.lastModified() <= classFile.lastModified() && mlFile.lastModified() <= javaFile.lastModified()) { 525 if (optionCheck.exists() && isNewerThanFiles(optionCheck, foundOptions)) { 526 if (!forceBuild) { 527 getProject().log(logHdr+" " + classFile.getName() + " and " + javaFile.getName() + " are newer than " + mlFile.getName(), logLevel); 528 upToDate++; 529 continue; 530 } 531 } else { 532 getProject().log(logHdr+" " + "One of the option files are newer than " + mlFile.getName() + " invoking XMLC" , logLevel); 533 } 534 } else { 535 getProject().log(logHdr+" " + classFile.getName() + " or " + javaFile.getName() + " is older than " + mlFile.getName() + " invoking XMLC", logLevel); 536 } 537 } else { 538 target = javaFile; 541 if (mlFile.lastModified() <= javaFile.lastModified()) { 542 if (optionCheck.exists() && isNewerThanFiles(optionCheck, foundOptions)) { 543 if (!forceBuild) { 544 getProject().log(logHdr+" " + javaFile.getName() + " is newer than " + mlFile.getName(), logLevel); 545 upToDate++; 546 continue; 547 } 548 } else { 549 getProject().log(logHdr+" " + "One of the option files are newer than " + mlFile.getName() + " invoking XMLC" , logLevel); 550 } 551 } else { 552 getProject().log(logHdr+" " + javaFile.getName() + " is older than " + mlFile.getName() + " invoking XMLC", logLevel); 553 target = javaFile; 554 } 555 } 556 } else { 557 target = classFile; 559 if (mlFile.lastModified() <= classFile.lastModified()) { 560 if (optionCheck.exists() && isNewerThanFiles(optionCheck, foundOptions)) { 561 if (!forceBuild) { 562 getProject().log(logHdr+" " + classFile.getName() + " is newer than " + mlFile.getName(), logLevel); 564 upToDate++; 565 continue; 566 } 567 } else { 568 getProject().log(logHdr+" " + "One of the option files are newer than " + mlFile.getName() + " invoking XMLC" , logLevel); 569 } 570 } else { 571 getProject().log(logHdr+" " + classFile.getName() + " is older than " + mlFile.getName() + " invoking XMLC", logLevel); 572 } 573 } 574 575 getProject().log(logHdr+" " + mlFile.getName() + " --> " + target.getName(), logLevel); 576 if (classPath != null) { 577 args.add("-classpath"); 578 args.add(classPath); 579 } 580 if (!compile) args.add("-nocompile"); 584 if (keep) args.add("-keep"); 585 if (verbose) args.add("-verbose"); 586 587 args.add("-class"); 588 args.add(className); 589 590 args.add("-sourceout"); 591 args.add(srcOutDir.toString()); 592 593 if (xmlcArgs != null) { 594 java.util.StringTokenizer tok = new java.util.StringTokenizer (xmlcArgs, " "); 598 while (tok.hasMoreElements()) { 599 args.add(tok.nextToken()); 600 } 601 } 602 603 String [] additionalArgs = cmdl.getArguments(); 604 for (int idx=0; idx < additionalArgs.length; idx++) { 605 args.add(additionalArgs[idx]); 606 } 607 608 if (foundOptionsFiles.length > 0) { 610 for (int optionsIdx = 0; optionsIdx < foundOptionsFiles.length; optionsIdx++) { 611 args.add(foundOptionsFiles[optionsIdx]); 612 getProject().log(logHdr+" Using options file " + foundOptionsFiles[optionsIdx], logLevel); 613 } 614 } else { 615 getProject().log(logHdr+"No options file could not be located for " + file + 616 " in paths " + javaFile.getParentFile() + ", " + 617 mlFile.getParentFile(), Project.MSG_WARN); 618 } 619 args.add(new File (srcDir, file).getAbsolutePath()); 621 622 getProject().log(logHdr+"args==" + args, logLevel); 623 624 getProject().log(logHdr+"Invoke XMLC on " + mlFile.getPath(), Project.MSG_INFO); 625 if (performExec != null) { 626 args.add(0, performExec); 627 String [] _args = (String [])args.toArray(new String [args.size()]); 628 Runtime runtime = Runtime.getRuntime(); 629 Process process = runtime.exec(_args); 630 int status = process.waitFor(); 631 if (status != 0) { 632 throw new BuildException("exit status of exec = "+status + toString(_args)); 633 } 634 } else { 635 String [] _args = (String [])args.toArray(new String [args.size()]); 636 637 String xmlcClassName = "org.enhydra.xml.xmlc.commands.xmlc.XMLC"; 640 641 Method m = null; 642 Class c = null; 643 try { 644 c = Class.forName(xmlcClassName, true, getClass().getClassLoader()); 645 m = c.getMethod("main", new Class [] { String [].class}); 646 } catch (Exception e) { 647 e.printStackTrace(); 649 throw new BuildException(e); 650 } 651 652 if (m != null) { 653 m.invoke(null,new Object [] { _args}); 654 } 655 } 656 657 660 if (optionCheck.exists()) { 661 optionCheck.delete(); 662 } 663 optionCheck.createNewFile(); 664 } catch (InvocationTargetException ite) { 665 ite.printStackTrace(); 666 throw new BuildException(ite); 667 } catch (Exception x) { 668 x.printStackTrace(); 669 throw new BuildException(x); 670 } 671 } 672 673 if (upToDate > 0) { 674 getProject().log(logHdr+"XMLC " + upToDate + " files were up-to-date, consider using force=\"true\"", logLevel); 675 } 676 } 677 678 685 static private String toString(String [] args) { 686 StringBuffer buffer = new StringBuffer (); 687 for (int i=0; i<args.length; i++) buffer.append(args[i]).append(" "); 688 return buffer.toString(); 689 } 690 691 695 public Commandline.Argument createArg() { 696 return cmdl.createArgument(); 697 } 698 699 public static class BooleanAttribute extends EnumeratedAttribute { 700 public String [] getValues() { 701 return new String [] {"yes", "no", "on", "off", "true", "false"}; 702 } 703 704 public boolean booleanValue() { 705 return (this.getValue().equals("yes") || this.getValue().equals("on") || this.getValue().equals("true")); 706 } 707 } 708 709 } 710 | Popular Tags |