| 1 16 package org.jboss.axis.message; 17 18 20 import org.jboss.axis.AxisFault; 21 import org.jboss.axis.Constants; 22 import org.jboss.axis.MessageContext; 23 import org.jboss.axis.MessagePart; 24 import org.jboss.axis.encoding.DeserializationContext; 25 import org.jboss.axis.encoding.DeserializationContextImpl; 26 import org.jboss.axis.encoding.Deserializer; 27 import org.jboss.axis.encoding.SerializationContext; 28 import org.jboss.axis.encoding.SerializationContextImpl; 29 import org.jboss.axis.enums.Style; 30 import org.jboss.axis.soap.SOAPConstants; 31 import org.jboss.axis.utils.Mapping; 32 import org.jboss.axis.utils.Messages; 33 import org.jboss.axis.utils.XMLUtils; 34 import org.jboss.logging.Logger; 35 import org.w3c.dom.Attr ; 36 import org.w3c.dom.DOMException ; 37 import org.w3c.dom.Document ; 38 import org.w3c.dom.Element ; 39 import org.w3c.dom.NamedNodeMap ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.NodeList ; 42 import org.w3c.dom.Text ; 43 import org.xml.sax.Attributes ; 44 import org.xml.sax.ContentHandler ; 45 import org.xml.sax.InputSource ; 46 import org.xml.sax.SAXException ; 47 import org.xml.sax.helpers.AttributesImpl ; 48 49 import javax.xml.namespace.QName ; 50 import javax.xml.rpc.JAXRPCException ; 51 import javax.xml.rpc.encoding.TypeMapping ; 52 import javax.xml.soap.Name ; 53 import javax.xml.soap.SOAPElement ; 54 import javax.xml.soap.SOAPException ; 55 import java.io.PrintWriter ; 56 import java.io.Reader ; 57 import java.io.StringReader ; 58 import java.io.StringWriter ; 59 import java.util.ArrayList ; 60 import java.util.Collections ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.Vector ; 64 65 68 public class SOAPElementAxisImpl extends SOAPElementImpl implements SOAPElement , Cloneable  69 { 70 private static Logger log = Logger.getLogger(SOAPElementAxisImpl.class.getName()); 71 72 protected String name; 73 protected String prefix; 74 protected String namespaceURI; 75 protected transient Attributes attributes = NullAttributes.singleton; 76 protected String id; 77 protected String href; 78 protected boolean _isRoot = true; 79 protected SOAPEnvelopeAxisImpl message; 80 81 protected transient DeserializationContext context; 82 83 protected transient QName typeQName; 84 85 protected Vector qNameAttrs; 86 87 protected transient SAX2EventRecorder recorder; 89 protected int startEventIndex; 90 protected int startContentsIndex; 91 protected int endEventIndex = -1; 92 93 protected Element elementRep; 95 96 private SOAPElementAxisImpl parent; 97 98 public ArrayList namespaces; 99 100 103 protected String encodingStyle; 104 105 108 protected Object objectValue; 109 110 private ChildElementList children = new ChildElementList(); 112 113 protected MessagePart soapPart; 114 115 private boolean immutable; 119 120 123 public SOAPElementAxisImpl() 124 { 125 super("unqualified-element"); 126 } 127 128 public SOAPElementAxisImpl(String localPart) 129 { 130 super(localPart); 131 namespaceURI = ""; 132 name = localPart; 133 } 134 135 public SOAPElementAxisImpl(String namespace, String localPart) 136 { 137 super(localPart, null, namespace); 138 namespaceURI = namespace; 139 name = localPart; 140 } 141 142 public SOAPElementAxisImpl(String localPart, String prefix, String namespace) 143 { 144 super(localPart, prefix, namespace); 145 this.namespaceURI = namespace; 146 this.name = localPart; 147 this.prefix = prefix; 148 if (prefix != null && prefix.length() > 0) 149 addMapping(new Mapping(namespace, prefix)); 150 } 151 152 public SOAPElementAxisImpl(Name eltName) 153 { 154 super(eltName); 155 this.namespaceURI = eltName.getURI(); 156 this.name = eltName.getLocalName(); 157 this.prefix = eltName.getPrefix(); 158 if (prefix != null && prefix.length() > 0) 159 addMapping(new Mapping(namespaceURI, prefix)); 160 } 161 162 public SOAPElementAxisImpl(String namespace, String localPart, Object value) 163 { 164 super(localPart, "", namespace); 165 this.namespaceURI = namespace; 166 this.name = localPart; 167 objectValue = value; 168 } 169 170 public SOAPElementAxisImpl(QName name, Object value) 171 { 172 super(name.getLocalPart(), name.getPrefix(), name.getNamespaceURI()); 173 this.namespaceURI = name.getNamespaceURI(); 174 this.prefix = name.getPrefix(); 175 this.name = name.getLocalPart(); 176 objectValue = value; 177 } 178 179 public SOAPElementAxisImpl(Element elem) 180 { 181 super(elem); 182 namespaceURI = elem.getNamespaceURI(); 183 prefix = elem.getPrefix(); 184 name = elem.getLocalName(); 185 elementRep = elem; 186 } 187 188 public SOAPElementAxisImpl(String namespace, String localPart, String prefix, Attributes attributes, DeserializationContext context) 189 throws AxisFault 190 { 191 192 super(localPart, prefix, namespace); 193 this.namespaceURI = namespace; 194 this.name = localPart; 195 this.prefix = prefix; 196 197 if (log.isDebugEnabled()) 198 { 199 log.debug(Messages.getMessage("newElem00", super.toString(), 200 "{" + prefix + "}" + localPart)); 201 for (int i = 0; attributes != null && i < attributes.getLength(); i++) 202 { 203 log.debug(" " + attributes.getQName(i) + " = '" + attributes.getValue(i) + "'"); 204 } 205 } 206 207 this.context = context; 208 this.startEventIndex = context.getStartOfMappingsPos(); 209 210 setNSMappings(context.getCurrentNSMappings()); 211 212 this.recorder = context.getRecorder(); 213 214 if (attributes != null && attributes.getLength() > 0) 215 { 216 this.attributes = attributes; 217 218 typeQName = context.getTypeFromAttributes(namespace, 219 localPart, 220 attributes); 221 222 String rootVal = attributes.getValue(Constants.URI_DEFAULT_SOAP_ENC, Constants.ATTR_ROOT); 223 if (rootVal != null) 224 _isRoot = rootVal.equals("1"); 225 226 id = attributes.getValue(Constants.ATTR_ID); 227 if (id != null) 229 { 230 context.registerElementByID(id, this); 231 if (recorder == null) 232 { 233 recorder = new SAX2EventRecorder(); 234 context.setRecorder(recorder); 235 } 236 } 237 238 MessageContext mc = context.getMessageContext(); 241 SOAPConstants sc = (mc != null) ? 242 mc.getSOAPConstants() : 243 SOAPConstants.SOAP11_CONSTANTS; 244 245 href = attributes.getValue(sc.getAttrHref()); 246 247 if (attributes.getValue(Constants.URI_DEFAULT_SOAP_ENC, Constants.ATTR_ARRAY_TYPE) != null) 249 typeQName = Constants.SOAP_ARRAY; 250 251 252 encodingStyle = 253 attributes.getValue(sc.getEncodingURI(), 254 Constants.ATTR_ENCODING_STYLE); 255 256 if (Constants.URI_SOAP12_NOENC.equals(encodingStyle)) 258 encodingStyle = null; 259 260 if (encodingStyle != null && 267 sc.equals(SOAPConstants.SOAP12_CONSTANTS) && 268 (mc.getOperationStyle() != Style.MESSAGE)) 269 { 270 TypeMapping tm = mc.getTypeMappingRegistry(). 271 getTypeMapping(encodingStyle); 272 if (tm == null || 273 (tm.equals(mc.getTypeMappingRegistry(). 274 getDefaultTypeMapping()))) 275 { 276 AxisFault badEncodingFault = new AxisFault(Constants.FAULT_SOAP12_DATAENCODINGUNKNOWN, 277 "bad encoding style", null, null); 278 throw badEncodingFault; 279 } 280 } 281 282 } 283 } 284 285 288 Deserializer fixupDeserializer; 289 290 public void setFixupDeserializer(Deserializer dser) 291 { 292 fixupDeserializer = dser; 294 } 295 296 public Deserializer getFixupDeserializer() 297 { 298 return fixupDeserializer; 299 } 300 301 public void setEndIndex(int endIndex) 302 { 303 endEventIndex = endIndex; 304 } 306 307 312 private boolean isDirty; 313 314 public boolean isDirty() 315 { 316 return isDirty; 317 } 319 320 public void setDirty(boolean dirty) 321 { 322 isDirty = dirty; 323 } 324 325 public boolean isRoot() 326 { 327 return _isRoot; 328 } 329 330 public String getID() 331 { 332 return id; 333 } 334 335 public String getHref() 336 { 337 return href; 338 } 339 340 public Attributes getAttributesEx() 341 { 342 return attributes; 343 } 344 345 public Node getFirstChild() 346 { 347 if (children != null && !children.isEmpty()) 348 { 349 return (Node )children.get(0); 350 } 351 else 352 { 353 return null; 354 } 355 } 356 357 public Node getLastChild() 358 { 359 List children = getChildren(); 360 if (children != null) 361 return (Node )children.get(children.size() - 1); 362 else 363 return null; 364 } 365 366 public Node getNextSibling() 367 { 368 SOAPElement parent = getParentElement(); 369 if (parent == null) 370 { 371 return null; 372 } 373 Iterator iter = parent.getChildElements(); 374 Node nextSibling = null; 375 while (iter.hasNext()) 376 { 377 if (iter.next().equals(this)) 378 { 379 if (iter.hasNext()) 380 { 381 return (Node )iter.next(); 382 } 383 else 384 { 385 return null; 386 } 387 } 388 } 389 return nextSibling; } 391 392 public Node getParentNode() 393 { 394 return parent; 395 } 396 397 public Node getPreviousSibling() 398 { 399 SOAPElement parent = getParentElement(); 400 Iterator iter = parent.getChildElements(); 401 Node previousSibling = null; 402 while (iter.hasNext()) 403 { 404 if (iter.next().equals(this)) 405 { 406 return previousSibling; 407 } 408 } 409 return previousSibling; } 411 412 public Node cloneNode(boolean deep) 413 { 414 try 415 { 416 SOAPElementAxisImpl clonedSelf = (SOAPElementAxisImpl)this.clonning(); 417 418 if (deep == true) 419 { 420 if (children != null) 421 { 422 for (int i = 0; i < children.size(); i++) 423 { 424 SOAPElementAxisImpl child = (SOAPElementAxisImpl)children.get(i); 425 if (child != null) 426 { SOAPElementAxisImpl clonedChild = (SOAPElementAxisImpl)child.cloneNode(deep); clonedChild.setParent(clonedSelf); 429 clonedChild.setOwnerDocument(soapPart); 430 } 431 } 432 } 433 } 434 return clonedSelf; 435 } 436 catch (Exception e) 437 { 438 return null; 439 } 440 } 441 442 472 protected Object clonning() throws CloneNotSupportedException  473 { 474 try 475 { 476 SOAPElementAxisImpl clonedME = null; 477 clonedME = (SOAPElementAxisImpl)this.clone(); 478 479 clonedME.setName(name); 480 clonedME.setNamespaceURI(namespaceURI); 481 clonedME.setPrefix(prefix); 482 483 clonedME.setAllAttributes(new AttributesImpl (attributes)); 485 487 clonedME.namespaces = new ArrayList (); 488 if (namespaces != null) 489 { 490 for (int i = 0; i < namespaces.size(); i++) 491 { 492 Mapping namespace = (Mapping)namespaces.get(i); 494 clonedME.addNamespaceDeclaration(namespace.getPrefix(), namespace.getNamespaceURI()); } 496 } 497 clonedME.detachAllChildren(); 499 500 clonedME.setParent(null); 502 clonedME.setDirty(isDirty()); 504 if (encodingStyle != null) 505 { 506 clonedME.setEncodingStyle(new String (encodingStyle)); 507 } 508 clonedME.setRecorder(recorder); 509 return clonedME; 510 } 511 catch (Exception ex) 512 { 513 return null; 514 } 515 } 516 517 public void setAllAttributes(Attributes attrs) 519 { 520 attributes = attrs; 521 } 522 523 public void detachAllChildren() 524 { 525 children.clear(); 526 } 527 528 public NodeList getChildNodes() 529 { 530 NodeListImpl nodeList = new NodeListImpl(); 531 for (int i = 0; i < children.size(); i++) 532 { 533 Node node = (Node )children.get(i); 534 nodeList.addNode(node); 535 } 536 return nodeList; 537 } 538 539 public boolean isSupported(String feature, String version) 540 { 541 return false; } 543 544 public Node appendChild(Node newChild) throws DOMException  545 { 546 super.appendChild(newChild); 547 children.add(newChild); 548 return newChild; 549 } 550 551 555 public void addChild(SOAPElementAxisImpl el) throws SOAPException  556 { 557 558 assertImmutable(); 559 560 el.parent = this; 561 children.add(el); 562 } 563 564 568 private void assertImmutable() 569 { 570 SOAPEnvelopeAxisImpl env = getSOAPEnvelope(); 571 if (env != null && env.isProcessingRPCInvocation() && immutable) 572 throw new JAXRPCException ("Cannot modify an immutable element"); 573 } 574 575 public Node removeChild(Node oldChild) throws DOMException  576 { 577 578 int position = children.indexOf(oldChild); 579 if (position < 0) 580 throw new DOMException (DOMException.NOT_FOUND_ERR, "MessageElement Not found"); 581 582 Iterator it = children.iterator(); 584 while (it.hasNext()) 585 { 586 Node node = (Node )it.next(); 587 if (node.equals(oldChild)) 588 { 589 log.debug("Remove child node: " + node.getNodeName()); 590 it.remove(); 591 } 592 } 593 594 setDirty(true); 595 return oldChild; 596 } 597 598 public Node insertBefore(Node newChild, Node refChild) throws DOMException  599 { 600 int position = children.indexOf(refChild); 601 if (position < 0) position = 0; 602 children.add(position, newChild); 603 setDirty(true); 604 return newChild; 605 } 606 607 public Node replaceChild(Node newChild, Node oldChild) throws DOMException  608 { 609 int position = children.indexOf(oldChild); 610 if (position < 0) 611 throw new DOMException (DOMException.NOT_FOUND_ERR, "MessageElement Not found"); 612 613 children.remove(position); 614 children.add(position, newChild); 615 setDirty(true); 616 return oldChild; 617 } 618 619 625 public Attributes getCompleteAttributes() 626 { 627 if (namespaces == null) 628 return attributes; 629 630 AttributesImpl attrs = null; 631 if (attributes == NullAttributes.singleton) 632 attrs = new AttributesImpl (); 633 else 634 attrs = new AttributesImpl (attributes); 635 636 for (Iterator iterator = namespaces.iterator(); iterator.hasNext();) 637 { 638 Mapping mapping = (Mapping)iterator.next(); 639 String prefix = mapping.getPrefix(); 640 String nsURI = mapping.getNamespaceURI(); 641 attrs.addAttribute(Constants.NS_URI_XMLNS, prefix, 642 "xmlns:" + prefix, nsURI, "CDATA"); 643 } 644 return attrs; 645 } 646 647 public String getName() 648 { 649 return (name); 650 } 651 652 public void setName(String name) 653 { 654 this.name = name; 655 } 656 657 public QName getQName() 658 { 659 return new QName (namespaceURI, name); 660 } 661 662 public void setQName(QName qName) 663 { 664 this.name = qName.getLocalPart(); 665 this.namespaceURI = qName.getNamespaceURI(); 666 } 667 668 public String getPrefix() 669 { 670 return (prefix); 671 } 672 673 public void setPrefix(String prefix) 674 { 675 this.prefix = prefix; 676 } 677 678 public Document getOwnerDocument() 679 { 680 return soapPart; 681 } 682 683 public NamedNodeMap getAttributes() 684 { 685 makeAttributesEditable(); 687 return convertAttrSAXtoDOM(); 688 } 689 690 698 699 private NamedNodeMap convertAttrSAXtoDOM() 700 { 701 try 702 { 703 org.w3c.dom.Document doc = org.jboss.axis.utils.XMLUtils.newDocument(); 704 705 AttributesImpl saxAttrs = (AttributesImpl )attributes; 706 NamedNodeMap domAttributes = new NamedNodeMapImpl(); 707 for (int i = 0; i < saxAttrs.getLength(); i++) 708 { 709 String uri = saxAttrs.getURI(i); 710 String qname = saxAttrs.getQName(i); 711 String value = saxAttrs.getValue(i); 712 713 if (uri != null && uri.trim().length() > 0) 714 { 715 if (uri.equals("intentionalNullURI")) 718 { 719 uri = null; 720 } 721 722 if (uri.equals(Constants.NS_URI_XMLNS) && qname.startsWith("xmlns:") == false) 723 qname = "xmlns:" + qname; 724 725 if (uri.equals(Constants.URI_DEFAULT_SCHEMA_XSI) && qname.startsWith("xsi:") == false) 726 qname = "xsi:" + qname; 727 728 Attr attr = doc.createAttributeNS(uri, qname); 729 attr.setValue(value); 730 domAttributes.setNamedItemNS(attr); 731 } 732 else 733 { 734 735 Attr attr = doc.createAttribute(qname); 736 attr.setValue(value); 737 domAttributes.setNamedItem(attr); 738 } 739 } 740 return domAttributes; 741 742 } 743 catch (Exception ex) 744 { 745 log.error("Cannot convert SAX to DOM attributes", ex); 746 return null; 747 } 748 749 } 750 751 public short getNodeType() 752 { 753 if (false) 754 { 755 return DOCUMENT_FRAGMENT_NODE; 756 } 757 else if (false) 758 { 759 return Node.ELEMENT_NODE; 760 } 761 else 762 { return Node.ELEMENT_NODE; 764 } 765 } 766 767 public void normalize() 768 { 769 } 771 772 public boolean hasAttributes() 773 { 774 return attributes.getLength() > 0; 775 } 776 777 public boolean hasChildNodes() 778 { 779 return children.size() > 0; 780 } 781 782 public String getLocalName() 783 { 784 return name; 785 } 786 787 public String getNamespaceURI() 788 { 789 return (namespaceURI); 790 } 791 792 public String getNodeValue() throws DOMException  793 { 794 throw new DOMException (DOMException.NO_DATA_ALLOWED_ERR, 795 "Cannot use TextNode.get in " + this); 796 } 797 798 public void setNamespaceURI(String nsURI) 799 { 800 namespaceURI = nsURI; 801 } 802 803 public QName getType() 804 { 805 if (typeQName == null && href != null && context != null) 807 { 808 SOAPElementAxisImpl referent = context.getElementByID(href); 809 if (referent != null) 810 { 811 typeQName = referent.getType(); 812 } 813 } 814 return typeQName; 815 } 816 817 public void setType(QName qname) 818 { 819 typeQName = qname; 820 } 821 822 public SAX2EventRecorder getRecorder() 823 { 824 return recorder; 825 } 826 827 public void setRecorder(SAX2EventRecorder rec) 828 { 829 recorder = rec; 830 } 831 832 838 public String getEncodingStyle() 839 { 840 if (encodingStyle == null) 841 { 842 if (parent == null) 843 return ""; 844 return parent.getEncodingStyle(); 845 } 846 return encodingStyle; 847 } 848 849 public void removeContents() 850 { 851 if (children != null) 853 { 854 for (int i = 0; i < children.size(); i++) 855 { 856 try 857 { 858 ((SOAPElementAxisImpl)children.get(i)).setParent(null); 859 } 860 catch (Exception e) 861 { 862 } 863 } 864 children.clear(); 866 } 867 } 868 869 public Iterator getVisibleNamespacePrefixes() 870 { 871 Vector prefixes = new Vector (); 872 873 if (parent != null) 875 { 87
|