1 19 package org.netbeans.tax; 20 21 import java.util.Arrays ; 22 import java.util.List ; 23 import java.util.LinkedList ; 24 import java.util.StringTokenizer ; 25 26 30 public class TreeAttlistDeclAttributeDef extends TreeNodeDecl.Content implements TreeNamedObjectMap.NamedObject { 31 32 33 public static final String PROP_NAME = "name"; 35 public static final String PROP_TYPE = "type"; 37 public static final String PROP_ENUMERATED_TYPE = "enumeratedType"; 39 public static final String PROP_DEFAULT_TYPE = "defaultType"; 41 public static final String PROP_DEFAULT_VALUE = "defaultValue"; 43 44 public static final short TYPE_CDATA = 0; 45 46 public static final short TYPE_ID = 1; 47 48 public static final short TYPE_IDREF = 2; 49 50 public static final short TYPE_IDREFS = 3; 51 52 public static final short TYPE_ENTITY = 4; 53 54 public static final short TYPE_ENTITIES = 5; 55 56 public static final short TYPE_NMTOKEN = 6; 57 58 public static final short TYPE_NMTOKENS = 7; 59 60 public static final short TYPE_ENUMERATED = 8; 61 62 public static final short TYPE_NOTATION = 9; 63 64 65 public static final short DEFAULT_TYPE_NULL = 0; 66 67 public static final short DEFAULT_TYPE_REQUIRED = 1; 68 69 public static final short DEFAULT_TYPE_IMPLIED = 2; 70 71 public static final short DEFAULT_TYPE_FIXED = 3; 72 73 74 public static final String [] NAMED_TYPE_LIST = new String [] { 75 "CDATA", "ID", "IDREF", "IDREFS", "ENTITY", "ENTITIES", "NMTOKEN", "NMTOKENS", null, "NOTATION" }; 86 87 88 public static final String [] NAMED_DEFAULT_TYPE_LIST = new String [] { 89 null, "#REQUIRED", "#IMPLIED", "#FIXED" }; 94 95 96 97 private String name; 98 99 100 private short type; 101 102 103 private String [] enumeratedType; 104 105 106 private short defaultType; 107 108 109 private String defaultValue; 110 111 112 private TreeNamedObjectMap.KeyListener mapKeyListener; 113 114 115 119 120 public TreeAttlistDeclAttributeDef (String name, short type, String [] enumeratedType, short defaultType, String defaultValue) throws InvalidArgumentException { 121 super (); 122 123 checkName (name); 124 checkType (type, enumeratedType); 125 checkDefaultType (defaultType, defaultValue); 126 this.name = name; 127 this.type = type; 128 this.enumeratedType = enumeratedType; 129 this.defaultType = defaultType; 130 this.defaultValue = defaultValue; 131 } 132 133 134 protected TreeAttlistDeclAttributeDef (TreeAttlistDeclAttributeDef attributeDef) { 135 super (attributeDef); 136 137 this.name = attributeDef.name; 138 this.type = attributeDef.type; 139 this.enumeratedType = arraycopy (attributeDef.enumeratedType); 140 this.defaultType = attributeDef.defaultType; 141 this.defaultValue = attributeDef.defaultValue; 142 } 143 144 145 149 151 private String [] arraycopy (String [] array) { 152 if ( array == null ) 153 return null; 154 155 int length = array.length; 156 String [] arrayCopy = new String [length]; 157 System.arraycopy (array, 0, arrayCopy, 0, length); 158 159 return arrayCopy; 160 } 161 162 163 167 169 public Object clone () { 170 return new TreeAttlistDeclAttributeDef (this); 171 } 172 173 175 public boolean equals (Object object, boolean deep) { 176 if (!!! super.equals (object, deep)) 177 return false; 178 179 TreeAttlistDeclAttributeDef peer = (TreeAttlistDeclAttributeDef) object; 180 if (!!! Util.equals (this.name, peer.name)) 181 return false; 182 if (this.type != peer.type) 183 return false; 184 if (!!! Util.equals (this.enumeratedType, peer.enumeratedType)) 185 return false; 186 if (this.defaultType != peer.defaultType) 187 return false; 188 if (!!! Util.equals (this.defaultValue, peer.defaultValue)) 189 return false; 190 191 return true; 192 } 193 194 197 public void merge (TreeObject treeObject) throws CannotMergeException { 198 super.merge (treeObject); 199 200 TreeAttlistDeclAttributeDef peer = (TreeAttlistDeclAttributeDef) treeObject; 201 setNameImpl (peer.getName ()); 202 setDefaultTypeImpl (peer.getDefaultType (), peer.getDefaultValue ()); 203 setTypeImpl (peer.getType (), peer.getEnumeratedType ()); 204 } 205 206 207 211 213 public final void removeFromContext () throws ReadOnlyException { 214 if ( isInContext () ) { 215 getOwnerAttlistDecl ().removeAttributeDef (this.getName ()); 216 } 217 } 218 219 220 224 226 public final TreeAttlistDecl getOwnerAttlistDecl () { 227 return (TreeAttlistDecl)getNodeDecl (); 228 } 229 230 231 235 237 public final String getElementName () { 238 if ( getNodeDecl () == null ) 239 return null; 240 return getOwnerAttlistDecl ().getElementName (); 241 } 242 243 245 public final String getName () { 246 return name; 247 } 248 249 251 private final void setNameImpl (String newName) { 252 String oldName = this.name; 253 254 this.name = newName; 255 256 fireMapKeyChanged (oldName); 257 } 260 261 265 public final void setName (String newName) throws ReadOnlyException, InvalidArgumentException { 266 if ( Util.equals (this.name, newName) ) 270 return; 271 checkReadOnly (); 272 checkName (newName); 273 274 setNameImpl (newName); 278 } 279 280 282 protected final void checkName (String name) throws InvalidArgumentException { 283 TreeUtilities.checkAttlistDeclAttributeName (name); 284 } 285 286 287 291 293 public final short getType () { 294 return type; 295 } 296 297 299 public final String getTypeName () { 300 try { 301 return NAMED_TYPE_LIST [type]; 302 } catch (ArrayIndexOutOfBoundsException exc) { 303 return null; 304 } 305 } 306 307 309 public final String [] getEnumeratedType () { 310 return enumeratedType; 311 } 312 313 315 public final String getEnumeratedTypeString () { 316 if ( enumeratedType == null ) { 317 return null; 318 } 319 StringBuffer sb = new StringBuffer (); 320 sb.append ("( ").append (enumeratedType[0]); for ( int i = 1; i < enumeratedType.length; i++ ) { 322 sb.append (" | ").append (enumeratedType [i]); } 324 sb.append (" )"); return sb.toString (); 326 } 327 328 public static final String [] createEnumeratedType (String enumeratedType) { 329 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeAttlistDeclAttributeDef.createEnumeratedType: enumeratedType = " + enumeratedType); 330 331 if ( enumeratedType == null ) { 332 return null; 333 } 334 int begin = enumeratedType.indexOf ("("); int end = enumeratedType.indexOf (")"); 337 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" begin = " + begin); 338 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" end = " + end); 339 340 if ( ( begin == -1 ) || 341 ( end == -1 ) ) { 342 return null; 343 } 344 String noParenthesis = enumeratedType.substring (begin + 1, end); 345 StringTokenizer st = new StringTokenizer (noParenthesis, "|"); List tokens = new LinkedList (); 347 while (st.hasMoreTokens ()) { 348 tokens.add (st.nextToken ().trim ()); 349 } 350 351 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" tokens = " + tokens); 352 353 if ( tokens.isEmpty () ) 354 return null; 355 356 String [] arrayType = (String [])tokens.toArray (new String [0]); 357 358 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" RETURN arrayType = " + arrayType); 359 360 return arrayType; 361 } 362 363 365 private final void setTypeImpl (short newType, String [] newEnumeratedType) { 366 369 this.type = newType; 370 this.enumeratedType = newEnumeratedType; 371 372 } 375 376 380 public final void setType (short newType, String [] newEnumeratedType) throws ReadOnlyException, InvalidArgumentException { 381 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeAttlistDeclAttributeDef.setType"); 382 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" newType = " + newType); 383 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" newEnumeratedType = " + ( newEnumeratedType == null ? null : Arrays.asList (newEnumeratedType))); 384 385 boolean setType = this.type != newType; 389 boolean setEnumeratedType = !!! Arrays.equals (this.enumeratedType, newEnumeratedType); 390 391 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" setEnumeratedType = " + setEnumeratedType); 392 393 if ( !!! setType && 394 !!! setEnumeratedType ) { 395 return; 396 } 397 checkReadOnly (); 398 checkType (newType, newEnumeratedType); 399 400 setTypeImpl (newType, newEnumeratedType); 404 } 405 406 407 409 protected final void checkType (short type, String [] enumeratedType) throws InvalidArgumentException { 410 TreeUtilities.checkAttlistDeclAttributeType (type); 411 TreeUtilities.checkAttlistDeclAttributeEnumeratedType (enumeratedType); 412 } 413 414 416 public static final short findType (String type) { 417 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeAttlistDeclAttributeDef::findType: type = " + type); 419 for ( short i = 0; i < NAMED_TYPE_LIST.length; i++ ) { 420 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" test_type = " + NAMED_TYPE_LIST[i]); 422 if ( Util.equals (NAMED_TYPE_LIST[i], type) ) { 423 return i; 424 } 425 } 426 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" type[" + type + "] not found"); 428 return -1; 429 } 430 431 435 437 public final short getDefaultType () { 438 return defaultType; 439 } 440 441 443 public final String getDefaultTypeName () { 444 try { 445 return NAMED_DEFAULT_TYPE_LIST [defaultType]; 446 } catch (ArrayIndexOutOfBoundsException exc) { 447 return null; 448 } 449 } 450 451 453 public final String getDefaultValue () { 454 return defaultValue; 455 } 456 457 459 private final void setDefaultTypeImpl (short newDefaultType, String newDefaultValue) { 460 463 this.defaultType = newDefaultType; 464 this.defaultValue = newDefaultValue; 465 466 } 469 470 474 public final void setDefaultType (short newDefaultType, String newDefaultValue) throws ReadOnlyException, InvalidArgumentException { 475 if ( Util.THIS.isLoggable() ) Util.THIS.debug ("TreeAttlistDeclAttributeDef.setDefaultType"); 476 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" newDefaultType = " + NAMED_DEFAULT_TYPE_LIST [newDefaultType]); 477 if ( Util.THIS.isLoggable() ) Util.THIS.debug (" newDefaultValue = " + newDefaultValue); 478 479 boolean setDefaultType = this.defaultType != newDefaultType; 483 boolean setDefaultValue = !!! Util.equals (this.defaultValue, newDefaultValue); 484 if ( !!! setDefaultType && 485 !!! setDefaultValue ) { 486 return; 487 } 488 checkReadOnly (); 489 checkDefaultType (newDefaultType, newDefaultValue); 490 491 setDefaultTypeImpl (newDefaultType, newDefaultValue); 495 } 496 497 499 protected final void checkDefaultType (short defaultType, String defaultValue) throws InvalidArgumentException { 500 TreeUtilities.checkAttlistDeclAttributeDefaultType (defaultType); 501 TreeUtilities.checkAttlistDeclAttributeDefaultValue (defaultValue); 502 } 503 504 506 public static final short findDefaultType (String defaultType) { 507 for ( short i = 0; i < NAMED_DEFAULT_TYPE_LIST.length; i++ ) { 508 if ( Util.equals (NAMED_DEFAULT_TYPE_LIST[i], defaultType) ) 509 return i; 510 } 511 return -1; 512 } 513 514 515 519 521 public Object mapKey () { 522 return getName (); 523 } 524 525 527 531 532 public void setKeyListener (TreeNamedObjectMap.KeyListener keyListener) { 533 mapKeyListener = keyListener; 534 } 535 536 private void fireMapKeyChanged (Object oldKey) { 537 if ( mapKeyListener == null ) { 538 return; 539 } 540 mapKeyListener.mapKeyChanged (oldKey); 541 } 542 543 } 544 | Popular Tags |