1 16 package org.apache.commons.betwixt; 17 18 import java.util.ArrayList ; 19 import java.util.List ; 20 21 import org.apache.commons.betwixt.digester.XMLIntrospectorHelper; 22 import org.apache.commons.betwixt.expression.Expression; 23 24 33 public class ElementDescriptor extends NodeDescriptor { 34 35 41 private AttributeDescriptor[] attributeDescriptors; 42 48 private ElementDescriptor[] elementDescriptors; 49 50 56 private Descriptor[] contentDescriptors; 57 58 62 private List attributeList; 63 64 68 private List elementList; 69 70 74 private List contentList; 75 76 78 private Expression contextExpression; 79 80 81 private boolean primitiveType; 82 88 private boolean isHollow = false; 89 90 94 private boolean wrapCollectionsInElement = true; 95 96 99 private Class implementationClass = null; 100 101 104 public ElementDescriptor() { 105 } 106 107 112 public ElementDescriptor(boolean primitiveType) { 113 this.primitiveType = primitiveType; 114 } 115 116 122 public ElementDescriptor(String localName) { 123 super( localName ); 124 } 125 126 127 128 134 public ElementDescriptor(String localName, String qualifiedName, String uri) { 135 super(localName, qualifiedName, uri); 136 } 137 138 143 public boolean hasChildren() { 144 return getElementDescriptors().length > 0; 145 } 146 147 152 public boolean hasAttributes() { 153 return getAttributeDescriptors().length > 0; 154 } 155 156 162 public boolean hasContent() { 163 return getContentDescriptors().length > 0; 164 } 165 166 174 public boolean isSimple() { 175 return !(hasAttributes()) && !(hasChildren()); 176 } 177 178 179 190 public void setWrapCollectionsInElement(boolean wrapCollectionsInElement) { 191 this.wrapCollectionsInElement = wrapCollectionsInElement; 192 } 193 194 205 public boolean isWrapCollectionsInElement() { 206 return this.wrapCollectionsInElement; 207 } 208 209 214 public void addAttributeDescriptor(AttributeDescriptor descriptor) { 215 if ( attributeList == null ) { 216 attributeList = new ArrayList (); 217 } 218 getAttributeList().add( descriptor ); 219 attributeDescriptors = null; 220 } 221 222 223 229 public AttributeDescriptor[] getAttributeDescriptors() { 230 if ( attributeDescriptors == null ) { 231 if ( attributeList == null ) { 232 attributeDescriptors = new AttributeDescriptor[0]; 233 } else { 234 attributeDescriptors = new AttributeDescriptor[ attributeList.size() ]; 235 attributeList.toArray( attributeDescriptors ); 236 237 attributeList = null; 239 } 240 } 241 return attributeDescriptors; 242 } 243 244 252 public void setAttributeDescriptors(AttributeDescriptor[] attributeDescriptors) { 253 this.attributeDescriptors = attributeDescriptors; 254 this.attributeList = null; 255 } 256 257 262 public void addElementDescriptor(ElementDescriptor descriptor) { 263 if ( elementList == null ) { 264 elementList = new ArrayList (); 265 } 266 getElementList().add( descriptor ); 267 elementDescriptors = null; 268 addContentDescriptor( descriptor ); 269 } 270 271 276 public ElementDescriptor[] getElementDescriptors() { 277 if ( elementDescriptors == null ) { 278 if ( elementList == null ) { 279 elementDescriptors = new ElementDescriptor[0]; 280 } else { 281 elementDescriptors = new ElementDescriptor[ elementList.size() ]; 282 elementList.toArray( elementDescriptors ); 283 284 elementList = null; 286 } 287 } 288 return elementDescriptors; 289 } 290 291 303 public ElementDescriptor getElementDescriptor(String name) { 304 305 ElementDescriptor elementDescriptor = null; 306 ElementDescriptor descriptorWithNullName = null; 307 ElementDescriptor[] elementDescriptors = getElementDescriptors(); 308 for (int i=0, size=elementDescriptors.length; i<size; i++) { 309 String elementName = elementDescriptors[i].getQualifiedName(); 310 if (name.equals(elementName)) { 311 elementDescriptor = elementDescriptors[i]; 312 break; 313 } 314 if (descriptorWithNullName == null && elementName == null) { 315 descriptorWithNullName = elementDescriptors[i]; 316 } 317 } 318 if (elementDescriptor == null) { 319 elementDescriptor = descriptorWithNullName; 320 } 321 return elementDescriptor; 322 } 323 324 325 332 public void setElementDescriptors(ElementDescriptor[] elementDescriptors) { 333 this.elementDescriptors = elementDescriptors; 334 this.elementList = null; 335 setContentDescriptors( elementDescriptors ); 336 } 337 338 344 public void addContentDescriptor(Descriptor descriptor) { 345 if ( contentList == null ) { 346 contentList = new ArrayList (); 347 } 348 getContentList().add( descriptor ); 349 contentDescriptors = null; 350 } 351 352 358 public Descriptor[] getContentDescriptors() { 359 if ( contentDescriptors == null ) { 360 if ( contentList == null ) { 361 contentDescriptors = new Descriptor[0]; 362 } else { 363 contentDescriptors = new Descriptor[ contentList.size() ]; 364 contentList.toArray( contentDescriptors ); 365 366 contentList = null; 368 } 369 } 370 return contentDescriptors; 371 } 372 373 385 public TextDescriptor getPrimaryBodyTextDescriptor() { 386 Descriptor[] descriptors = getContentDescriptors(); 389 for (int i=0, size=descriptors.length; i<size; i++) { 390 if (descriptors[i] instanceof TextDescriptor) { 391 return (TextDescriptor) descriptors[i]; 392 } 393 } 394 return null; 396 } 397 398 404 public void setContentDescriptors(Descriptor[] contentDescriptors) { 405 this.contentDescriptors = contentDescriptors; 406 this.contentList = null; 407 } 408 409 413 public Expression getContextExpression() { 414 return contextExpression; 415 } 416 417 421 public void setContextExpression(Expression contextExpression) { 422 this.contextExpression = contextExpression; 423 } 424 425 431 public boolean isPrimitiveType() { 432 return primitiveType; 433 } 434 435 441 public void setPrimitiveType(boolean primitiveType) { 442 this.primitiveType = primitiveType; 443 } 444 445 448 457 protected List getAttributeList() { 458 if ( attributeList == null ) { 459 if ( attributeDescriptors != null ) { 460 int size = attributeDescriptors.length; 461 attributeList = new ArrayList ( size ); 462 for ( int i = 0; i < size; i++ ) { 463 attributeList.add( attributeDescriptors[i] ); 464 } 465 attributeDescriptors = null; 467 } else { 468 attributeList = new ArrayList (); 469 } 470 } 471 return attributeList; 472 } 473 474 483 protected List getElementList() { 484 if ( elementList == null ) { 485 if ( elementDescriptors != null ) { 486 int size = elementDescriptors.length; 487 elementList = new ArrayList ( size ); 488 for ( int i = 0; i < size; i++ ) { 489 elementList.add( elementDescriptors[i] ); 490 } 491 elementDescriptors = null; 493 } else { 494 elementList = new ArrayList (); 495 } 496 } 497 return elementList; 498 } 499 500 510 protected List getContentList() { 511 if ( contentList == null ) { 512 if ( contentDescriptors != null ) { 513 int size = contentDescriptors.length; 514 contentList = new ArrayList ( size ); 515 for ( int i = 0; i < size; i++ ) { 516 contentList.add( contentDescriptors[i] ); 517 } 518 contentDescriptors = null; 520 } else { 521 contentList = new ArrayList (); 522 } 523 } 524 return contentList; 525 } 526 527 532 public Class getImplementationClass() { 533 return implementationClass; 534 } 535 536 542 public void setImplementationClass(Class implementationClass) { 543 this.implementationClass = implementationClass; 544 } 545 546 550 public boolean isCollective() { 551 boolean result = false; 552 Class type = getPropertyType(); 553 if (type != null) { 554 result = XMLIntrospectorHelper.isLoopType(type); 555 } 556 return result; 557 } 558 559 562 public ElementDescriptor findParent(ElementDescriptor elementDescriptor) { 563 ElementDescriptor result = null; 564 ElementDescriptor[] elementDescriptors = getElementDescriptors(); 565 for (int i=0, size=elementDescriptors.length; i<size; i++) { 566 if (elementDescriptors[i].equals(elementDescriptor)) { 567 result = this; 568 break; 569 } 570 else 571 { 572 result = elementDescriptors[i].findParent(elementDescriptor); 573 if (result != null) { 574 break; 575 } 576 } 577 } 578 return result; 579 } 580 581 586 public String toString() { 587 return 588 "ElementDescriptor[qname=" + getQualifiedName() + ",pname=" + getPropertyName() 589 + ",class=" + getPropertyType() + ",singular=" + getSingularPropertyType() 590 + ",updater=" + getUpdater() + ",wrap=" + isWrapCollectionsInElement() + "]"; 591 } 592 593 603 public boolean isHollow() { 604 return isHollow; 605 } 606 607 617 public void setHollow(boolean isHollow) { 618 this.isHollow = isHollow; 619 } 620 } 621 | Popular Tags |