KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xerces > internal > dom > DocumentTypeImpl


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package com.sun.org.apache.xerces.internal.dom;
59
60 import org.w3c.dom.DOMException JavaDoc;
61 import org.w3c.dom.DocumentType JavaDoc;
62 import org.w3c.dom.Node JavaDoc;
63 import org.w3c.dom.NamedNodeMap JavaDoc;
64 import java.util.Hashtable JavaDoc;
65 import org.w3c.dom.UserDataHandler JavaDoc;
66
67 /**
68  * This class represents a Document Type <em>declaraction</em> in
69  * the document itself, <em>not</em> a Document Type Definition (DTD).
70  * An XML document may (or may not) have such a reference.
71  * <P>
72  * DocumentType is an Extended DOM feature, used in XML documents but
73  * not in HTML.
74  * <P>
75  * Note that Entities and Notations are no longer children of the
76  * DocumentType, but are parentless nodes hung only in their
77  * appropriate NamedNodeMaps.
78  * <P>
79  * This area is UNDERSPECIFIED IN REC-DOM-Level-1-19981001
80  * Most notably, absolutely no provision was made for storing
81  * and using Element and Attribute information. Nor was the linkage
82  * between Entities and Entity References nailed down solidly.
83  *
84  * @author Arnaud Le Hors, IBM
85  * @author Joe Kesselman, IBM
86  * @author Andy Clark, IBM
87  * @version $Id: DocumentTypeImpl.java,v 1.25 2004/01/16 16:23:50 elena Exp $
88  * @since PR-DOM-Level-1-19980818.
89  */

90 public class DocumentTypeImpl
91 extends ParentNode
92 implements DocumentType JavaDoc {
93     
94     //
95
// Constants
96
//
97

98     /** Serialization version. */
99     static final long serialVersionUID = 7751299192316526485L;
100     
101     //
102
// Data
103
//
104

105     /** Document type name. */
106     protected String JavaDoc name;
107     
108     /** Entities. */
109     protected NamedNodeMapImpl entities;
110     
111     /** Notations. */
112     protected NamedNodeMapImpl notations;
113     
114     // NON-DOM
115

116     /** Elements. */
117     protected NamedNodeMapImpl elements;
118     
119     // DOM2: support public ID.
120
protected String JavaDoc publicID;
121     
122     // DOM2: support system ID.
123
protected String JavaDoc systemID;
124     
125     // DOM2: support internal subset.
126
protected String JavaDoc internalSubset;
127     
128     /** The following are required for compareDocumentPosition
129      */

130     // Doctype number. Doc types which have no owner may be assigned
131
// a number, on demand, for ordering purposes for compareDocumentPosition
132
private int doctypeNumber=0;
133     
134     //
135
// Constructors
136
//
137
private Hashtable JavaDoc userData = null;
138     
139     /** Factory method for creating a document type node. */
140     public DocumentTypeImpl(CoreDocumentImpl ownerDocument, String JavaDoc name) {
141         super(ownerDocument);
142         
143         this.name = name;
144         // DOM
145
entities = new NamedNodeMapImpl(this);
146         notations = new NamedNodeMapImpl(this);
147         
148         // NON-DOM
149
elements = new NamedNodeMapImpl(this);
150         
151     } // <init>(CoreDocumentImpl,String)
152

153     /** Factory method for creating a document type node. */
154     public DocumentTypeImpl(CoreDocumentImpl ownerDocument,
155     String JavaDoc qualifiedName,
156     String JavaDoc publicID, String JavaDoc systemID) {
157         this(ownerDocument, qualifiedName);
158         this.publicID = publicID;
159         this.systemID = systemID;
160         
161     } // <init>(CoreDocumentImpl,String)
162

163     //
164
// DOM2: methods.
165
//
166

167     /**
168      * Introduced in DOM Level 2. <p>
169      *
170      * Return the public identifier of this Document type.
171      * @since WD-DOM-Level-2-19990923
172      */

173     public String JavaDoc getPublicId() {
174         if (needsSyncData()) {
175             synchronizeData();
176         }
177         return publicID;
178     }
179     /**
180      * Introduced in DOM Level 2. <p>
181      *
182      * Return the system identifier of this Document type.
183      * @since WD-DOM-Level-2-19990923
184      */

185     public String JavaDoc getSystemId() {
186         if (needsSyncData()) {
187             synchronizeData();
188         }
189         return systemID;
190     }
191     
192     /**
193      * NON-DOM. <p>
194      *
195      * Set the internalSubset given as a string.
196      */

197     public void setInternalSubset(String JavaDoc internalSubset) {
198         if (needsSyncData()) {
199             synchronizeData();
200         }
201         this.internalSubset = internalSubset;
202     }
203     
204     /**
205      * Introduced in DOM Level 2. <p>
206      *
207      * Return the internalSubset given as a string.
208      * @since WD-DOM-Level-2-19990923
209      */

210     public String JavaDoc getInternalSubset() {
211         if (needsSyncData()) {
212             synchronizeData();
213         }
214         return internalSubset;
215     }
216     
217     //
218
// Node methods
219
//
220

221     /**
222      * A short integer indicating what type of node this is. The named
223      * constants for this value are defined in the org.w3c.dom.Node interface.
224      */

225     public short getNodeType() {
226         return Node.DOCUMENT_TYPE_NODE;
227     }
228     
229     /**
230      * Returns the document type name
231      */

232     public String JavaDoc getNodeName() {
233         if (needsSyncData()) {
234             synchronizeData();
235         }
236         return name;
237     }
238     
239     /** Clones the node. */
240     public Node JavaDoc cloneNode(boolean deep) {
241         
242         DocumentTypeImpl newnode = (DocumentTypeImpl)super.cloneNode(deep);
243         // NamedNodeMaps must be cloned explicitly, to avoid sharing them.
244
newnode.entities = entities.cloneMap(newnode);
245         newnode.notations = notations.cloneMap(newnode);
246         newnode.elements = elements.cloneMap(newnode);
247         
248         return newnode;
249         
250     } // cloneNode(boolean):Node
251

252     /*
253      * Get Node text content
254      * @since DOM Level 3
255      */

256     public String JavaDoc getTextContent() throws DOMException JavaDoc {
257         return null;
258     }
259     
260     /*
261      * Set Node text content
262      * @since DOM Level 3
263      */

264     public void setTextContent(String JavaDoc textContent)
265     throws DOMException JavaDoc {
266         // no-op
267
}
268     
269     /**
270      * DOM Level 3 WD- Experimental.
271      * Override inherited behavior from ParentNodeImpl to support deep equal.
272      */

273     public boolean isEqualNode(Node JavaDoc arg) {
274         
275         if (!super.isEqualNode(arg)) {
276             return false;
277         }
278         
279         if (needsSyncData()) {
280             synchronizeData();
281         }
282         DocumentTypeImpl argDocType = (DocumentTypeImpl) arg;
283         
284         //test if the following string attributes are equal: publicId,
285
//systemId, internalSubset.
286
if ((getPublicId() == null && argDocType.getPublicId() != null)
287         || (getPublicId() != null && argDocType.getPublicId() == null)
288         || (getSystemId() == null && argDocType.getSystemId() != null)
289         || (getSystemId() != null && argDocType.getSystemId() == null)
290         || (getInternalSubset() == null
291         && argDocType.getInternalSubset() != null)
292         || (getInternalSubset() != null
293         && argDocType.getInternalSubset() == null)) {
294             return false;
295         }
296         
297         if (getPublicId() != null) {
298             if (!getPublicId().equals(argDocType.getPublicId())) {
299                 return false;
300             }
301         }
302         
303         if (getSystemId() != null) {
304             if (!getSystemId().equals(argDocType.getSystemId())) {
305                 return false;
306             }
307         }
308         
309         if (getInternalSubset() != null) {
310             if (!getInternalSubset().equals(argDocType.getInternalSubset())) {
311                 return false;
312             }
313         }
314         
315         //test if NamedNodeMaps entities and notations are equal
316
NamedNodeMapImpl argEntities = argDocType.entities;
317         
318         if ((entities == null && argEntities != null)
319         || (entities != null && argEntities == null))
320             return false;
321         
322         if (entities != null && argEntities != null) {
323             if (entities.getLength() != argEntities.getLength())
324                 return false;
325             
326             for (int index = 0; entities.item(index) != null; index++) {
327                 Node JavaDoc entNode1 = entities.item(index);
328                 Node JavaDoc entNode2 =
329                 argEntities.getNamedItem(entNode1.getNodeName());
330                 
331                 if (!((NodeImpl) entNode1).isEqualNode((NodeImpl) entNode2))
332                     return false;
333             }
334         }
335         
336         NamedNodeMapImpl argNotations = argDocType.notations;
337         
338         if ((notations == null && argNotations != null)
339         || (notations != null && argNotations == null))
340             return false;
341         
342         if (notations != null && argNotations != null) {
343             if (notations.getLength() != argNotations.getLength())
344                 return false;
345             
346             for (int index = 0; notations.item(index) != null; index++) {
347                 Node JavaDoc noteNode1 = notations.item(index);
348                 Node JavaDoc noteNode2 =
349                 argNotations.getNamedItem(noteNode1.getNodeName());
350                 
351                 if (!((NodeImpl) noteNode1).isEqualNode((NodeImpl) noteNode2))
352                     return false;
353             }
354         }
355         
356         return true;
357     } //end isEqualNode
358

359     
360     /**
361      * NON-DOM
362      * set the ownerDocument of this node and its children
363      */

364     void setOwnerDocument(CoreDocumentImpl doc) {
365         super.setOwnerDocument(doc);
366         entities.setOwnerDocument(doc);
367         notations.setOwnerDocument(doc);
368         elements.setOwnerDocument(doc);
369     }
370     
371     /** NON-DOM
372      * Get the number associated with this doctype.
373      */

374     protected int getNodeNumber() {
375         // If the doctype has a document owner, get the node number
376
// relative to the owner doc
377
if (getOwnerDocument()!=null)
378             return super.getNodeNumber();
379         
380         // The doctype is disconnected and not associated with any document.
381
// Assign the doctype a number relative to the implementation.
382
if (doctypeNumber==0) {
383             
384             CoreDOMImplementationImpl cd = (CoreDOMImplementationImpl)CoreDOMImplementationImpl.getDOMImplementation();
385             doctypeNumber = cd.assignDocTypeNumber();
386         }
387         return doctypeNumber;
388     }
389     
390     //
391
// DocumentType methods
392
//
393

394     /**
395      * Name of this document type. If we loaded from a DTD, this should
396      * be the name immediately following the DOCTYPE keyword.
397      */

398     public String JavaDoc getName() {
399         
400         if (needsSyncData()) {
401             synchronizeData();
402         }
403         return name;
404         
405     } // getName():String
406

407     /**
408      * Access the collection of general Entities, both external and
409      * internal, defined in the DTD. For example, in:
410      * <p>
411      * <pre>
412      * &lt;!doctype example SYSTEM "ex.dtd" [
413      * &lt;!ENTITY foo "foo"&gt;
414      * &lt;!ENTITY bar "bar"&gt;
415      * &lt;!ENTITY % baz "baz"&gt;
416      * ]&gt;
417      * </pre>
418      * <p>
419      * The Entities map includes foo and bar, but not baz. It is promised that
420      * only Nodes which are Entities will exist in this NamedNodeMap.
421      * <p>
422      * For HTML, this will always be null.
423      * <p>
424      * Note that "built in" entities such as &amp; and &lt; should be
425      * converted to their actual characters before being placed in the DOM's
426      * contained text, and should be converted back when the DOM is rendered
427      * as XML or HTML, and hence DO NOT appear here.
428      */

429     public NamedNodeMap JavaDoc getEntities() {
430         if (needsSyncChildren()) {
431             synchronizeChildren();
432         }
433         return entities;
434     }
435     
436     /**
437      * Access the collection of Notations defined in the DTD. A
438      * notation declares, by name, the format of an XML unparsed entity
439      * or is used to formally declare a Processing Instruction target.
440      */

441     public NamedNodeMap JavaDoc getNotations() {
442         if (needsSyncChildren()) {
443             synchronizeChildren();
444         }
445         return notations;
446     }
447     
448     //
449
// Public methods
450
//
451

452     /**
453      * NON-DOM: Subclassed to flip the entities' and notations' readonly switch
454      * as well.
455      * @see NodeImpl#setReadOnly
456      */

457     public void setReadOnly(boolean readOnly, boolean deep) {
458         
459         if (needsSyncChildren()) {
460             synchronizeChildren();
461         }
462         super.setReadOnly(readOnly, deep);
463         
464         // set read-only property
465
elements.setReadOnly(readOnly, true);
466         entities.setReadOnly(readOnly, true);
467         notations.setReadOnly(readOnly, true);
468         
469     } // setReadOnly(boolean,boolean)
470

471     /**
472      * NON-DOM: Access the collection of ElementDefinitions.
473      * @see ElementDefinitionImpl
474      */

475     public NamedNodeMap JavaDoc getElements() {
476         if (needsSyncChildren()) {
477             synchronizeChildren();
478         }
479         return elements;
480     }
481     
482     public Object JavaDoc setUserData(String JavaDoc key,
483     Object JavaDoc data, UserDataHandler JavaDoc handler) {
484         if(userData == null)
485             userData = new Hashtable JavaDoc();
486         if (data == null) {
487             if (userData != null) {
488                 Object JavaDoc o = userData.remove(key);
489                 if (o != null) {
490                     UserDataRecord r = (UserDataRecord) o;
491                     return r.fData;
492                 }
493             }
494             return null;
495         }
496         else {
497             Object JavaDoc o = userData.put(key, new UserDataRecord(data, handler));
498             if (o != null) {
499                 UserDataRecord r = (UserDataRecord) o;
500                 return r.fData;
501             }
502         }
503         return null;
504     }
505     
506     public Object JavaDoc getUserData(String JavaDoc key) {
507         if (userData == null) {
508             return null;
509         }
510         Object JavaDoc o = userData.get(key);
511         if (o != null) {
512             UserDataRecord r = (UserDataRecord) o;
513             return r.fData;
514         }
515         return null;
516     }
517     
518     protected Hashtable JavaDoc getUserDataRecord(){
519         return userData;
520     }
521     
522 } // class DocumentTypeImpl
523
Popular Tags