KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > metadata > MetaDataDocument


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

23
24 package org.enhydra.xml.xmlc.metadata;
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.util.HashMap JavaDoc;
31
32 import org.enhydra.apache.xerces.dom.DOMImplementationImpl;
33 import org.enhydra.apache.xerces.dom.DocumentImpl;
34 import org.enhydra.xml.dom.DOMInfo;
35 import org.enhydra.xml.io.DOMFormatter;
36 import org.enhydra.xml.io.DOMParser;
37 import org.enhydra.xml.io.ErrorReporter;
38 import org.enhydra.xml.io.InputSourceOps;
39 import org.enhydra.xml.io.OutputOptions;
40 import org.enhydra.xml.io.XMLEntityResolver;
41 import org.enhydra.xml.xmlc.XMLCError;
42 import org.enhydra.xml.xmlc.XMLCException;
43 import org.w3c.dom.DOMException JavaDoc;
44 import org.w3c.dom.DOMImplementation JavaDoc;
45 import org.w3c.dom.Document JavaDoc;
46 import org.w3c.dom.DocumentType JavaDoc;
47 import org.w3c.dom.Element JavaDoc;
48 import org.xml.sax.InputSource JavaDoc;
49 import org.xml.sax.SAXException JavaDoc;
50
51 //FIXME: modify Xerces to take a already created document class.
52
//FIXME: use DOMImplementation
53

54 /**
55  * Metadata XML document associated with a Document being parsed. Data maybe
56  * set from the command line or read from an XML file (or both).
57  */

58 public class MetaDataDocument extends DocumentImpl {
59     /** Document type name */
60     public static final String JavaDoc DOC_TYPE_NAME = "xmlc";
61
62     /** Public id for created metadata files */
63     public static final String JavaDoc PUBLIC_ID
64         = "-//ENHYDRA//DTD XMLC 1.1//EN";
65
66     /** System id for created metadata files */
67     public static final String JavaDoc SYSTEM_ID
68         = "http://www.enhydra.org/xml/xmlc/xmlc-1.1.dtd";
69
70     /** Error messages */
71     private static final String JavaDoc ERR_PARSE_FAILED
72         = "XMLC metadata file parse failed: ";
73     private static final String JavaDoc ERR_WRITE_FAILED
74         = "XMLC metadata file write failed: ";
75
76     /**
77      * Compile-time debugging.
78      */

79     private static final boolean DEBUG = false;
80
81     /**
82      * Extension for XMLC metadata files.
83      */

84     public static final String JavaDoc METADATA_FILE_EXTENSION = ".xmlc";
85
86     /**
87      * Table of tag name to element class.
88      */

89     private static HashMap JavaDoc tagClassTable = new HashMap JavaDoc();
90
91     /**
92      * Signature used to locate constructor of element class.
93      */

94     private static final Class JavaDoc[] elemClassConstructorSign
95         = new Class JavaDoc[]{Document JavaDoc.class};
96
97     /**
98      * Static constructor.
99      */

100     static {
101         // Initialize tag table.
102
tagClassTable.put(JavaCompilerSection.TAG_NAME, JavaCompilerSection.class);
103         tagClassTable.put(CompileOptions.TAG_NAME, CompileOptions.class);
104         tagClassTable.put(InputDocument.TAG_NAME, InputDocument.class);
105         tagClassTable.put(Include.TAG_NAME, Include.class);
106         tagClassTable.put(DeleteElement.TAG_NAME, DeleteElement.class);
107         tagClassTable.put(DOMEdits.TAG_NAME, DOMEdits.class);
108         tagClassTable.put(DocumentClass.TAG_NAME, DocumentClass.class);
109         tagClassTable.put(DocumentSection.TAG_NAME, DocumentSection.class);
110         tagClassTable.put(ElementDef.TAG_NAME, ElementDef.class);
111         tagClassTable.put(HTMLAttr.TAG_NAME, HTMLAttr.class);
112         tagClassTable.put(HTMLTag.TAG_NAME, HTMLTag.class);
113         tagClassTable.put(HTMLTagSet.TAG_NAME, HTMLTagSet.class);
114         tagClassTable.put(HTMLCompatibility.TAG_NAME, HTMLCompatibility.class);
115         tagClassTable.put(HTMLSection.TAG_NAME, HTMLSection.class);
116         tagClassTable.put(Implements.TAG_NAME, Implements.class);
117         tagClassTable.put(JavacOption.TAG_NAME, JavacOption.class);
118         tagClassTable.put(Parser.TAG_NAME, Parser.class);
119         tagClassTable.put(TagClass.TAG_NAME, TagClass.class);
120         tagClassTable.put(URLRegExpMapping.TAG_NAME, URLRegExpMapping.class);
121         tagClassTable.put(URLMapping.TAG_NAME, URLMapping.class);
122         tagClassTable.put(XCatalog.TAG_NAME, XCatalog.class);
123         tagClassTable.put(MetaData.TAG_NAME, MetaData.class);
124     }
125
126     /**
127      * Do work of parsing a XMLC metadata file.
128      */

129     static private MetaDataDocument doParseMetaData(InputSource JavaDoc inputSource,
130                                                     ErrorReporter reporter,
131                                                     ClassLoader JavaDoc classLoader)
132         throws XMLCException, IOException JavaDoc, SAXException JavaDoc {
133
134         DOMParser parser = new DOMParser();
135         XMLEntityResolver resolver = new XMLEntityResolver();
136         if (DEBUG) {
137             resolver.setDebugWriter(new PrintWriter JavaDoc(System.err, true));
138         }
139
140         if (classLoader != null) {
141             resolver.addClassLoader(classLoader);
142         }
143         resolver.setDefaultResolving();
144         parser.setEntityResolver(resolver);
145
146         parser.setErrorHandler(reporter);
147         parser.setDocumentClassName(MetaDataDocument.class.getName());
148
149         MetaDataDocument document
150             = (MetaDataDocument)parser.parse(inputSource);
151         if (reporter.getErrorCnt() > 0) {
152             throw new XMLCException(ERR_PARSE_FAILED + reporter.getErrorCnt()
153                                     + " errors, (see log file): "
154                                     + InputSourceOps.getName(inputSource));
155         }
156         if (DEBUG) {
157             DOMInfo.printTree("Document after parse: "
158                               + InputSourceOps.getName(inputSource),
159                               document, System.err);
160         }
161
162         document.completeModifications();
163
164         if (DEBUG) {
165             DOMInfo.printTree("Document after completeModifications: " + InputSourceOps.getName(inputSource),
166                               document, System.err);
167         }
168         return document;
169     }
170
171     /**
172      * Parse a XMLC metadata file.
173      *
174      * @param inputSource Specification of file to parse.
175      * @param reporter Object use to report errors.
176      * @param classLoader If not null, class loader to use in resolving
177      * entities to be found on the classpath.
178      */

179     static public MetaDataDocument parseMetaData(InputSource JavaDoc inputSource,
180                                                  ErrorReporter reporter,
181                                                  ClassLoader JavaDoc classLoader)
182         throws XMLCException {
183         try {
184             return doParseMetaData(inputSource, reporter, classLoader);
185         } catch (IOException JavaDoc except) {
186             throw new XMLCException(ERR_PARSE_FAILED + InputSourceOps.getName(inputSource),
187                                     except);
188         } catch (SAXException JavaDoc except) {
189             throw new XMLCException(ERR_PARSE_FAILED + InputSourceOps.getName(inputSource),
190                                     except);
191         }
192     }
193
194     /**
195      * Construct an empty instace of a metadata document. This is for use of
196      * the parse, use newInstance to create a new document.
197      */

198     public MetaDataDocument() {
199     }
200
201     /**
202      * Construct an new instace of a metadata document.
203      */

204     private MetaDataDocument(DocumentType JavaDoc docType) {
205         super(docType);
206     }
207
208     /**
209      * Create a new, uninitialized document. Used when constructing
210      * a document programatically.
211      */

212     public static MetaDataDocument newInstance() {
213         DOMImplementation JavaDoc domImpl = DOMImplementationImpl.getDOMImplementation();
214         DocumentType JavaDoc docType = domImpl.createDocumentType(DOC_TYPE_NAME,
215                                                           PUBLIC_ID,
216                                                           SYSTEM_ID);
217         return new MetaDataDocument(docType);
218     }
219
220     /**
221      * Serialize this object to a XML file.
222      */

223     public void serialize(File JavaDoc file) throws XMLCException {
224         if (DEBUG) {
225             DOMInfo.printTree("Document before write: " + file,
226                               this, System.err);
227         }
228         OutputOptions outputOptions = new OutputOptions();
229         outputOptions.setPrettyPrinting(true);
230         outputOptions.setPreserveSpace(false);
231         DOMFormatter domFormatter = new DOMFormatter(outputOptions);
232         
233         try {
234             domFormatter.write(this, file);
235         } catch (IOException JavaDoc except) {
236             throw new XMLCException(ERR_WRITE_FAILED + file, except);
237         }
238     }
239     
240     /**
241      * Get the name of the metadata output file
242      *
243      * davidli: this is needed to be public for debugging/logging purpose
244      */

245     public File JavaDoc getMetadataOutputFile () {
246         DocumentClass documentClass = getMetaData().getDocumentClass();
247     String JavaDoc fileName;
248
249         if (documentClass.getGenerate().generateInterface()) {
250             fileName = documentClass.getUnqualInterfaceName() + METADATA_FILE_EXTENSION;
251         } else {
252             fileName = documentClass.getUnqualClassName() + METADATA_FILE_EXTENSION;
253         }
254
255     File JavaDoc metaDataOutputDir = documentClass.getJavaClassSource().getParentFile();
256     if (metaDataOutputDir == null) {
257         metaDataOutputDir = new File JavaDoc(getMetaData().getCompileOptions().getClassOutputRoot());
258         String JavaDoc packageName = documentClass.getPackageName();
259         if (packageName != null) {
260         packageName = packageName.replace('.', File.separatorChar);
261         metaDataOutputDir = new File JavaDoc(getMetaData().getCompileOptions().getClassOutputRoot(), packageName);
262         }
263     }
264
265     metaDataOutputDir.mkdirs();
266         return new File JavaDoc(metaDataOutputDir, fileName);
267     }
268
269     /**
270      * Serialize this object to a XML file using the default name.
271      */

272     public void serialize() throws XMLCException {
273         DocumentClass documentClass = getMetaData().getDocumentClass();
274         if (documentClass == null) {
275             throw new XMLCError("No DocumentClass metadata object; can't determine default name");
276         }
277
278         serialize(getMetadataOutputFile());
279     }
280
281     /**
282      * Get the root MetaData object, which must exist.
283      */

284     public MetaData getMetaData() {
285         MetaData root = (MetaData)getDocumentElement();
286         if (root == null) {
287             root = (MetaData)createElement(MetaData.TAG_NAME);
288             appendChild(root);
289         }
290         return root;
291     }
292     
293     /**
294      * Create a XMLC metadata document given its class.
295      */

296     public Element JavaDoc createElement(Class JavaDoc elementClass) {
297         try {
298             Constructor JavaDoc constr = elementClass.getConstructor(elemClassConstructorSign);
299             return (Element JavaDoc)constr.newInstance(new Object JavaDoc[]{this});
300         } catch (Exception JavaDoc except) {
301             throw new XMLCError(except);
302         }
303     }
304
305     /**
306      * Create a XMLC metadata document element.
307      */

308     public Element JavaDoc createElement(String JavaDoc tagName) throws DOMException JavaDoc {
309         Class JavaDoc elementClass = (Class JavaDoc)tagClassTable.get(tagName);
310         if (elementClass == null) {
311             throw new XMLCError("Invalid tag name: \"" + tagName + "\"");
312         }
313         return createElement(elementClass);
314     }
315
316     /**
317      * Create a XMLC metadata document element. in a namespace.
318      */

319     public Element JavaDoc createElementNS(String JavaDoc namespaceURI,
320                                    String JavaDoc qualifiedName) {
321     if ((namespaceURI == null) || (namespaceURI.length() == 0)) {
322         return createElement(qualifiedName);
323     } else {
324             // FIXME: this should work in a namespace.
325
throw new XMLCError("createElementNS not implemented");
326         }
327     }
328
329     /**
330      * Complete modifications to DOM. This should be called after a set of
331      * modifcations have been completed (it is called after a read). It is
332      * used to synchronize data in different areas of the DOM.
333      */

334     public void completeModifications() throws XMLCException {
335         getMetaData().completeModifications();
336     }
337
338     /**
339      * Merge another MetaData document into this document.
340      */

341     public void merge(MetaDataDocument srcDocument) {
342         getMetaData().mergeElement(srcDocument.getMetaData());
343     }
344 }
345
Popular Tags