KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > compiler > Compiler


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: Compiler.java,v 1.3 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.compiler;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29
30 import org.enhydra.xml.dom.DOMInfo;
31 import org.enhydra.xml.io.DOMFormatter;
32 import org.enhydra.xml.io.ErrorReporter;
33 import org.enhydra.xml.io.InputSourceOps;
34 import org.enhydra.xml.io.OutputOptions;
35 import org.enhydra.xml.xmlc.XMLCException;
36 import org.enhydra.xml.xmlc.dom.XMLCDocument;
37 import org.enhydra.xml.xmlc.metadata.CompileOptions;
38 import org.enhydra.xml.xmlc.metadata.DocumentClass;
39 import org.enhydra.xml.xmlc.metadata.MetaData;
40 import org.w3c.dom.Document JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42
43 //FIXME: InputSource is obtained both here and in Parse...
44

45 /**
46  * XMLC compiler class. This class implements the steps in compiling a
47  * document. This is a programatic interface and does not have a main or
48  * handle command line options.
49  */

50 public class Compiler {
51     /*
52      * Document metadata.
53      */

54     private MetaData fMetaData;
55
56     /*
57      * Compile options from metadata.
58      */

59     private CompileOptions fCompileOptions;
60
61     /**
62      * Enable verbose output.
63      */

64     private boolean fVerbose = false;
65
66     /**
67      * Input source being compiled.
68      */

69     private InputSource JavaDoc fInputSource;
70
71     /*
72      * Document being compiled.
73      */

74     private Document fDocument;
75
76     /*
77      * Error output.
78      */

79     ErrorReporter fErrorReporter;
80
81     /*
82      * Trace and verbose output.
83      */

84     private PrintWriter JavaDoc fTraceOut;
85
86     /*
87      * XML document management object.
88      */

89     private XMLCDocument fXmlcDoc;
90
91     /**
92      * Constructor.
93      * @param errorReporter Object called to handle and output errors.
94      * @param traceOut Object to write trace and document information to.
95      */

96     public Compiler(ErrorReporter errorReporter,
97                     PrintWriter JavaDoc traceOut) {
98         fErrorReporter = errorReporter;
99         fTraceOut = traceOut;
100     }
101
102     /*
103      * Parse the page into the DOM and perform various checks and edits.
104      */

105     private void parsePage() throws XMLCException, IOException JavaDoc {
106         if (fVerbose) {
107             fTraceOut.println(">>> parsing " + InputSourceOps.getName(fInputSource));
108         }
109
110         Parse parser = new Parse(fErrorReporter, fTraceOut);
111         fXmlcDoc = parser.parse(fMetaData);
112         fDocument = fXmlcDoc.getDocument();
113
114         if (fCompileOptions.getPrintDocumentInfo()) {
115             if (fVerbose) {
116                 fTraceOut.println(">>> printing document info");
117             }
118             PrintInfo info = new PrintInfo(fDocument, fXmlcDoc);
119             info.printInfo(fTraceOut);
120         }
121
122         EditDOM domEditor = new EditDOM(fMetaData);
123         domEditor.edit(fXmlcDoc);
124
125         if (fCompileOptions.getPrintDOM()) {
126             if (fVerbose) {
127                 fTraceOut.println(">>> dumping document hierarchy");
128             }
129             DOMInfo.printTree("DOM hierarchy", fDocument, fTraceOut);
130         }
131     }
132
133     /*
134      * Generate the Java source files.
135      */

136     private void generateJavaSource() throws XMLCException, IOException JavaDoc {
137         if (fVerbose) {
138             fTraceOut.println(">>> generating code");
139         }
140         ClassGenerator codeGen =
141             new ClassGenerator(fMetaData, fXmlcDoc,
142                                (fCompileOptions.getPrintAccessorInfo()
143                                 ? fTraceOut : null));
144         codeGen.generateJavaSource(fVerbose ? fTraceOut : null);
145     }
146
147     /*
148      * Compile the generate source.
149      */

150     private void compileJavaSource() throws XMLCException {
151         if (fVerbose) {
152             fTraceOut.println(">>> compiling code");
153         }
154         Javac javac = new Javac();
155         javac.compile(fMetaData, fErrorReporter,
156                       (fVerbose ? fTraceOut : null));
157     }
158
159     /*
160      * Write the DOM to a file.
161      */

162     private void writeDOM() throws IOException JavaDoc {
163         File JavaDoc docOut = new File JavaDoc(fCompileOptions.getDocumentOutput());
164         String JavaDoc dir = docOut.getParent();
165         if (dir != null) {
166             new File JavaDoc(dir).mkdirs();
167         }
168         
169         OutputOptions options = DOMFormatter.getDefaultOutputOptions(fXmlcDoc.getDocument());
170         options.setEncoding(fXmlcDoc.getEncoding());
171         DOMFormatter formatter = new DOMFormatter(options);
172         formatter.write(fXmlcDoc.getDocument(), docOut);
173     }
174
175     /*
176      * Generate the Jave source and compile the page.
177      */

178     private void compileDocument()
179         throws XMLCException, IOException JavaDoc {
180         DocumentClass documentClass = fMetaData.getDocumentClass();
181         boolean saveWarnings = fErrorReporter.getPrintWarnings();
182         fErrorReporter.setPrintWarnings(fCompileOptions.getWarnings());
183         try {
184             generateJavaSource();
185
186             if (fCompileOptions.getCompileSource()) {
187                 compileJavaSource();
188             }
189         
190         /* generate the meta data file for recompiling/deferred deferred parsing */
191         if (documentClass.getRecompilation() || documentClass.getDeferredParsing()) {
192         if (fVerbose) {
193             fTraceOut.println(">>> output metadata to " + fMetaData.getDocument().getMetadataOutputFile().getAbsolutePath());
194         }
195         fMetaData.getDocument().serialize();
196         }
197         } finally {
198             fErrorReporter.setPrintWarnings(saveWarnings);
199             if (!fCompileOptions.getKeepGeneratedSource()) {
200                 if (documentClass.getJavaClassSource() != null) {
201                     documentClass.getJavaClassSource().delete();
202                 }
203                 if (documentClass.getJavaInterfaceSource() != null) {
204                     documentClass.getJavaInterfaceSource().delete();
205                 }
206             }
207         }
208     }
209
210     /*
211      * Parse arguments and compile the page.
212      */

213     public void compile(MetaData metaData)
214             throws XMLCException, IOException JavaDoc {
215         
216         fMetaData = metaData;
217         fCompileOptions = fMetaData.getCompileOptions();
218         fVerbose = fCompileOptions.getVerbose();
219
220         String JavaDoc inputDocument = fMetaData.getInputDocument().getUrl();
221         if (inputDocument == null) {
222             throw new XMLCException("input document not specified");
223         }
224         fInputSource = new InputSource JavaDoc(inputDocument);
225
226         parsePage();
227         if (fCompileOptions.getCreateSource()) {
228             compileDocument();
229         }
230
231         // Write document after edits, if requested.
232
if (fCompileOptions.getDocumentOutput() != null) {
233             writeDOM();
234         }
235         if (fVerbose) {
236             fTraceOut.println(">>> completed");
237         }
238     }
239 }
240
Popular Tags