KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > lazydom > LazyDOMBuilder


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

23
24 package org.enhydra.xml.lazydom;
25
26 import java.io.File JavaDoc;
27 import java.io.IOException JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29 import java.lang.reflect.Constructor JavaDoc;
30 import java.lang.reflect.InvocationTargetException JavaDoc;
31
32 import org.enhydra.xml.driver.TestException;
33 import org.enhydra.xml.io.ErrorReporter;
34 import org.enhydra.xml.xmlc.XMLCException;
35 import org.enhydra.xml.xmlc.compiler.Parse;
36 import org.enhydra.xml.xmlc.dom.XMLCDocument;
37 import org.enhydra.xml.xmlc.metadata.DocumentClass;
38 import org.enhydra.xml.xmlc.metadata.InputDocument;
39 import org.enhydra.xml.xmlc.metadata.MetaData;
40 import org.enhydra.xml.xmlc.metadata.MetaDataDocument;
41 import org.w3c.dom.DocumentType JavaDoc;
42
43 /**
44  * Class to handle creating LazyDOM document instances for tests, either by
45  * creating or by parsing. Handles conversions from parsed LazyDOM to an
46  * unexpanded LazyDOM. This uses XMLC's wrapper around parser to handle the
47  * parsing. It would be nice if there XMLC parsing was more cleanly seperated
48  * from the rest of XMLC.
49  */

50 class LazyDOMBuilder {
51     /** Compile-time debugging, enables more tracing */
52     private static final boolean DEBUG = false;
53
54     /** Signatures of constructors */
55     private static final Class JavaDoc[] XML_CONSTR_SIGN
56         = new Class JavaDoc[]{DocumentType JavaDoc.class, TemplateDOM.class};
57     private static final Class JavaDoc[] HTML_CONSTR_SIGN
58         = new Class JavaDoc[]{TemplateDOM.class};
59
60     /** Trace output, non-null if enabled */
61     private PrintWriter JavaDoc fTraceOut;
62     
63     /** Class for XMLC DOM factory */
64     private Class JavaDoc fDomFactoryClass;
65
66     /** Writer for messages. */
67     private PrintWriter JavaDoc fMsgWriter;
68
69     /** Constructor. */
70     public LazyDOMBuilder(PrintWriter JavaDoc msgWriter,
71                           Class JavaDoc domFactoryClass) {
72         if (DEBUG) {
73             fTraceOut = msgWriter;
74         }
75         fMsgWriter = msgWriter;
76         fDomFactoryClass = domFactoryClass;
77     }
78
79     /**
80      * Parse a document using XMLC parser wrapper to do the dirty work.
81      */

82     private LazyDocument parseFile(File JavaDoc srcFile)
83         throws IOException JavaDoc, XMLCException {
84         Parse parser = new Parse(new ErrorReporter(fMsgWriter), fTraceOut);
85         MetaData metaData = MetaDataDocument.newInstance().getMetaData();
86         InputDocument inputDoc = metaData.getInputDocument();
87         inputDoc.setUrl(srcFile.getPath());
88         DocumentClass docClass = metaData.getDocumentClass();
89         docClass.setDomFactory(fDomFactoryClass.getName());
90
91         // HTML test pages have some warnings, just drop em
92
metaData.getParser().setWarnings(false);
93         
94         if (DEBUG) {
95             metaData.getCompileOptions().setVerbose(true);
96             metaData.getCompileOptions().setPrintDocumentInfo(true);
97         }
98
99         XMLCDocument xmlcDoc = parser.parse(metaData);
100         return (LazyDocument)xmlcDoc.getDocument();
101     }
102
103     /**
104      * Attempt to createa a new instance of a XML LazyDocument, given a
105      * template.
106      */

107     private LazyDocument newXmlLazyDoc(Class JavaDoc lazyDocumentClass,
108                                        TemplateDOM templateDOM)
109         throws NoSuchMethodException JavaDoc, InstantiationException JavaDoc,
110                IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
111         Constructor JavaDoc constr
112             = lazyDocumentClass.getConstructor(XML_CONSTR_SIGN);
113         return (LazyDocument)constr.newInstance(new Object JavaDoc[]{null, templateDOM});
114     }
115
116     /**
117      * Attempt to createa a new instance of a HTML LazyDocument, given a
118      * template.
119      */

120     private LazyDocument newHtmlLazyDoc(Class JavaDoc lazyDocumentClass,
121                                        TemplateDOM templateDOM)
122         throws NoSuchMethodException JavaDoc, InstantiationException JavaDoc,
123                IllegalAccessException JavaDoc, InvocationTargetException JavaDoc {
124         Constructor JavaDoc constr
125             = lazyDocumentClass.getConstructor(HTML_CONSTR_SIGN);
126         return (LazyDocument)constr.newInstance(new Object JavaDoc[]{templateDOM});
127     }
128
129     /**
130      * Create a new instance of a LazyDocument, given a template.
131      */

132     private LazyDocument newLazyDoc(Class JavaDoc lazyDocumentClass,
133                                     TemplateDOM templateDOM) {
134         // Try for XML, then HTML constructor pattern
135
try {
136             try {
137                 return newXmlLazyDoc(lazyDocumentClass, templateDOM);
138             } catch (NoSuchMethodException JavaDoc except) {
139                 // ignored
140
}
141             return newHtmlLazyDoc(lazyDocumentClass, templateDOM);
142         } catch (Exception JavaDoc except) {
143             throw new TestException("failed to create instance of: " + lazyDocumentClass,
144                                     except);
145         }
146     }
147
148     /**
149      * Parse a document into an expanded LazyDOM.
150      */

151     public LazyDocument parseUnexpanded(File JavaDoc srcFile) {
152         try {
153             LazyDocument expandedDoc = parseFile(srcFile);
154             TemplateDOM templateDom = new TemplateDOM(expandedDoc);
155             return newLazyDoc(expandedDoc.getClass(), templateDom);
156         } catch (IOException JavaDoc except) {
157             throw new TestException(srcFile.getPath(), except);
158         } catch (XMLCException except) {
159             throw new TestException(srcFile.getPath(), except);
160         }
161     }
162             
163 }
164
165
166
Popular Tags