1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /*
  17. * $Id: Compile.java,v 1.17 2004/02/16 21:07:51 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.cmdline;
  20. import java.io.File;
  21. import java.net.URL;
  22. import java.util.Vector;
  23. import com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt.GetOpt;
  24. import com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt.GetOptsException;
  25. import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
  26. import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
  27. /**
  28. * @author Jacek Ambroziak
  29. * @author Santiago Pericas-Geertsen
  30. * @author G. Todd Miller
  31. * @author Morten Jorgensen
  32. */
  33. public final class Compile {
  34. // Versioning numbers for the compiler -v option output
  35. private static int VERSION_MAJOR = 1;
  36. private static int VERSION_MINOR = 4;
  37. private static int VERSION_DELTA = 0;
  38. // This variable should be set to false to prevent any methods in this
  39. // class from calling System.exit(). As this is a command-line tool,
  40. // calling System.exit() is normally OK, but we also want to allow for
  41. // this class being used in other ways as well.
  42. private static boolean _allowExit = true;
  43. public static void printUsage() {
  44. StringBuffer vers = new StringBuffer("XSLTC version " +
  45. VERSION_MAJOR + "." + VERSION_MINOR +
  46. ((VERSION_DELTA > 0) ? ("."+VERSION_DELTA) : ("")));
  47. System.err.println(vers + "\n" +
  48. new ErrorMsg(ErrorMsg.COMPILE_USAGE_STR));
  49. if (_allowExit) System.exit(-1);
  50. }
  51. /**
  52. * This method implements the command line compiler. See the USAGE_STRING
  53. * constant for a description. It may make sense to move the command-line
  54. * handling to a separate package (ie. make one xsltc.cmdline.Compiler
  55. * class that contains this _main() method and one xsltc.cmdline.Transform
  56. * class that contains the DefaultRun stuff).
  57. */
  58. public static void _main(String[] args) {
  59. try {
  60. boolean inputIsURL = false;
  61. boolean useStdIn = false;
  62. boolean classNameSet = false;
  63. final GetOpt getopt = new GetOpt(args, "o:d:j:p:uxhsinv");
  64. if (args.length < 1) printUsage();
  65. final XSLTC xsltc = new XSLTC();
  66. xsltc.init();
  67. int c;
  68. while ((c = getopt.getNextOption()) != -1) {
  69. switch(c) {
  70. case 'i':
  71. useStdIn = true;
  72. break;
  73. case 'o':
  74. xsltc.setClassName(getopt.getOptionArg());
  75. classNameSet = true;
  76. break;
  77. case 'd':
  78. xsltc.setDestDirectory(getopt.getOptionArg());
  79. break;
  80. case 'p':
  81. xsltc.setPackageName(getopt.getOptionArg());
  82. break;
  83. case 'j':
  84. xsltc.setJarFileName(getopt.getOptionArg());
  85. break;
  86. case 'x':
  87. xsltc.setDebug(true);
  88. break;
  89. case 'u':
  90. inputIsURL = true;
  91. break;
  92. case 's':
  93. _allowExit = false;
  94. break;
  95. case 'n':
  96. xsltc.setTemplateInlining(true); // used to be 'false'
  97. break;
  98. case 'v':
  99. // fall through to case h
  100. case 'h':
  101. default:
  102. printUsage();
  103. break;
  104. }
  105. }
  106. boolean compileOK;
  107. if (useStdIn) {
  108. if (!classNameSet) {
  109. System.err.println(new ErrorMsg(ErrorMsg.COMPILE_STDIN_ERR));
  110. if (_allowExit) System.exit(-1);
  111. }
  112. compileOK = xsltc.compile(System.in, xsltc.getClassName());
  113. }
  114. else {
  115. // Generate a vector containg URLs for all stylesheets specified
  116. final String[] stylesheetNames = getopt.getCmdArgs();
  117. final Vector stylesheetVector = new Vector();
  118. for (int i = 0; i < stylesheetNames.length; i++) {
  119. final String name = stylesheetNames[i];
  120. URL url;
  121. if (inputIsURL)
  122. url = new URL(name);
  123. else
  124. url = (new File(name)).toURL();
  125. stylesheetVector.addElement(url);
  126. }
  127. compileOK = xsltc.compile(stylesheetVector);
  128. }
  129. // Compile the stylesheet and output class/jar file(s)
  130. if (compileOK) {
  131. xsltc.printWarnings();
  132. if (xsltc.getJarFileName() != null) xsltc.outputToJar();
  133. if (_allowExit) System.exit(0);
  134. }
  135. else {
  136. xsltc.printWarnings();
  137. xsltc.printErrors();
  138. if (_allowExit) System.exit(-1);
  139. }
  140. }
  141. catch (GetOptsException ex) {
  142. System.err.println(ex);
  143. printUsage(); // exits with code '-1'
  144. }
  145. catch (Exception e) {
  146. e.printStackTrace();
  147. if (_allowExit) System.exit(-1);
  148. }
  149. }
  150. }