KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > xmlc > XMLCRunner


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  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common.xmlc;
24
25 // Kelp imports
26
import org.enhydra.kelp.common.bridge.Generator;
27 import org.enhydra.kelp.common.bridge.Parser;
28 import org.enhydra.kelp.common.bridge.PrintInfo;
29 import org.enhydra.kelp.common.bridge.XMLCConnectionFactory;
30 import org.enhydra.kelp.common.node.OtterXMLCNode;
31
32 // Enhydra imports
33
import org.enhydra.xml.xmlc.commands.xmlc.*;
34 import org.enhydra.xml.xmlc.commands.options.*;
35 import org.enhydra.xml.xmlc.compiler.EditDOM;
36 import org.enhydra.xml.xmlc.dom.XMLCDocument;
37 import org.enhydra.xml.xmlc.parsers.*;
38 import org.enhydra.xml.xmlc.XMLCUtil;
39 import org.enhydra.xml.xmlc.XMLCVersion;
40 import org.enhydra.xml.xmlc.XMLCException;
41 import org.w3c.dom.*;
42 import org.xml.sax.SAXException JavaDoc;
43
44 // Standard imports
45
import java.io.*;
46 import java.lang.reflect.Constructor JavaDoc;
47 import java.lang.reflect.Method JavaDoc;
48 import java.util.Enumeration JavaDoc;
49 import java.util.Hashtable JavaDoc;
50 import java.util.Vector JavaDoc;
51 import java.util.ResourceBundle JavaDoc;
52
53 /**
54  * Class derived from the XMLC compiler program for use in this wizard.
55  * TODO: Mark may change some things that will require changes to this class.
56  */

57 public class XMLCRunner {
58
59     // string not to be resourced
60
static ResourceBundle JavaDoc res =
61         ResourceBundle.getBundle("org.enhydra.kelp.common.Res"); // nores
62
private final String JavaDoc JAVA_LANG = "java.lang."; // nores
63

64     /*
65      * Object containing the parsed standard options.
66      */

67     private OtterXMLCNode node;
68
69     /*
70      * Document being compiled.
71      */

72     private Document document;
73
74     /*
75      * Trace and verbose output.
76      */

77     private PrintWriter traceWriter = new PrintWriter(System.err, true);
78
79     /*
80      * XML DOM management object.
81      */

82     private XMLCDocument xmlcDoc;
83
84     /**
85      * Constructor declaration
86      *
87      *
88      * @see
89      */

90     public XMLCRunner() {}
91
92     /**
93      * Method declaration
94      *
95      *
96      * @param tw
97      *
98      * @see
99      */

100     public void setTraceWriter(PrintWriter tw) {
101         traceWriter = tw;
102     }
103
104     /**
105      * Method declaration
106      *
107      *
108      * @return
109      *
110      * @see
111      */

112     public PrintWriter getTraceWriter() {
113         return traceWriter;
114     }
115
116     /*
117      * Print out a stack trace. This works around bugs in SAXException that
118      * cause printing a stack trace to generate a null pointer exception. We
119      * can't just fix this in the OpenXML source (we did), because other
120      * parsers include that same broken class.
121      */

122
123     /**
124      * Method declaration
125      *
126      *
127      * @param except
128      *
129      * @see
130      */

131     private void printStackTrace(Throwable JavaDoc except) {
132
133         // FIXME: needed???
134
if (except instanceof SAXException JavaDoc) {
135             Exception JavaDoc except2 = ((SAXException JavaDoc) except).getException();
136
137             if (except2 != null) {
138                 except2.printStackTrace();
139                 return;
140             }
141         }
142         except.printStackTrace();
143     }
144
145     /*
146      * Handle an exception, printing the approriate error
147      * message.
148      */

149
150     /**
151      * Method declaration
152      *
153      *
154      * @param except
155      *
156      * @see
157      */

158     public void printException(Throwable JavaDoc except) {
159         except.printStackTrace();
160
161         // Must check that options have been parsed before using them.
162
if (except instanceof XMLCException) {
163             System.err.println(res.getString("Error_") + except.getMessage());
164             if ((node.getMetaDataHandler() != null)
165                     && (node.getMetaDataHandler().getVerbose())) {
166                 printStackTrace(except);
167             }
168
169             // If this is result of a java.lang exception, print stack.
170
Throwable JavaDoc cause = ((XMLCException) except).getCause();
171
172             if ((cause != null)
173                     && cause.getClass().getName().startsWith(JAVA_LANG)) {
174                 printStackTrace(cause);
175             }
176         } else if ((except instanceof FileNotFoundException)
177                    || (except instanceof IOException)) {
178             System.err.println(res.getString("Error_")
179                                + except.getClass().getName()
180                                + res.getString("LineEnd")
181                                + except.getMessage());
182             if ((node.getMetaDataHandler() != null)
183                     && (node.getMetaDataHandler().getVerbose())) {
184                 printStackTrace(except);
185             }
186         } else {
187             System.err.println(res.getString("Error_")
188                                + except.getClass().getName()
189                                + res.getString("LineEnd")
190                                + except.getMessage());
191             printStackTrace(except);
192         }
193         System.exit(1);
194     }
195
196     /*
197      * Parse the page into the DOM and perform various checks and edits.
198      */

199
200     /**
201      * Method declaration
202      *
203      *
204      * @throws FileNotFoundException
205      * @throws IOException
206      * @throws SAXException
207      * @throws XMLCException
208      *
209      * @see
210      */

211
212     /*
213      * Parse the page into the DOM and perform various checks and edits.
214      */

215     private void parsePage()
216             throws Throwable JavaDoc {
217         if (node.getMetaDataHandler().getVerbose()) {
218             traceWriter.println(res.getString("_parsing")
219                                 + node.getMetaDataHandler().getInputDocument());
220         }
221         Parser parser = null;
222
223         parser =
224             XMLCConnectionFactory.createParser(traceWriter,
225                                                node.getMetaDataHandler());
226         xmlcDoc = (XMLCDocument) parser.parse();
227         document = xmlcDoc.getDocument();
228         if (node.getMetaDataHandler().getPrintDocumentInfo()) {
229             if (node.getMetaDataHandler().getVerbose()) {
230                 traceWriter.println(res.getString("_printing_document"));
231             }
232             PrintInfo info = XMLCConnectionFactory.createPrintInfo(document,
233                     xmlcDoc);
234
235             info.printInfo(traceWriter);
236         }
237         EditDOM editDOM = null;
238
239         editDOM =
240             XMLCConnectionFactory.createEditDOM(node.getMetaDataHandler());
241         editDOM.edit(xmlcDoc);
242         if (node.getMetaDataHandler().getPrintDOM()) {
243             if (node.getMetaDataHandler().getVerbose()) {
244                 traceWriter.println(res.getString("_dumping_document"));
245             }
246             XMLCUtil.printNode(res.getString("DOM_hierarchy"), document,
247                                XMLCUtil.PRINT_ALL, traceWriter);
248         }
249     }
250
251     /*
252      * Generate the Java source files.
253      */

254
255     /**
256      * Method declaration
257      *
258      *
259      * @exception IOException
260      * @exception XMLCException
261      *
262      * @see
263      */

264     private void generateJavaSource() throws XMLCException, IOException {
265         PrintWriter accessorWriter = null;
266         PrintWriter verboseWriter = null;
267         Generator codeGen = null;
268
269         if (node.getMetaDataHandler().getPrintAccessorInfo()) {
270             accessorWriter = traceWriter;
271         }
272         if (node.getMetaDataHandler().getVerbose()) {
273             traceWriter.println(res.getString("_generating_code"));
274             verboseWriter = traceWriter;
275         }
276         codeGen =
277             XMLCConnectionFactory.createGenerator(node.getMetaDataHandler(),
278                                                   xmlcDoc, accessorWriter);
279         codeGen.generateJavaSource(verboseWriter);
280     }
281
282     /*
283      * Generate method information without generating code.
284      */

285
286     /**
287      * Method declaration
288      *
289      *
290      * @exception IOException
291      * @exception XMLCException
292      *
293      * @see
294      */

295     private void generateJavaSourceInfo() throws XMLCException, IOException {
296         if (node.getMetaDataHandler().getVerbose()) {
297             traceWriter.println(res.getString("_generating_code_info"));
298         }
299         Generator codeGen = XMLCConnectionFactory.createGenerator(node.getMetaDataHandler(),
300                 xmlcDoc,
301                 (node.getMetaDataHandler().getPrintAccessorInfo()
302                  ? traceWriter : null));
303
304         codeGen.generateJavaSource(node.getMetaDataHandler().getVerbose()
305                                    ? traceWriter : null);
306     }
307
308     /**
309      * Write the DOM to a file.
310      */

311     private void writeDOM() throws IOException {
312         File outputFile =
313             new File(node.getMetaDataHandler().getDocumentOutput());
314         String JavaDoc dir = outputFile.getParent();
315
316         if (dir != null) {
317             new File(dir).mkdirs();
318         }
319         PrintWriter domOut =
320             new PrintWriter(new BufferedOutputStream(new FileOutputStream(node.getMetaDataHandler().getDocumentOutput())));
321
322         try {
323             domOut.println(xmlcDoc.toDocument());
324         }
325         finally {
326             domOut.close();
327         }
328     }
329
330     /*
331      * Generate the Jave source and Compile the page.
332      */

333
334     /**
335      * Method declaration
336      *
337      *
338      * @exception FileNotFoundException
339      * @exception IOException
340      * @exception XMLCException
341      *
342      * @see
343      */

344     private void compilePage()
345             throws XMLCException, FileNotFoundException, IOException {
346         generateJavaSource();
347
348         // do not compile Java and always check for recomp.
349
if (node.getMetaDataHandler().getRecompilation()) {
350             File op = node.getOptionFileForRecomp();
351
352             if (!op.getParentFile().exists()) {
353                 op.getParentFile().mkdirs();
354             }
355             node.getMetaDataHandler().save(node.getOptionFileForRecomp());
356
357             // node.getMetaDataHandler().getMetaData().getMetaDataDocument().serialize();
358
}
359
360         // do not compile Java
361
// never delete source.
362
}
363
364     /**
365      * Print the XMLC version number.
366      */

367     private void printVersion() {
368         System.out.println(res.getString("Enhydra_XMLC_version")
369                            + XMLCVersion.VERSION);
370         System.out.println(res.getString("See_http_www_enhydra"));
371     }
372
373     /*
374      * Parse arguments and compile the page.
375      */

376
377     /**
378      * Method declaration
379      *
380      *
381      * @throws FileNotFoundException
382      * @throws IOException
383      * @throws SAXException
384      * @throws XMLCException
385      *
386      * @see
387      */

388     public void compile()
389             throws Throwable JavaDoc {
390
391         // Setup and document parsing.
392
node.initProjectOptions();
393         if (node.getMetaDataHandler().getPrintVersion()) {
394             printVersion();
395             if (node.getMetaDataHandler().getInputDocument() == null) {
396                 return;
397             }
398         }
399         parsePage();
400         if (node.getMetaDataHandler().getDocumentOutput() == null) {
401             compilePage();
402         } else {
403             if (node.getMetaDataHandler().getPrintAccessorInfo()) {
404                 generateJavaSourceInfo();
405             }
406             writeDOM();
407         }
408         if (node.getMetaDataHandler().getVerbose()) {
409             traceWriter.println(res.getString("_completed"));
410         }
411     }
412
413     /**
414      * Method declaration
415      *
416      *
417      * @param cn
418      *
419      * @see
420      */

421     public void setNode(OtterXMLCNode n) {
422         node = n;
423     }
424
425 }
426
Popular Tags