001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: Compile.java 468647 2006-10-28 06:59:33Z minchau $
020     */
021    
022    package org.apache.xalan.xsltc.cmdline;
023    
024    import java.io.File;
025    import java.net.URL;
026    import java.util.Vector;
027    
028    import org.apache.xalan.xsltc.cmdline.getopt.GetOpt;
029    import org.apache.xalan.xsltc.cmdline.getopt.GetOptsException;
030    import org.apache.xalan.xsltc.compiler.XSLTC;
031    import org.apache.xalan.xsltc.compiler.util.ErrorMsg;
032    
033    /**
034     * @author Jacek Ambroziak
035     * @author Santiago Pericas-Geertsen
036     * @author G. Todd Miller
037     * @author Morten Jorgensen
038     */
039    public final class Compile {
040    
041        // Versioning numbers  for the compiler -v option output
042        private static int VERSION_MAJOR = 1;
043        private static int VERSION_MINOR = 4;
044        private static int VERSION_DELTA = 0;
045     
046    
047        public static void printUsage() {
048            StringBuffer vers = new StringBuffer("XSLTC version " + 
049                VERSION_MAJOR + "." + VERSION_MINOR + 
050                ((VERSION_DELTA > 0) ? ("."+VERSION_DELTA) : ("")));
051            System.err.println(vers + "\n" + 
052                    new ErrorMsg(ErrorMsg.COMPILE_USAGE_STR));
053        }
054    
055        /** 
056         * This method implements the command line compiler. See the USAGE_STRING
057         * constant for a description. It may make sense to move the command-line
058         * handling to a separate package (ie. make one xsltc.cmdline.Compiler
059         * class that contains this main() method and one xsltc.cmdline.Transform
060         * class that contains the DefaultRun stuff).
061         */
062        public static void main(String[] args) {
063            try {
064                boolean inputIsURL = false;
065                boolean useStdIn = false;
066                boolean classNameSet = false;
067                final GetOpt getopt = new GetOpt(args, "o:d:j:p:uxhsinv");
068                if (args.length < 1) printUsage();
069    
070                final XSLTC xsltc = new XSLTC();
071                xsltc.init();
072    
073                int c;
074                while ((c = getopt.getNextOption()) != -1) {
075                    switch(c) {
076                    case 'i':
077                        useStdIn = true;
078                        break;
079                    case 'o':
080                        xsltc.setClassName(getopt.getOptionArg());
081                        classNameSet = true;
082                        break;
083                    case 'd':
084                        xsltc.setDestDirectory(getopt.getOptionArg());
085                        break;
086                    case 'p':
087                        xsltc.setPackageName(getopt.getOptionArg());
088                        break;
089                    case 'j':  
090                        xsltc.setJarFileName(getopt.getOptionArg());
091                        break;
092                    case 'x':
093                        xsltc.setDebug(true);
094                        break;
095                    case 'u':
096                        inputIsURL = true;
097                        break;
098                    case 'n':
099                        xsltc.setTemplateInlining(true);    // used to be 'false'
100                        break;
101                    case 'v':
102                        // fall through to case h
103                    case 'h':
104                    default:
105                        printUsage();
106                        break; 
107                    }
108                }
109    
110                boolean compileOK;
111    
112                if (useStdIn) {
113                    if (!classNameSet) {
114                        System.err.println(new ErrorMsg(ErrorMsg.COMPILE_STDIN_ERR));
115                    }
116                    compileOK = xsltc.compile(System.in, xsltc.getClassName());
117                }
118                else {
119                    // Generate a vector containg URLs for all stylesheets specified
120                    final String[] stylesheetNames = getopt.getCmdArgs();
121                    final Vector   stylesheetVector = new Vector();
122                    for (int i = 0; i < stylesheetNames.length; i++) {
123                        final String name = stylesheetNames[i];
124                        URL url;
125                        if (inputIsURL)
126                            url = new URL(name);
127                        else
128                            url = (new File(name)).toURL();
129                        stylesheetVector.addElement(url);
130                    }
131                    compileOK = xsltc.compile(stylesheetVector);
132                }
133    
134                // Compile the stylesheet and output class/jar file(s)
135                if (compileOK) {
136                    xsltc.printWarnings();
137                    if (xsltc.getJarFileName() != null) xsltc.outputToJar();
138                }
139                else {
140                    xsltc.printWarnings();
141                    xsltc.printErrors();
142                }
143            }
144            catch (GetOptsException ex) {
145                System.err.println(ex);
146                printUsage(); // exits with code '-1'
147            }
148            catch (Exception e) {
149                e.printStackTrace();
150            }
151        }
152    
153    }