KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > tax > TreeDocumentType


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.tax;
20
21 import org.netbeans.tax.spec.Document;
22 import org.netbeans.tax.spec.DocumentType;
23 import org.netbeans.tax.spec.DTD;
24
25 import java.util.*;
26
27 /**
28  *
29  * @author Libor Kramolis
30  * @version 0.1
31  */

32 public class TreeDocumentType extends AbstractTreeDTD implements TreeDTDRoot, Document.Child {
33     /** */
34     public static final String JavaDoc PROP_ELEMENT_NAME = "elementName"; // NOI18N
35
/** */
36     public static final String JavaDoc PROP_PUBLIC_ID = "publicId"; // NOI18N
37
/** */
38     public static final String JavaDoc PROP_SYSTEM_ID = "systemId"; // NOI18N
39

40     
41     /** */
42     private String JavaDoc elementName;
43     
44     /** -- can be null. */
45     private String JavaDoc publicId;
46     
47     /** -- can be null. */
48     private String JavaDoc systemId;
49
50     // strong reference to keep a key in bellow map
51
private DTDIdentity dtdIdentity;
52
53     // holds DTD-ID -> TreeDocumentFragment mapping
54
private static final WeakHashMap externalEntities = new WeakHashMap();
55
56     private String JavaDoc internalDTDText; //!!! it is accesed by introspection, it a hack
57

58     //
59
// init
60
//
61

62     /**
63      * Creates new TreeDocumentType.
64      * @throws InvalidArgumentException
65      */

66     public TreeDocumentType (String JavaDoc elementName, String JavaDoc publicId, String JavaDoc systemId) throws InvalidArgumentException {
67         super ();
68         
69         checkElementName (elementName);
70         checkPublicId (publicId);
71         checkSystemId (systemId);
72         
73         this.elementName = elementName;
74         this.publicId = publicId;
75         this.systemId = systemId;
76         this.dtdIdentity = new DTDIdentity();
77
78     }
79     
80     
81     /** Creates new TreeDocumentType.
82      * @throws InvalidArgumentException
83      */

84     public TreeDocumentType (String JavaDoc elementName) throws InvalidArgumentException {
85         this (elementName, null, null);
86     }
87     
88     /** Creates new TreeDocumentType -- copy constructor. */
89     protected TreeDocumentType (TreeDocumentType documentType, boolean deep) {
90         super (documentType, deep);
91         
92         this.elementName = documentType.elementName;
93         this.publicId = documentType.publicId;
94         this.systemId = documentType.systemId;
95         this.internalDTDText = documentType.internalDTDText;
96         this.dtdIdentity = documentType.dtdIdentity;
97     }
98     
99     
100     //
101
// from TreeObject
102
//
103

104     /**
105      */

106     public Object JavaDoc clone (boolean deep) {
107         return new TreeDocumentType (this, deep);
108     }
109     
110     /**
111      */

112     public boolean equals (Object JavaDoc object, boolean deep) {
113         if (!!! super.equals (object, deep))
114             return false;
115         
116         TreeDocumentType peer = (TreeDocumentType) object;
117         if (!!! Util.equals (this.getElementName (), peer.getElementName ()))
118             return false;
119         if (!!! Util.equals (this.getPublicId (), peer.getPublicId ()))
120             return false;
121         if (!!! Util.equals (this.getSystemId (), peer.getSystemId ()))
122             return false;
123         if (!!! Util.equals (this.dtdIdentity, peer.dtdIdentity))
124             return false;
125         
126         return true;
127     }
128     
129     /*
130      * Merges documet root name, publicId and system ID properties.
131      * External DTD list merging is delegated.
132      */

133     public void merge (TreeObject treeObject) throws CannotMergeException {
134         super.merge (treeObject);
135         
136         TreeDocumentType peer = (TreeDocumentType) treeObject;
137         
138         setElementNameImpl (peer.getElementName ());
139         setPublicIdImpl (peer.getPublicId ());
140         setSystemIdImpl (peer.getSystemId ());
141         internalDTDText = peer.internalDTDText;
142         dtdIdentity = peer.dtdIdentity;
143     }
144     
145     
146     //
147
// read only
148
//
149

150     
151     /**
152      * It's not propagated to exteranl entity. It's always read only.
153      */

154     protected void setReadOnly (boolean newReadOnly) {
155         super.setReadOnly (newReadOnly);
156     }
157     
158     
159     //
160
// parent
161
//
162

163     
164     /**
165      */

166     public boolean hasChildNodes (Class JavaDoc childClass, boolean recursive) {
167         TreeObjectList external = getExternalDTD();
168         Iterator externalIterator = external != null ?
169                 external.iterator() : Collections.EMPTY_SET.iterator();
170         Iterator[] its = new Iterator[] {
171             getChildNodes ().iterator (),
172             externalIterator
173         };
174         
175         for (int i = 0; i<its.length; i++) {
176             Iterator it = its[i];
177             while (it.hasNext ()) {
178                 TreeChild child = (TreeChild)it.next ();
179                 
180                 // add matching leaf node
181

182                 if (childClass == null || childClass.isAssignableFrom (child.getClass ())) {
183                     return true;
184                 }
185                 
186                 // do recursive descent into kids
187

188                 if ( recursive && (child instanceof TreeParentNode) ) {
189                     if ( ((TreeParentNode)child).hasChildNodes (childClass, true) == true ) {
190                         return true;
191                     }
192                 }
193             }
194         }
195         return false;
196     }
197     
198     /**
199      * @return copy collection containing references from internal and
200      * optionally external part of DTD
201      */

202     public Collection getChildNodes (Class JavaDoc childClass, boolean recursive) {
203         Collection allChildNodes = new LinkedList ();
204         TreeObjectList external = getExternalDTD();
205         Iterator externalIterator = external != null ?
206                 external.iterator() : Collections.EMPTY_SET.iterator();
207
208         Iterator[] its = new Iterator[] {
209             getChildNodes ().iterator (),
210             externalIterator
211         };
212         
213         for (int i = 0; i<its.length; i++) {
214             Iterator it = its[i];
215             while (it.hasNext ()) {
216                 TreeChild child = (TreeChild)it.next ();
217                 if (childClass == null || childClass.isAssignableFrom (child.getClass ())) {
218                     allChildNodes.add (child);
219                 }
220                 
221                 if ( recursive && (child instanceof TreeParentNode) ) {
222                     allChildNodes.addAll (((TreeParentNode)child).getChildNodes (childClass, true));
223                 }
224             }
225         }
226         
227         return allChildNodes;
228     }
229     
230     
231     //
232
// itself
233
//
234

235     /**
236      * Return read only child list representing external DTD content
237      * or <code>null</code> if unknown.
238      */

239     public final TreeObjectList getExternalDTD () {
240         TreeDTDFragment fragment = (TreeDTDFragment) externalEntities.get(dtdIdentity);
241         if (fragment == null) {
242             return null;
243         } else {
244             return fragment.getChildNodes();
245         }
246     }
247     
248     /**
249      */

250     public final String JavaDoc getElementName () {
251         return elementName;
252     }
253     
254     /**
255      */

256     private final void setElementNameImpl (String JavaDoc newElementName) {
257         String JavaDoc oldElementName = this.elementName;
258         
259         this.elementName = newElementName;
260         
261         firePropertyChange (PROP_ELEMENT_NAME, oldElementName, newElementName);
262     }
263     
264     /**
265      * @throws ReadOnlyException
266      * @throws InvalidArgumentException
267      */

268     public final void setElementName (String JavaDoc newElementName) throws ReadOnlyException, InvalidArgumentException {
269         //
270
// check new value
271
//
272
if ( Util.equals (this.elementName, newElementName) )
273             return;
274         checkReadOnly ();
275         checkElementName (newElementName);
276         
277         //
278
// set new value
279
//
280
setElementNameImpl (newElementName);
281     }
282     
283     /**
284      */

285     protected final void checkElementName (String JavaDoc elementName) throws InvalidArgumentException {
286         TreeUtilities.checkDocumentTypeElementName (elementName);
287     }
288     
289     /**
290      */

291     public final String JavaDoc getPublicId () {
292         return publicId;
293     }
294     
295     /**
296      */

297     private final void setPublicIdImpl (String JavaDoc newPublicId) {
298         String JavaDoc oldPublicId = this.publicId;
299         
300         this.publicId = newPublicId;
301
302         firePropertyChange (PROP_PUBLIC_ID, oldPublicId, newPublicId);
303     }
304     
305     /**
306      * @throws ReadOnlyException
307      * @throws InvalidArgumentException
308      */

309     public final void setPublicId (String JavaDoc newPublicId) throws ReadOnlyException, InvalidArgumentException {
310         //
311
// check new value
312
//
313
if ( Util.equals (this.publicId, newPublicId) )
314             return;
315         checkReadOnly ();
316         checkPublicId (newPublicId);
317         
318         //
319
// set new value
320
//
321
setPublicIdImpl (newPublicId);
322     }
323     
324     /**
325      */

326     protected final void checkPublicId (String JavaDoc publicId) throws InvalidArgumentException {
327         TreeUtilities.checkDocumentTypePublicId (publicId);
328     }
329     
330     
331     /**
332      */

333     public final String JavaDoc getSystemId () {
334         return systemId;
335     }
336     
337     /**
338      */

339     private final void setSystemIdImpl (String JavaDoc newSystemId) {
340         String JavaDoc oldSystemId = this.systemId;
341         
342         this.systemId = newSystemId;
343         
344         firePropertyChange (PROP_SYSTEM_ID, oldSystemId, newSystemId);
345     }
346     
347     /**
348      * @throws ReadOnlyException
349      * @throws InvalidArgumentException
350      */

351     public final void setSystemId (String JavaDoc newSystemId) throws ReadOnlyException, InvalidArgumentException {
352         //
353
// check new value
354
//
355
if ( Util.equals (this.systemId, newSystemId) )
356             return;
357         checkReadOnly ();
358         checkSystemId (newSystemId);
359         
360         //
361
// set new value
362
//
363
setSystemIdImpl (newSystemId);
364     }
365     
366     /**
367      */

368     protected final void checkSystemId (String JavaDoc systemId) throws InvalidArgumentException {
369         TreeUtilities.checkDocumentTypeSystemId (systemId);
370     }
371     
372     
373     //
374
// TreeObjectList.ContentManager
375
//
376

377     /**
378      */

379     protected TreeObjectList.ContentManager createChildListContentManager () {
380         return new ChildListContentManager ();
381     }
382     
383
384     /**
385      * Internal DTD content manager.
386      * All kids use as parent node wrapping TreeDocumentType.
387      * All kids must be DocumentType.Child instances.
388      */

389     protected class ChildListContentManager extends AbstractTreeDTD.ChildListContentManager {
390         
391         /**
392          */

393         public TreeNode getOwnerNode () {
394             return TreeDocumentType.this;
395         }
396         
397         /**
398          */

399         public void checkAssignableObject (Object JavaDoc obj) {
400             super.checkAssignableObject (obj);
401             checkAssignableClass (DocumentType.Child.class, obj);
402         }
403         
404     } // end: class ChildListContentManager
405

406     
407
408     /**
409      * Get DTDIdentity proxy for this class. It's a live object.
410      */

411     public final DTDIdentity getDTDIdentity() {
412         return dtdIdentity;
413     }
414
415     /**
416      * Set new external DTD model. Note that it can be shared by
417      * several TreeDocumentType instances.
418      */

419     public final void setExternalDTD(TreeDocumentFragment externalDTD) {
420         externalEntities.put(getDTDIdentity(), externalDTD);
421     }
422
423     /**
424      * Defines doctype identity based on its public ID and system ID pairs.
425      * Can be used as key if such equalince/identity is required.
426      * @see #getDTDIdentity
427      */

428     public final class DTDIdentity {
429
430         private DTDIdentity() {
431         }
432
433         private String JavaDoc getPublicId() {
434             return publicId;
435         }
436
437         private String JavaDoc getSystemId() {
438             return systemId;
439         }
440
441         public boolean equals(Object JavaDoc o) {
442             if (o == this) return true;
443             if (o instanceof DTDIdentity) {
444                 DTDIdentity peer = (DTDIdentity) o;
445                 if (Util.equals(peer.getPublicId(), publicId) == false) return false;
446                 if (Util.equals(peer.getSystemId(), systemId) == false) return false;
447                 return true;
448             }
449             return false;
450         }
451
452         public int hashCode() {
453             int h1 = publicId != null ? publicId.hashCode() : 13;
454             int h2 = systemId != null ? systemId.hashCode() : 37;
455             return h1 ^ h2;
456         }
457     }
458 }
459
Popular Tags