📄 serializationcontext.java
字号:
} } /** * Are we doing multirefs? * @return true or false */ public boolean getDoMultiRefs() { return doMultiRefs; } /** * Set whether we are doing multirefs */ public void setDoMultiRefs (boolean shouldDo) { doMultiRefs = shouldDo; } /** * Set whether or not we should write XML declarations. * @param sendDecl true/false */ public void setSendDecl(boolean sendDecl) { sendXMLDecl = sendDecl; } /** * Get whether or not to write xsi:type attributes. * @return true/false */ public boolean shouldSendXSIType() { return sendXSIType; } /** * Get the TypeMapping we're using. * @return TypeMapping or null */ public TypeMapping getTypeMapping() { // Always allow the default mappings if (msgContext == null) return DefaultTypeMappingImpl.getSingletonDelegate(); String encodingStyle = msgContext.getEncodingStyle(); if (encodingStyle == null) encodingStyle = soapConstants.getEncodingURI(); return (TypeMapping) msgContext. getTypeMappingRegistry().getTypeMapping(encodingStyle); } /** * Get the TypeMappingRegistry we're using. * @return TypeMapping or null */ public TypeMappingRegistry getTypeMappingRegistry() { if (msgContext == null) return null; return msgContext.getTypeMappingRegistry(); } /** * Get a prefix for a namespace URI. This method will ALWAYS * return a valid prefix - if the given URI is already mapped in this * serialization, we return the previous prefix. If it is not mapped, * we will add a new mapping and return a generated prefix of the form * "ns<num>". * @param uri is the namespace uri * @return prefix */ public String getPrefixForURI(String uri) { return getPrefixForURI(uri, null, false); } /** * Get a prefix for the given namespace URI. If one has already been * defined in this serialization, use that. Otherwise, map the passed * default prefix to the URI, and return that. If a null default prefix * is passed, use one of the form "ns<num>" */ public String getPrefixForURI(String uri, String defaultPrefix) { return getPrefixForURI(uri, defaultPrefix, false); } /** * Get a prefix for the given namespace URI. If one has already been * defined in this serialization, use that. Otherwise, map the passed * default prefix to the URI, and return that. If a null default prefix * is passed, use one of the form "ns<num>" */ public String getPrefixForURI(String uri, String defaultPrefix, boolean attribute) { if ((uri == null) || (uri.length() == 0)) return null; // If we're looking for an attribute prefix, we shouldn't use the // "" prefix, but always register/find one. String prefix = nsStack.getPrefix(uri, attribute); if (prefix == null) { prefix = (String)preferredPrefixes.get(uri); if (prefix == null) { if (defaultPrefix == null) { prefix = "ns" + lastPrefixIndex++; while(nsStack.getNamespaceURI(prefix)!=null) { prefix = "ns" + lastPrefixIndex++; } } else { prefix = defaultPrefix; } } registerPrefixForURI(prefix, uri); } return prefix; } /** * Register prefix for the indicated uri * @param prefix * @param uri is the namespace uri */ public void registerPrefixForURI(String prefix, String uri) { if (debugEnabled) { log.debug(Messages.getMessage("register00", prefix, uri)); } if ((uri != null) && (prefix != null)) { if (noNamespaceMappings) { nsStack.push(); noNamespaceMappings = false; } String activePrefix = nsStack.getPrefix(uri,true); if(activePrefix == null || !activePrefix.equals(prefix)) { nsStack.add(uri, prefix); } } } /** * Return the current message */ public Message getCurrentMessage() { if (msgContext == null) return null; return msgContext.getCurrentMessage(); } /** * Get the MessageContext we're operating with */ public MessageContext getMessageContext() { return msgContext; } /** * Returns this context's encoding style. If we've got a message * context then we'll get the style from that; otherwise we'll * return a default. * * @return a <code>String</code> value */ public String getEncodingStyle() { return msgContext == null ? Use.DEFAULT.getEncoding() : msgContext.getEncodingStyle(); } /** * Returns whether this context should be encoded or not. * * @return a <code>boolean</code> value */ public boolean isEncoded() { return Constants.isSOAP_ENC(getEncodingStyle()); } /** * Convert QName to a string of the form <prefix>:<localpart> * @param qName * @return prefixed qname representation for serialization. */ public String qName2String(QName qName, boolean writeNS) { String prefix = null; String namespaceURI = qName.getNamespaceURI(); String localPart = qName.getLocalPart(); if(localPart != null && localPart.length() > 0) { int index = localPart.indexOf(':'); if(index!=-1){ prefix = localPart.substring(0,index); if(prefix.length()>0 && !prefix.equals("urn")){ registerPrefixForURI(prefix, namespaceURI); localPart = localPart.substring(index+1); } else { prefix = null; } } localPart = Utils.getLastLocalPart(localPart); } if (namespaceURI.length() == 0) { if (writeNS) { // If this is unqualified (i.e. prefix ""), set the default // namespace to "" String defaultNS = nsStack.getNamespaceURI(""); if (defaultNS != null && defaultNS.length() > 0) { registerPrefixForURI("", ""); } } } else { prefix = getPrefixForURI(namespaceURI); } if ((prefix == null) || (prefix.length() == 0)) return localPart; return prefix + ':' + localPart; } public String qName2String(QName qName) { return qName2String(qName, false); } /** * Convert attribute QName to a string of the form <prefix>:<localpart> * There are slightly different rules for attributes: * - There is no default namespace * - any attribute in a namespace must have a prefix * * @param qName QName * @return prefixed qname representation for serialization. */ public String attributeQName2String(QName qName) { String prefix = null; String uri = qName.getNamespaceURI(); if (uri.length() > 0) { prefix = getPrefixForURI(uri, null, true); } if ((prefix == null) || (prefix.length() == 0)) return qName.getLocalPart(); return prefix + ':' + qName.getLocalPart(); } /** * Get the QName associated with the specified class. * @param cls Class of an object requiring serialization. * @return appropriate QName associated with the class. */ public QName getQNameForClass(Class cls) { return getTypeMapping().getTypeQName(cls); } /** * Indicates whether the object should be interpretted as a primitive * for the purposes of multi-ref processing. A primitive value * is serialized directly instead of using id/href pairs. Thus * primitive serialization/deserialization is slightly faster. * @param value to be serialized * @return true/false */ public boolean isPrimitive(Object value) { if (value == null) return true; Class javaType = value.getClass(); if (javaType.isPrimitive()) return true; if (javaType == String.class) return true; if (Calendar.class.isAssignableFrom(javaType)) return true; if (Date.class.isAssignableFrom(javaType)) return true; if (HexBinary.class.isAssignableFrom(javaType)) return true; if (Element.class.isAssignableFrom(javaType)) return true; if (javaType == byte[].class) return true; // There has been discussion as to whether arrays themselves should // be regarded as multi-ref. // Here are the three options: // 1) Arrays are full-fledged Objects and therefore should always be // multi-ref'd (Pro: This is like java. Con: Some runtimes don't // support this yet, and it requires more stuff to be passed over the wire.) // 2) Arrays are not full-fledged Objects and therefore should // always be passed as single ref (note the elements of the array // may be multi-ref'd.) (Pro: This seems reasonable, if a user // wants multi-referencing put the array in a container. Also // is more interop compatible. Con: Not like java serialization.) // 3) Arrays of primitives should be single ref, and arrays of // non-primitives should be multi-ref. (Pro: Takes care of the // looping case. Con: Seems like an obtuse rule.) // // Changing the code from (1) to (2) to see if interop fairs better. if (javaType.isArray()) return true; // Note that java.lang wrapper classes (i.e. java.lang.Integer) are // not primitives unless the corresponding type is an xsd type. // (If the wrapper maps to a soap encoded primitive, it can be nillable // and multi-ref'd). QName qName = getQNameForClass(javaType); if (qName != null && Constants.isSchemaXSD(qName.getNamespaceURI())) { if (SchemaUtils.isSimpleSchemaType(qName)) { return true; } } return false; } /** * 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. * 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 */ public void serialize(QName elemQName, Attributes attributes, Object value) throws IOException { serialize(elemQName, attributes, value, null, null, null, null);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -