KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > dom > DocTypeBuilder


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

23
24 package org.enhydra.xml.xmlc.dom;
25
26 import java.util.HashMap JavaDoc;
27
28 import org.enhydra.xml.xmlc.XMLCError;
29 import org.w3c.dom.DocumentType JavaDoc;
30
31 /**
32  * Class for building a DOM DocumentType. Used by XMLC parsers to build a document
33  * DOM.
34  * <P>
35  * Since the DOM level 1 does not address storing all of the information
36  * from the DOCTYPE and DTDs required by XMLC, routine are provided to
37  * add this information. Only a subset of the information is actually saved.
38  * The internal subset is needed to reproduce the DOCTYPE declaration in
39  * the compiled document. The attribute declarations from the external
40  * subset are needed to build the table of ID attributes used to generate
41  * access methods.
42  */

43 public class DocTypeBuilder {
44     /**
45      * Factory for creating the document.
46      */

47     private XMLCDomFactory domFactory;
48
49     /**
50      * DocumentType object, null until created.
51      */

52     private DocumentType JavaDoc docType;
53
54     /**
55      * Information needed to build DocumentType.
56      */

57     private String JavaDoc docTypeName; // Root element.
58
private String JavaDoc docSystemId;
59     private String JavaDoc docPublicId;
60
61     /*
62      * Internal subset as a string.
63      */

64     private String JavaDoc internalSubsetStr;
65
66     /**
67      * Flag to indicate that getCreateDocType() has been called. After this
68      * call, the attributes of this class can't be changed. A seperate flag
69      * is used, as the docType object maybe null.
70      */

71     private boolean createdDocType = false;
72
73     /**
74      * Table of element to id attribute name mappings.
75      */

76     private HashMap JavaDoc elementIdAttrs = new HashMap JavaDoc();
77
78     /**
79      * Constructor.
80      *
81      * @param domFactory Factory class for Documents.
82      */

83     public DocTypeBuilder(XMLCDomFactory domFactory) {
84         this.domFactory = domFactory;
85     }
86
87     /**
88      * Get the document type object, creating if necessary. Once created,
89      * no modifications can be made to this object. If setDocumentTypeName()
90      * has not been called, null is return. This is the cause when a
91      * document doesn't have a DTD.
92      */

93     public DocumentType JavaDoc getCreateDocType() {
94         if ((docType == null) && (docTypeName != null)) {
95             docType = domFactory.createDocumentType(docTypeName, docPublicId,
96                                                     docSystemId, internalSubsetStr);
97         }
98         // This is set even if we didn't actually build a DocumentType
99
createdDocType = true;
100         return docType;
101     }
102
103     /**
104      * Generate an error if the doc type has been created, indicating
105      * a bug in the code using this class.
106      */

107     private void checkIfAlreadyCreated() {
108         if (createdDocType) {
109             throw new XMLCError("XMLC bug: attempt to add document type data after DocumentType object has been created");
110         }
111     }
112
113     /**
114      * Set the document type name (rootElement).
115      *
116      * @param name The Document type name (also root node name).
117      */

118     public void setDocumentTypeName(String JavaDoc name) {
119         checkIfAlreadyCreated();
120         docTypeName = name;
121     }
122     
123     /**
124      * Get the document type name (rootElement).
125      */

126     public String JavaDoc getDocumentTypeName() {
127         return docTypeName;
128     }
129
130     /**
131      * Set the publicId.
132      *
133      * @param publicId Document type public id or null if standalone.
134      */

135     public void setPublicId(String JavaDoc publicId) {
136         checkIfAlreadyCreated();
137         docPublicId = publicId;
138     }
139
140     /**
141      * Get the publicId.
142      */

143     public String JavaDoc getPublicId() {
144         return docPublicId;
145     }
146
147     /**
148      * Set the systemId.
149      *
150      * @param systemId Document type system id or null if standalone.
151      */

152     public void setSystemId(String JavaDoc systemId) {
153         checkIfAlreadyCreated();
154         docSystemId = systemId;
155     }
156
157     /**
158      * Get the systemId.
159      */

160     public String JavaDoc getSystemId() {
161         return docSystemId;
162     }
163
164     /**
165      * Define an element id attribute.
166      *
167      * @param elementName The name of the element.
168      * @param attributeName The name of the ID attribute.
169      * @param internalSubset Is this part of the internal or
170      * external subset? Internal declarations take precedence.
171      */

172     public void addIdAttribute(String JavaDoc elementName,
173                                String JavaDoc attributeName,
174                                boolean internalSubset) {
175         checkIfAlreadyCreated();
176
177         boolean exists = elementIdAttrs.containsKey(elementName);
178         if ((!exists) || (exists && internalSubset)) {
179             elementIdAttrs.put(elementName, attributeName);
180         }
181     }
182
183     /**
184      * Get the id attribute name for an element. XML only allows one
185      * id attribute per element type.
186      *
187      * @param elementName The name of the element.
188      * @return The attribute name or null if no ID attribute is defined.
189      */

190     public String JavaDoc getIdAttribute(String JavaDoc elementName) {
191         return (String JavaDoc)elementIdAttrs.get(elementName);
192     }
193
194     /**
195      * Add an EntityReference object.
196      *
197      * @param entity The name of the entity.
198      * @param internalSubset Is this part of the internal or
199      * external subset?
200      */

201     public void addEntityReference(String JavaDoc name,
202                                    boolean internalSubset) {
203         checkIfAlreadyCreated();
204     }
205
206     /**
207      * Add a document type declaration.
208      *
209      * @param name The element name.
210      * @param contentSpec The content specification.
211      * @param internalSubset Is this part of the internal or
212      * external subset?
213      */

214     public void addElementDecl(String JavaDoc name,
215                                String JavaDoc contentSpec,
216                                boolean internalSubset) {
217         checkIfAlreadyCreated();
218     }
219
220     /**
221      * Add an attribute declaration.
222      *
223      * @param elementName The element name.
224      * @param attrName The attribute name.
225      * @param attrType The attribute type specification:
226      * CDATA, ID, IDREF, IDREFS, NMTOKEN, NMTOKENS,
227      * ENTITY, ENTITIES, NOTATION or ENUMERATION.
228      * @param attrEnum - Enumeration for NOTATION and ENUMERATION.
229      * @param defaultDecl The default value declaration,
230      * REQUIRED, IMPLIED, FIXED (or DEFAULT).
231      * @param internalSubset Is this part of the internal or
232      * external subset?
233      */

234     public void addAttributeDecl(String JavaDoc elementName,
235                                  String JavaDoc attrName,
236                                  String JavaDoc attrType,
237                                  String JavaDoc attrEnum,
238                                  String JavaDoc defaultDecl,
239                                  boolean internalSubset) {
240         checkIfAlreadyCreated();
241     }
242
243     /**
244      * Add an internal entity.
245      *
246      * @param name The entity name.
247      * @param entityValue The value of the entity.
248      * @param paramEntity Is this a parameter or general entity?
249      * @param internalSubset Is this part of the internal or
250      * external subset?
251      */

252     public void addInternalEntityDecl(String JavaDoc name,
253                                       String JavaDoc entityValue,
254                                       boolean paramEntity,
255                                       boolean internalSubset) {
256         checkIfAlreadyCreated();
257     }
258
259     /**
260      * Add an external entity.
261      *
262      * @param name The entity name.
263      * @param systemId Document type system id.
264      * @param publicId Document type public id, or null if not specified.
265      * @param paramEntity Is this a parameter or general entity?
266      * @param internalSubset Is this part of the internal or
267      * external subset?
268      */

269     public void addExternalEntityDecl(String JavaDoc name,
270                                       String JavaDoc systemId,
271                                       String JavaDoc publicId,
272                                       boolean paramEntity,
273                                       boolean internalSubset) {
274         checkIfAlreadyCreated();
275     }
276     
277     /**
278      * Add an unparsed entity.
279      *
280      * @param name The entity name.
281      * @param notationName The notation the entity references.
282      * @param internalSubset Is this part of the internal or
283      * external subset?
284      */

285     public void addUnparsedEntityDecl(String JavaDoc name,
286                                       String JavaDoc notationName,
287                                       boolean internalSubset) {
288         checkIfAlreadyCreated();
289     }
290
291     /**
292      * Add a notation.
293      *
294      * @param name The notation name.
295      * @param systemId Document type system id.
296      * @param publicId Document type public id, or null if not specified.
297      * @param internalSubset Is this part of the internal or
298      * external subset?
299      */

300     public void addNotationDecl(String JavaDoc name,
301                                 String JavaDoc systemId,
302                                 String JavaDoc publicId,
303                                 boolean internalSubset) {
304         checkIfAlreadyCreated();
305     }
306
307     /**
308      * Add internal subset as a single string.
309      */

310     public void setInternalSubset(String JavaDoc subsetStr) {
311         checkIfAlreadyCreated();
312         internalSubsetStr = subsetStr;
313     }
314
315     /**
316      * Get the internal subset as a single string.
317      */

318     public String JavaDoc getInternalSubset() {
319         return internalSubsetStr;
320     }
321 }
322
Popular Tags