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 852 public Object getObjectValue(Class cls) throws Exception { 853 if (objectValue == null) { 854 objectValue = getValueAsType(getType(), cls); 855 } 856 return objectValue; 857 } 858 859 869 public void setObjectValue(Object newValue) throws SOAPException { 870 if (children != null && !children.isEmpty()) { 871 SOAPException exc = new SOAPException (Messages.getMessage("childPresent")); 872 log.error(Messages.getMessage("childPresent"), exc); 873 throw exc; 874 } 875 if (textRep != null) { 876 SOAPException exc = new SOAPException (Messages.getMessage("xmlPresent")); 877 log.error(Messages.getMessage("xmlPresent"), exc); 878 throw exc; 879 } 880 this.objectValue = newValue; 881 } 882 883 public Object getValueAsType(QName type) throws Exception 884 { 885 return getValueAsType(type, null); 886 } 887 888 897 public Object getValueAsType(QName type, Class cls) throws Exception 898 { 899 if (context == null) { 900 throw new Exception (Messages.getMessage("noContext00")); 901 } 902 903 Deserializer dser = null; 904 if (cls == null) { 905 dser = context.getDeserializerForType(type); 906 } else { 907 dser = context.getDeserializerForClass(cls); 908 } 909 if (dser == null) { 910 throw new Exception (Messages.getMessage("noDeser00", "" + type)); 911 } 912 913 boolean oldVal = context.isDoneParsing(); 914 context.deserializing(true); 915 context.pushElementHandler(new EnvelopeHandler((SOAPHandler)dser)); 916 917 publishToHandler((org.xml.sax.ContentHandler ) context); 918 919 context.deserializing(oldVal); 920 921 return dser.getValue(); 922 } 923 924 927 protected static class QNameAttr { 928 public QName name; 929 public QName value; 930 } 931 932 939 940 public void addAttribute(String namespace, String localName, 941 QName value) 942 { 943 if (qNameAttrs == null) { 944 qNameAttrs = new Vector (); 945 } 946 947 QNameAttr attr = new QNameAttr(); 948 attr.name = new QName (namespace, localName); 949 attr.value = value; 950 951 qNameAttrs.addElement(attr); 952 } 954 955 962 public void addAttribute(String namespace, String localName, 963 String value) 964 { 965 AttributesImpl attributes = makeAttributesEditable(); 966 attributes.addAttribute(namespace, localName, "", "CDATA", 967 value); 968 } 969 970 979 public void addAttribute(String attrPrefix, String namespace, String localName, 980 String value) 981 { 982 AttributesImpl attributes = makeAttributesEditable(); 983 String attrName = localName; 984 if (attrPrefix != null && attrPrefix.length() > 0) { 985 attrName = attrPrefix + ":" + localName; 986 } 987 attributes.addAttribute(namespace, localName, attrName, "CDATA", 988 value); 989 } 990 991 996 public void setAttribute(String namespace, String localName, 997 String value) 998 { 999 AttributesImpl attributes = makeAttributesEditable(); 1000 1001 int idx = attributes.getIndex(namespace, localName); 1002 if (idx > -1) { 1003 if (value != null) { 1005 attributes.setValue(idx, value); 1006 } else { 1007 attributes.removeAttribute(idx); 1008 } 1009 return; 1010 } 1011 1012 addAttribute(namespace, localName, value); 1013 } 1014 1015 1020 public String getAttributeValue(String localName) 1021 { 1022 if (attributes == null) { 1023 return null; 1024 } 1025 return attributes.getValue(localName); 1026 } 1027 1028 1032 public void setEnvelope(SOAPEnvelope env) 1033 { 1034 env.setDirty(true); 1035 message = env; 1036 } 1037 1038 1042 public SOAPEnvelope getEnvelope() 1043 { 1044 return message; 1045 } 1046 1047 1052 public MessageElement getRealElement() 1053 { 1054 if (href == null) { 1055 return this; 1056 } 1057 1058 Object obj = context.getObjectByRef(href); 1059 if (obj == null) { 1060 return null; 1061 } 1062 1063 if (!(obj instanceof MessageElement)) { 1064 return null; 1065 } 1066 1067 return (MessageElement) obj; 1068 } 1069 1070 1077 public Document getAsDocument() throws Exception 1078 { 1079 String elementString = getAsString(); 1080 1081 Reader reader = new StringReader (elementString); 1082 Document doc = XMLUtils.newDocument(new InputSource (reader)); 1083 if (doc == null) { 1084 throw new Exception ( 1085 Messages.getMessage("noDoc00", elementString)); 1086 } 1087 return doc; 1088 } 1089 1090 1099 public String getAsString() throws Exception { 1100 SerializationContext serializeContext = null; 1101 StringWriter writer = new StringWriter (); 1102 MessageContext msgContext; 1103 if (context != null) { 1104 msgContext = context.getMessageContext(); 1105 } else { 1106 msgContext = MessageContext.getCurrentContext(); 1107 } 1108 serializeContext = new SerializationContext(writer, msgContext); 1109 serializeContext.setSendDecl(false); 1110 setDirty(false); 1111 output(serializeContext); 1112 writer.close(); 1113 1114 return writer.getBuffer().toString(); 1115 } 1116 1117 1125 public Element getAsDOM() throws Exception 1126 { 1127 return getAsDocument().getDocumentElement(); 1128 } 1129 1130 1135 public void publishToHandler(ContentHandler handler) throws SAXException 1136 { 1137 if (recorder == null) { 1138 throw new SAXException (Messages.getMessage("noRecorder00")); 1139 } 1140 1141 recorder.replay(startEventIndex, endEventIndex, handler); 1142 } 1143 1144 1149 public void publishContents(ContentHandler handler) throws SAXException 1150 { 1151 if (recorder == null) { 1152 throw new SAXException (Messages.getMessage("noRecorder00")); 1153 } 1154 1155 recorder.replay(startContentsIndex, endEventIndex-1, handler); 1156 } 1157 1158 1165 public final void output(SerializationContext outputContext) throws Exception 1166 { 1167 if ((recorder != null) && (!_isDirty)) { 1168 recorder.replay(startEventIndex, 1169 endEventIndex, 1170 new SAXOutputter(outputContext)); 1171 return; 1172 } 1173 1174 if (qNameAttrs != null) { 1176 for (int i = 0; i < qNameAttrs.size(); i++) { 1177 QNameAttr attr = (QNameAttr)qNameAttrs.get(i); 1178 QName attrName = attr.name; 1179 setAttribute(attrName.getNamespaceURI(), 1180 attrName.getLocalPart(), 1181 outputContext.qName2String(attr.value)); 1182 } 1183 } 1184 1185 1189 if (encodingStyle != null) { 1190 MessageContext mc = outputContext.getMessageContext(); 1191 SOAPConstants soapConstants = (mc != null) ? 1192 mc.getSOAPConstants() : 1193 SOAPConstants.SOAP11_CONSTANTS; 1194 if (parent == null) { 1195 if (!"".equals(encodingStyle)) { 1197 setAttribute(soapConstants.getEnvelopeURI(), 1198 Constants.ATTR_ENCODING_STYLE, 1199 encodingStyle); 1200 } 1201 } else if (!encodingStyle.equals(((MessageElement)parent).getEncodingStyle())) { 1202 setAttribute(soapConstants.getEnvelopeURI(), 1203 Constants.ATTR_ENCODING_STYLE, 1204 encodingStyle); 1205 } 1206 } 1207 1208 outputImpl(outputContext); 1209 } 1210 1211 1216 protected void outputImpl(SerializationContext outputContext) throws Exception 1217 { 1218 if (textRep != null) { 1219 boolean oldPretty = outputContext.getPretty(); 1220 outputContext.setPretty(false); 1221 if (textRep instanceof CDATASection ) { 1222 outputContext.writeString("<![CDATA["); 1223 outputContext.writeString(textRep.getData()); 1224 outputContext.writeString("]]>"); 1225 } else if (textRep instanceof Comment ) { 1226 outputContext.writeString("<!--"); 1227 outputContext.writeString(textRep.getData()); 1228 outputContext.writeString("-->"); 1229 } else if (textRep instanceof Text) { 1230 outputContext.writeSafeString(textRep.getData()); 1231 } 1232 outputContext.setPretty(oldPretty); 1233 return; 1234 } 1235 1236 if (prefix != null) 1237 outputContext.registerPrefixForURI(prefix, namespaceURI); 1238 1239 if (namespaces != null) { 1240 for (Iterator i = namespaces.iterator(); i.hasNext();) { 1241 Mapping mapping = (Mapping) i.next(); 1242 outputContext.registerPrefixForURI(mapping.getPrefix(), mapping.getNamespaceURI()); 1243 } 1244 } 1245 1246 if (objectValue != null) { 1247 outputContext.serialize(new QName (namespaceURI, name), 1248 attributes, 1249 objectValue); 1250 return; 1251 } 1252 1253 outputContext.startElement(new QName (namespaceURI, name), attributes); 1254 if (children != null) { 1255 for (Iterator it = children.iterator(); it.hasNext();) { 1256 ((NodeImpl)it.next()).output(outputContext); 1257 } 1258 } 1259 outputContext.endElement(); 1260 } 1261 1262 1270 public String toString() { 1271 try { 1272 return getAsString(); 1273 } 1274 catch( Exception exp ) { 1275 log.error(Messages.getMessage("exception00"), exp); 1278 return super.toString(); 1280 } 1281 } 1282 1283 1289 public void addMapping(Mapping map) { 1290 if (namespaces == null) { 1291 namespaces = new ArrayList (); 1292 } 1293 namespaces.add(map); 1294 } 1295 1296 1298 1305 public SOAPElement addChildElement(Name childName) throws SOAPException { 1306 MessageElement child = new MessageElement(childName.getLocalName(), 1307 childName.getPrefix(), 1308 childName.getURI()); 1309 addChild(child); 1310 return child; 1311 } 1312 1313 1320 public SOAPElement addChildElement(String localName) throws SOAPException { 1321 MessageElement child = new MessageElement(getNamespaceURI(), 1323 localName); 1324 addChild(child); 1325 return child; 1326 } 1327 1328 1336 public SOAPElement addChildElement(String localName, 1337 String prefixName) throws SOAPException { 1338 MessageElement child = new MessageElement(getNamespaceURI(prefixName), 1339 localName); 1340 child.setPrefix(prefixName); 1341 addChild(child); 1342 return child; 1343 } 1344 1345 1354 public SOAPElement addChildElement(String localName, 1355 String childPrefix, 1356 String uri) throws SOAPException { 1357 MessageElement child = new MessageElement(uri, localName); 1358 child.setPrefix(childPrefix); 1359 child.addNamespaceDeclaration(childPrefix, uri); 1360 addChild(child); 1361 return child; 1362 } 1363 1364 1370 public SOAPElement addChildElement(SOAPElement element) 1371 throws SOAPException { 1372 try { 1373 addChild((MessageElement)element); 1374 setDirty(true); 1375 return element; 1376 } catch (ClassCastException e) { 1377 throw new SOAPException (e); 1378 } 1379 } 1380 1381 1386 public SOAPElement addTextNode(String s) throws SOAPException { 1387 try { 1388 Text text = getOwnerDocument().createTextNode(s); 1389 ((org.apache.axis.message.Text)text).setParentElement(this); 1390 return this; 1391 } catch (java.lang.IncompatibleClassChangeError e) { 1392 Text text = new org.apache.axis.message.Text(s); 1393 this.appendChild(text); 1394 return this; 1395 } catch (ClassCastException e) { 1396 throw new SOAPException (e); 1397 } 1398 } 1399 1400 1408 public SOAPElement addAttribute(Name attrName, String value) 1409 throws SOAPException { 1410 try { 1411 addAttribute(attrName.getPrefix(), attrName.getURI(), attrName.getLocalName(), value); 1412 } catch (RuntimeException t) { 1413 throw new SOAPException (t); 1414 } 1415 return this; 1416 } 1417 1418 1429 public SOAPElement addNamespaceDeclaration(String prefix, 1430 String uri) 1431 throws SOAPException { 1432 try { 1433 Mapping map = new Mapping(uri, prefix); 1434 addMapping(map); 1435 } catch (RuntimeException t) { 1436 throw new SOAPException (t); 1438 } 1439 return this; 1440 } 1441 1442 1448 public String getAttributeValue(Name attrName) { 1449 return attributes.getValue(attrName.getURI(), attrName.getLocalName()); 1450 } 1451 1452 1460 public Iterator getAllAttributes() { 1461 int num = attributes.getLength(); 1462 Vector attrs = new Vector (num); 1463 for (int i = 0; i < num; i++) { 1464 String q = attributes.getQName(i); 1465 String prefix = ""; 1466 if (q != null) { 1467 int idx = q.indexOf(":"); 1468 if (idx > 0) { 1469 prefix = q.substring(0, idx); 1470 } else { 1471 prefix= ""; 1472 } 1473 } 1474 1475 attrs.add(new PrefixedQName(attributes.getURI(i), 1476 attributes.getLocalName(i), 1477 prefix)); 1478 } 1479 return attrs.iterator(); 1480 } 1481 1482 1484 1490 public Iterator getNamespacePrefixes() { 1491 Vector prefixes = new Vector (); 1492 for (int i = 0; namespaces != null && i < namespaces.size(); i++) { 1493 prefixes.add(((Mapping)namespaces.get(i)).getPrefix()); 1494 } 1495 return prefixes.iterator(); 1496 } 1497 1498 1503 public Name getElementName() { 1504 return new PrefixedQName(getNamespaceURI(), getName(), getPrefix()); 1505 } 1506 1507 1513 public boolean removeAttribute(Name attrName) { 1514 AttributesImpl attributes = makeAttributesEditable(); 1515 boolean removed = false; 1516 1517 for (int i = 0; i < attributes.getLength() && !removed; i++) { 1518 if (attributes.getURI(i).equals(attrName.getURI()) && 1519 attributes.getLocalName(i).equals(attrName.getLocalName())) { 1520 attributes.removeAttribute(i); 1521 removed = true; 1522 } 1523 } 1524 return removed; 1525 } 1526 1527 1533 public boolean removeNamespaceDeclaration(String namespacePrefix) { 1534 makeAttributesEditable(); 1535 boolean removed = false; 1536 1537 for (int i = 0; namespaces != null && i < namespaces.size() && !removed; i++) { 1538 if (((Mapping)namespaces.get(i)).getPrefix().equals(namespacePrefix)) { 1539 namespaces.remove(i); 1540 removed = true; 1541 } 1542 } 1543 return removed; 1544 } 1545 1546 1553 public Iterator getChildElements() { 1554 initializeChildren(); 1555 return children.iterator(); 1556 } 1557 1558 1565 public MessageElement getChildElement(QName qname) { 1566 if (children != null) { 1567 for (Iterator i = children.iterator(); i.hasNext();) { 1568 MessageElement child = (MessageElement) i.next(); 1569 if (child.getQName().equals(qname)) 1570 return child; 1571 } 1572 } 1573 return null; 1574 } 1575 1576 1584 public Iterator getChildElements(QName qname) { 1585 initializeChildren(); 1586 int num = children.size(); 1587 Vector c = new Vector (num); 1588 for (int i = 0; i < num; i++) { 1589 MessageElement child = (MessageElement)children.get(i); 1590 Name cname = child.getElementName(); 1591 if (cname.getURI().equals(qname.getNamespaceURI()) && 1592 cname.getLocalName().equals(qname.getLocalPart())) { 1593 c.add(child); 1594 } 1595 } 1596 return c.iterator(); 1597 } 1598 1599 1608 public Iterator getChildElements(Name childName) { 1609 return getChildElements(new QName (childName.getURI(), childName.getLocalName())); 1610 } 1611 1612 1614 1618 public String getTagName() { 1619 return prefix == null ? name : prefix + ":" + name; 1620 } 1621 1622 1628 public void removeAttribute(String attrName) throws DOMException { 1629 AttributesImpl impl = (AttributesImpl )attributes; 1630 int index = impl.getIndex(attrName); 1631 if(index >= 0){ 1632 AttributesImpl newAttrs = new AttributesImpl (); 1633 for(int i = 0; i < impl.getLength(); i++){ if(i != index){ 1636 String uri = impl.getURI(i); 1637 String local = impl.getLocalName(i); 1638 String qname = impl.getQName(i); 1639 String type = impl.getType(i); 1640 String value = impl.getValue(i); 1641 newAttrs.addAttribute(uri,local,qname,type,value); 1642 } 1643 } 1644 attributes = newAttrs; 1646 } 1647 } 1648 1649 1656 public boolean hasAttribute(String attrName) { 1657 if(attrName == null) attrName = ""; 1659 1660 for(int i = 0; i < attributes.getLength(); i++){ 1661 if(attrName.equals(attributes.getQName(i))) 1662 return true; 1663 } 1664 return false; 1665 } 1666 1667 1673 public String getAttribute(String attrName) { 1674 return attributes.getValue(attrName); 1675 } 1676 1677 1688 public void removeAttributeNS(String namespace, String localName) throws DOMException { 1689 makeAttributesEditable(); 1690 Name name = new PrefixedQName(namespace, localName, null); 1691 removeAttribute(name); 1692 } 1693 1694 1701 public void setAttribute(String name, String value) throws DOMException { 1702 AttributesImpl impl = makeAttributesEditable(); 1703 int index = impl.getIndex(name); 1704 if (index < 0) { String uri = ""; 1706 String localname = name; 1707 String qname = name; 1708 String type = "CDDATA"; 1709 impl.addAttribute(uri, localname, qname, type, value); 1710 } else { impl.setLocalName(index, value); 1712 } 1713 } 1714 1715 1722 public boolean hasAttributeNS(String namespace, String localName) { 1723 if (namespace == null) { 1724 namespace = ""; 1725 } 1726 if (localName == null) { 1728 localName = ""; 1729 } 1730 1731 for(int i = 0; i < attributes.getLength(); i++){ 1732 if( namespace.equals(attributes.getURI(i)) 1733 && localName.equals(attributes.getLocalName(i))) 1734 return true; 1735 } 1736 return false; 1737 } 1738 1739 1748 public Attr getAttributeNode(String attrName) { 1749 return null; 1750 } 1751 1752 1758 public Attr removeAttributeNode(Attr oldAttr) throws DOMException { 1759 makeAttributesEditable(); 1760 Name name = new PrefixedQName(oldAttr.getNamespaceURI(), oldAttr.getLocalName(), oldAttr.getPrefix()); 1761 removeAttribute(name); 1762 return oldAttr; 1763 } 1764 1765 1774 public Attr setAttributeNode(Attr newAttr) throws DOMException { 1775 return newAttr; 1776 } 1777 1778 1786 public Attr setAttributeNodeNS(Attr newAttr) throws DOMException { 1787 AttributesImpl attributes = makeAttributesEditable(); 1789 attributes.addAttribute(newAttr.getNamespaceURI(), 1791 newAttr.getLocalName(), 1792 newAttr.getLocalName(), 1793 "CDATA", 1794 newAttr.getValue()); 1795 return null; 1796 } 1797 1798 1803 public NodeList getElementsByTagName(String tagName) { 1804 NodeListImpl nodelist = new NodeListImpl(); 1805 for (int i = 0; children != null && i < children.size(); i++) { 1806 if (children.get(i) instanceof Node ) { 1807 Node el = (Node )children.get(i); 1808 if (el.getLocalName() != null && el.getLocalName() 1809 .equals(tagName)) 1810 nodelist.addNode(el); 1811 if (el instanceof Element ) { 1812 NodeList grandchildren = 1813 ((Element )el).getElementsByTagName(tagName); 1814 for (int j = 0; j < grandchildren.getLength(); j++) { 1815 nodelist.addNode(grandchildren.item(j)); 1816 } 1817 } 1818 } 1819 } 1820 return nodelist; 1821 } 1822 1823 1831 public String getAttributeNS(String namespaceURI, String localName) { 1832 if(namespaceURI == null) { 1833 namespaceURI = ""; 1834 } 1835 for (int i = 0; i < attributes.getLength(); i++) { 1836 if (attributes.getURI(i).equals(namespaceURI) && 1837 attributes.getLocalName(i).equals(localName)) { 1838 return attributes.getValue(i); 1839 } 1840 } 1841 return null; 1842 } 1843 1844 1852 public void setAttributeNS(String namespaceURI, String qualifiedName, 1853 String value) 1854 throws DOMException 1855 { 1856 AttributesImpl attributes = makeAttributesEditable(); 1857 String localName = qualifiedName.substring(qualifiedName.indexOf(":")+1, qualifiedName.length()); 1858 1859 if (namespaceURI == null) { 1860 namespaceURI = "intentionalNullURI"; 1861 } 1862 attributes.addAttribute(namespaceURI, 1863 localName, 1864 qualifiedName, 1865 "CDATA", 1866 value); 1867 } 1868 1869 1876 public Attr getAttributeNodeNS(String namespace, String localName) { 1877 return null; } 1879 1880 1886 public NodeList getElementsByTagNameNS(String namespace, 1887 String localName) 1888 { 1889 return getElementsNS(this,namespace,localName); 1890 } 1891 1892 1899 protected NodeList getElementsNS(org.w3c.dom.Element parentElement, 1900 String namespace, String localName) 1901 { 1902 NodeList children = parentElement.getChildNodes(); 1903 NodeListImpl matches = new NodeListImpl(); 1904 1905 for (int i = 0; i < children.getLength(); i++) { 1906 if (children.item(i) instanceof Text) { 1907 continue; 1908 } 1909 Element child = (Element ) children.item(i); 1910 if (namespace.equals(child.getNamespaceURI()) && 1911 localName.equals(child.getLocalName())) { 1912 matches.addNode(child); 1913 } 1914 matches.addNodeList(child.getElementsByTagNameNS(namespace, 1916 localName)); 1917 } 1918 return matches; 1919 } 1920 1921 1927 public Node item(int index) { 1928 if (children != null && children.size() > index) { 1929 return (Node ) children.get(index); 1930 } else { 1931 return null; 1932 } 1933 } 1934 1935 1942 public int getLength() 1943 { 1944 return (children == null) ? 0 : children.size(); 1945 } 1946 1947 1949 1951 protected MessageElement findElement(Vector vec, String namespace, 1952 String localPart) 1953 { 1954 if (vec.isEmpty()) { 1955 return null; 1956 } 1957 1958 QName qname = new QName (namespace, localPart); 1959 Enumeration e = vec.elements(); 1960 MessageElement element; 1961 while (e.hasMoreElements()) { 1962 element = (MessageElement) e.nextElement(); 1963 if (element.getQName().equals(qname)) { 1964 return element; 1965 } 1966 } 1967 1968 return null; 1969 } 1970 1971 1978 public boolean equals(Object obj) 1979 { 1980 if (obj == null || !(obj instanceof MessageElement)) { 1981 return false; 1982 } 1983 if (this == obj) { 1984 return true; 1985 } 1986 if (!this.getLocalName().equals(((MessageElement) obj).getLocalName())) { 1987 return false; 1988 } 1989 return toString().equals(obj.toString()); 1990 } 1991 1992 1997 private void copyNode(org.w3c.dom.Node element) { 1998 copyNode(this, element); 1999 } 2000 2001 2006 private void copyNode(MessageElement dest, org.w3c.dom.Node source) 2007 { 2008 dest.setPrefix(source.getPrefix()); 2009 if(source.getLocalName() != null) { 2010 dest.setQName(new QName (source.getNamespaceURI(), source.getLocalName())); 2011 } 2012 else 2013 { 2014 dest.setQName(new QName (source.getNamespaceURI(), source.getNodeName())); 2015 } 2016 2017 NamedNodeMap attrs = source.getAttributes(); 2018 for(int i = 0; i < attrs.getLength(); i++){ 2019 Node att = attrs.item(i); 2020 if (att.getNamespaceURI() != null && 2021 att.getPrefix() != null && 2022 att.getNamespaceURI().equals(Constants.NS_URI_XMLNS) && 2023 "xmlns".equals(att.getPrefix())) { 2024 Mapping map = new Mapping(att.getNodeValue(), att.getLocalName()); 2025 dest.addMapping(map); 2026 } 2027 if(att.getLocalName() != null) { 2028 dest.addAttribute(att.getPrefix(), 2029 (att.getNamespaceURI() != null ? att.getNamespaceURI() : ""), 2030 att.getLocalName(), 2031 att.getNodeValue()); 2032 } else if (att.getNodeName() != null) { 2033 dest.addAttribute(att.getPrefix(), 2034 (att.getNamespaceURI() != null ? att.getNamespaceURI() : ""), 2035 att.getNodeName(), 2036 att.getNodeValue()); 2037 } 2038 } 2039 2040 NodeList children = source.getChildNodes(); 2041 for(int i = 0; i < children.getLength(); i++){ 2042 Node child = children.item(i); 2043 if(child.getNodeType()==TEXT_NODE || 2044 child.getNodeType()==CDATA_SECTION_NODE || 2045 child.getNodeType()==COMMENT_NODE ) { 2046 org.apache.axis.message.Text childElement = 2047 new org.apache.axis.message.Text((CharacterData )child); 2048 dest.appendChild(childElement); 2049 } else { 2050 MessageElement childElement = new MessageElement(); 2051 dest.appendChild(childElement); 2052 copyNode(childElement, child); 2053 } 2054 } 2055 } 2056 2057 2065 public String getValue() { 2066 2083 2084 if (textRep != null) { 2085 return textRep.getNodeValue(); 2087 } 2088 2089 if (objectValue != null) { 2090 return getValueDOM(); 2091 } 2092 2093 for (Iterator i = getChildElements(); i.hasNext(); ) { 2094 NodeImpl n = (NodeImpl) i.next(); 2095 if (n instanceof Text) { 2096 return ((Text)n).getNodeValue(); 2097 } 2098 } 2099 2100 return null; 2101 } 2102 2103 protected String getValueDOM() { 2104 try { 2105 Element element = getAsDOM(); 2106 if (element.hasChildNodes()) { 2107 Node node = element.getFirstChild(); 2108 if (node.getNodeType() == Node.TEXT_NODE) { 2109 return node.getNodeValue(); 2110 } 2111 } 2112 } catch (Exception t) { 2113 log.debug("getValue()", t); 2114 } 2115 return null; 2116 } 2117 2118 public void setValue( String value ) 2119 { 2120 if (children==null) { 2122 try { 2123 setObjectValue(value); 2124 } catch ( SOAPException soape ) { 2125 log.debug("setValue()", soape); 2126 } 2127 } 2128 super.setValue(value); 2129 } 2130 2131 public Document getOwnerDocument() { 2132 Document doc = null; 2133 if (context != null && context.getEnvelope() != null && 2134 context.getEnvelope().getOwnerDocument() != null) { 2135 doc = context.getEnvelope().getOwnerDocument(); 2136 } 2137 if(doc == null) { 2138 doc = super.getOwnerDocument(); 2139 } 2140 if (doc == null) { 2141 doc = new SOAPDocumentImpl(null); 2142 } 2143 return doc; 2144 } 2145} 2146 | Popular Tags |