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