1 55 56 package org.jboss.axis.encoding; 57 58 import org.jboss.axis.AxisEngine; 59 import org.jboss.axis.Constants; 60 import org.jboss.axis.Handler; 61 import org.jboss.axis.Message; 62 import org.jboss.axis.MessageContext; 63 import org.jboss.axis.attachments.Attachments; 64 import org.jboss.axis.client.Call; 65 import org.jboss.axis.description.OperationDesc; 66 import org.jboss.axis.description.TypeDesc; 67 import org.jboss.axis.encoding.ser.BaseSerializerFactory; 68 import org.jboss.axis.encoding.ser.JAFDataHandlerSerializer; 69 import org.jboss.axis.enums.Use; 70 import org.jboss.axis.handlers.soap.SOAPService; 71 import org.jboss.axis.schema.SchemaVersion; 72 import org.jboss.axis.soap.SOAPConstants; 73 import org.jboss.axis.types.HexBinary; 74 import org.jboss.axis.utils.IDKey; 75 import org.jboss.axis.utils.JavaUtils; 76 import org.jboss.axis.utils.Mapping; 77 import org.jboss.axis.utils.Messages; 78 import org.jboss.axis.utils.NSStack; 79 import org.jboss.axis.utils.XMLUtils; 80 import org.jboss.axis.wsdl.symbolTable.SchemaUtils; 81 import org.jboss.axis.wsdl.symbolTable.SymbolTable; 82 import org.jboss.logging.Logger; 83 import org.w3c.dom.Attr ; 84 import org.w3c.dom.CDATASection ; 85 import org.w3c.dom.CharacterData ; 86 import org.w3c.dom.Comment ; 87 import org.w3c.dom.Element ; 88 import org.w3c.dom.NamedNodeMap ; 89 import org.w3c.dom.Node ; 90 import org.w3c.dom.NodeList ; 91 import org.w3c.dom.Text ; 92 import org.xml.sax.Attributes ; 93 import org.xml.sax.helpers.AttributesImpl ; 94 95 import javax.xml.namespace.QName ; 96 import javax.xml.rpc.JAXRPCException ; 97 import javax.xml.rpc.holders.QNameHolder ; 98 import javax.xml.soap.SOAPMessage ; 99 import java.io.IOException ; 100 import java.io.Writer ; 101 import java.lang.reflect.Method ; 102 import java.util.ArrayList ; 103 import java.util.Calendar ; 104 import java.util.Date ; 105 import java.util.HashMap ; 106 import java.util.HashSet ; 107 import java.util.Iterator ; 108 import java.util.Stack ; 109 110 117 public class SerializationContextImpl implements SerializationContext 118 { 119 private static Logger log = Logger.getLogger(SerializationContextImpl.class.getName()); 120 121 private NSStack nsStack = new NSStack(); 122 private boolean writingStartTag = false; 123 private boolean onlyXML = true; 124 private int indent = 0; 125 private boolean startOfDocument = true; 126 private Writer writer; 127 private int lastPrefixIndex = 1; 128 private MessageContext msgContext; 129 private QName currentXMLType; 130 private Stack elementStack = new Stack(); 132 133 136 private SOAPConstants soapConstants = SOAPConstants.SOAP11_CONSTANTS; 137 138 private boolean pretty; 139 private static QName multirefQName = new QName ("", "multiRef"); 140 private static Class [] getSerializerClasses = 141 new Class []{String .class, Class .class, QName .class}; 142 143 151 private boolean doMultiRefs = false; 152 153 156 private boolean sendXMLDecl = true; 157 158 161 private boolean sendXSIType = true; 162 163 167 private boolean sendMinimizedElements = true; 168 169 173 private HashMap multiRefValues = null; 174 private int multiRefIndex = -1; 175 176 private boolean noNamespaceMappings = true; 177 178 private boolean noDefaultNamespace; 182 183 class MultiRefItem 184 { 185 String id; 186 QName xmlType; 187 Boolean sendType; 188 Object value; 189 190 MultiRefItem(String id, 191 QName xmlType, 192 Boolean sendType, Object value) 193 { 194 this.id = id; 195 this.xmlType = xmlType; 196 this.sendType = sendType; 197 this.value = value; 198 } 199 200 } 201 202 213 private HashSet secondLevelObjects = null; 214 private Object forceSer = null; 215 private boolean outputMultiRefsFlag = false; 216 217 220 SchemaVersion schemaVersion = SchemaVersion.SCHEMA_2001; 221 222 226 HashMap preferredPrefixes = new HashMap (); 227 228 233 public SerializationContextImpl(Writer writer) 234 { 235 this.writer = writer; 236 initialize(); 237 } 238 239 private void initialize() 240 { 241 preferredPrefixes.put(soapConstants.getEncodingURI(), 244 Constants.NS_PREFIX_SOAP_ENC); 245 preferredPrefixes.put(Constants.NS_URI_XML, 246 Constants.NS_PREFIX_XML); 247 preferredPrefixes.put(schemaVersion.getXsdURI(), 248 Constants.NS_PREFIX_SCHEMA_XSD); 249 preferredPrefixes.put(schemaVersion.getXsiURI(), 250 Constants.NS_PREFIX_SCHEMA_XSI); 251 } 252 253 254 260 public SerializationContextImpl(Writer writer, MessageContext msgContext) 261 { 262 this.writer = writer; 263 this.msgContext = msgContext; 264 265 Handler optionSource = null; 266 if (msgContext != null) 267 { 268 soapConstants = msgContext.getSOAPConstants(); 269 270 if (optionSource == null) 272 optionSource = msgContext.getAxisEngine(); 273 274 schemaVersion = msgContext.getSchemaVersion(); 276 277 Boolean shouldSendDecl = (Boolean )optionSource.getOption(AxisEngine.PROP_XML_DECL); 278 if (shouldSendDecl != null) 279 sendXMLDecl = shouldSendDecl.booleanValue(); 280 281 Boolean shouldSendMultiRefs = 282 (Boolean )msgContext.getProperty(AxisEngine.PROP_DOMULTIREFS); 283 284 if (shouldSendMultiRefs == null) 285 shouldSendMultiRefs = 286 (Boolean )optionSource.getOption(AxisEngine.PROP_DOMULTIREFS); 287 288 if (shouldSendMultiRefs != null) 289 doMultiRefs = shouldSendMultiRefs.booleanValue(); 290 291 Boolean shouldSendMinimized = 292 (Boolean )optionSource.getOption(AxisEngine.PROP_SEND_MINIMIZED_ELEMENTS); 293 294 if (shouldSendMinimized != null) 295 sendMinimizedElements = shouldSendMinimized.booleanValue(); 296 297 if (!msgContext.isPropertyTrue(Call.SEND_TYPE_ATTR, true)) 301 sendXSIType = false; 302 303 Boolean opt = (Boolean )optionSource.getOption(AxisEngine.PROP_SEND_XSI); 304 if ((opt != null) && (opt.equals(Boolean.FALSE))) 305 { 306 sendXSIType = false; 307 } 308 309 OperationDesc operation = msgContext.getOperation(); 312 if (operation != null) 313 { 314 if (operation.getUse() != Use.ENCODED) 315 { 316 sendXSIType = false; 317 doMultiRefs = false; 318 } 319 } 320 else 321 { 322 SOAPService service = msgContext.getService(); 324 if (service != null) 325 { 326 if (service.getUse() != Use.ENCODED) 327 { 328 sendXSIType = false; 329 doMultiRefs = false; 330 } 331 } 332 } 333 } 334 335 initialize(); 337 } 338 339 344 public boolean getPretty() 345 { 346 return pretty; 347 } 348 349 354 public void setPretty(boolean pretty) 355 { 356 this.pretty = pretty; 357 } 358 359 364 public boolean getDoMultiRefs() 365 { 366 return doMultiRefs; 367 } 368 369 372 public void setDoMultiRefs(boolean shouldDo) 373 { 374 doMultiRefs = shouldDo; 375 } 376 377 382 public void setSendDecl(boolean sendDecl) 383 { 384 sendXMLDecl = sendDecl; 385 } 386 387 392 public boolean shouldSendXSIType() 393 { 394 return sendXSIType; 395 } 396 397 402 public boolean isNoDefaultNamespace() 403 { 404 return noDefaultNamespace; 405 } 406 407 410 public void setNoDefaultNamespace(boolean noDefaultNamespace) 411 { 412 this.noDefaultNamespace = noDefaultNamespace; 413 } 414 415 420 public TypeMapping getTypeMapping() 421 { 422 if (msgContext == null) 424 return DefaultTypeMappingImpl.getSingleton(); 425 426 String encodingStyle = msgContext.getEncodingStyle(); 427 if (encodingStyle == null) 428 encodingStyle = soapConstants.getEncodingURI(); 429 return (TypeMapping)msgContext. 430 getTypeMappingRegistry().getTypeMapping(encodingStyle); 431 } 432 433 438 public TypeMappingRegistry getTypeMappingRegistry() 439 { 440 if (msgContext == null) 441 return null; 442 return msgContext.getTypeMappingRegistry(); 443 } 444 445 455 public String getPrefixForURI(String uri) 456 { 457 return getPrefixForURI(uri, null, false); 458 } 459 460 466 public String getPrefixForURI(String uri, String defaultPrefix) 467 { 468 return getPrefixForURI(uri, defaultPrefix, false); 469 } 470 471 477 public String getPrefixForURI(String uri, String defaultPrefix, boolean attribute) 478 { 479 if ((uri == null) || (uri.length() == 0)) 480 return null; 481 482 String prefix = nsStack.getPrefix(uri, attribute); 485 486 if (prefix == null) 487 { 488 prefix = (String )preferredPrefixes.get(uri); 489 490 if (prefix == null) 491 { 492 if (defaultPrefix == null) 493 { 494 prefix = "ns" + lastPrefixIndex++; 495 } 496 else 497 { 498 prefix = defaultPrefix; 499 } 500 } 501 502 registerPrefixForURI(prefix, uri); 503 } 504 505 return prefix; 506 } 507 508 514 public void registerPrefixForURI(String prefix, String uri) 515 { 516 if (log.isDebugEnabled()) 517 log.debug("registerPrefixForURI(" + prefix + ", " + uri + ")"); 518 519 if ("".equals(uri)) 521 { 522 log.warn("Ignoring invalid namespace mapping: [prefix=" + prefix + ",uri=" + uri + "]"); 523 return; 524 } 525 526 if ((uri != null) && (prefix != null)) 527 { 528 if (noNamespaceMappings) 529 { 530 nsStack.push(); 531 noNamespaceMappings = false; 532 } 533 nsStack.add(uri, prefix); 534 } 535 } 536 537 540 public Message getCurrentMessage() 541 { 542 if (msgContext == null) 543 return null; 544 return msgContext.getCurrentMessage(); 545 } 546 547 550 public MessageContext getMessageContext() 551 { 552 return msgContext; 553 } 554 555 561 public String qName2String(QName qName, boolean writeNS) 562 { 563 String prefix = null; 564 String namespaceURI = qName.getNamespaceURI(); 565 566 if (namespaceURI.length() == 0) 567 { 568 if (writeNS) 569 { 570 String defaultNS = nsStack.getNamespaceURI(""); 573 if (defaultNS != null && defaultNS.length() > 0) 574 { 575 registerPrefixForURI("", ""); 576 } 577 } 578 } 579 else 580 { 581 prefix = getPrefixForURI(namespaceURI); 582 } 583 584 if ((prefix == null) || (prefix.length() == 0)) 585 return qName.getLocalPart(); 586 587 StringBuffer sb = new StringBuffer (prefix); 588 sb.append(':'); 589 sb.append(qName.getLocalPart()); 590 return sb.toString(); 591 } 592 593 public String qName2String(QName qName) 594 { 595 return qName2String(qName, false); 596 } 597 598 607 public String attributeQName2String(QName qName) 608 { 609 String prefix = null; 610 611 if (qName.getNamespaceURI().length() > 0) 612 { 613 prefix = getPrefixForURI(qName.getNamespaceURI(), null, true); 614 } 615 616 if ((prefix == null) || (prefix.length() == 0)) 617 return qName.getLocalPart(); 618 619 StringBuffer sb = new StringBuffer (prefix); 620 sb.append(':'); 621 sb.append(qName.getLocalPart()); 622 return sb.toString(); 623 } 624 625 631 public QName getQNameForClass(Class cls) 632 { 633 return getTypeMapping().getTypeQName(cls); 634 } 635 636 645 public boolean isPrimitive(Object value) 646 { 647 if (value == null) return true; 648 649 Class javaType = value.getClass(); 650 651 if (javaType.isPrimitive()) return true; 652 653 if (javaType == String .class) return true; 654 if (Calendar .class.isAssignableFrom(javaType)) return true; 655 if (Date .class.isAssignableFrom(javaType)) return true; 656 if (HexBinary.class.isAssignableFrom(javaType)) return true; 657 if (Element .class.isAssignableFrom(javaType)) return true; 658 if (javaType == byte[].class) return true; 659 660 if (javaType.isArray()) return true; 677 678 QName qName = getQNameForClass(javaType); 683 if (qName != null && Constants.isSchemaXSD(qName.getNamespaceURI())) 684 { 685 if (SchemaUtils.isSimpleSchemaType(qName)) 686 { 687 return true; 688 } 689 } 690 691 return false; 692 } 693 694 714 public void serialize(QName elemQName, 715 Attributes attributes, 716 Object value) 717 throws IOException 718 { 719 serialize(elemQName, attributes, value, null, true, null); 720 } 721 722 743 public void serialize(QName elemQName, 744 Attributes attributes, 745 Object value, 746 QName xmlType, 747 boolean sendNull, 748 Boolean sendType) 749 throws IOException 750 { 751 boolean shouldSendType = (sendType == null) ? shouldSendXSIType() : 752 sendType.booleanValue(); 753 754 if (value == null) 755 { 756 if (sendNull) 759 { 760 AttributesImpl attrs = new AttributesImpl (); 761 if (attributes != null && 0 < attributes.getLength()) 762 attrs.setAttributes(attributes); 763 if (shouldSendType) 764 attrs = (AttributesImpl )setTypeAttribute(attrs, xmlType); 765 String nil = schemaVersion.getNilQName().getLocalPart(); 766 attrs.addAttribute(schemaVersion.getXsiURI(), nil, "xsi:" + nil, 767 "CDATA", "1"); 768 startElement(elemQName, attrs); 769 endElement(); 770 } 771 return; 772 } 773 774 Message msg = getCurrentMessage(); 775 if (null != msg) 776 { 777 Attachments attachments = msg.getAttachmentsImpl(); 779 780 if (null != attachments) 781 { 782 if (attachments.isAttachment(value) || getSerializer(value.getClass(), xmlType, null) instanceof JAFDataHandlerSerializer) 784 { 785 serializeActual(elemQName, attributes, value, xmlType, sendType); 788 789 return; 792 } 793 } 794 } 795 796 801 810 if (doMultiRefs && (msgContext == null || msgContext.isEncoded()) && 811 (value != forceSer) && !isPrimitive(value)) 812 { 813 if (multiRefIndex == -1) 814 multiRefValues = new HashMap (); 815 816 String id; 817 818 MultiRefItem mri = (MultiRefItem)multiRefValues.get(getIdentityKey(value)); 820 if (mri == null) 821 { 822 multiRefIndex++; 825 id = "id" + multiRefIndex; 826 mri = new MultiRefItem(id, xmlType, sendType, value); 827 multiRefValues.put(getIdentityKey(value), mri); 828 829 833 if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) 834 { 835 AttributesImpl attrs = new AttributesImpl (); 836 if (attributes != null && 0 < attributes.getLength()) 837 attrs.setAttributes(attributes); 838 attrs.addAttribute("", Constants.ATTR_ID, "id", "CDATA", 839 id); 840 serializeActual(elemQName, attrs, value, xmlType, sendType); 841 return; 842 } 843 844 845 858 if (outputMultiRefsFlag) 859 { 860 if (secondLevelObjects == null) 861 secondLevelObjects = new HashSet (); 862 secondLevelObjects.add(getIdentityKey(value)); 863 } 864 } 865 else 866 { 867 id = mri.id; 869 } 870 871 AttributesImpl attrs = new AttributesImpl (); 873 if (attributes != null && 0 < attributes.getLength()) 874 attrs.setAttributes(attributes); 875 attrs.addAttribute("", soapConstants.getAttrHref(), soapConstants.getAttrHref(), 876 "CDATA", '#' + id); 877 878 startElement(elemQName, attrs); 879 endElement(); 880 return; 881 } 882 883 if (value == forceSer) 889 forceSer = null; 890 891 serializeActual(elemQName, attributes, value, xmlType, sendType); 893 } 894 895 904 private IDKey getIdentityKey(Object value) 905 { 906 return new IDKey(value); 907 } 908 909 914 public void outputMultiRefs() throws IOException 915 { 916 if (!doMultiRefs || (multiRefValues == null) || 917 soapConstants == SOAPConstants.SOAP12_CONSTANTS) 918 return; 919 outputMultiRefsFlag = true; 920 AttributesImpl attrs = new AttributesImpl (); 921 attrs.addAttribute("", "", "", "", ""); 922 923 String encodingURI = soapConstants.getEncodingURI(); 924 String prefix = getPrefixForURI(encodingURI); 926 String root = prefix + ":root"; 927 attrs.addAttribute(encodingURI, Constants.ATTR_ROOT, root, 928 "CDATA", "0"); 929 930 addEncodingStyleAttribute(attrs); 932 933 HashSet keys = new HashSet (); 936 keys.addAll(multiRefValues.keySet()); 937 Iterator i = keys.iterator(); 938 while (i.hasNext()) 939 { 940 while (i.hasNext()) 941 { 942 Object val = i.next(); 943 MultiRefItem mri = (MultiRefItem)multiRefValues.get(val); 944 attrs.setAttribute(0, "", Constants.ATTR_ID, "id", "CDATA", 945 mri.id); 946 947 forceSer = mri.value; 948 949 serialize(multirefQName, attrs, mri.value, 954 mri.xmlType, 955 true, 956 Boolean.TRUE); } 958 959 if (secondLevelObjects != null) 964 { 965 i = secondLevelObjects.iterator(); 966 secondLevelObjects = null; 967 } 968 } 969 970 forceSer = null; 972 outputMultiRefsFlag = false; 973 multiRefValues = null; 974 multiRefIndex = -1; 975 secondLevelObjects = null; 976 } 977 978 982 private void addEncodingStyleAttribute(AttributesImpl attrs) 983 { 984 String encodingStyle; 985 986 if (msgContext != null) 987 { 988 encodingStyle = msgContext.getEncodingStyle(); 989 } 990 else 991 { 992 encodingStyle = soapConstants.getEncodingURI(); 993 } 994 995 String encStyle = getPrefixForURI(soapConstants.getEnvelopeURI()) + ':' + Constants.ATTR_ENCODING_STYLE; 996 attrs.addAttribute(soapConstants.getEnvelopeURI(), Constants.ATTR_ENCODING_STYLE, encStyle, "CDATA", encodingStyle); 997 } 998 999 public void startDocument() throws IOException 1000 { 1001 if (startOfDocument && sendXMLDecl) 1002 { 1003 writer.write("<?xml version=\"1.0\" encoding=\""); 1004 String encoding = null; 1008 if (msgContext != null) 1009 { 1010 encoding = (String )msgContext.getProperty(SOAPMessage.CHARACTER_SET_ENCODING); 1011 } 1012 if (encoding == null) 1013 { 1014 encoding = XMLUtils.getEncoding(); 1015 } 1016 writer.write(encoding); 1017 writer.write("\"?>\n"); 1018 startOfDocument = false; 1019 } 1020 } 1021 1022 public void endDocument() throws IOException 1023 { 1024 } 1025 1026 1033 public void startElement(QName qName, Attributes attributes) 1034 throws IOException 1035 { 1036 ArrayList vecQNames = new ArrayList (); 1037 if (log.isDebugEnabled()) 1038 { 1039 log.debug(Messages.getMessage("startElem00", 1040 "[" + qName.getNamespaceURI() + "]:" + qName.getLocalPart())); 1041 } 1042 1043 if (writingStartTag) 1044 { 1045 writer.write('>'); 1046 if (pretty) writer.write(JavaUtils.LS); 1047 indent++; 1048 } 1049 1050 if (pretty) for (int i = 0; i < indent; i++) writer.write(' '); 1051 String elementQName = qName2String(qName, true); 1052 writer.write('<'); 1053 1054 writer.write(elementQName); 1055 1056 if (attributes != null) 1057 { 1058 for (int i = 0; i < attributes.getLength(); i++) 1059 { 1060 String qname = attributes.getQName(i); 1061 writer.write(' '); 1062 1063 String prefix = ""; 1064 String uri = attributes.getURI(i); 1065 if (uri != null && uri.length() > 0) 1066 { 1067 if (qname.length() == 0) 1068 { 1069 prefix = getPrefixForURI(uri); 1071 } 1072 else 1073 { 1074 int idx = qname.indexOf(':'); 1076 if (idx > -1) 1077 { 1078 prefix = qname.substring(0, idx); 1079 prefix = getPrefixForURI(uri, prefix, true); 1080 } 1081 1082 if (uri.equals(Constants.NS_URI_XMLNS)) 1083 { 1084 prefix = "xmlns"; 1085 } 1086 } 1087 1088 if (prefix.length() > 0) 1089 { 1090 qname = prefix + ':' + attributes.getLocalName(i); 1091 } 1092 else 1093 { 1094 qname = attributes.getLocalName(i); 1095 } 1096 } 1097 else 1098 { 1099 qname = attributes.getQName(i); 1100 if (qname.length() == 0) 1101 qname = attributes.getLocalName(i); 1102 } 1103 1104 if (qname.startsWith("xmlns")) 1105 { 1106 vecQNames.add(qname); 1107 } 1108 writer.write(qname); 1109 writer.write("=\""); 1110 writer.write(XMLUtils.xmlEncodeString(attributes.getValue(i))); 1111 writer.write('"'); 1112 } 1113 } 1114 1115 if (noNamespaceMappings) 1116 { 1117 nsStack.push(); 1118 } 1119 else 1120 { 1121 for (Mapping map = nsStack.topOfFrame(); map != null; map = nsStack.next()) 1122 { 1123 StringBuffer sb = new StringBuffer ("xmlns"); 1124 boolean isDefaultNS = map.getPrefix().equals(""); 1125 if (!isDefaultNS || !isNoDefaultNamespace()) 1126 { 1127 if (!isDefaultNS) 1128 { 1129 sb.append(':'); 1130 sb.append(map.getPrefix()); 1131 } 1132 1133 if (vecQNames.indexOf(sb.toString()) == -1) 1134 { 1135 writer.write(' '); 1136 sb.append("=\""); 1137 sb.append(map.getNamespaceURI()); 1138 sb.append('"'); 1139 writer.write(sb.toString()); 1140 } 1141 } 1142 } 1143 1144 noNamespaceMappings = true; 1145 } 1146 1147 writingStartTag = true; 1148 1149 elementStack.push(qName); 1150 1151 onlyXML = true; 1152 } 1153 1154 1157 public void endElement() 1158 throws IOException 1159 { 1160 QName qname = (QName )elementStack.pop(); 1161 String elementQName = qName2String(qname, true); 1162 1163 if (log.isDebugEnabled()) 1164 { 1165 log.debug(Messages.getMessage("endElem00", "" + elementQName)); 1166 } 1167 1168 nsStack.pop(); 1169 1170 if (writingStartTag) 1171 { 1172 if (sendMinimizedElements) 1173 { 1174 writer.write("/>"); 1175 } 1176 else 1177 { 1178 writer.write("></"); 1179 writer.write(elementQName); 1180 writer.write('>'); 1181 } 1182 if (pretty) writer.write(JavaUtils.LS); 1183 writingStartTag = false; 1184 return; 1185 } 1186 1187 if (onlyXML) 1188 { 1189 indent--; 1190 if (pretty) for (int i = 0; i < indent; i++) writer.write(' '); 1191 } 1192 writer.write("</"); 1193 writer.write(elementQName); 1194 writer.write('>'); 1195 if (pretty) if (indent > 0) writer.write(JavaUtils.LS); 1196 onlyXML = true; 1197 } 1198 1199 1207 public void writeChars(char[] p1, int p2, int p3) 1208 throws IOException 1209 { 1210 if (writingStartTag) 1211 { 1212 writer.write('>'); 1213 writingStartTag = false; 1214 } 1215 writeSafeString(String.valueOf(p1, p2, p3)); 1216 onlyXML = false; 1217 } 1218 1219 1224 public void writeString(String string) 1225 throws IOException 1226 { 1227 if (writingStartTag) 1228 { 1229 writer.write('>'); 1230 writingStartTag = false; 1231 } 1232 writer.write(string); 1233 onlyXML = false; 1234 } 1235 1236 1242 public void writeSafeString(String string) 1243 throws IOException 1244 { 1245 writeString(XMLUtils.xmlEncodeString(string)); 1246 } 1247 1248 1253 public void writeDOMElement(Element el) 1254 throws IOException 1255 { 1256 AttributesImpl attributes = null; 1257 NamedNodeMap attrMap = el.getAttributes(); 1258 1259 if (attrMap.getLength() > 0) 1260 { 1261 attributes = new AttributesImpl (); 1262 for (int i = 0; i < attrMap.getLength(); i++) 1263 { 1264 Attr attr = (Attr )attrMap.item(i); 1265 String tmp = attr.getNamespaceURI(); 1266 if (tmp != null && tmp.equals(Constants.NS_URI_XMLNS)) 1267 { 1268 String prefix = attr.getLocalName(); 1269 if (prefix != null) 1270 { 1271 if (prefix.equals("xmlns")) 1272 prefix = ""; 1273 String nsURI = attr.getValue(); 1274 registerPrefixForURI(prefix, nsURI); 1275 } 1276 continue; 1277 } 1278 1279 attributes.addAttribute(attr.getNamespaceURI(), 1280 attr.getLocalName(), 1281 attr.getName(), 1282 "CDATA", attr.getValue()); 1283 } 1284 } 1285 1286 String namespaceURI = el.getNamespaceURI(); 1287 String localPart = el.getLocalName(); 1288 if (localPart == null) 1289 localPart = el.getNodeName(); 1290 1291 QName qName = new QName (namespaceURI, localPart); 1292 1293 startElement(qName, attributes); 1294 1295 NodeList children = el.getChildNodes(); 1296 for (int i = 0; i < children.getLength(); i++) 1297 { 1298 Node child = children.item(i); 1299 if (child instanceof Element ) 1300 { 1301 writeDOMElement((Element )child); 1302 } 1303 else if (child instanceof CDATASection ) 1304 { 1305 writeString("<![CDATA["); 1306 writeString(((Text )child).getData()); 1307 writeString("]]>"); 1308 } 1309 else if (child instanceof Comment ) 1310 { 1311 writeString("<!--"); 1312 writeString(((CharacterData )child).getData()); 1313 writeString("-->"); 1314 } 1315 else if (child instanceof Text ) 1316 { 1317 writeSafeString(((Text )child).getData()); 1318 } 1319 } 1320 1321 endElement(); 1322 } 1323 1324 1331 public final Serializer getSerializerForJavaType(Class javaType) 1332 { 1333 SerializerFactory serF = null; 1334 Serializer ser = null; 1335 try 1336 { 1337 serF = (SerializerFactory)getTypeMapping().getSerializer(javaType); 1338 if (serF != null) 1339 { 1340 ser = (Serializer)serF.getSerializerAs(Constants.AXIS_SAX); 1341 } 1342 } 1343 catch (JAXRPCException e) 1344 { 1345 } 1346 1347 return ser; 1348 } 1349 1350 1357 public Attributes setTypeAttribute(Attributes attributes, QName type) 1358 { 1359 if (type == null || 1360 type.getLocalPart().indexOf(SymbolTable.ANON_TOKEN) >= 0 || 1361 ((attributes != null) && 1362 (attributes.getIndex(Constants.URI_DEFAULT_SCHEMA_XSI, 1363 "type") != -1))) 1364 return attributes; 1365 1366 AttributesImpl attrs = new AttributesImpl (); 1367 if (attributes != null && 0 < attributes.getLength()) 1368 attrs.setAttributes(attributes); 1369 1370 String prefix = getPrefixForURI(Constants.URI_DEFAULT_SCHEMA_XSI, 1371 "xsi"); 1372 1373 attrs.addAttribute(Constants.URI_DEFAULT_SCHEMA_XSI, 1374 "type", 1375 prefix + ":type", 1376 "CDATA", attributeQName2String(type)); 1377 return attrs; 1378 } 1379 1380 1390 private void serializeActual(QName elemQName, 1391 Attributes attributes, 1392 Object value, 1393 QName xmlType, 1394 Boolean sendType) 1395 throws IOException 1396 { 1397 boolean shouldSendType = (sendType == null) ? shouldSendXSIType() : 1398 sendType.booleanValue(); 1399 1400 if (value != null) 1401 { 1402 Class javaType = value.getClass(); 1403 TypeMapping tm = getTypeMapping(); 1404 1405 if (tm == null) 1406 { 1407 throw new IOException (Messages.getMessage("noSerializer00", 1408 value.getClass().getName(), 1409 "" + this)); 1410 } 1411 1412 currentXMLType = xmlType; 1416 1417 if (Constants.equals(Constants.XSD_ANYTYPE, xmlType)) 1419 { 1420 xmlType = null; 1421 shouldSendType = true; 1422 } 1423 1424 QNameHolder actualXMLType = new QNameHolder (); 1426 Serializer ser = getSerializer(javaType, xmlType, actualXMLType); 1427 1428 if (ser != null) 1429 { 1430 if (shouldSendType || (xmlType != null && !xmlType.equals(actualXMLType.value))) 1434 { 1435 if (msgContext != null && msgContext.isEncoded()) 1438 attributes = setTypeAttribute(attributes, actualXMLType.value); 1439 } 1440 1441 ser.serialize(elemQName, attributes, value, this); 1454 return; 1455 } 1456 1457 try 1460 { 1461 Method method = value.getClass().getMethod("getSerializer", getSerializerClasses); 1462 if (method != null) 1463 { 1464 Serializer serializer = (Serializer)method.invoke(value, 1465 new Object []{"", value.getClass(), elemQName}); 1466 TypeDesc typedesc = TypeDesc.getTypeDescForClass(value.getClass()); 1467 if (typedesc != null) 1468 { 1469 QName qname = typedesc.getXmlType(); 1470 if (qname != null) 1471 { 1472 attributes = setTypeAttribute(attributes, 1473 qname); 1474 } 1475 } 1476 serializer.serialize(elemQName, attributes, value, this); 1477 return; 1478 } 1479 } 1480 catch (Exception e) 1481 { 1482 } 1483 1484 throw new IOException (Messages.getMessage("noSerializer00", 1485 value.getClass().getName(), "" + tm)); 1486 } 1487 } 1489 1490 1495 public QName getCurrentXMLType() 1496 { 1497 return currentXMLType; 1498 } 1499 1500 1504 private SerializerFactory getSerializerFactoryFromInterface(Class javaType, 1505 QName xmlType, 1506 TypeMapping tm) 1507 { 1508 SerializerFactory serFactory = null; 1509 Class [] interfaces = javaType.getInterfaces(); 1510 if (interfaces != null) 1511 { 1512 for (int i = 0; i < interfaces.length; i++) 1513 { 1514 Class iface = interfaces[i]; 1515 serFactory = (SerializerFactory)tm.getSerializer(iface, 1516 xmlType); 1517 if (serFactory == null) 1518 serFactory = getSerializerFactoryFromInterface(iface, xmlType, tm); 1519 if (serFactory != null) 1520 break; 1521 1522 } 1523 } 1524 return serFactory; 1525 } 1526 1527 1537 private Serializer getSerializer(Class javaType, QName xmlType, QNameHolder actualXMLType) 1538 { 1539 1540 log.debug("Enter:getSerializer: [class=" + javaType + ",xmlType=" + xmlType + "]"); 1541 1542 SerializerFactory serFactory = null; 1543 TypeMapping tm = getTypeMapping(); 1544 if (actualXMLType != null) 1545 { 1546 actualXMLType.value = null; 1547 } 1548 1549 while (javaType != null) 1550 { 1551 serFactory = (SerializerFactory)tm.getSerializer(javaType, xmlType); 1552 if (serFactory != null) 1553 break; 1554 1555 serFactory = getSerializerFactoryFromInterface(javaType, xmlType, tm); 1557 1558 if (serFactory != null) 1560 break; 1561 1562 javaType = javaType.getSuperclass(); 1563 } 1564 1565 Serializer ser = null; 1567 if (serFactory != null) 1568 { 1569 ser = (Serializer)serFactory.getSerializerAs(Constants.AXIS_SAX); 1570 1571 if (actualXMLType != null) 1572 { 1573 if (serFactory instanceof BaseSerializerFactory) 1577 { 1578 actualXMLType.value = 1579 ((BaseSerializerFactory)serFactory).getXMLType(); 1580 } 1581 if (actualXMLType.value == null) 1582 { 1583 actualXMLType.value = 1584 ((TypeMappingImpl)tm).getXMLType(javaType, 1585 xmlType); 1586 } 1587 } 1588 } 1589 1590 log.debug("Exit:getSerializer: " + ser); 1591 1592 return ser; 1593 } 1594 1595 public String getValueAsString(Object value, QName xmlType) throws IOException 1596 { 1597 Serializer ser = getSerializer(value.getClass(), xmlType, null); 1598 if (!(ser instanceof SimpleValueSerializer)) 1599 { 1600 throw new IOException (Messages.getMessage("needSimpleValueSer", 1601 ser.getClass().getName())); 1602 } 1603 SimpleValueSerializer simpleSer = (SimpleValueSerializer)ser; 1604 return simpleSer.getValueAsString(value, this); 1605 } 1606 1607 1609 public Stack getElementStack() 1610 { 1611 return elementStack; 1612 } 1613} 1614 | Popular Tags |