1 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 32 public class TreeDocumentType extends AbstractTreeDTD implements TreeDTDRoot, Document.Child { 33 34 public static final String PROP_ELEMENT_NAME = "elementName"; 36 public static final String PROP_PUBLIC_ID = "publicId"; 38 public static final String PROP_SYSTEM_ID = "systemId"; 40 41 42 private String elementName; 43 44 45 private String publicId; 46 47 48 private String systemId; 49 50 private DTDIdentity dtdIdentity; 52 53 private static final WeakHashMap externalEntities = new WeakHashMap(); 55 56 private String internalDTDText; 58 62 66 public TreeDocumentType (String elementName, String publicId, String 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 84 public TreeDocumentType (String elementName) throws InvalidArgumentException { 85 this (elementName, null, null); 86 } 87 88 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 104 106 public Object clone (boolean deep) { 107 return new TreeDocumentType (this, deep); 108 } 109 110 112 public boolean equals (Object 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 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 150 151 154 protected void setReadOnly (boolean newReadOnly) { 155 super.setReadOnly (newReadOnly); 156 } 157 158 159 163 164 166 public boolean hasChildNodes (Class 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 182 if (childClass == null || childClass.isAssignableFrom (child.getClass ())) { 183 return true; 184 } 185 186 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 202 public Collection getChildNodes (Class 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 235 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 250 public final String getElementName () { 251 return elementName; 252 } 253 254 256 private final void setElementNameImpl (String newElementName) { 257 String oldElementName = this.elementName; 258 259 this.elementName = newElementName; 260 261 firePropertyChange (PROP_ELEMENT_NAME, oldElementName, newElementName); 262 } 263 264 268 public final void setElementName (String newElementName) throws ReadOnlyException, InvalidArgumentException { 269 if ( Util.equals (this.elementName, newElementName) ) 273 return; 274 checkReadOnly (); 275 checkElementName (newElementName); 276 277 setElementNameImpl (newElementName); 281 } 282 283 285 protected final void checkElementName (String elementName) throws InvalidArgumentException { 286 TreeUtilities.checkDocumentTypeElementName (elementName); 287 } 288 289 291 public final String getPublicId () { 292 return publicId; 293 } 294 295 297 private final void setPublicIdImpl (String newPublicId) { 298 String oldPublicId = this.publicId; 299 300 this.publicId = newPublicId; 301 302 firePropertyChange (PROP_PUBLIC_ID, oldPublicId, newPublicId); 303 } 304 305 309 public final void setPublicId (String newPublicId) throws ReadOnlyException, InvalidArgumentException { 310 if ( Util.equals (this.publicId, newPublicId) ) 314 return; 315 checkReadOnly (); 316 checkPublicId (newPublicId); 317 318 setPublicIdImpl (newPublicId); 322 } 323 324 326 protected final void checkPublicId (String publicId) throws InvalidArgumentException { 327 TreeUtilities.checkDocumentTypePublicId (publicId); 328 } 329 330 331 333 public final String getSystemId () { 334 return systemId; 335 } 336 337 339 private final void setSystemIdImpl (String newSystemId) { 340 String oldSystemId = this.systemId; 341 342 this.systemId = newSystemId; 343 344 firePropertyChange (PROP_SYSTEM_ID, oldSystemId, newSystemId); 345 } 346 347 351 public final void setSystemId (String newSystemId) throws ReadOnlyException, InvalidArgumentException { 352 if ( Util.equals (this.systemId, newSystemId) ) 356 return; 357 checkReadOnly (); 358 checkSystemId (newSystemId); 359 360 setSystemIdImpl (newSystemId); 364 } 365 366 368 protected final void checkSystemId (String systemId) throws InvalidArgumentException { 369 TreeUtilities.checkDocumentTypeSystemId (systemId); 370 } 371 372 373 377 379 protected TreeObjectList.ContentManager createChildListContentManager () { 380 return new ChildListContentManager (); 381 } 382 383 384 389 protected class ChildListContentManager extends AbstractTreeDTD.ChildListContentManager { 390 391 393 public TreeNode getOwnerNode () { 394 return TreeDocumentType.this; 395 } 396 397 399 public void checkAssignableObject (Object obj) { 400 super.checkAssignableObject (obj); 401 checkAssignableClass (DocumentType.Child.class, obj); 402 } 403 404 } 406 407 408 411 public final DTDIdentity getDTDIdentity() { 412 return dtdIdentity; 413 } 414 415 419 public final void setExternalDTD(TreeDocumentFragment externalDTD) { 420 externalEntities.put(getDTDIdentity(), externalDTD); 421 } 422 423 428 public final class DTDIdentity { 429 430 private DTDIdentity() { 431 } 432 433 private String getPublicId() { 434 return publicId; 435 } 436 437 private String getSystemId() { 438 return systemId; 439 } 440 441 public boolean equals(Object 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 |