📄 serializationcontext.java
字号:
} /** * Serialize the indicated value as an element with the name * indicated by elemQName. * The attributes are additional attribute to be serialized on the element. * The value is the object being serialized. (It may be serialized * directly or serialized as an mult-ref'd item) * The value is an Object, which may be a wrapped primitive, the * javaType is the actual unwrapped object type. * The xmlType is the QName of the type that is used to set * xsi:type. If not specified, xsi:type is set by using the javaType to * find an appopriate xmlType from the TypeMappingRegistry. * Null values and the xsi:type flag will be sent or not depending * on previous configuration of this SerializationContext. * @param elemQName is the QName of the element * @param attributes are additional attributes * @param value is the object to serialize * @param xmlType is the qname of the type or null. * @deprecated use serialize(QName, Attributes, Object, QName, Class) instead */ public void serialize(QName elemQName, Attributes attributes, Object value, QName xmlType) throws IOException { serialize(elemQName, attributes, value, xmlType, null, null, null); } /** * Serialize the indicated value as an element with the name * indicated by elemQName. * The attributes are additional attribute to be serialized on the element. * The value is the object being serialized. (It may be serialized * directly or serialized as an mult-ref'd item) * The value is an Object, which may be a wrapped primitive, the * javaType is the actual unwrapped object type. * The xmlType is the QName of the type that is used to set * xsi:type. If not specified, xsi:type is set by using the javaType to * find an appopriate xmlType from the TypeMappingRegistry. * Null values and the xsi:type flag will be sent or not depending * on previous configuration of this SerializationContext. * @param elemQName is the QName of the element * @param attributes are additional attributes * @param value is the object to serialize * @param xmlType is the qname of the type or null. * @param javaType is the java type of the value */ public void serialize(QName elemQName, Attributes attributes, Object value, QName xmlType, Class javaType) throws IOException { serialize(elemQName, attributes, value, xmlType, javaType, null, null); } /** * Serialize the indicated value as an element with the name * indicated by elemQName. * The attributes are additional attribute to be serialized on the element. * The value is the object being serialized. (It may be serialized * directly or serialized as an mult-ref'd item) * The value is an Object, which may be a wrapped primitive. * The xmlType (if specified) is the QName of the type that is used to set * xsi:type. * The sendNull flag indicates whether null values should be sent over the * wire (default is to send such values with xsi:nil="true"). * The sendType flag indicates whether the xsi:type flag should be sent * (default is true). * @param elemQName is the QName of the element * @param attributes are additional attributes * @param value is the object to serialize * @param xmlType is the qname of the type or null. * @param sendNull determines whether to send null values. * @param sendType determines whether to set xsi:type attribute. * * @deprecated use serialize(QName, Attributes, Object, QName, * Boolean, Boolean) instead. */ public void serialize(QName elemQName, Attributes attributes, Object value, QName xmlType, boolean sendNull, Boolean sendType) throws IOException { serialize( elemQName, attributes, value, xmlType, null, (sendNull) ? Boolean.TRUE : Boolean.FALSE, sendType); } /** * Serialize the indicated value as an element with the name * indicated by elemQName. * The attributes are additional attribute to be serialized on the element. * The value is the object being serialized. (It may be serialized * directly or serialized as an mult-ref'd item) * The value is an Object, which may be a wrapped primitive. * The xmlType (if specified) is the QName of the type that is used to set * xsi:type. * The sendNull flag indicates whether to end an element with an xsi:nil="true" attribute for null * variables (if Boolean.TRUE), or nothing (if Boolean.FALSE). * The sendType flag indicates whether the xsi:type flag should be sent * (default is true). * @param elemQName is the QName of the element * @param attributes are additional attributes * @param value is the object to serialize * @param xmlType is the qname of the type or null. * @param sendNull determines whether to send null values. * @param sendType determines whether to set xsi:type attribute. */ public void serialize(QName elemQName, Attributes attributes, Object value, QName xmlType, Boolean sendNull, Boolean sendType) throws IOException { serialize(elemQName, attributes, value, xmlType, null, sendNull, sendType); } /** * Serialize the indicated value as an element with the name * indicated by elemQName. * The attributes are additional attribute to be serialized on the element. * The value is the object being serialized. (It may be serialized * directly or serialized as an mult-ref'd item) * The value is an Object, which may be a wrapped primitive. * The xmlType (if specified) is the QName of the type that is used to set * xsi:type. * The sendNull flag indicates whether to end an element with an xsi:nil="true" attribute for null * variables (if Boolean.TRUE), or nothing (if Boolean.FALSE). * The sendType flag indicates whether the xsi:type flag should be sent * (default is true). * @param elemQName is the QName of the element * @param attributes are additional attributes * @param value is the object to serialize * @param xmlType is the qname of the type or null. * @param javaType is the java type of the value * @param sendNull determines whether to send null values. * @param sendType determines whether to set xsi:type attribute. */ public void serialize(QName elemQName, Attributes attributes, Object value, QName xmlType, Class javaClass, Boolean sendNull, Boolean sendType) throws IOException { boolean sendXSITypeCache = sendXSIType; if (sendType != null) { sendXSIType = sendType.booleanValue(); } boolean shouldSendType = shouldSendXSIType(); try { Boolean sendNullCache = this.sendNull; if (sendNull != null) { this.sendNull = sendNull; } else { sendNull = this.sendNull; } if (value == null) { // If the value is null, the element is // passed with xsi:nil="true" to indicate that no object is present. if (this.sendNull.booleanValue()) { AttributesImpl attrs = new AttributesImpl(); if (attributes != null && 0 < attributes.getLength()) attrs.setAttributes(attributes); if (shouldSendType) attrs = (AttributesImpl) setTypeAttribute(attrs, xmlType); String nil = schemaVersion.getNilQName().getLocalPart(); attrs.addAttribute(schemaVersion.getXsiURI(), nil, "xsi:" + nil, "CDATA", "true"); startElement(elemQName, attrs); endElement(); } this.sendNull = sendNullCache; return; } Message msg= getCurrentMessage(); if(null != msg){ //Get attachments. returns null if no attachment support. Attachments attachments= getCurrentMessage().getAttachmentsImpl(); if( null != attachments && attachments.isAttachment(value)){ //Attachment support and this is an object that should be treated as an attachment. //Allow an the attachment to do its own serialization. serializeActual(elemQName, attributes, value, xmlType, javaClass, sendType); //No need to add to mulitRefs. Attachment data stream handled by // the message; this.sendNull = sendNullCache; return; } } // If multi-reference is enabled and this object value is not a primitive // and we are not forcing serialization of the object, then generate // an element href (and store the object for subsequent outputMultiRef // processing). // NOTE : you'll notice that everywhere we register objects in the // multiRefValues and secondLevelObjects collections, we key them // using getIdentityKey(value) instead of the Object reference itself. // THIS IS IMPORTANT, and please make sure you understand what's // going on if you change any of this code. It's this way to make // sure that individual Objects are serialized separately even if the // hashCode() and equals() methods have been overloaded to make two // Objects appear equal. if (doMultiRefs && isEncoded() && (value != forceSer) && !isPrimitive(value)) { if (multiRefIndex == -1) multiRefValues = new HashMap(); String id; // Look for a multi-ref descriptor for this Object. MultiRefItem mri = (MultiRefItem)multiRefValues.get( getIdentityKey(value)); if (mri == null) { // Didn't find one, so create one, give it a new ID, and store // it for next time. multiRefIndex++; id = "id" + multiRefIndex; mri = new MultiRefItem (id, xmlType, sendType, value); multiRefValues.put(getIdentityKey(value), mri); /** * If we're SOAP 1.2, we can "inline" the serializations, * so put it out now, with it's ID. */ if (soapConstants == SOAPConstants.SOAP12_CONSTANTS) { AttributesImpl attrs = new AttributesImpl(); if (attributes != null && 0 < attributes.getLength()) attrs.setAttributes(attributes); attrs.addAttribute("", Constants.ATTR_ID, "id", "CDATA", id); serializeActual(elemQName, attrs, value, xmlType, javaClass, sendType); this.sendNull = sendNullCache; return; } /** If we're in the middle of writing out * the multi-refs, we've already cloned the list of objects * and so even though we add a new one to multiRefValues, * it won't get serialized this time around. * * To deal with this, we maintain a list of "second level" * Objects - ones that need serializing as a result of * serializing the first level. When outputMultiRefs() is * nearly finished, it checks to see if secondLevelObjects * is empty, and if not, it goes back and loops over those * Objects. This can happen N times depending on how deep * the Object graph goes. */ if (outputMultiRefsFlag) { if (secondLevelObjects == null) secondLevelObjects = new HashSet(); secondLevelObjects.add(getIdentityKey(value)); } } else { // Found one, remember it's ID id = mri.id; } // Serialize an HREF to our object AttributesImpl attrs = new AttributesImpl(); if (attributes != null && 0 < attributes.getLength()) attrs.setAttributes(attributes); attrs.addAttribute("", soapConstants.getAttrHref(), soapConstants.getAttrHref(), "CDATA", '#' + id); startElement(elemQName, attrs); endElement(); this.sendNull = sendNullCache; return; } // The forceSer variable is set by outputMultiRefs to force // serialization of this object via the serialize(...) call // below. However, if the forced object contains a self-reference, we // get into an infinite loop..which is why it is set back to null // before the actual serialization. if (value == forceSer) forceSer = null; // Actually serialize the value. (i.e. not an href like above) serializeActual(elemQName, attributes, value, xmlType, javaClass, sendType); } finally { sendXSIType = sendXSITypeCache; } } /** * Get an IDKey that represents the unique identity of the object. * This is used as a unique key into a HashMap which will * not give false hits on other Objects where hashCode() and equals() * have been overriden to match. * * @param value the Object to hash * @return a unique IDKey for the identity */ private IDKey getIdentityKey(Object value) { return new IDKey(value); } /** * The serialize method uses hrefs to reference all non-primitive * values. These values are stored and serialized by calling * outputMultiRefs after the serialize method completes. */ public void outputMultiRefs() throws IOException { if (!doMultiRefs || (multiRefValues == null) || soapConstants == SOAPConstants.SOAP12_CONSTANTS) return; outputMultiRefsFlag = true; AttributesImpl attrs = new AttributesImpl(); attrs.addAttribute("","","","",""); String encodingURI = soapConstants.getEncodingURI(); // explicitly state that this attribute is not a root String prefix = getPrefixForURI(encodingURI); String root = prefix + ":root"; attrs.addAttribute(encodingURI, Constants.ATTR_ROOT, root, "CDATA", "0"); // Make sure we put the encodingStyle on each multiref element we
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -