Source code: org/enhydra/xml/xmlc/dom/DocTypeBuilder.java
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.4.22.1 2000/10/11 10:20:42 markd Exp $
22 */
23
24 package org.enhydra.xml.xmlc.dom;
25
26 import org.enhydra.xml.xmlc.*;
27 import org.w3c.dom.*;
28 import java.util.HashMap;
29
30 /**
31 * Class for building a DOM DocumentType. Used by XMLC parsers to build a document
32 * DOM.
33 * <P>
34 * Since the DOM level 1 does not address storing all of the information
35 * from the DOCTYPE and DTDs required by XMLC, routine are provided to
36 * add this information. Only a subset of the information is actually saved.
37 * The internal subset is needed to reproduce the DOCTYPE declaration in
38 * the compiled document. The attribute declarations from the external
39 * subset are needed to build the table of ID attributes used to generate
40 * access methods.
41 */
42 public class DocTypeBuilder {
43 /**
44 * Factory for creating the document.
45 */
46 private XMLCDomFactory domFactory;
47
48 /**
49 * DocumentType object, null until created.
50 */
51 private DocumentType docType;
52
53 /**
54 * Information needed to build DocumentType.
55 */
56 private String docTypeName; // Root element.
57 private String docSystemId;
58 private String docPublicId;
59
60 /*
61 * Internal subset as a string.
62 */
63 private String internalSubsetStr;
64
65 /**
66 * Flag to indicate that getCreateDocType() has been called. After this
67 * call, the attributes of this class can't be changed. A seperate flag
68 * is used, as the docType object maybe null.
69 */
70 private boolean createdDocType = false;
71
72 /**
73 * Table of element to id attribute name mappings.
74 */
75 private HashMap elementIdAttrs = new HashMap();
76
77 /**
78 * Constructor.
79 *
80 * @param domFactory Factory class for Documents.
81 */
82 public DocTypeBuilder(XMLCDomFactory domFactory) {
83 this.domFactory = domFactory;
84 }
85
86 /**
87 * Get the document type object, creating if necessary. Once created,
88 * no modifications can be made to this object. If setDocumentTypeName()
89 * has not been called, null is return. This is the cause when a
90 * document doesn't have a DTD.
91 */
92 public DocumentType getCreateDocType() {
93 if ((docType == null) && (docTypeName != null)) {
94 docType = domFactory.createDocumentType(docTypeName, docPublicId,
95 docSystemId, internalSubsetStr);
96 }
97 // This is set even if we didn't actually build a DocumentType
98 createdDocType = true;
99 return docType;
100 }
101
102 /**
103 * Generate an error if the doc type has been created, indicating
104 * a bug in the code using this class.
105 */
106 private void checkIfAlreadyCreated() {
107 if (createdDocType) {
108 throw new XMLCError("XMLC bug: attempt to add document type data after DocumentType object has been created");
109 }
110 }
111
112 /**
113 * Set the document type name (rootElement).
114 *
115 * @param name The Document type name (also root node name).
116 */
117 public void setDocumentTypeName(String name) {
118 checkIfAlreadyCreated();
119 docTypeName = name;
120 }
121
122 /**
123 * Get the document type name (rootElement).
124 */
125 public String getDocumentTypeName() {
126 return docTypeName;
127 }
128
129 /**
130 * Set the publicId.
131 *
132 * @param publicId Document type public id or null if standalone.
133 */
134 public void setPublicId(String publicId) {
135 checkIfAlreadyCreated();
136 docPublicId = publicId;
137 }
138
139 /**
140 * Get the publicId.
141 */
142 public String getPublicId() {
143 return docPublicId;
144 }
145
146 /**
147 * Set the systemId.
148 *
149 * @param systemId Document type system id or null if standalone.
150 */
151 public void setSystemId(String systemId) {
152 checkIfAlreadyCreated();
153 docSystemId = systemId;
154 }
155
156 /**
157 * Get the systemId.
158 */
159 public String getSystemId() {
160 return docSystemId;
161 }
162
163 /**
164 * Define an element id attribute.
165 *
166 * @param elementName The name of the element.
167 * @param attributeName The name of the ID attribute.
168 * @param internalSubset Is this part of the internal or
169 * external subset? Internal declarations take precedence.
170 */
171 public void addIdAttribute(String elementName,
172 String attributeName,
173 boolean internalSubset) {
174 checkIfAlreadyCreated();
175
176 boolean exists = elementIdAttrs.containsKey(elementName);
177 if ((!exists) || (exists && internalSubset)) {
178 elementIdAttrs.put(elementName, attributeName);
179 }
180 }
181
182 /**
183 * Get the id attribute name for an element. XML only allows one
184 * id attribute per element type.
185 *
186 * @param elementName The name of the element.
187 * @returns The attribute name or null if no ID attribute is defined.
188 */
189 public String getIdAttribute(String elementName) {
190 return (String)elementIdAttrs.get(elementName);
191 }
192
193 /**
194 * Add an EntityReference object.
195 *
196 * @param entity The name of the entity.
197 * @param internalSubset Is this part of the internal or
198 * external subset?
199 */
200 public void addEntityReference(String name,
201 boolean internalSubset) {
202 checkIfAlreadyCreated();
203 }
204
205 /**
206 * Add a document type declaration.
207 *
208 * @param name The element name.
209 * @param contentSpec The content specification.
210 * @param internalSubset Is this part of the internal or
211 * external subset?
212 */
213 public void addElementDecl(String name,
214 String contentSpec,
215 boolean internalSubset) {
216 checkIfAlreadyCreated();
217 }
218
219 /**
220 * Add an attribute declaration.
221 *
222 * @param elementName The element name.
223 * @param attrName The attribute name.
224 * @param attrType The attribute type specification:
225 * CDATA, ID, IDREF, IDREFS, NMTOKEN, NMTOKENS,
226 * ENTITY, ENTITIES, NOTATION or ENUMERATION.
227 * @param attrEnum - Enumeration for NOTATION and ENUMERATION.
228 * @param defaultDecl The default value declaration,
229 * REQUIRED, IMPLIED, FIXED (or DEFAULT).
230 * @param internalSubset Is this part of the internal or
231 * external subset?
232 */
233 public void addAttributeDecl(String elementName,
234 String attrName,
235 String attrType,
236 String attrEnum,
237 String defaultDecl,
238 boolean internalSubset) {
239 checkIfAlreadyCreated();
240 }
241
242 /**
243 * Add an internal entity.
244 *
245 * @param name The entity name.
246 * @param entityValue The value of the entity.
247 * @param paramEntity Is this a parameter or general entity?
248 * @param internalSubset Is this part of the internal or
249 * external subset?
250 */
251 public void addInternalEntityDecl(String name,
252 String entityValue,
253 boolean paramEntity,
254 boolean internalSubset) {
255 checkIfAlreadyCreated();
256 }
257
258 /**
259 * Add an external entity.
260 *
261 * @param name The entity name.
262 * @param systemId Document type system id.
263 * @param publicId Document type public id, or null if not specified.
264 * @param paramEntity Is this a parameter or general entity?
265 * @param internalSubset Is this part of the internal or
266 * external subset?
267 */
268 public void addExternalEntityDecl(String name,
269 String systemId,
270 String publicId,
271 boolean paramEntity,
272 boolean internalSubset) {
273 checkIfAlreadyCreated();
274 }
275
276 /**
277 * Add an unparsed entity.
278 *
279 * @param name The entity name.
280 * @param notationName The notation the entity references.
281 * @param internalSubset Is this part of the internal or
282 * external subset?
283 */
284 public void addUnparsedEntityDecl(String name,
285 String notationName,
286 boolean internalSubset) {
287 checkIfAlreadyCreated();
288 }
289
290 /**
291 * Add a notation.
292 *
293 * @param name The notation name.
294 * @param systemId Document type system id.
295 * @param publicId Document type public id, or null if not specified.
296 * @param internalSubset Is this part of the internal or
297 * external subset?
298 */
299 public void addNotationDecl(String name,
300 String systemId,
301 String publicId,
302 boolean internalSubset) {
303 checkIfAlreadyCreated();
304 }
305
306 /**
307 * Add internal subset as a single string.
308 */
309 public void setInternalSubset(String subsetStr) {
310 checkIfAlreadyCreated();
311 internalSubsetStr = subsetStr;
312 }
313
314 /**
315 * Get the internal subset as a single string.
316 */
317 public String getInternalSubset() {
318 return internalSubsetStr;
319 }
320 }