1 19 package org.netbeans.tax; 20 21 import java.util.Iterator ; 22 23 import org.netbeans.tax.spec.Element; 24 import org.netbeans.tax.spec.Attribute; 25 26 33 public class TreeAttribute extends TreeNode implements Element.Attribute, TreeNamedObjectMap.NamedObject { 34 35 36 public static final String PROP_NAME = "name"; 38 public static final String PROP_VALUE = "value"; 40 public static final String PROP_OWNER_ELEMENT = "ownerElement"; 42 public static final String PROP_SPECIFIED = "specified"; 44 45 private TreeElement ownerElement; 47 48 private TreeName name; 50 51 private TreeObjectList valueList; 52 53 54 private boolean specified; 56 57 private TreeNamedObjectMap.KeyListener mapKeyListener; 58 59 60 64 71 public TreeAttribute (String qName, String value, boolean specified) throws InvalidArgumentException { 72 super (); 73 74 TreeName treeName = new TreeName (qName); 75 checkName (treeName); 76 checkValue (value); 77 78 this.name = treeName; 79 this.specified = specified; 80 this.valueList = new TreeObjectList (createValueListContentManager ()); 81 setValueImpl (value); 82 83 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeAttribute::INIT : name = " + qName + " : specified = " + specified); } 85 86 92 public TreeAttribute (String qName, String value) throws InvalidArgumentException { 93 this (qName, value, true); 94 } 95 96 97 100 protected TreeAttribute (TreeAttribute attribute) { 101 super (attribute); 102 103 this.name = attribute.name; 104 this.specified = true; this.valueList = new TreeObjectList (createValueListContentManager ()); 106 this.valueList.addAll ((TreeObjectList)attribute.valueList.clone ()); 107 } 108 109 110 114 116 public Object clone () { 117 return new TreeAttribute (this); 118 } 119 120 122 public boolean equals (Object object, boolean deep) { 123 if (!!! super.equals (object, deep)) 124 return false; 125 126 TreeAttribute peer = (TreeAttribute) object; 127 if (!!! Util.equals (this.getTreeName (), peer.getTreeName ())) 128 return false; 129 if (this.specified != peer.isSpecified ()) 130 return false; 131 if (!!! Util.equals (this.valueList, peer.valueList)) 132 return false; 133 134 return true; 135 } 136 137 140 public void merge (TreeObject treeObject) throws CannotMergeException { 141 super.merge (treeObject); 142 143 TreeAttribute peer = (TreeAttribute) treeObject; 144 145 try { 146 setTreeNameImpl (peer.getTreeName ()); 147 setSpecifiedImpl (peer.isSpecified ()); 148 valueList.merge (peer.valueList); 149 } catch (Exception exc) { 150 throw new CannotMergeException (treeObject, exc); 151 } 152 } 153 154 155 159 160 162 protected void setReadOnly (boolean newReadOnly) { 163 super.setReadOnly (newReadOnly); 164 165 valueList.setReadOnly (newReadOnly); 166 } 167 168 169 173 175 public final boolean isInContext () { 176 return ( getOwnerElement () != null ); 177 } 178 179 181 public final void removeFromContext () throws ReadOnlyException { 182 if ( isInContext () ) { 183 getOwnerElement ().removeAttribute (this); 184 } 185 } 186 187 188 192 194 public final String getQName () { 195 return name.getQualifiedName (); 196 } 197 198 202 public final void setQName (String name) throws ReadOnlyException, InvalidArgumentException { 203 setTreeName (new TreeName (name)); 204 } 205 206 208 public final TreeName getTreeName () { 209 return name; 210 } 211 212 214 private final void setTreeNameImpl (TreeName newName) { 215 TreeName oldName = this.name; 216 217 this.name = newName; 218 219 fireMapKeyChanged (oldName); 220 firePropertyChange (PROP_NAME, oldName, newName); 221 } 222 223 227 public final void setTreeName (TreeName newName) throws ReadOnlyException, InvalidArgumentException { 228 if ( Util.equals (this.name, newName) ) 232 return; 233 checkReadOnly (); 234 checkName (newName); 235 236 setTreeNameImpl (newName); 240 } 241 242 244 protected final void checkName (TreeName name) throws InvalidArgumentException { 245 TreeUtilities.checkAttributeName (name); 246 } 247 248 249 250 public boolean isSpecified () { 251 return specified; 252 } 253 254 258 private void setSpecifiedImpl (boolean newValue) { 259 if (this.specified == newValue) 260 return; 261 262 Boolean oldValue = this.specified ? Boolean.TRUE : Boolean.FALSE; 263 264 this.specified = newValue; 265 266 firePropertyChange (PROP_SPECIFIED, oldValue, newValue ? Boolean.TRUE : Boolean.FALSE); 267 } 268 269 272 public final TreeObjectList getValueList () { 273 return valueList; 274 } 275 276 279 public final String getValue () { 280 StringBuffer value = new StringBuffer (23); 281 Iterator it = valueList.iterator (); 282 283 while (it.hasNext ()) { 284 Object next = it.next (); 285 if (next instanceof TreeData) { 286 value.append (((TreeData)next).getData ()); 287 } else if (next instanceof TreeGeneralEntityReference) { 288 value.append ("&" + ((TreeGeneralEntityReference)next).getName () + ";"); } else if (next instanceof TreeCharacterReference) { 291 value.append (((TreeCharacterReference)next).getData ()); 292 } 293 } 294 return value.toString (); 295 } 296 297 300 public final String getNonNormalizedValue () { 301 StringBuffer value = new StringBuffer (23); 302 Iterator it = valueList.iterator (); 303 304 while (it.hasNext ()) { 305 Object next = it.next (); 306 if (next instanceof TreeData) { 307 value.append (((TreeData)next).getData ()); 308 } else if (next instanceof TreeGeneralEntityReference) { 309 value.append ("&" + ((TreeGeneralEntityReference)next).getName () + ";"); } else if (next instanceof TreeCharacterReference) { 311 value.append ("&" + ((TreeCharacterReference)next).getName () + ";"); } 313 } 314 return value.toString (); 315 } 316 317 321 private final void setValueImpl (String newValue) { 322 String oldValue = this.getValue (); 323 324 this.valueList.clear (); 325 326 if ( newValue.length () != 0) { 327 try { 328 TreeText newText = new TreeText (newValue); 329 this.valueList.add (newText); 330 } catch (TreeException exc) { 331 } 333 } 334 335 firePropertyChange (PROP_VALUE, oldValue, newValue); 336 } 337 338 344 public final void setValue (String newValue) throws ReadOnlyException, InvalidArgumentException { 345 if ( Util.equals (this.getValue (), newValue) ) 349 return; 350 checkReadOnly (); 351 checkValue (newValue); 352 353 setValueImpl (newValue); 357 } 358 359 362 protected final void checkValue (String value) throws InvalidArgumentException { 363 TreeUtilities.checkAttributeValue (value); 364 } 365 366 367 368 372 375 public final TreeNamespace getNamespace () { 376 if (getOwnerElement () != null) { 377 TreeElement owner = getOwnerElement (); 378 TreeNamespaceContext ctx = owner.getNamespaceContext (); 379 String prefix = getNamespacePrefix (); 380 String uri = ctx.getURI (prefix); 381 if (uri == null) { 382 return TreeNamespace.NO_NAMESPACE; 383 } else { 384 return new TreeNamespace (prefix, uri); 385 } 386 } 387 return TreeNamespace.NO_NAMESPACE; 388 } 389 390 392 public final String getNamespacePrefix () { 393 return name.getPrefix (); 394 } 395 396 397 399 public final String getNamespaceURI () { 400 return getNamespace ().getURI (); 401 } 402 403 404 406 public final String getLocalName () { 407 return name.getName (); 408 } 409 410 411 415 417 public Object mapKey () { 418 return getTreeName (); 419 } 420 421 423 427 428 public void setKeyListener (TreeNamedObjectMap.KeyListener keyListener) { 429 mapKeyListener = keyListener; 430 } 431 432 private void fireMapKeyChanged (Object oldKey) { 433 if ( mapKeyListener == null ) { 434 return; 435 } 436 mapKeyListener.mapKeyChanged (oldKey); 437 } 438 439 440 444 446 public final TreeDocumentRoot getOwnerDocument () { 447 if ( getOwnerElement () == null ) 448 return null; 449 return getOwnerElement ().getOwnerDocument (); 450 } 451 452 456 458 public final TreeElement getOwnerElement () { 459 return ownerElement; 460 } 461 462 464 protected final void setOwnerElement (TreeElement newOwnerElement) { 465 if (Util.equals (ownerElement, newOwnerElement)) 466 return; 467 468 TreeElement oldOwnerElement = this.ownerElement; 469 470 this.ownerElement = newOwnerElement; 471 472 firePropertyChange (getEventChangeSupport ().createEvent (PROP_OWNER_ELEMENT, oldOwnerElement, newOwnerElement)); 473 } 474 475 476 477 481 483 protected TreeObjectList.ContentManager createValueListContentManager () { 484 return new ValueListContentManager (); 485 } 486 487 488 491 protected class ValueListContentManager extends TreeObjectList.ContentManager { 492 493 495 public TreeNode getOwnerNode () { 496 return TreeAttribute.this; 497 } 498 499 501 public void checkAssignableObject (Object obj) { 502 super.checkAssignableObject (obj); 503 checkAssignableClass (Attribute.Value.class, obj); 504 } 505 506 507 public void objectInserted (TreeObject obj) { 508 TreeAttribute.this.firePropertyChange (PROP_VALUE, TreeAttribute.this.valueList, obj); } 510 511 512 public void objectRemoved (TreeObject obj) { 513 TreeAttribute.this.firePropertyChange (PROP_VALUE, TreeAttribute.this.valueList, obj); } 515 516 517 public void orderChanged (int[] permutation) { 518 TreeAttribute.this.firePropertyChange (PROP_VALUE, TreeAttribute.this.valueList, permutation); } 520 521 } 523 } 524 | Popular Tags |