1 19 package org.netbeans.tax; 20 21 import java.util.Collection ; 22 23 import org.netbeans.tax.spec.DTD; 24 import org.netbeans.tax.spec.ParameterEntityReference; 25 import org.netbeans.tax.spec.DocumentType; 26 import org.netbeans.tax.spec.ConditionalSection; 27 28 import org.netbeans.tax.decl.EMPTYType; 29 import org.netbeans.tax.decl.parser.ParserReader; 30 import org.netbeans.tax.decl.parser.ContentSpecParser; 31 32 37 public class TreeElementDecl extends TreeNodeDecl implements DTD.Child, ParameterEntityReference.Child, DocumentType.Child, ConditionalSection.Child { 38 39 public static final String PROP_NAME = "name"; 41 public static final String PROP_CONTENT_TYPE = "contentType"; 43 44 45 private String name; 46 47 48 private ContentType contentType; 49 50 53 54 58 61 public TreeElementDecl (String name, ContentType contentType) throws InvalidArgumentException { 62 super (); 63 64 checkName (name); 65 checkContentType (contentType); 66 67 this.name = name; 68 this.contentType = contentType; 69 this.contentType.setNodeDecl (this); 70 } 71 72 73 80 81 protected TreeElementDecl (TreeElementDecl elementDecl) { 82 super (elementDecl); 83 84 this.name = elementDecl.name; 85 this.contentType = (ContentType)elementDecl.contentType.clone (); 86 } 87 88 89 93 95 public Object clone () { 96 return new TreeElementDecl (this); 97 } 98 99 101 public boolean equals (Object object, boolean deep) { 102 if (!!! super.equals (object, deep)) 103 return false; 104 105 TreeElementDecl peer = (TreeElementDecl) object; 106 if (!!! Util.equals (this.getName (), peer.getName ())) 107 return false; 108 if (!!! Util.equals (this.contentType, peer.contentType)) 109 return false; 110 111 return true; 112 } 113 114 117 public void merge (TreeObject treeObject) throws CannotMergeException { 118 super.merge (treeObject); 119 120 TreeElementDecl peer = (TreeElementDecl) treeObject; 121 122 setNameImpl (peer.getName ()); 123 setContentTypeImpl (peer.getContentType ()); 124 } 126 127 128 132 134 public final String getName () { 135 return name; 136 } 137 138 140 private final void setNameImpl (String newName) { 141 String oldName = this.name; 142 143 this.name = newName; 144 145 firePropertyChange (PROP_NAME, oldName, newName); 146 } 147 148 152 public final void setName (String newName) throws ReadOnlyException, InvalidArgumentException { 153 if ( Util.equals (this.name, newName) ) 157 return; 158 checkReadOnly (); 159 checkName (newName); 160 161 setNameImpl (newName); 165 } 166 167 169 protected final void checkName (String name) throws InvalidArgumentException { 170 TreeUtilities.checkElementDeclName (name); 171 } 172 173 175 public final ContentType getContentType () { 176 return contentType; 177 } 178 179 181 private final void setContentTypeImpl (ContentType newContentType) { 182 ContentType oldContentType = this.contentType; 183 184 this.contentType = newContentType; 185 186 firePropertyChange (PROP_CONTENT_TYPE, oldContentType, newContentType); 187 } 188 189 193 public final void setContentType (ContentType newContentType) throws ReadOnlyException, InvalidArgumentException { 194 if ( Util.equals (this.contentType, newContentType) ) 198 return; 199 checkReadOnly (); 200 checkContentType (newContentType); 201 202 setContentTypeImpl (newContentType); 206 } 207 208 212 public final void setContentType (String newContentType) throws ReadOnlyException, InvalidArgumentException { 213 setContentType (new ContentSpecParser ().parseModel (new ParserReader (newContentType))); 214 } 215 216 218 protected final void checkContentType (ContentType contentType) throws InvalidArgumentException { 219 TreeUtilities.checkElementDeclContentType (contentType); 220 } 221 222 223 public boolean isMixed () { 224 return getContentType ().isMixed (); 225 } 226 227 228 public boolean isEmpty () { 229 return ! (allowText () || allowElements ()); 230 } 231 232 233 public boolean allowText () { 234 return getContentType ().allowText (); 235 } 236 237 238 public boolean allowElements () { 239 return getContentType ().allowElements (); 240 } 241 242 243 244 public Collection getAttributeDefs () { 245 if ( getOwnerDTD () == null ) { 246 return null; 247 } 248 return getOwnerDTD ().getAttributeDeclarations (getName ()); 249 } 250 251 252 256 259 public abstract static class ContentType extends Content implements Comparable { 260 265 268 273 274 private String multiplicity; 275 276 private int index; 278 private static int counter = 0; 280 281 285 286 protected ContentType (TreeElementDecl elementDecl) { 287 super (elementDecl); 288 289 this.multiplicity = new String (); 290 291 this.index = counter++; 292 } 293 294 295 296 protected ContentType () { 297 this ((TreeElementDecl) null); 298 } 299 300 301 protected ContentType (ContentType contentType) { 302 super (contentType); 303 304 this.multiplicity = contentType.multiplicity; 305 306 this.index = counter++; 307 } 308 309 313 315 public boolean equals (Object object, boolean deep) { 316 if (!!! super.equals (object, deep)) 317 return false; 318 319 ContentType peer = (ContentType) object; 320 if (this.index != peer.index) 321 return false; 322 if (!!! Util.equals (this.getMultiplicity (), peer.getMultiplicity ())) 323 return false; 324 325 return true; 326 } 327 328 333 public void merge (TreeObject treeObject) throws CannotMergeException { 334 super.merge (treeObject); 335 336 ContentType peer = (ContentType) treeObject; 337 338 index = peer.index; 339 setMultiplicity (peer.getMultiplicity ()); 340 } 341 342 343 347 349 public final void removeFromContext () throws ReadOnlyException { 350 if ( isInContext () ) { 351 getOwnerElementDecl ().setContentTypeImpl (new EMPTYType ()); 352 } 353 } 354 355 356 360 362 public final TreeElementDecl getOwnerElementDecl () { 363 return (TreeElementDecl)getNodeDecl (); 364 } 365 366 367 369 public void setMultiplicity (char s) { 370 multiplicity = new String (new char[] {s}); 371 } 372 373 375 public void setMultiplicity (String s) { 376 multiplicity = s; 377 } 378 379 392 public void addMultiplicity (String s) { 393 if (multiplicity.equals (s)) 394 return; 395 396 if ("".equals (multiplicity)) { multiplicity = s; 398 } else if ("".equals (s)) { } else { 401 multiplicity = "*"; } 403 } 404 405 406 407 public String getMultiplicity () { 408 return multiplicity; 409 } 410 411 412 public boolean isMixed () { 413 return allowText () && allowElements (); 414 } 415 416 417 public abstract boolean allowElements (); 418 419 420 public abstract boolean allowText (); 421 422 423 public abstract String toString (); 424 425 426 public int compareTo (final Object obj) { 427 if (this.equals (obj)) 428 return 0; 429 430 ContentType type = (ContentType) obj; 431 432 return index - type.index; 433 } 434 435 } 437 } 438 | Popular Tags |