1 16 package org.apache.axis2.om.impl.llom; 17 18 import org.apache.axis2.om.*; 19 import org.apache.axis2.om.impl.llom.serialize.StreamWriterToContentHandlerConverter; 20 import org.apache.axis2.om.impl.llom.traverse.OMChildrenIterator; 21 import org.apache.axis2.om.impl.llom.traverse.OMChildrenQNameIterator; 22 import org.apache.axis2.om.impl.llom.util.EmptyIterator; 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 26 import javax.xml.namespace.QName ; 27 import javax.xml.stream.XMLStreamException; 28 import javax.xml.stream.XMLStreamReader; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 32 35 public class OMElementImpl extends OMNodeImpl 36 implements OMElement, OMConstants { 37 38 protected OMNamespace ns; 39 40 43 protected String localName; 44 47 protected OMNode firstChild; 48 49 50 53 protected HashMap namespaces = null; 54 55 58 protected HashMap attributes = null; 59 60 63 private Log log = LogFactory.getLog(getClass()); 64 65 68 protected int noPrefixNamespaceCounter = 0; 69 private OMNode lastChild; 70 71 79 public OMElementImpl(String localName, OMNamespace ns, OMContainer parent, 80 OMXMLParserWrapper builder) { 81 super(parent); 82 this.localName = localName; 83 if (ns != null) { 84 setNamespace(handleNamespace(ns)); 85 } 86 this.builder = builder; 87 firstChild = null; 88 } 89 90 94 protected OMElementImpl(OMContainer parent) { 95 super(parent); 96 this.done = true; 97 } 98 99 105 public OMElementImpl(String localName, OMNamespace ns) { 106 super(null); 107 this.localName = localName; 108 this.done = true; 109 if (ns != null) { 110 setNamespace(handleNamespace(ns)); 111 } 112 } 113 114 public OMElementImpl(String localName, OMNamespace ns, OMContainer parent) { 115 super(parent); 116 this.localName = localName; 117 this.done = true; 118 if (ns != null) { 119 setNamespace(handleNamespace(ns)); 120 } 121 } 122 123 130 public OMElementImpl(QName qname, OMContainer parent) throws OMException { 131 super(parent); 132 this.localName = qname.getLocalPart(); 133 this.done = true; 134 handleNamespace(qname, parent); 135 } 136 137 143 private void handleNamespace(QName qname, OMContainer parent) { 144 OMNamespace ns; 145 146 String namespaceURI = qname.getNamespaceURI(); 148 if (!"".equals(namespaceURI)) { 149 ns = findNamespace(qname.getNamespaceURI(), 150 qname.getPrefix()); 151 159 164 if ((ns == null) && !"".equals(namespaceURI)) { 165 String prefix = qname.getPrefix(); 166 if (!"".equals(prefix)) { 167 ns = declareNamespace(namespaceURI, prefix); 168 } else { 169 ns = declareNamespace(namespaceURI, getNextNamespacePrefix()); 170 } 171 } 172 if (ns != null) { 173 this.setNamespace(ns); 174 176 } 177 } 178 } 179 180 186 private OMNamespace handleNamespace(OMNamespace ns) { 187 OMNamespace namespace = findNamespace(ns.getName(), 188 ns.getPrefix()); 189 if (namespace == null) { 190 namespace = declareNamespace(ns); 191 } 192 return namespace; 193 } 194 195 201 public void addChild(OMNode child) { 202 addChild((OMNodeImpl) child); 203 } 204 205 215 public Iterator getChildrenWithName(QName elementQName) throws OMException { 216 return new OMChildrenQNameIterator((OMNodeImpl) getFirstChild(), 217 elementQName); 218 } 219 220 227 public OMElement getFirstChildWithName(QName elementQName) throws OMException { 228 OMChildrenQNameIterator omChildrenQNameIterator = 229 new OMChildrenQNameIterator((OMNodeImpl) getFirstChild(), 230 elementQName); 231 OMNode omNode = null; 232 if (omChildrenQNameIterator.hasNext()) { 233 omNode = (OMNode) omChildrenQNameIterator.next(); 234 } 235 236 return ((omNode != null) && (OMNode.ELEMENT_NODE == omNode.getType())) ? (OMElement) omNode : null; 237 238 } 239 240 245 private void addChild(OMNodeImpl child) { 246 if (firstChild == null) { 247 firstChild = child; 248 child.setPreviousSibling(null); 249 } else { 250 child.setPreviousSibling(lastChild); 251 lastChild.setNextSibling(child); 252 } 253 child.setNextSibling(null); 254 child.setParent(this); 255 lastChild = child; 256 257 } 258 259 266 public OMNode getNextSibling() throws OMException { 267 while (!done) { 268 builder.next(); 269 } 270 return super.getNextSibling(); 271 } 272 273 279 public Iterator getChildren() { 280 return new OMChildrenIterator(getFirstChild()); 281 } 282 283 290 public OMNamespace declareNamespace(String uri, String prefix) { 291 OMNamespaceImpl ns = new OMNamespaceImpl(uri, prefix); 292 return declareNamespace(ns); 293 } 294 295 296 300 public OMNamespace declareNamespace(OMNamespace namespace) { 301 if (namespaces == null) { 302 this.namespaces = new HashMap (5); 303 } 304 namespaces.put(namespace.getPrefix(), namespace); 305 return namespace; 306 } 307 308 319 public OMNamespace findNamespace(String uri, String prefix) 320 throws OMException { 321 322 OMNamespace namespace = findDeclaredNamespace(uri, prefix); 324 if (namespace != null) { 325 return namespace; 326 } 327 328 if (parent != null) { 330 if (parent instanceof OMElement) { 339 return ((OMElementImpl) parent).findNamespace(uri, prefix); 340 } 341 } 342 return null; 343 } 344 345 354 private OMNamespace findDeclaredNamespace(String uri, String prefix) 355 throws OMException { 356 if (namespaces == null) { 357 return null; 358 } 359 if (prefix == null || "".equals(prefix)) { 360 Iterator namespaceListIterator = namespaces.values().iterator(); 361 while (namespaceListIterator.hasNext()) { 362 OMNamespace omNamespace = 363 (OMNamespace) namespaceListIterator.next(); 364 if (omNamespace.getName().equals(uri)) { 365 return omNamespace; 366 } 367 } 368 return null; 369 } else { 370 return (OMNamespace) namespaces.get(prefix); 371 } 372 } 373 374 379 public Iterator getAllDeclaredNamespaces() { 380 if (namespaces == null) { 381 return null; 382 } 383 return namespaces.values().iterator(); 384 } 385 386 394 public OMAttribute getFirstAttribute(QName qname) throws OMException { 395 if (attributes == null) { 396 return null; 397 } 398 return (OMAttribute) attributes.get(qname); 399 } 400 401 406 public Iterator getAttributes() { 407 if (attributes == null) { 408 return new EmptyIterator(); 409 } 410 return attributes.values().iterator(); 411 } 412 413 public Iterator getAttributes(QName qname) { 414 return null; } 417 418 425 public OMAttribute addAttribute(OMAttribute attr) { 426 if (attributes == null) { 427 this.attributes = new HashMap (5); 428 } 429 attributes.put(attr.getQName(), attr); 430 return attr; 431 } 432 433 438 public void removeAttribute(OMAttribute attr) { 439 if (attributes != null) { 440 attributes.remove(attr.getQName()); 441 } 442 } 443 444 452 public OMAttribute addAttribute(String attributeName, String value, 453 OMNamespace ns) { 454 OMNamespace namespace = null; 455 if (ns != null) { 456 namespace = findNamespace(ns.getName(), ns.getPrefix()); 457 if (namespace == null) { 458 throw new OMException("Given OMNamespace(" + ns.getName() + ns.getPrefix() 459 + ") for " 460 + "this attribute is not declared in the scope of this element. First declare the namespace" 461 + " and then use it with the attribute"); 462 } 463 } 464 return addAttribute(new OMAttributeImpl(attributeName, ns, value)); 465 } 466 467 472 public void setBuilder(OMXMLParserWrapper wrapper) { 473 this.builder = wrapper; 474 } 475 476 481 public OMXMLParserWrapper getBuilder() { 482 return builder; 483 } 484 485 488 public void buildNext() { 489 builder.next(); 490 } 491 492 497 public OMNode getFirstChild() { 498 while ((firstChild == null) && !done) { 499 buildNext(); 500 } 501 return firstChild; 502 } 503 504 509 public void setFirstChild(OMNode firstChild) { 510 this.firstChild = firstChild; 511 } 512 513 519 public OMNode detach() throws OMException { 520 if (!done) { 521 build(); 522 } else { 523 super.detach(); 524 } 525 return this; 526 } 527 528 533 public boolean isComplete() { 534 return done; 535 } 536 537 544 public int getType() throws OMException { 545 return OMNode.ELEMENT_NODE; 546 } 547 548 552 public XMLStreamReader getXMLStreamReader() { 553 return getXMLStreamReader(true); 554 } 555 556 560 public XMLStreamReader getXMLStreamReaderWithoutCaching() { 561 return getXMLStreamReader(false); 562 } 563 564 568 private XMLStreamReader getXMLStreamReader(boolean cache) { 569 if ((builder == null) && !cache) { 570 throw new UnsupportedOperationException ("This element was not created in a manner to be switched"); 571 } 572 return new OMStAXWrapper(builder, this, cache); 573 } 574 575 582 public void setText(String text) { 583 584 OMNode child = this.getFirstChild(); 585 while (child != null) { 586 if (child.getType() == OMNode.TEXT_NODE) { 587 child.detach(); 588 } 589 child = child.getNextSibling(); 590 } 591 592 this.addChild(OMAbstractFactory.getOMFactory().createText(this, text)); 593 } 594 595 600 public String getText() { 601 String childText = ""; 602 OMNode child = this.getFirstChild(); 603 OMText textNode = null; 604 605 while (child != null) { 606 if (child.getType() == OMNode.TEXT_NODE) { 607 textNode = (OMText) child; 608 if (textNode.getText() != null && !"".equals(textNode.getText().trim())) { 609 childText += textNode.getText().trim(); 610 } 611 } 612 child = child.getNextSibling(); 613 } 614 615 return childText; 616 } 617 618 624 public void serializeWithCache(OMOutput omOutput) throws XMLStreamException { 625 serialize(omOutput, true); 626 } 627 628 631 protected void serialize(OMOutput omOutput, boolean cache) throws XMLStreamException { 632 633 short builderType = PULL_TYPE_BUILDER; if (builder != null) { 636 builderType = this.builder.getBuilderType(); 637 } 638 if ((builderType == PUSH_TYPE_BUILDER) 639 && (builder.getRegisteredContentHandler() == null)) { 640 builder.registerExternalContentHandler(new StreamWriterToContentHandlerConverter(omOutput)); 641 } 642 643 644 if (!cache) { 645 if (this.firstChild != null) { 647 OMSerializerUtil.serializeStartpart(this, omOutput); 648 firstChild.serialize(omOutput); 649 OMSerializerUtil.serializeEndpart(omOutput); 650 } else if (!this.done) { 651 if (builderType == PULL_TYPE_BUILDER) { 652 OMSerializerUtil.serializeByPullStream(this, omOutput); 653 } else { 654 OMSerializerUtil.serializeStartpart(this, omOutput); 655 builder.setCache(cache); 656 builder.next(); 657 OMSerializerUtil.serializeEndpart(omOutput); 658 } 659 } else { 660 OMSerializerUtil.serializeNormal(this, omOutput, cache); 661 } 662 663 if (this.nextSibling != null) { 665 nextSibling.serialize(omOutput); 666 } else if (this.parent != null) { 667 if (!this.parent.isComplete()) { 668 builder.setCache(cache); 669 builder.next(); 670 } 671 } 672 } else { 673 OMSerializerUtil.serializeNormal(this, omOutput, cache); 675 OMNode nextSibling = this.getNextSibling(); 677 if (nextSibling != null) { 678 nextSibling.serializeWithCache(omOutput); 679 } 680 } 681 } 682 683 686 695 public void serialize(OMOutput omOutput) throws XMLStreamException { 696 this.serialize(omOutput, false); 697 } 698 699 700 705 private String getNextNamespacePrefix() { 706 return "ns" + ++noPrefixNamespaceCounter; 707 } 708 709 public OMElement getFirstElement() { 710 OMNode node = getFirstChild(); 711 while (node != null) { 712 if (node.getType() == OMNode.ELEMENT_NODE) { 713 return (OMElement) node; 714 } else { 715 node = node.getNextSibling(); 716 } 717 } 718 return null; 719 } 720 721 736 737 742 public String getLocalName() { 743 return localName; 744 } 745 746 751 public void setLocalName(String localName) { 752 this.localName = localName; 753 } 754 755 761 public OMNamespace getNamespace() throws OMException { 762 return ns; 769 } 770 771 772 775 public void setNamespace(OMNamespace namespace) { 776 if (ns != null) { 777 OMNamespace ns = this.findNamespace(namespace.getName(), namespace.getPrefix()); 778 if (ns == null) { 779 ns = this.declareNamespace(namespace); 780 } 781 } 782 this.ns = namespace; 783 } 784 785 790 public QName getQName() { 791 QName qName = null; 792 793 if (ns != null) { 794 if (ns.getPrefix() != null) { 795 qName = new QName (ns.getName(), localName, ns.getPrefix()); 796 } else { 797 qName = new QName (ns.getName(), localName); 798 } 799 } else { 800 qName = new QName (localName); 801 } 802 return qName; 803 } 804 805 810 public void discard() throws OMException { 811 if (done) { 812 this.detach(); 813 } else { 814 builder.discard(this); 815 } 816 } 817 } 818 | Popular Tags |