KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xerces > dom > DocumentTypeImpl


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.xerces.dom;
18
19 import org.w3c.dom.DOMException JavaDoc;
20 import org.w3c.dom.DocumentType JavaDoc;
21 import org.w3c.dom.Node JavaDoc;
22 import org.w3c.dom.NamedNodeMap JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import org.w3c.dom.UserDataHandler JavaDoc;
25
26 /**
27  * This class represents a Document Type <em>declaraction</em> in
28  * the document itself, <em>not</em> a Document Type Definition (DTD).
29  * An XML document may (or may not) have such a reference.
30  * <P>
31  * DocumentType is an Extended DOM feature, used in XML documents but
32  * not in HTML.
33  * <P>
34  * Note that Entities and Notations are no longer children of the
35  * DocumentType, but are parentless nodes hung only in their
36  * appropriate NamedNodeMaps.
37  * <P>
38  * This area is UNDERSPECIFIED IN REC-DOM-Level-1-19981001
39  * Most notably, absolutely no provision was made for storing
40  * and using Element and Attribute information. Nor was the linkage
41  * between Entities and Entity References nailed down solidly.
42  *
43  * @xerces.internal
44  *
45  * @author Arnaud Le Hors, IBM
46  * @author Joe Kesselman, IBM
47  * @author Andy Clark, IBM
48  * @version $Id: DocumentTypeImpl.java,v 1.29 2005/05/02 22:02:22 mrglavas Exp $
49  * @since PR-DOM-Level-1-19980818.
50  */

51 public class DocumentTypeImpl
52     extends ParentNode
53     implements DocumentType JavaDoc {
54
55     //
56
// Constants
57
//
58

59     /** Serialization version. */
60     static final long serialVersionUID = 7751299192316526485L;
61     
62     //
63
// Data
64
//
65

66     /** Document type name. */
67     protected String JavaDoc name;
68
69     /** Entities. */
70     protected NamedNodeMapImpl entities;
71     
72     /** Notations. */
73     protected NamedNodeMapImpl notations;
74
75     // NON-DOM
76

77     /** Elements. */
78     protected NamedNodeMapImpl elements;
79     
80     // DOM2: support public ID.
81
protected String JavaDoc publicID;
82     
83     // DOM2: support system ID.
84
protected String JavaDoc systemID;
85     
86     // DOM2: support internal subset.
87
protected String JavaDoc internalSubset;
88
89     /** The following are required for compareDocumentPosition
90     */

91     // Doctype number. Doc types which have no owner may be assigned
92
// a number, on demand, for ordering purposes for compareDocumentPosition
93
private int doctypeNumber=0;
94
95     //
96
// Constructors
97
//
98
private Hashtable JavaDoc userData = null;
99     /** Factory method for creating a document type node. */
100     public DocumentTypeImpl(CoreDocumentImpl ownerDocument, String JavaDoc name) {
101         super(ownerDocument);
102
103         this.name = name;
104         // DOM
105
entities = new NamedNodeMapImpl(this);
106         notations = new NamedNodeMapImpl(this);
107
108         // NON-DOM
109
elements = new NamedNodeMapImpl(this);
110
111     } // <init>(CoreDocumentImpl,String)
112

113     /** Factory method for creating a document type node. */
114     public DocumentTypeImpl(CoreDocumentImpl ownerDocument,
115                             String JavaDoc qualifiedName,
116                             String JavaDoc publicID, String JavaDoc systemID) {
117         this(ownerDocument, qualifiedName);
118         this.publicID = publicID;
119         this.systemID = systemID;
120
121     } // <init>(CoreDocumentImpl,String)
122

123     //
124
// DOM2: methods.
125
//
126

127     /**
128      * Introduced in DOM Level 2. <p>
129      *
130      * Return the public identifier of this Document type.
131      * @since WD-DOM-Level-2-19990923
132      */

133     public String JavaDoc getPublicId() {
134         if (needsSyncData()) {
135             synchronizeData();
136         }
137         return publicID;
138     }
139     /**
140      * Introduced in DOM Level 2. <p>
141      *
142      * Return the system identifier of this Document type.
143      * @since WD-DOM-Level-2-19990923
144      */

145     public String JavaDoc getSystemId() {
146         if (needsSyncData()) {
147             synchronizeData();
148         }
149         return systemID;
150     }
151     
152     /**
153      * NON-DOM. <p>
154      *
155      * Set the internalSubset given as a string.
156      */

157     public void setInternalSubset(String JavaDoc internalSubset) {
158         if (needsSyncData()) {
159             synchronizeData();
160         }
161         this.internalSubset = internalSubset;
162     }
163
164     /**
165      * Introduced in DOM Level 2. <p>
166      *
167      * Return the internalSubset given as a string.
168      * @since WD-DOM-Level-2-19990923
169      */

170     public String JavaDoc getInternalSubset() {
171         if (needsSyncData()) {
172             synchronizeData();
173         }
174         return internalSubset;
175     }
176     
177     //
178
// Node methods
179
//
180

181     /**
182      * A short integer indicating what type of node this is. The named
183      * constants for this value are defined in the org.w3c.dom.Node interface.
184      */

185     public short getNodeType() {
186         return Node.DOCUMENT_TYPE_NODE;
187     }
188     
189     /**
190      * Returns the document type name
191      */

192     public String JavaDoc getNodeName() {
193         if (needsSyncData()) {
194             synchronizeData();
195         }
196         return name;
197     }
198
199     /** Clones the node. */
200     public Node JavaDoc cloneNode(boolean deep) {
201
202         DocumentTypeImpl newnode = (DocumentTypeImpl)super.cloneNode(deep);
203         // NamedNodeMaps must be cloned explicitly, to avoid sharing them.
204
newnode.entities = entities.cloneMap(newnode);
205         newnode.notations = notations.cloneMap(newnode);
206         newnode.elements = elements.cloneMap(newnode);
207
208         return newnode;
209
210     } // cloneNode(boolean):Node
211

212     /*
213      * Get Node text content
214      * @since DOM Level 3
215      */

216     public String JavaDoc getTextContent() throws DOMException JavaDoc {
217         return null;
218     }
219
220     /*
221      * Set Node text content
222      * @since DOM Level 3
223      */

224     public void setTextContent(String JavaDoc textContent)
225         throws DOMException JavaDoc {
226         // no-op
227
}
228     
229     /**
230       * DOM Level 3 WD- Experimental.
231       * Override inherited behavior from ParentNodeImpl to support deep equal.
232       */

233     public boolean isEqualNode(Node JavaDoc arg) {
234         
235         if (!super.isEqualNode(arg)) {
236             return false;
237         }
238         
239         if (needsSyncData()) {
240             synchronizeData();
241         }
242         DocumentTypeImpl argDocType = (DocumentTypeImpl) arg;
243
244         //test if the following string attributes are equal: publicId,
245
//systemId, internalSubset.
246
if ((getPublicId() == null && argDocType.getPublicId() != null)
247             || (getPublicId() != null && argDocType.getPublicId() == null)
248             || (getSystemId() == null && argDocType.getSystemId() != null)
249             || (getSystemId() != null && argDocType.getSystemId() == null)
250             || (getInternalSubset() == null
251                 && argDocType.getInternalSubset() != null)
252             || (getInternalSubset() != null
253                 && argDocType.getInternalSubset() == null)) {
254             return false;
255         }
256
257         if (getPublicId() != null) {
258             if (!getPublicId().equals(argDocType.getPublicId())) {
259                 return false;
260             }
261         }
262
263         if (getSystemId() != null) {
264             if (!getSystemId().equals(argDocType.getSystemId())) {
265                 return false;
266             }
267         }
268
269         if (getInternalSubset() != null) {
270             if (!getInternalSubset().equals(argDocType.getInternalSubset())) {
271                 return false;
272             }
273         }
274
275         //test if NamedNodeMaps entities and notations are equal
276
NamedNodeMapImpl argEntities = argDocType.entities;
277
278         if ((entities == null && argEntities != null)
279             || (entities != null && argEntities == null))
280             return false;
281
282         if (entities != null && argEntities != null) {
283             if (entities.getLength() != argEntities.getLength())
284                 return false;
285
286             for (int index = 0; entities.item(index) != null; index++) {
287                 Node JavaDoc entNode1 = entities.item(index);
288                 Node JavaDoc entNode2 =
289                     argEntities.getNamedItem(entNode1.getNodeName());
290
291                 if (!((NodeImpl) entNode1).isEqualNode((NodeImpl) entNode2))
292                     return false;
293             }
294         }
295
296         NamedNodeMapImpl argNotations = argDocType.notations;
297
298         if ((notations == null && argNotations != null)
299             || (notations != null && argNotations == null))
300             return false;
301
302         if (notations != null && argNotations != null) {
303             if (notations.getLength() != argNotations.getLength())
304                 return false;
305
306             for (int index = 0; notations.item(index) != null; index++) {
307                 Node JavaDoc noteNode1 = notations.item(index);
308                 Node JavaDoc noteNode2 =
309                     argNotations.getNamedItem(noteNode1.getNodeName());
310
311                 if (!((NodeImpl) noteNode1).isEqualNode((NodeImpl) noteNode2))
312                     return false;
313             }
314         }
315
316         return true;
317     } //end isEqualNode
318

319
320     /**
321      * NON-DOM
322      * set the ownerDocument of this node and its children
323      */

324     void setOwnerDocument(CoreDocumentImpl doc) {
325         super.setOwnerDocument(doc);
326         entities.setOwnerDocument(doc);
327         notations.setOwnerDocument(doc);
328         elements.setOwnerDocument(doc);
329     }
330
331     /** NON-DOM
332         Get the number associated with this doctype.
333     */

334     protected int getNodeNumber() {
335          // If the doctype has a document owner, get the node number
336
// relative to the owner doc
337
if (getOwnerDocument()!=null)
338             return super.getNodeNumber();
339
340          // The doctype is disconnected and not associated with any document.
341
// Assign the doctype a number relative to the implementation.
342
if (doctypeNumber==0) {
343           
344             CoreDOMImplementationImpl cd = (CoreDOMImplementationImpl)CoreDOMImplementationImpl.getDOMImplementation();
345             doctypeNumber = cd.assignDocTypeNumber();
346          }
347          return doctypeNumber;
348     }
349
350     //
351
// DocumentType methods
352
//
353

354     /**
355      * Name of this document type. If we loaded from a DTD, this should
356      * be the name immediately following the DOCTYPE keyword.
357      */

358     public String JavaDoc getName() {
359
360         if (needsSyncData()) {
361             synchronizeData();
362         }
363         return name;
364
365     } // getName():String
366

367     /**
368      * Access the collection of general Entities, both external and
369      * internal, defined in the DTD. For example, in:
370      * <p>
371      * <pre>
372      * &lt;!doctype example SYSTEM "ex.dtd" [
373      * &lt;!ENTITY foo "foo"&gt;
374      * &lt;!ENTITY bar "bar"&gt;
375      * &lt;!ENTITY % baz "baz"&gt;
376      * ]&gt;
377      * </pre>
378      * <p>
379      * The Entities map includes foo and bar, but not baz. It is promised that
380      * only Nodes which are Entities will exist in this NamedNodeMap.
381      * <p>
382      * For HTML, this will always be null.
383      * <p>
384      * Note that "built in" entities such as &amp; and &lt; should be
385      * converted to their actual characters before being placed in the DOM's
386      * contained text, and should be converted back when the DOM is rendered
387      * as XML or HTML, and hence DO NOT appear here.
388      */

389     public NamedNodeMap JavaDoc getEntities() {
390         if (needsSyncChildren()) {
391             synchronizeChildren();
392             }
393         return entities;
394     }
395
396     /**
397      * Access the collection of Notations defined in the DTD. A
398      * notation declares, by name, the format of an XML unparsed entity
399      * or is used to formally declare a Processing Instruction target.
400      */

401     public NamedNodeMap JavaDoc getNotations() {
402         if (needsSyncChildren()) {
403             synchronizeChildren();
404             }
405         return notations;
406     }
407
408     //
409
// Public methods
410
//
411

412     /**
413      * NON-DOM: Subclassed to flip the entities' and notations' readonly switch
414      * as well.
415      * @see NodeImpl#setReadOnly
416      */

417     public void setReadOnly(boolean readOnly, boolean deep) {
418         
419         if (needsSyncChildren()) {
420             synchronizeChildren();
421         }
422         super.setReadOnly(readOnly, deep);
423
424         // set read-only property
425
elements.setReadOnly(readOnly, true);
426         entities.setReadOnly(readOnly, true);
427         notations.setReadOnly(readOnly, true);
428
429     } // setReadOnly(boolean,boolean)
430

431     /**
432      * NON-DOM: Access the collection of ElementDefinitions.
433      * @see ElementDefinitionImpl
434      */

435     public NamedNodeMap JavaDoc getElements() {
436         if (needsSyncChildren()) {
437             synchronizeChildren();
438         }
439         return elements;
440     }
441     
442     public Object JavaDoc setUserData(String JavaDoc key,
443     Object JavaDoc data, UserDataHandler JavaDoc handler) {
444         if(userData == null)
445             userData = new Hashtable JavaDoc();
446         if (data == null) {
447             if (userData != null) {
448                 Object JavaDoc o = userData.remove(key);
449                 if (o != null) {
450                     UserDataRecord r = (UserDataRecord) o;
451                     return r.fData;
452                 }
453             }
454             return null;
455         }
456         else {
457             Object JavaDoc o = userData.put(key, new UserDataRecord(data, handler));
458             if (o != null) {
459                 UserDataRecord r = (UserDataRecord) o;
460                 return r.fData;
461             }
462         }
463         return null;
464     }
465     
466     public Object JavaDoc getUserData(String JavaDoc key) {
467         if (userData == null) {
468             return null;
469         }
470         Object JavaDoc o = userData.get(key);
471         if (o != null) {
472             UserDataRecord r = (UserDataRecord) o;
473             return r.fData;
474         }
475         return null;
476     }
477     
478     protected Hashtable JavaDoc getUserDataRecord(){
479         return userData;
480     }
481     
482 } // class DocumentTypeImpl
483
Popular Tags