| 1 16 17 package org.apache.axis.message; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Constants; 21 import org.apache.axis.MessageContext; 22 import org.apache.axis.components.logger.LogFactory; 23 import org.apache.axis.encoding.DeserializationContext; 24 import org.apache.axis.encoding.Deserializer; 25 import org.apache.axis.encoding.SerializationContext; 26 import org.apache.axis.encoding.TextSerializationContext; 27 import org.apache.axis.constants.Style; 28 import org.apache.axis.soap.SOAPConstants; 29 import org.apache.axis.utils.Mapping; 30 import org.apache.axis.utils.Messages; 31 import org.apache.axis.utils.XMLUtils; 32 import org.apache.commons.logging.Log; 33 import org.w3c.dom.Attr ; 34 import org.w3c.dom.CDATASection ; 35 import org.w3c.dom.CharacterData ; 36 import org.w3c.dom.Comment ; 37 import org.w3c.dom.DOMException ; 38 import org.w3c.dom.Document ; 39 import org.w3c.dom.Element ; 40 import org.w3c.dom.Node ; 41 import org.w3c.dom.NodeList ; 42 import org.w3c.dom.Text ; 43 import org.w3c.dom.NamedNodeMap ; 44 import org.xml.sax.Attributes ; 45 import org.xml.sax.ContentHandler ; 46 import org.xml.sax.InputSource ; 47 import org.xml.sax.SAXException ; 48 import org.xml.sax.helpers.AttributesImpl ; 49 50 import javax.xml.namespace.QName ; 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 javax.xml.parsers.ParserConfigurationException ; 56 import java.io.Reader ; 57 import java.io.Serializable ; 58 import java.io.StringReader ; 59 import java.io.StringWriter ; 60 import java.util.ArrayList ; 61 import java.util.Enumeration ; 62 import java.util.Iterator ; 63 import java.util.List ; 64 import java.util.Vector ; 65 66 73 public class MessageElement extends NodeImpl implements SOAPElement , 74 Serializable , 75 org.w3c.dom.NodeList , Cloneable  77 { 78 protected static Log log = 79 LogFactory.getLog(MessageElement.class.getName()); 80 81 private static final Mapping enc11Mapping = 82 new Mapping(Constants.URI_SOAP11_ENC, 83 "SOAP-ENC"); 84 85 private static final Mapping enc12Mapping = 86 new Mapping(Constants.URI_SOAP12_ENC, 87 "SOAP-ENC"); 88 89 protected String id; 90 protected String href; 91 protected boolean _isRoot = true; 92 protected SOAPEnvelope message = null; 93 94 protected transient DeserializationContext context; 95 96 protected transient QName typeQName = null; 97 98 protected Vector qNameAttrs = null; 99 100 protected transient SAX2EventRecorder recorder = null; 102 protected int startEventIndex = 0; 103 protected int startContentsIndex = 0; 104 protected int endEventIndex = -1; 105 106 public ArrayList namespaces = null; 107 108 109 protected String encodingStyle = null; 110 111 112 private Object objectValue = null; 113 114 116 public MessageElement() 117 { 118 } 119 120 125 public MessageElement(String namespace, String localPart) 126 { 127 namespaceURI = namespace; 128 name = localPart; 129 } 130 131 137 public MessageElement(String localPart, String prefix, String namespace) 138 { 139 this.namespaceURI = namespace; 140 this.name = localPart; 141 this.prefix = prefix; 142 addMapping(new Mapping(namespace, prefix)); 143 } 144 145 150 public MessageElement(Name eltName) 151 { 152 this(eltName.getLocalName(),eltName.getPrefix(), eltName.getURI()); 153 } 154 155 162 public MessageElement(String namespace, String localPart, Object value) 163 { 164 this(namespace, localPart); 165 objectValue = value; 166 } 167 168 172 public MessageElement(QName name) 173 { 174 this(name.getNamespaceURI(), name.getLocalPart()); 175 } 176 177 183 public MessageElement(QName name, Object value) 184 { 185 this(name.getNamespaceURI(), name.getLocalPart()); 186 objectValue = value; 187 } 188 189 193 public MessageElement(Element elem) 194 { 195 namespaceURI = elem.getNamespaceURI(); 196 name = elem.getLocalName(); 197 copyNode(elem); 198 } 199 200 204 public MessageElement(CharacterData text) 205 { 206 textRep = text; 207 namespaceURI = text.getNamespaceURI(); 208 name = text.getLocalName(); 209 } 210 211 234 public MessageElement(String namespace, String localPart, String prefix, 235 Attributes attributes, DeserializationContext context) 236 throws AxisFault 237 { 238 if (log.isDebugEnabled()) { 239 log.debug(Messages.getMessage("newElem00", super.toString(), 240 "{" + prefix + "}" + localPart)); 241 for (int i = 0; attributes != null && i < attributes.getLength(); i++) { 242 log.debug(" " + attributes.getQName(i) + " = '" + attributes.getValue(i) + "'"); 243 } 244 } 245 this.namespaceURI = namespace; 246 this.name = localPart; 247 this.prefix = prefix; 248 249 this.context = context; 250 this.startEventIndex = context.getStartOfMappingsPos(); 251 252 setNSMappings(context.getCurrentNSMappings()); 253 254 this.recorder = context.getRecorder(); 255 256 if (attributes != null && attributes.getLength() > 0) { 257 this.attributes = attributes; 258 259 this.typeQName = context.getTypeFromAttributes(namespace, 260 localPart, 261 attributes); 262 263 String rootVal = attributes.getValue(Constants.URI_DEFAULT_SOAP_ENC, Constants.ATTR_ROOT); 264 265 if (rootVal != null) { 266 _isRoot = "1".equals(rootVal); 267 } 268 269 id = attributes.getValue(Constants.ATTR_ID); 270 if (id != null) { 272 context.registerElementByID(id, this); 273 if (recorder == null) { 274 recorder = new SAX2EventRecorder(); 275 context.setRecorder(recorder); 276 } 277 } 278 279 MessageContext mc = context.getMessageContext(); 282 SOAPConstants sc = (mc != null) ? 283 mc.getSOAPConstants() : 284 SOAPConstants.SOAP11_CONSTANTS; 285 286 href = attributes.getValue(sc.getAttrHref()); 287 288 if (attributes.getValue(Constants.URI_DEFAULT_SOAP_ENC, Constants.ATTR_ARRAY_TYPE) != null) { 290 typeQName = Constants.SOAP_ARRAY; 291 } 292 293 294 encodingStyle = 295 attributes.getValue(sc.getEncodingURI(), 296 Constants.ATTR_ENCODING_STYLE); 297 298 if (Constants.URI_SOAP12_NOENC.equals(encodingStyle)) 300 encodingStyle = null; 301 302 if (encodingStyle != null && 309 sc.equals(SOAPConstants.SOAP12_CONSTANTS) && 310 (mc.getOperationStyle() != Style.MESSAGE)) { 311 TypeMapping tm = mc.getTypeMappingRegistry(). 312 getTypeMapping(encodingStyle); 313 if (tm == null || 314 (tm.equals(mc.getTypeMappingRegistry(). 315 getDefaultTypeMapping()))) { 316 AxisFault badEncodingFault = new AxisFault( 317 Constants.FAULT_SOAP12_DATAENCODINGUNKNOWN, 318 "bad encoding style", null, null); 319 throw badEncodingFault; 320 } 321 } 322 323 } 324 } 325 326 331 public DeserializationContext getDeserializationContext() 332 { 333 return context; 334 } 335 336 338 protected Deserializer fixupDeserializer; 339 340 public void setFixupDeserializer(Deserializer dser) 341 { 342 fixupDeserializer = dser; 344 } 345 346 public Deserializer getFixupDeserializer() 347 { 348 return fixupDeserializer; 349 } 350 351 355 public void setEndIndex(int endIndex) 356 { 357 endEventIndex = endIndex; 358 } 360 361 365 public boolean isRoot() { return _isRoot; } 366 367 371 public String getID() { return id; } 372 373 377 public String getHref() { return href; } 378 379 384 public Attributes getAttributesEx() { return attributes; } 385 386 387 412 public Node cloneNode(boolean deep) { 413 try{ 414 MessageElement clonedSelf = (MessageElement) cloning(); 415 416 if(deep){ 417 if(children != null){ 418 for(int i =0; i < children.size(); i++){ 419 NodeImpl child = (NodeImpl)children.get(i); 420 if(child != null) { NodeImpl clonedChild = (NodeImpl)child.cloneNode(deep); clonedChild.setParent(clonedSelf); 423 clonedChild.setOwnerDocument(getOwnerDocument()); 424 425 clonedSelf.childDeepCloned( child, clonedChild ); 426 } 427 } 428 } 429 } 430 return clonedSelf; 431 } 432 catch(Exception e){ 433 return null; 434 } 435 } 436 437 protected void childDeepCloned( NodeImpl oldNode, NodeImpl newNode ) 442 { 443 } 444 445 474 protected Object cloning() throws CloneNotSupportedException  475 { 476 try{ 477 MessageElement clonedME = null; 478 clonedME = (MessageElement)this.clone(); 479 480 clonedME.setName(name); 481 clonedME.setNamespaceURI(namespaceURI); 482 clonedME.setPrefix(prefix); 483 484 clonedME.setAllAttributes(new AttributesImpl (attributes)); 486 488 clonedME.namespaces = new ArrayList (); 489 if(namespaces != null){ 490 for(int i = 0; i < namespaces.size(); i++){ 491 Mapping namespace = (Mapping)namespaces.get(i); 493 clonedME.addNamespaceDeclaration(namespace.getPrefix(), namespace.getNamespaceURI()); } 495 } 496 clonedME.children = new ArrayList (); 497 498 clonedME.parent = null; 500 clonedME.setDirty(this._isDirty); 502 if(encodingStyle != null){ 503 clonedME.setEncodingStyle(encodingStyle); 504 } 505 return clonedME; 506 }catch(Exception ex){ 507 return null; 508 } 509 } 510 511 512 516 public void setAllAttributes(Attributes attrs){ 517 attributes = attrs; 518 } 519 520 523 public void detachAllChildren() 524 { 525 removeContents(); 526 } 527 528 534 public Attributes getCompleteAttributes() { 535 if (namespaces == null) { 536 return attributes; 537 } 538 539 AttributesImpl attrs = null; 540 if (attributes == NullAttributes.singleton) { 541 attrs = new AttributesImpl (); 542 } else { 543 attrs = new AttributesImpl (attributes); 544 } 545 546 for (Iterator iterator = namespaces.iterator(); iterator.hasNext();) { 547 Mapping mapping = (Mapping) iterator.next(); 548 String prefix = mapping.getPrefix(); 549 String nsURI = mapping.getNamespaceURI(); 550 attrs.addAttribute(Constants.NS_URI_XMLNS, prefix, 551 "xmlns:" + prefix, nsURI, "CDATA"); 552 } 553 return attrs; 554 } 555 556 560 public String getName() { 561 return name; 562 } 563 564 568 public void setName(String name) { 569 this.name = name; 570 } 571 572 576 public QName getQName() { 577 return new QName (namespaceURI, name); 578 } 579 580 584 public void setQName(QName qName) { 585 this.name = qName.getLocalPart(); 586 this.namespaceURI = qName.getNamespaceURI(); 587 } 588 589 593 public void setNamespaceURI(String nsURI) { 594 namespaceURI = nsURI; 595 } 596 597 603 public QName getType() { 604 if (typeQName == null && href != null && context != null) { 606 MessageElement referent = context.getElementByID(href); 607 if (referent != null) { 608 typeQName = referent.getType(); 609 } 610 } 611 return typeQName; 612 } 613 614 618 public void setType(QName qname) { 619 typeQName = qname; 620 } 621 622 626 public SAX2EventRecorder getRecorder() { 627 return recorder; 628 } 629 630 634 public void setRecorder(SAX2EventRecorder rec) { 635 recorder = rec; 636 } 637 638 644 public String getEncodingStyle() { 645 if (encodingStyle == null) { 646 if (parent == null) { 647 return ""; 648 } 649 return ((MessageElement) parent).getEncodingStyle(); 650 } 651 return encodingStyle; 652 } 653 654 658 public void removeContents() { 659 if (children != null) { 661 for (int i = 0; i < children.size(); i++) { 662 try { 663 ((NodeImpl) children.get(i)).setParent(null); 664 } catch (SOAPException e) { 665 log.debug("ignoring", e); 666 } 667 } 668 children.clear(); 670 setDirty(true); 671 } 672 } 673 674 679 public Iterator getVisibleNamespacePrefixes() { 680 Vector prefixes = new Vector (); 681 682 if(parent !=null){ 684 Iterator parentsPrefixes = ((MessageElement)parent).getVisibleNamespacePrefixes(); 685 if(parentsPrefixes != null){ 686 while(parentsPrefixes.hasNext()){ 687 prefixes.add(parentsPrefixes.next()); 688 } 689 } 690 } 691 Iterator mine = getNamespacePrefixes(); 692 if(mine != null){ 693 while(mine.hasNext()){ 694 prefixes.add(mine.next()); 695 } 696 } 697 return prefixes.iterator(); 698 } 699 700 712 public void setEncodingStyle(String encodingStyle) throws SOAPException { 713 if (encodingStyle == null) { 714 encodingStyle = ""; 715 } 716 717 this.encodingStyle = encodingStyle; 718 719 if (encodingStyle.equals(Constants.URI_SOAP11_ENC)) { 722 addMapping(enc11Mapping); 723 } else if (encodingStyle.equals(Constants.URI_SOAP12_ENC)) { 724 addMapping(enc12Mapping); 725 } 726 } 727 728 732 public void addChild(MessageElement el) throws SOAPException  733 { 734 if (objectValue != null) { 735 IllegalStateException exc = 736 new IllegalStateException (Messages.getMessage("valuePresent")); 737 log.error(Messages.getMessage("valuePresent"), exc); 738 throw exc; 739 } 740 initializeChildren(); 741 children.add(el); 742 el.parent = this; 743 } 744 745 749 public List getChildren() 750 { 751 return children; 752 } 753 754 759 public void setContentsIndex(int index) 760 { 761 startContentsIndex = index; 762 } 763 764 768 public void setNSMappings(ArrayList namespaces) 769 { 770 this.namespaces = namespaces; 771 } 772 773 778 public String getPrefix(String searchNamespaceURI) { 779 if ((searchNamespaceURI == null) || ("".equals(searchNamespaceURI))) 780 return null; 781 782 if (href != null && getRealElement() != null) { 783 return getRealElement().getPrefix(searchNamespaceURI); 784 } 785 786 for (int i = 0; namespaces != null && i < namespaces.size(); i++) { 787 Mapping map = (Mapping) namespaces.get(i); 788 if (map.getNamespaceURI().equals(searchNamespaceURI)) { 789 return map.getPrefix(); 790 } 791 } 792 793 if (parent != null) { 794 return ((MessageElement) parent).getPrefix(searchNamespaceURI); 795 } 796 797 return null; 798 } 799 800 806 public String getNamespaceURI(String searchPrefix) { 807 if (searchPrefix == null) { 808 searchPrefix = ""; 809 } 810 811 if (href != null && getRealElement() != null) { 812 return getRealElement().getNamespaceURI(searchPrefix); 813 } 814 815 for (int i = 0; namespaces != null && i < namespaces.size(); i++) { 816 Mapping map = (Mapping) namespaces.get(i); 817 if (map.getPrefix().equals(searchPrefix)) { 818 return map.getNamespaceURI(); 819 } 820 } 821 822 if (parent != null) { 823 return ((MessageElement) parent).getNamespaceURI(searchPrefix); 824 } 825 826 if (log.isDebugEnabled()) { 827 log.debug(Messages.getMessage("noPrefix00", "" + this, searchPrefix)); 828 } 829 830 return null; 831 } 832 833 837 public Object getObjectValue() { 838 Object obj = null; 839 try { 840 obj = getObjectValue(null); 841 } catch (Exception e) { 842 log.debug("getValue()", e); 843 } 844 return obj; 845 } 846 847 |