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