1 16 17 package org.apache.axis.message; 18 19 import org.apache.axis.components.logger.LogFactory; 20 import org.apache.axis.encoding.SerializationContext; 21 import org.apache.axis.i18n.Messages; 22 import org.apache.commons.logging.Log; 23 import org.w3c.dom.Attr ; 24 import org.w3c.dom.CDATASection ; 25 import org.w3c.dom.CharacterData ; 26 import org.w3c.dom.Comment ; 27 import org.w3c.dom.DOMException ; 28 import org.w3c.dom.Document ; 29 import org.w3c.dom.NamedNodeMap ; 30 import org.w3c.dom.Node ; 31 import org.w3c.dom.NodeList ; 32 import org.w3c.dom.Text ; 33 import org.xml.sax.Attributes ; 34 import org.xml.sax.helpers.AttributesImpl ; 35 36 import javax.xml.soap.SOAPElement ; 37 import javax.xml.soap.SOAPException ; 38 import java.io.Serializable ; 39 import java.util.ArrayList ; 40 import java.util.Iterator ; 41 42 45 public class NodeImpl implements org.w3c.dom.Node , javax.xml.soap.Node , 46 Serializable , Cloneable { 47 48 protected static Log log = 49 LogFactory.getLog(NodeImpl.class.getName()); 50 51 protected String name; 52 protected String prefix; 53 protected String namespaceURI; 54 protected transient Attributes attributes = NullAttributes.singleton; 55 56 protected Document document = null; 57 protected NodeImpl parent = null; 58 protected ArrayList children = null; 59 60 protected CharacterData textRep = null; 62 63 protected boolean _isDirty = false; 64 private static final String NULL_URI_NAME = "intentionalNullURI"; 65 66 69 public NodeImpl() { 70 } 71 72 76 public NodeImpl(CharacterData text) { 77 textRep = text; 78 namespaceURI = text.getNamespaceURI(); 79 name = text.getLocalName(); 80 } 81 82 85 public short getNodeType() { 86 if (this.textRep != null) { 87 if (textRep instanceof Comment ) { 88 return COMMENT_NODE; 89 } else if (textRep instanceof CDATASection ) { 90 return CDATA_SECTION_NODE; 91 } else { 92 return TEXT_NODE; 93 } 94 } else if (false) { 95 return DOCUMENT_FRAGMENT_NODE; 96 } else if (false) { 97 return Node.ELEMENT_NODE; 98 } else { return Node.ELEMENT_NODE; 100 } 101 } 102 103 118 public void normalize() { 119 } 121 122 129 public boolean hasAttributes() { 130 return attributes.getLength() > 0; 131 } 132 133 139 public boolean hasChildNodes() { 140 return (children != null && !children.isEmpty()); 141 } 142 143 152 public String getLocalName() { 153 return name; 154 } 155 156 172 public String getNamespaceURI() { 173 return (namespaceURI); 174 } 175 176 179 public String getNodeName() { 180 return (prefix != null && prefix.length() > 0) ? 181 prefix + ":" + name : name; 182 } 183 184 193 public String getNodeValue() throws DOMException { 194 if (textRep == null) { 195 return null; 196 } else { 197 return textRep.getData(); 198 } 199 } 200 201 232 public String getPrefix() { 233 return (prefix); 234 } 235 236 245 public void setNodeValue(String nodeValue) throws DOMException { 246 throw new DOMException (DOMException.NO_DATA_ALLOWED_ERR, 247 "Cannot use TextNode.set in " + this); 248 } 249 250 281 public void setPrefix(String prefix) { 282 this.prefix = prefix; 283 } 284 285 290 public void setOwnerDocument(Document doc) { 291 document = doc; 292 } 293 294 301 public Document getOwnerDocument() { 302 if(document == null) { 303 NodeImpl node = getParent(); 304 if (node != null) { 305 return node.getOwnerDocument(); 306 } 307 } 308 return document; 309 } 310 311 315 public NamedNodeMap getAttributes() { 316 makeAttributesEditable(); 318 return convertAttrSAXtoDOM(attributes); 319 } 320 321 325 public Node getFirstChild() { 326 if (children != null && !children.isEmpty()) { 327 return (Node ) children.get(0); 328 } else { 329 return null; 330 } 331 } 332 333 337 public Node getLastChild() { 338 if (children != null && !children.isEmpty()) { 339 return (Node ) children.get(children.size() - 1); 340 } else { 341 return null; 342 } 343 } 344 345 349 public Node getNextSibling() { 350 SOAPElement parent = getParentElement(); 351 if (parent == null) { 352 return null; 353 } 354 Iterator iter = parent.getChildElements(); 355 Node nextSibling = null; 356 while (iter.hasNext()) { 357 if (iter.next() == this) { 358 if (iter.hasNext()) { 359 return (Node ) iter.next(); 360 } else { 361 return null; 362 } 363 } 364 } 365 return nextSibling; } 367 368 376 public Node getParentNode() { 377 return (Node ) getParent(); 378 } 379 380 384 public Node getPreviousSibling() { 385 SOAPElement parent = getParentElement(); 386 if (parent == null) { 387 return null; 388 } 389 NodeList nl = parent.getChildNodes(); 390 int len = nl.getLength(); 391 int i = 0; 392 Node previousSibling = null; 393 while (i < len) { 394 if (nl.item(i) == this) { 395 return previousSibling; 396 } 397 previousSibling = nl.item(i); 398 i++; 399 } 400 return previousSibling; } 402 403 428 public Node cloneNode(boolean deep) { 429 return new NodeImpl(textRep); 430 } 431 432 437 public NodeList getChildNodes() { 438 if (children == null) { 439 return NodeListImpl.EMPTY_NODELIST; 440 } else { 441 return new NodeListImpl(children); 442 } 443 } 444 445 460 public boolean isSupported(String feature, String version) { 461 return false; } 463 464 483 public Node appendChild(Node newChild) throws DOMException { 484 if (newChild == null) { 485 throw new DOMException 486 (DOMException.HIERARCHY_REQUEST_ERR, 487 "Can't append a null node."); 488 } 489 initializeChildren(); 490 ((NodeImpl) newChild).detachNode(); 494 children.add(newChild); 495 ((NodeImpl) newChild).parent = this; 496 setDirty(true); 497 return newChild; 498 } 499 500 510 public Node removeChild(Node oldChild) throws DOMException { 511 if (removeNodeFromChildList((NodeImpl) oldChild)) { 512 setDirty(true); 513 return oldChild; 514 } 515 throw new DOMException (DOMException.NOT_FOUND_ERR, 516 "NodeImpl Not found"); 517 } 518 519 private boolean removeNodeFromChildList(NodeImpl n) { 520 boolean removed = false; 521 initializeChildren(); 522 final Iterator itr = children.iterator(); 523 while (itr.hasNext()) { 524 final NodeImpl node = (NodeImpl) itr.next(); 525 if (node == n) { 526 removed = true; 527 itr.remove(); 528 } 529 } 530 return removed; 531 } 532 533 557 public Node insertBefore(Node newChild, Node refChild) throws DOMException { 558 initializeChildren(); 559 int position = children.indexOf(refChild); 560 if (position < 0) { 561 position = 0; 562 } 563 children.add(position, newChild); 564 setDirty(true); 565 return newChild; 566 } 567 568 591 public Node replaceChild(Node newChild, Node oldChild) throws DOMException { 592 initializeChildren(); 593 int position = children.indexOf(oldChild); 594 if (position < 0) { 595 throw new DOMException (DOMException.NOT_FOUND_ERR, 596 "NodeImpl Not found"); 597 } 598 children.remove(position); 599 children.add(position, newChild); 600 setDirty(true); 601 return oldChild; 602 } 603 604 613 public String getValue() { 614 return textRep.getNodeValue(); 615 } 616 617 627 public void setParentElement(SOAPElement parent) throws SOAPException { 628 if (parent == null) 629 throw new IllegalArgumentException ( 630 Messages.getMessage("nullParent00")); 631 try { 632 setParent((NodeImpl) parent); 633 } catch (Throwable t) { 634 throw new SOAPException (t); 635 } 636 } 637 638 649 public SOAPElement getParentElement() { 650 return (SOAPElement ) getParent(); 651 } 652 653 658 public void detachNode() { 659 if (parent != null) { 660 parent.removeChild(this); 661 parent = null; 662 } 663 } 664 665 674 public void recycleNode() { 675 } 677 678 690 public void setValue(String value) { 691 if (this instanceof org.apache.axis.message.Text) { 692 setNodeValue(value); 693 } else if (children != null) { 694 if (children.size() != 1) { 695 throw new IllegalStateException ( "setValue() may not be called on a non-Text node with more than one child." ); 696 } 697 javax.xml.soap.Node child = (javax.xml.soap.Node ) children.get(0); 698 if (!(child instanceof org.apache.axis.message.Text)) { 699 throw new IllegalStateException ( "setValue() may not be called on a non-Text node with a non-Text child." ); 700 } 701 ((javax.xml.soap.Text )child).setNodeValue(value); 702 } else { 703 appendChild(new org.apache.axis.message.Text(value)); 704 } 705 } 706 707 712 protected AttributesImpl makeAttributesEditable() { 713 if (attributes == null || attributes instanceof NullAttributes) { 714 attributes = new AttributesImpl (); 715 } else if (!(attributes instanceof AttributesImpl )) { 716 attributes = new AttributesImpl (attributes); 717 } 718 return (AttributesImpl ) attributes; 719 } 720 721 728 protected NamedNodeMap convertAttrSAXtoDOM(Attributes saxAttr) { 729 try { 730 org.w3c.dom.Document doc = org.apache.axis.utils.XMLUtils.newDocument(); 731 AttributesImpl saxAttrs = (AttributesImpl ) saxAttr; 732 NamedNodeMap domAttributes = new NamedNodeMapImpl(); 733 for (int i = 0; i < saxAttrs.getLength(); i++) { 734 String uri = saxAttrs.getURI(i); 735 String qname = saxAttrs.getQName(i); 736 String value = saxAttrs.getValue(i); 737 if (uri != null && uri.trim().length() > 0) { 738 if (NULL_URI_NAME.equals(uri)) { 741 uri = null; 742 } 743 Attr attr = doc.createAttributeNS(uri, qname); 744 attr.setValue(value); 745 domAttributes.setNamedItemNS(attr); 746 } else { 747 Attr attr = doc.createAttribute(qname); 748 attr.setValue(value); 749 domAttributes.setNamedItem(attr); 750 } 751 } 752 return domAttributes; 753 } catch (Exception ex) { 754 log.error(Messages.getMessage("saxToDomFailed00"),ex); 755 756 return null; 757 } 758 } 759 760 763 protected void initializeChildren() { 764 if (children == null) { 765 children = new ArrayList (); 766 } 767 } 768 769 773 protected NodeImpl getParent() { 774 return parent; 775 } 776 777 783 protected void setParent(NodeImpl parent) throws SOAPException { 784 if (this.parent == parent) { 785 return; 786 } 787 if (this.parent != null) { 788 this.parent.removeChild(this); 789 } 790 if (parent != null) { 791 parent.appendChild(this); 792 } 793 this.parent = parent; 794 } 795 796 801 public void output(SerializationContext context) throws Exception { 802 if (textRep == null) 803 return; 804 boolean oldPretty = context.getPretty(); 805 context.setPretty(false); 806 if (textRep instanceof CDATASection ) { 807 context.writeString("<![CDATA["); 808 context.writeString(((org.w3c.dom.Text ) textRep).getData()); 809 context.writeString("]]>"); 810 } else if (textRep instanceof Comment ) { 811 context.writeString("<!--"); 812 context.writeString(((CharacterData ) textRep).getData()); 813 context.writeString("-->"); 814 } else if (textRep instanceof Text) { 815 context.writeSafeString(((Text) textRep).getData()); 816 } 817 context.setPretty(oldPretty); 818 } 819 820 824 public boolean isDirty() { 825 return _isDirty; 826 } 827 828 833 public void setDirty(boolean dirty) 834 { 835 _isDirty = dirty; 836 if (_isDirty && parent != null) { 837 ((NodeImpl) parent).setDirty(true); 838 } 839 } 840 } 841 | Popular Tags |