1 18 package org.apache.tools.ant.taskdefs.optional; 19 20 import java.io.BufferedReader ; 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.PrintWriter ; 24 import java.io.StringReader ; 25 import java.io.StringWriter ; 26 import java.util.Enumeration ; 27 import java.util.Hashtable ; 28 import java.util.Properties ; 29 import java.util.StringTokenizer ; 30 import java.util.Vector ; 31 import netrexx.lang.Rexx; 32 import org.apache.tools.ant.BuildException; 33 import org.apache.tools.ant.DirectoryScanner; 34 import org.apache.tools.ant.Project; 35 import org.apache.tools.ant.taskdefs.MatchingTask; 36 import org.apache.tools.ant.types.EnumeratedAttribute; 37 import org.apache.tools.ant.util.FileUtils; 38 39 91 public class NetRexxC extends MatchingTask { 92 93 private boolean binary; 95 private String classpath; 96 private boolean comments; 97 private boolean compact = true; private boolean compile = true; 99 private boolean console; 100 private boolean crossref; 101 private boolean decimal = true; 102 private File destDir; 103 private boolean diag; 104 private boolean explicit; 105 private boolean format; 106 private boolean keep; 107 private boolean logo = true; 108 private boolean replace; 109 private boolean savelog; 110 private File srcDir; 111 private boolean sourcedir = true; private boolean strictargs; 113 private boolean strictassign; 114 private boolean strictcase; 115 private boolean strictimport; 116 private boolean strictprops; 117 private boolean strictsignal; 118 private boolean symbols; 119 private boolean time; 120 private String trace = "trace2"; 121 private boolean utf8; 122 private String verbose = "verbose3"; 123 private boolean suppressMethodArgumentNotUsed = false; 124 private boolean suppressPrivatePropertyNotUsed = false; 125 private boolean suppressVariableNotUsed = false; 126 private boolean suppressExceptionNotSignalled = false; 127 private boolean suppressDeprecation = false; 128 129 static final String MSG_METHOD_ARGUMENT_NOT_USED 131 = "Warning: Method argument is not used"; 132 static final String MSG_PRIVATE_PROPERTY_NOT_USED 133 = "Warning: Private property is defined but not used"; 134 static final String MSG_VARIABLE_NOT_USED 135 = "Warning: Variable is set but not used"; 136 static final String MSG_EXCEPTION_NOT_SIGNALLED 137 = "is in SIGNALS list but is not signalled within the method"; 138 static final String MSG_DEPRECATION = "has been deprecated"; 139 140 private Vector compileList = new Vector (); 142 private Hashtable filecopyList = new Hashtable (); 143 144 148 public void setBinary(boolean binary) { 149 this.binary = binary; 150 } 151 152 153 157 public void setClasspath(String classpath) { 158 this.classpath = classpath; 159 } 160 161 162 168 public void setComments(boolean comments) { 169 this.comments = comments; 170 } 171 172 173 179 public void setCompact(boolean compact) { 180 this.compact = compact; 181 } 182 183 184 191 public void setCompile(boolean compile) { 192 this.compile = compile; 193 if (!this.compile && !this.keep) { 194 this.keep = true; 195 } 196 } 197 198 199 205 public void setConsole(boolean console) { 206 this.console = console; 207 } 208 209 210 214 public void setCrossref(boolean crossref) { 215 this.crossref = crossref; 216 } 217 218 219 226 public void setDecimal(boolean decimal) { 227 this.decimal = decimal; 228 } 229 230 231 236 public void setDestDir(File destDirName) { 237 destDir = destDirName; 238 } 239 240 241 245 public void setDiag(boolean diag) { 246 this.diag = diag; 247 } 248 249 250 256 public void setExplicit(boolean explicit) { 257 this.explicit = explicit; 258 } 259 260 261 266 public void setFormat(boolean format) { 267 this.format = format; 268 } 269 270 271 277 public void setJava(boolean java) { 278 log("The attribute java is currently unused.", Project.MSG_WARN); 279 } 280 281 282 289 public void setKeep(boolean keep) { 290 this.keep = keep; 291 } 292 293 294 298 public void setLogo(boolean logo) { 299 this.logo = logo; 300 } 301 302 303 309 public void setReplace(boolean replace) { 310 this.replace = replace; 311 } 312 313 314 320 public void setSavelog(boolean savelog) { 321 this.savelog = savelog; 322 } 323 324 325 332 public void setSourcedir(boolean sourcedir) { 333 this.sourcedir = sourcedir; 334 } 335 336 337 341 public void setSrcDir(File srcDirName) { 342 srcDir = srcDirName; 343 } 344 345 346 354 public void setStrictargs(boolean strictargs) { 355 this.strictargs = strictargs; 356 } 357 358 359 363 public void setStrictassign(boolean strictassign) { 364 this.strictassign = strictassign; 365 } 366 367 368 372 public void setStrictcase(boolean strictcase) { 373 this.strictcase = strictcase; 374 } 375 376 377 384 public void setStrictimport(boolean strictimport) { 385 this.strictimport = strictimport; 386 } 387 388 389 395 public void setStrictprops(boolean strictprops) { 396 this.strictprops = strictprops; 397 } 398 399 400 405 public void setStrictsignal(boolean strictsignal) { 406 this.strictsignal = strictsignal; 407 } 408 409 410 416 public void setSymbols(boolean symbols) { 417 this.symbols = symbols; 418 } 419 420 421 427 public void setTime(boolean time) { 428 this.time = time; 429 } 430 431 437 public void setTrace(TraceAttr trace) { 438 this.trace = trace.getValue(); 439 } 440 441 447 public void setTrace(String trace) { 448 TraceAttr t = new TraceAttr(); 449 450 t.setValue(trace); 451 setTrace(t); 452 } 453 454 455 461 public void setUtf8(boolean utf8) { 462 this.utf8 = utf8; 463 } 464 465 466 470 public void setVerbose(VerboseAttr verbose) { 471 this.verbose = verbose.getValue(); 472 } 473 474 475 479 public void setVerbose(String verbose) { 480 VerboseAttr v = new VerboseAttr(); 481 482 v.setValue(verbose); 483 setVerbose(v); 484 } 485 486 492 public void setSuppressMethodArgumentNotUsed(boolean suppressMethodArgumentNotUsed) { 493 this.suppressMethodArgumentNotUsed = suppressMethodArgumentNotUsed; 494 } 495 496 497 503 public void setSuppressPrivatePropertyNotUsed(boolean suppressPrivatePropertyNotUsed) { 504 this.suppressPrivatePropertyNotUsed = suppressPrivatePropertyNotUsed; 505 } 506 507 508 514 public void setSuppressVariableNotUsed(boolean suppressVariableNotUsed) { 515 this.suppressVariableNotUsed = suppressVariableNotUsed; 516 } 517 518 519 525 public void setSuppressExceptionNotSignalled(boolean suppressExceptionNotSignalled) { 526 this.suppressExceptionNotSignalled = suppressExceptionNotSignalled; 527 } 528 529 530 535 public void setSuppressDeprecation(boolean suppressDeprecation) { 536 this.suppressDeprecation = suppressDeprecation; 537 } 538 539 540 545 public void init() { 546 String p; 547 548 if ((p = getProject().getProperty("ant.netrexxc.binary")) != null) { 549 this.binary = Project.toBoolean(p); 550 } 551 if ((p = getProject().getProperty("ant.netrexxc.comments")) != null) { 553 this.comments = Project.toBoolean(p); 554 } 555 if ((p = getProject().getProperty("ant.netrexxc.compact")) != null) { 556 this.compact = Project.toBoolean(p); 557 } 558 if ((p = getProject().getProperty("ant.netrexxc.compile")) != null) { 559 this.compile = Project.toBoolean(p); 560 } 561 if ((p = getProject().getProperty("ant.netrexxc.console")) != null) { 562 this.console = Project.toBoolean(p); 563 } 564 if ((p = getProject().getProperty("ant.netrexxc.crossref")) != null) { 565 this.crossref = Project.toBoolean(p); 566 } 567 if ((p = getProject().getProperty("ant.netrexxc.decimal")) != null) { 568 this.decimal = Project.toBoolean(p); 569 } 571 if ((p = getProject().getProperty("ant.netrexxc.diag")) != null) { 572 this.diag = Project.toBoolean(p); 573 } 574 if ((p = getProject().getProperty("ant.netrexxc.explicit")) != null) { 575 this.explicit = Project.toBoolean(p); 576 } 577 if ((p = getProject().getProperty("ant.netrexxc.format")) != null) { 578 this.format = Project.toBoolean(p); 579 } 580 if ((p = getProject().getProperty("ant.netrexxc.keep")) != null) { 581 this.keep = Project.toBoolean(p); 582 } 583 if ((p = getProject().getProperty("ant.netrexxc.logo")) != null) { 584 this.logo = Project.toBoolean(p); 585 } 586 if ((p = getProject().getProperty("ant.netrexxc.replace")) != null) { 587 this.replace = Project.toBoolean(p); 588 } 589 if ((p = getProject().getProperty("ant.netrexxc.savelog")) != null) { 590 this.savelog = Project.toBoolean(p); 591 } 593 if ((p = getProject().getProperty("ant.netrexxc.sourcedir")) != null) { 594 this.sourcedir = Project.toBoolean(p); 595 } 596 if ((p = getProject().getProperty("ant.netrexxc.strictargs")) != null) { 597 this.strictargs = Project.toBoolean(p); 598 } 599 if ((p = getProject().getProperty("ant.netrexxc.strictassign")) != null) { 600 this.strictassign = Project.toBoolean(p); 601 } 602 if ((p = getProject().getProperty("ant.netrexxc.strictcase")) != null) { 603 this.strictcase = Project.toBoolean(p); 604 } 605 if ((p = getProject().getProperty("ant.netrexxc.strictimport")) != null) { 606 this.strictimport = Project.toBoolean(p); 607 } 608 if ((p = getProject().getProperty("ant.netrexxc.strictprops")) != null) { 609 this.strictprops = Project.toBoolean(p); 610 } 611 if ((p = getProject().getProperty("ant.netrexxc.strictsignal")) != null) { 612 this.strictsignal = Project.toBoolean(p); 613 } 614 if ((p = getProject().getProperty("ant.netrexxc.symbols")) != null) { 615 this.symbols = Project.toBoolean(p); 616 } 617 if ((p = getProject().getProperty("ant.netrexxc.time")) != null) { 618 this.time = Project.toBoolean(p); 619 } 620 if ((p = getProject().getProperty("ant.netrexxc.trace")) != null) { 621 setTrace(p); 622 } 623 if ((p = getProject().getProperty("ant.netrexxc.utf8")) != null) { 624 this.utf8 = Project.toBoolean(p); 625 } 626 if ((p = getProject().getProperty("ant.netrexxc.verbose")) != null) { 627 setVerbose(p); 628 } 629 if ((p = getProject().getProperty("ant.netrexxc.suppressMethodArgumentNotUsed")) != null) { 630 this.suppressMethodArgumentNotUsed = Project.toBoolean(p); 631 } 632 if ((p = getProject().getProperty("ant.netrexxc.suppressPrivatePropertyNotUsed")) != null) { 633 this.suppressPrivatePropertyNotUsed = Project.toBoolean(p); 634 } 635 if ((p = getProject().getProperty("ant.netrexxc.suppressVariableNotUsed")) != null) { 636 this.suppressVariableNotUsed = Project.toBoolean(p); 637 } 638 if ((p = getProject().getProperty("ant.netrexxc.suppressExceptionNotSignalled")) != null) { 639 this.suppressExceptionNotSignalled = Project.toBoolean(p); 640 } 641 if ((p = getProject().getProperty("ant.netrexxc.suppressDeprecation")) != null) { 642 this.suppressDeprecation = Project.toBoolean(p); 643 } 644 } 645 646 647 651 public void execute() throws BuildException { 652 653 if (srcDir == null || destDir == null) { 655 throw new BuildException("srcDir and destDir attributes must be set!"); 656 } 657 658 DirectoryScanner ds = getDirectoryScanner(srcDir); 662 663 String [] files = ds.getIncludedFiles(); 664 665 scanDir(srcDir, destDir, files); 666 667 copyFilesToDestination(); 669 670 if (compileList.size() > 0) { 672 log("Compiling " + compileList.size() + " source file" 673 + (compileList.size() == 1 ? "" : "s") 674 + " to " + destDir); 675 doNetRexxCompile(); 676 } 677 } 678 679 680 684 private void scanDir(File srcDir, File destDir, String [] files) { 685 for (int i = 0; i < files.length; i++) { 686 File srcFile = new File (srcDir, files[i]); 687 File destFile = new File (destDir, files[i]); 688 String filename = files[i]; 689 if (filename.toLowerCase().endsWith(".nrx")) { 694 File classFile = 695 new File (destDir, 696 filename.substring(0, filename.lastIndexOf('.')) + ".class"); 697 698 if (!compile || srcFile.lastModified() > classFile.lastModified()) { 699 filecopyList.put(srcFile.getAbsolutePath(), destFile.getAbsolutePath()); 700 compileList.addElement(destFile.getAbsolutePath()); 701 } 702 } else { 703 if (srcFile.lastModified() > destFile.lastModified()) { 704 filecopyList.put(srcFile.getAbsolutePath(), destFile.getAbsolutePath()); 705 } 706 } 707 } 708 } 709 710 711 712 private void copyFilesToDestination() { 713 if (filecopyList.size() > 0) { 714 log("Copying " + filecopyList.size() + " file" 715 + (filecopyList.size() == 1 ? "" : "s") 716 + " to " + destDir.getAbsolutePath()); 717 718 Enumeration e = filecopyList.keys(); 719 720 while (e.hasMoreElements()) { 721 String fromFile = (String ) e.nextElement(); 722 String toFile = (String ) filecopyList.get(fromFile); 723 724 try { 725 FileUtils.getFileUtils().copyFile(fromFile, toFile); 726 } catch (IOException ioe) { 727 String msg = "Failed to copy " + fromFile + " to " + toFile 728 + " due to " + ioe.getMessage(); 729 730 throw new BuildException(msg, ioe); 731 } 732 } 733 } 734 } 735 736 737 738 private void doNetRexxCompile() throws BuildException { 739 log("Using NetRexx compiler", Project.MSG_VERBOSE); 740 741 String classpath = getCompileClasspath(); 742 StringBuffer compileOptions = new StringBuffer (); 743 744 String [] compileOptionsArray = getCompileOptionsAsArray(); 747 String [] fileListArray = new String [compileList.size()]; 748 Enumeration e = compileList.elements(); 749 int j = 0; 750 751 while (e.hasMoreElements()) { 752 fileListArray[j] = (String ) e.nextElement(); 753 j++; 754 } 755 String [] compileArgs = new String [compileOptionsArray.length + fileListArray.length]; 757 758 for (int i = 0; i < compileOptionsArray.length; i++) { 759 compileArgs[i] = compileOptionsArray[i]; 760 } 761 for (int i = 0; i < fileListArray.length; i++) { 762 compileArgs[i + compileOptionsArray.length] = fileListArray[i]; 763 } 764 765 compileOptions.append("Compilation args: "); 767 for (int i = 0; i < compileOptionsArray.length; i++) { 768 compileOptions.append(compileOptionsArray[i]); 769 compileOptions.append(" "); 770 } 771 log(compileOptions.toString(), Project.MSG_VERBOSE); 772 773 String eol = System.getProperty("line.separator"); 774 StringBuffer niceSourceList = new StringBuffer ("Files to be compiled:" + eol); 775 776 for (int i = 0; i < compileList.size(); i++) { 777 niceSourceList.append(" "); 778 niceSourceList.append(compileList.elementAt(i).toString()); 779 niceSourceList.append(eol); 780 } 781 782 log(niceSourceList.toString(), Project.MSG_VERBOSE); 783 784 String currentClassPath = System.getProperty("java.class.path"); 787 Properties currentProperties = System.getProperties(); 788 789 currentProperties.put("java.class.path", classpath); 790 791 try { 792 StringWriter out = new StringWriter (); 793 int rc = 794 COM.ibm.netrexx.process.NetRexxC.main(new Rexx(compileArgs), new PrintWriter (out)); 795 String sdir = srcDir.getAbsolutePath(); 796 String ddir = destDir.getAbsolutePath(); 797 boolean doReplace = !(sdir.equals(ddir)); 798 int dlen = ddir.length(); 799 String l; 800 BufferedReader in = new BufferedReader (new StringReader (out.toString())); 801 802 log("replacing destdir '" + ddir + "' through sourcedir '" 803 + sdir + "'", Project.MSG_VERBOSE); 804 while ((l = in.readLine()) != null) { 805 int idx; 806 807 while (doReplace && ((idx = l.indexOf(ddir)) != -1)) { 808 l = (new StringBuffer (l)).replace(idx, idx + dlen, sdir).toString(); 810 } 811 if (suppressMethodArgumentNotUsed 813 && l.indexOf(MSG_METHOD_ARGUMENT_NOT_USED) != -1) { 814 log(l, Project.MSG_VERBOSE); 815 } else if (suppressPrivatePropertyNotUsed 816 && l.indexOf(MSG_PRIVATE_PROPERTY_NOT_USED) != -1) { 817 log(l, Project.MSG_VERBOSE); 818 } else if (suppressVariableNotUsed 819 && l.indexOf(MSG_VARIABLE_NOT_USED) != -1) { 820 log(l, Project.MSG_VERBOSE); 821 } else if (suppressExceptionNotSignalled 822 && l.indexOf(MSG_EXCEPTION_NOT_SIGNALLED) != -1) { 823 log(l, Project.MSG_VERBOSE); 824 } else if (suppressDeprecation 825 && l.indexOf(MSG_DEPRECATION) != -1) { 826 log(l, Project.MSG_VERBOSE); 827 } else if (l.indexOf("Error:") != -1) { 828 log(l, Project.MSG_ERR); 830 } else if (l.indexOf("Warning:") != -1) { 831 log(l, Project.MSG_WARN); 833 } else { 834 log(l, Project.MSG_INFO); } 836 } 837 if (rc > 1) { 838 throw new BuildException("Compile failed, messages should " 839 + "have been provided."); 840 } 841 } catch (IOException ioe) { 842 throw new BuildException("Unexpected IOException while " 843 + "playing with Strings", ioe); 844 } finally { 845 currentProperties = System.getProperties(); 848 currentProperties.put("java.class.path", currentClassPath); 849 } 850 851 } 852 853 854 855 private String getCompileClasspath() { 856 StringBuffer classpath = new StringBuffer (); 857 858 classpath.append(destDir.getAbsolutePath()); 861 862 if (this.classpath != null) { 864 addExistingToClasspath(classpath, this.classpath); 865 } 866 867 return classpath.toString(); 870 } 871 872 873 874 private String [] getCompileOptionsAsArray() { 875 Vector options = new Vector (); 876 877 options.addElement(binary ? "-binary" : "-nobinary"); 878 options.addElement(comments ? "-comments" : "-nocomments"); 879 options.addElement(compile ? "-compile" : "-nocompile"); 880 options.addElement(compact ? "-compact" : "-nocompact"); 881 options.addElement(console ? "-console" : "-noconsole"); 882 options.addElement(crossref ? "-crossref" : "-nocrossref"); 883 options.addElement(decimal ? "-decimal" : "-nodecimal"); 884 options.addElement(diag ? "-diag" : "-nodiag"); 885 options.addElement(explicit ? "-explicit" : "-noexplicit"); 886 options.addElement(format ? "-format" : "-noformat"); 887 options.addElement(keep ? "-keep" : "-nokeep"); 888 options.addElement(logo ? "-logo" : "-nologo"); 889 options.addElement(replace ? "-replace" : "-noreplace"); 890 options.addElement(savelog ? "-savelog" : "-nosavelog"); 891 options.addElement(sourcedir ? "-sourcedir" : "-nosourcedir"); 892 options.addElement(strictargs ? "-strictargs" : "-nostrictargs"); 893 options.addElement(strictassign ? "-strictassign" : "-nostrictassign"); 894 options.addElement(strictcase ? "-strictcase" : "-nostrictcase"); 895 options.addElement(strictimport ? "-strictimport" : "-nostrictimport"); 896 options.addElement(strictprops ? "-strictprops" : "-nostrictprops"); 897 options.addElement(strictsignal ? "-strictsignal" : "-nostrictsignal"); 898 options.addElement(symbols ? "-symbols" : "-nosymbols"); 899 options.addElement(time ? "-time" : "-notime"); 900 options.addElement("-" + trace); 901 options.addElement(utf8 ? "-utf8" : "-noutf8"); 902 options.addElement("-" + verbose); 903 904 String [] results = new String [options.size()]; 905 906 options.copyInto(results); 907 return results; 908 } 909 910 911 921 private void addExistingToClasspath(StringBuffer target, String source) { 922 StringTokenizer tok = new StringTokenizer (source, 923 System.getProperty("path.separator"), false); 924 925 while (tok.hasMoreTokens()) { 926 File f = getProject().resolveFile(tok.nextToken()); 927 928 if (f.exists()) { 929 target.append(File.pathSeparator); 930 target.append(f.getAbsolutePath()); 931 } else { 932 log("Dropping from classpath: " 933 + f.getAbsolutePath(), Project.MSG_VERBOSE); 934 } 935 } 936 937 } 938 939 940 943 public static class TraceAttr extends EnumeratedAttribute { 944 945 public String [] getValues() { 946 return new String []{"trace", "trace1", "trace2", "notrace"}; 947 } 948 } 949 950 953 public static class VerboseAttr extends EnumeratedAttribute { 954 955 public String [] getValues() { 956 return new String []{"verbose", "verbose0", "verbose1", 957 "verbose2", "verbose3", "verbose4", 958 "verbose5", "noverbose"}; 959 } 960 } 961 } 962 963 | Popular Tags |