KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xalan > internal > xsltc > cmdline > Compile


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
20 package com.sun.org.apache.xalan.internal.xsltc.cmdline;
21
22 import java.io.File JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.Vector JavaDoc;
25
26 import com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt.GetOpt;
27 import com.sun.org.apache.xalan.internal.xsltc.cmdline.getopt.GetOptsException;
28 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC;
29 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
30
31 /**
32  * @author Jacek Ambroziak
33  * @author Santiago Pericas-Geertsen
34  * @author G. Todd Miller
35  * @author Morten Jorgensen
36  */

37 public final class Compile {
38
39     // Versioning numbers for the compiler -v option output
40
private static int VERSION_MAJOR = 1;
41     private static int VERSION_MINOR = 4;
42     private static int VERSION_DELTA = 0;
43  
44
45     // This variable should be set to false to prevent any methods in this
46
// class from calling System.exit(). As this is a command-line tool,
47
// calling System.exit() is normally OK, but we also want to allow for
48
// this class being used in other ways as well.
49
private static boolean _allowExit = true;
50
51     public static void printUsage() {
52         StringBuffer JavaDoc vers = new StringBuffer JavaDoc("XSLTC version " +
53         VERSION_MAJOR + "." + VERSION_MINOR +
54         ((VERSION_DELTA > 0) ? ("."+VERSION_DELTA) : ("")));
55     System.err.println(vers + "\n" +
56         new ErrorMsg(ErrorMsg.COMPILE_USAGE_STR));
57     if (_allowExit) System.exit(-1);
58     }
59
60     /**
61      * This method implements the command line compiler. See the USAGE_STRING
62      * constant for a description. It may make sense to move the command-line
63      * handling to a separate package (ie. make one xsltc.cmdline.Compiler
64      * class that contains this _main() method and one xsltc.cmdline.Transform
65      * class that contains the DefaultRun stuff).
66      */

67     public static void _main(String JavaDoc[] args) {
68     try {
69         boolean inputIsURL = false;
70         boolean useStdIn = false;
71         boolean classNameSet = false;
72         final GetOpt getopt = new GetOpt(args, "o:d:j:p:uxhsinv");
73         if (args.length < 1) printUsage();
74
75         final XSLTC xsltc = new XSLTC();
76         xsltc.init();
77
78         int c;
79         while ((c = getopt.getNextOption()) != -1) {
80         switch(c) {
81         case 'i':
82             useStdIn = true;
83             break;
84         case 'o':
85             xsltc.setClassName(getopt.getOptionArg());
86             classNameSet = true;
87             break;
88         case 'd':
89             xsltc.setDestDirectory(getopt.getOptionArg());
90             break;
91         case 'p':
92             xsltc.setPackageName(getopt.getOptionArg());
93             break;
94         case 'j':
95             xsltc.setJarFileName(getopt.getOptionArg());
96             break;
97         case 'x':
98             xsltc.setDebug(true);
99             break;
100         case 'u':
101             inputIsURL = true;
102             break;
103         case 's':
104             _allowExit = false;
105             break;
106         case 'n':
107             xsltc.setTemplateInlining(true); // used to be 'false'
108
break;
109         case 'v':
110             // fall through to case h
111
case 'h':
112         default:
113             printUsage();
114             break;
115         }
116         }
117
118         boolean compileOK;
119
120         if (useStdIn) {
121         if (!classNameSet) {
122             System.err.println(new ErrorMsg(ErrorMsg.COMPILE_STDIN_ERR));
123             if (_allowExit) System.exit(-1);
124         }
125         compileOK = xsltc.compile(System.in, xsltc.getClassName());
126         }
127         else {
128         // Generate a vector containg URLs for all stylesheets specified
129
final String JavaDoc[] stylesheetNames = getopt.getCmdArgs();
130         final Vector JavaDoc stylesheetVector = new Vector JavaDoc();
131         for (int i = 0; i < stylesheetNames.length; i++) {
132             final String JavaDoc name = stylesheetNames[i];
133             URL JavaDoc url;
134             if (inputIsURL)
135             url = new URL JavaDoc(name);
136             else
137             url = (new File JavaDoc(name)).toURL();
138             stylesheetVector.addElement(url);
139         }
140         compileOK = xsltc.compile(stylesheetVector);
141         }
142
143         // Compile the stylesheet and output class/jar file(s)
144
if (compileOK) {
145         xsltc.printWarnings();
146         if (xsltc.getJarFileName() != null) xsltc.outputToJar();
147         if (_allowExit) System.exit(0);
148         }
149         else {
150         xsltc.printWarnings();
151         xsltc.printErrors();
152         if (_allowExit) System.exit(-1);
153         }
154     }
155     catch (GetOptsException ex) {
156         System.err.println(ex);
157         printUsage(); // exits with code '-1'
158
}
159     catch (Exception JavaDoc e) {
160         e.printStackTrace();
161         if (_allowExit) System.exit(-1);
162     }
163     }
164
165 }
166
Popular Tags