KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > commands > xmlc > XMLC


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: XMLC.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.commands.xmlc;
25
26 import java.io.IOException JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28
29 import org.enhydra.xml.io.ErrorReporter;
30 import org.enhydra.xml.xmlc.XMLCException;
31 import org.enhydra.xml.xmlc.XMLCVersion;
32 import org.enhydra.xml.xmlc.commands.ErrorHandling;
33 import org.enhydra.xml.xmlc.compiler.Compiler;
34 import org.enhydra.xml.xmlc.metadata.CompileOptions;
35 import org.enhydra.xml.xmlc.metadata.JavaCompilerSection;
36 import org.enhydra.xml.xmlc.metadata.MetaData;
37
38 /**
39  * XMLC compiler program. See the user manual for details on the command
40  * line. Will look for a system property `org.enhydra.xml.xmlc.javac' to find
41  * the compiler to use if -javac is not specified.
42  */

43 public class XMLC {
44     /**
45      * System property to pass default javac.
46      */

47     public static final String JavaDoc JAVAC_PROPERTY = "org.enhydra.xml.xmlc.javac";
48
49     /**
50      * Command line parsing object.
51      */

52     private XMLCOptionsParser fOptionsParser = new XMLCOptionsParser();
53
54     /**
55      * Enable verbose output.
56      */

57     private boolean fVerbose = false;
58
59     /*
60      * Stream to use for stdout.
61      */

62     private PrintWriter JavaDoc fStdout;
63
64     /*
65      * Error output.
66      */

67     private ErrorReporter fErrorReporter;
68
69     /*
70      * Trace and verbose output.
71      */

72     private PrintWriter JavaDoc fTraceOut;
73
74     /**
75      * Constructor specifying output stream for error messages.
76      */

77     public XMLC(PrintWriter JavaDoc stdout,
78                 PrintWriter JavaDoc stderr) {
79         fStdout = stdout;
80         fErrorReporter = new ErrorReporter(stderr);
81         fTraceOut = stderr;
82     }
83
84     /**
85      * Constructor with standard output streams.
86      */

87     public XMLC() {
88         this(new PrintWriter JavaDoc(System.out, true),
89              new PrintWriter JavaDoc(System.err, true));
90     }
91
92     /*
93      * Print the XMLC version number.
94      */

95     private void printVersion() {
96         fStdout.println("Enhydra XMLC version " + XMLCVersion.VERSION);
97         fStdout.println("See http://xmlc.enhydra.org for latest distribution");
98     }
99
100     /**
101      * Parse arguments.
102      */

103     private MetaData parseArgs(String JavaDoc[] args) throws XMLCException, IOException JavaDoc {
104         MetaData metaData = fOptionsParser.parse(args, fErrorReporter);
105         
106         // Default javac if supplied by a property.
107
String JavaDoc defaultJavac = System.getProperty(JAVAC_PROPERTY);
108         if (defaultJavac != null) {
109             JavaCompilerSection compilerSection = metaData.getJavaCompilerSection();
110             if (!compilerSection.isJavacSpecified()) {
111                 compilerSection.setJavac(defaultJavac);
112             }
113         }
114         return metaData;
115     }
116
117     /**
118      * Parse arguments and compile the page. If this method is used,
119      * errors are thrown, exit will not be called.
120      */

121     public void compile(String JavaDoc[] args) throws XMLCException, IOException JavaDoc {
122         
123         // Setup and document parsing.
124
MetaData metaData = parseArgs(args);
125         CompileOptions compileOptions = metaData.getCompileOptions();
126         fVerbose = compileOptions.getVerbose();
127
128         if (compileOptions.getPrintVersion()) {
129             printVersion();
130         }
131
132         if (metaData.getInputDocument().getUrl() != null) {
133             Compiler JavaDoc compiler = new Compiler JavaDoc(fErrorReporter,
134                                              fTraceOut);
135             compiler.compile(metaData);
136         }
137     }
138
139     /*
140      * Parse arguments and compile the page. Print errors that
141      * are thrown.
142      */

143     private void compileHandleErrors(String JavaDoc[] args) {
144         try {
145             compile(args);
146         } catch (Exception JavaDoc except) {
147             ErrorHandling.handleException(except, fVerbose);
148         }
149     }
150
151     /*
152      * Program entry point.
153      */

154     public static void main(String JavaDoc[] args) {
155         XMLC xmlc = new XMLC();
156         xmlc.compileHandleErrors(args);
157     }
158 }
159
Popular Tags