📄 serializationcontext.java
字号:
endElement(); } /** * Convenience method to get the Serializer for a specific * java type * @param javaType is Class for a type to serialize * @return Serializer */ public final Serializer getSerializerForJavaType(Class javaType) { SerializerFactory serF = null; Serializer ser = null; try { serF = (SerializerFactory) getTypeMapping().getSerializer(javaType); if (serF != null) { ser = (Serializer) serF.getSerializerAs(Constants.AXIS_SAX); } } catch (JAXRPCException e) { } return ser; } /** * Obtains the type attribute that should be serialized and returns the new list of Attributes * @param attributes of the qname * @param type is the qname of the type * @return new list of Attributes */ public Attributes setTypeAttribute(Attributes attributes, QName type) { SchemaVersion schema = SchemaVersion.SCHEMA_2001; if (msgContext != null) { schema = msgContext.getSchemaVersion(); } if (type == null || type.getLocalPart().indexOf(SymbolTable.ANON_TOKEN) >= 0 || ((attributes != null) && (attributes.getIndex(schema.getXsiURI(), "type") != -1))) return attributes; AttributesImpl attrs = new AttributesImpl(); if (attributes != null && 0 < attributes.getLength() ) attrs.setAttributes(attributes); String prefix = getPrefixForURI(schema.getXsiURI(), "xsi"); attrs.addAttribute(schema.getXsiURI(), "type", prefix + ":type", "CDATA", attributeQName2String(type)); return attrs; } /** * Invoked to do the actual serialization of the qName (called by serialize above). * additional attributes that will be serialized with the qName. * @param elemQName is the QName of the element * @param attributes are additional attributes * @param value is the object to serialize * @param xmlType (optional) is the desired type QName. * @param sendType indicates whether the xsi:type attribute should be set. */ private void serializeActual(QName elemQName, Attributes attributes, Object value, QName xmlType, Class javaClass, Boolean sendType) throws IOException { boolean shouldSendType = (sendType == null) ? shouldSendXSIType() : sendType.booleanValue(); if (value != null) { TypeMapping tm = getTypeMapping(); if (tm == null) { throw new IOException( Messages.getMessage("noSerializer00", value.getClass().getName(), "" + this)); } // Set currentXMLType to the one desired one. // Note for maxOccurs usage this xmlType is the // type of the component not the type of the array. currentXMLType = xmlType; // if we're looking for xsd:anyType, accept anything... if (Constants.equals(Constants.XSD_ANYTYPE,xmlType)){ xmlType = null; shouldSendType = true; } // Try getting a serializer for the prefered xmlType QNameHolder actualXMLType = new QNameHolder(); Class javaType = getActualJavaClass(xmlType, javaClass, value); Serializer ser = getSerializer(javaType, xmlType, actualXMLType); if ( ser != null ) { // Send the xmlType if indicated or if // the actual xmlType is different than the // prefered xmlType if (shouldSendType || (xmlType != null && (!xmlType.equals(actualXMLType.value)))) { if(!isEncoded()) { if (Constants.isSOAP_ENC(actualXMLType.value.getNamespaceURI())) { // Don't write SOAP_ENC types (i.e. Array) if we're not using encoding } else if (javaType.isPrimitive() && javaClass != null && JavaUtils.getWrapperClass(javaType) == javaClass) { // Don't write xsi:type when serializing primitive wrapper value as primitive type. } else { if(!(javaType.isArray() && xmlType != null && Constants.isSchemaXSD(xmlType.getNamespaceURI())) ) { writeXMLType = actualXMLType.value; } } } else { writeXMLType = actualXMLType.value; } } // ----------------- // NOTE: I have seen doc/lit tests that use // the type name as the element name in multi-ref cases // (for example <soapenc:Array ... >) // In such cases the xsi:type is not passed along. // ----------------- // The multiref QName is our own fake name. // It may be beneficial to set the name to the // type name, but I didn't see any improvements // in the interop tests. //if (name.equals(multirefQName) && type != null) // name = type; ser.serialize(elemQName, attributes, value, this); return; } throw new IOException(Messages.getMessage("noSerializer00", value.getClass().getName(), "" + tm)); } // !!! Write out a generic null, or get type info from somewhere else? } /** * Returns the java class for serialization. * If the xmlType is xsd:anyType or javaType is array or javaType is java.lang.Object * the java class for serialization is the class of obj. * If the obj is not array and the obj's class does not match with the javaType, * the java class for serialization is the javaType. * Otherwise, the java class for serialization is the obj's class. * * @param xmlType the qname of xml type * @param javaType the java class from serializer * @param obj the object to serialize * @return the java class for serialization */ private Class getActualJavaClass(QName xmlType, Class javaType, Object obj) { Class cls = obj.getClass(); if ((xmlType != null && Constants.isSchemaXSD(xmlType.getNamespaceURI()) && "anyType".equals(xmlType.getLocalPart())) || (javaType != null && (javaType.isArray() || javaType == Object.class))) { return cls; } if (javaType != null && !javaType.isAssignableFrom(cls) && !cls.isArray()) { return javaType; } return cls; } private Serializer getSerializerFromClass(Class javaType, QName qname) { Serializer serializer = null; try { Method method = MethodCache.getInstance().getMethod(javaType, SERIALIZER_METHOD, SERIALIZER_CLASSES); if (method != null) { serializer = (Serializer) method.invoke(null, new Object[] {getEncodingStyle(), javaType, qname}); } } catch (NoSuchMethodException e) { } catch (IllegalAccessException e) { } catch (InvocationTargetException e) { } return serializer; } /** * Get the currently prefered xmlType * @return QName of xmlType or null */ public QName getCurrentXMLType() { return currentXMLType; } /** * Walk the interfaces of a class looking for a serializer for that * interface. Include any parent interfaces in the search also. * */ private SerializerFactory getSerializerFactoryFromInterface(Class javaType, QName xmlType, TypeMapping tm) { SerializerFactory serFactory = null ; Class [] interfaces = javaType.getInterfaces(); if (interfaces != null) { for (int i = 0; i < interfaces.length; i++) { Class iface = interfaces[i]; serFactory = (SerializerFactory) tm.getSerializer(iface, xmlType); if (serFactory == null) serFactory = getSerializerFactoryFromInterface(iface, xmlType, tm); if (serFactory != null) break; } } return serFactory; } /** * getSerializer * Attempts to get a serializer for the indicated javaType and xmlType. * @param javaType is the type of the object * @param xmlType is the preferred qname type. * @param actualXMLType is set to a QNameHolder or null. * If a QNameHolder, the actual xmlType is returned. * @return found class/serializer or null **/ private Serializer getSerializer(Class javaType, QName xmlType, QNameHolder actualXMLType) { SerializerFactory serFactory = null ; TypeMapping tm = getTypeMapping(); if (actualXMLType != null) { actualXMLType.value = null; } while (javaType != null) { // check type mapping serFactory = (SerializerFactory) tm.getSerializer(javaType, xmlType); if (serFactory != null) { break; } // check the class for serializer Serializer serializer = getSerializerFromClass(javaType, xmlType); if (serializer != null) { if (actualXMLType != null) { TypeDesc typedesc = TypeDesc.getTypeDescForClass(javaType); if (typedesc != null) { actualXMLType.value = typedesc.getXmlType(); } } return serializer; } // Walk my interfaces... serFactory = getSerializerFactoryFromInterface(javaType, xmlType, tm); if (serFactory != null) { break; } // Finally, head to my superclass javaType = javaType.getSuperclass(); } // Using the serialization factory, create a serializer Serializer ser = null; if ( serFactory != null ) { ser = (Serializer) serFactory.getSerializerAs(Constants.AXIS_SAX); if (actualXMLType != null) { // Get the actual qname xmlType from the factory. // If not found via the factory, fall back to a less // performant solution. if (serFactory instanceof BaseSerializerFactory) { actualXMLType.value = ((BaseSerializerFactory) serFactory).getXMLType(); } boolean encoded = isEncoded(); if (actualXMLType.value == null || (!encoded && (actualXMLType.value.equals(Constants.SOAP_ARRAY) || actualXMLType.value.equals(Constants.SOAP_ARRAY12)))) { actualXMLType.value = tm.getXMLType(javaType, xmlType, encoded); } } } return ser; } public String getValueAsString(Object value, QName xmlType, Class javaClass) throws IOException { Class cls = value.getClass(); cls = getActualJavaClass(xmlType, javaClass, value); Serializer ser = getSerializer(cls, xmlType, null); // The java type is an array, but we need a simple type. if (ser instanceof ArraySerializer) { SimpleListSerializerFactory factory = new SimpleListSerializerFactory(cls, xmlType); ser = (Serializer) factory.getSerializerAs(getEncodingStyle()); } if (!(ser instanceof SimpleValueSerializer)) { throw new IOException( Messages.getMessage("needSimpleValueSer", ser.getClass().getName())); } SimpleValueSerializer simpleSer = (SimpleValueSerializer)ser; return simpleSer.getValueAsString(value, this); } public void setWriteXMLType(QName type) { writeXMLType = type; } public XMLEncoder getEncoder() { if(encoder == null) { encoder = XMLUtils
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -