package org.jboss.axis.encoding;
import org.jboss.axis.AxisEngine;
import org.jboss.axis.Constants;
import org.jboss.axis.Handler;
import org.jboss.axis.Message;
import org.jboss.axis.MessageContext;
import org.jboss.axis.attachments.Attachments;
import org.jboss.axis.client.Call;
import org.jboss.axis.description.OperationDesc;
import org.jboss.axis.description.TypeDesc;
import org.jboss.axis.encoding.ser.BaseSerializerFactory;
import org.jboss.axis.encoding.ser.JAFDataHandlerSerializer;
import org.jboss.axis.enums.Use;
import org.jboss.axis.handlers.soap.SOAPService;
import org.jboss.axis.schema.SchemaVersion;
import org.jboss.axis.soap.SOAPConstants;
import org.jboss.axis.types.HexBinary;
import org.jboss.axis.utils.IDKey;
import org.jboss.axis.utils.JavaUtils;
import org.jboss.axis.utils.Mapping;
import org.jboss.axis.utils.Messages;
import org.jboss.axis.utils.NSStack;
import org.jboss.axis.utils.XMLUtils;
import org.jboss.axis.wsdl.symbolTable.SchemaUtils;
import org.jboss.axis.wsdl.symbolTable.SymbolTable;
import org.jboss.logging.Logger;
import org.w3c.dom.Attr;
import org.w3c.dom.CDATASection;
import org.w3c.dom.CharacterData;
import org.w3c.dom.Comment;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;
import javax.xml.namespace.QName;
import javax.xml.rpc.JAXRPCException;
import javax.xml.rpc.holders.QNameHolder;
import javax.xml.soap.SOAPMessage;
import java.io.IOException;
import java.io.Writer;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Stack;
public class SerializationContextImpl implements SerializationContext
{
private static Logger log = Logger.getLogger(SerializationContextImpl.class.getName());
private NSStack nsStack = new NSStack();
private boolean writingStartTag = false;
private boolean onlyXML = true;
private int indent = 0;
private boolean startOfDocument = true;
private Writer writer;
private int lastPrefixIndex = 1;
private MessageContext msgContext;
private QName currentXMLType;
private Stack elementStack = new Stack();
private SOAPConstants soapConstants = SOAPConstants.SOAP11_CONSTANTS;
private boolean pretty;
private static QName multirefQName = new QName("", "multiRef");
private static Class[] getSerializerClasses =
new Class[]{String.class, Class.class, QName.class};
private boolean doMultiRefs = false;
private boolean sendXMLDecl = true;
private boolean sendXSIType = true;
private boolean sendMinimizedElements = true;
private HashMap multiRefValues = null;
private int multiRefIndex = -1;
private boolean noNamespaceMappings = true;
private boolean noDefaultNamespace;
class MultiRefItem
{
String id;
QName xmlType;
Boolean sendType;
Object value;
MultiRefItem(String id,
QName xmlType,
Boolean sendType, Object value)
{
this.id = id;
this.xmlType = xmlType;
this.sendType = sendType;
this.value = value;
}
}
private HashSet secondLevelObjects = null;
private Object forceSer = null;
private boolean outputMultiRefsFlag = false;
SchemaVersion schemaVersion = SchemaVersion.SCHEMA_2001;
HashMap preferredPrefixes = new HashMap();
public SerializationContextImpl(Writer writer)
{
this.writer = writer;
initialize();
}
private void initialize()
{
preferredPrefixes.put(soapConstants.getEncodingURI(),
Constants.NS_PREFIX_SOAP_ENC);
preferredPrefixes.put(Constants.NS_URI_XML,
Constants.NS_PREFIX_XML);
preferredPrefixes.put(schemaVersion.getXsdURI(),
Constants.NS_PREFIX_SCHEMA_XSD);
preferredPrefixes.put(schemaVersion.getXsiURI(),
Constants.NS_PREFIX_SCHEMA_XSI);
}
public SerializationContextImpl(Writer writer, MessageContext msgContext)
{
this.writer = writer;
this.msgContext = msgContext;
Handler optionSource = null;
if (msgContext != null)
{
soapConstants = msgContext.getSOAPConstants();
if (optionSource == null)
optionSource = msgContext.getAxisEngine();
schemaVersion = msgContext.getSchemaVersion();
Boolean shouldSendDecl = (Boolean)optionSource.getOption(AxisEngine.PROP_XML_DECL);
if (shouldSendDecl != null)
sendXMLDecl = shouldSendDecl.booleanValue();
Boolean shouldSendMultiRefs =
(Boolean)msgContext.getProperty(AxisEngine.PROP_DOMULTIREFS);
if (shouldSendMultiRefs == null)
shouldSendMultiRefs =
(Boolean)optionSource.getOption(AxisEngine.PROP_DOMULTIREFS);
if (shouldSendMultiRefs != null)
doMultiRefs = shouldSendMultiRefs.booleanValue();
Boolean shouldSendMinimized =
(Boolean)optionSource.getOption(AxisEngine.PROP_SEND_MINIMIZED_ELEMENTS);
if (shouldSendMinimized != null)
sendMinimizedElements = shouldSendMinimized.booleanValue();
if (!msgContext.isPropertyTrue(Call.SEND_TYPE_ATTR, true))
sendXSIType = false;
Boolean opt = (Boolean)optionSource.getOption(AxisEngine.PROP_SEND_XSI);
if ((opt != null) && (opt.equals(Boolean.FALSE)))
{
sendXSIType = false;
}
OperationDesc operation = msgContext.getOperation();
if (operation != null)
{
if (operation.getUse() != Use.ENCODED)
{
sendXSIType = false;
doMultiRefs = false;
}
}
else
{
SOAPService service = msgContext.getService();
if (service != null)
{
if (service.getUse() != Use.ENCODED)
{
sendXSIType = false;
doMultiRefs = false;
}
}
}
}
initialize();
}
public boolean getPretty()
{
return pretty;
}
public void setPretty(boolean pretty)
{
this.pretty = pretty;
}
public boolean getDoMultiRefs()
{
return doMultiRefs;
}
public void setDoMultiRefs(boolean shouldDo)
{
doMultiRefs = shouldDo;
}
public void setSendDecl(boolean sendDecl)
{
sendXMLDecl = sendDecl;
}
public boolean shouldSendXSIType()
{
return sendXSIType;
}
public boolean isNoDefaultNamespace()
{
return noDefaultNamespace;
}
public void setNoDefaultNamespace(boolean noDefaultNamespace)
{
this.noDefaultNamespace = noDefaultNamespace;
}
public TypeMapping getTypeMapping()
{
if (msgContext == null)
return DefaultTypeMappingImpl.getSingleton();
String encodingStyle = msgContext.getEncodingStyle();
if (encodingStyle == null)
encodingStyle = soapConstants.getEncodingURI();
return (TypeMapping)msgContext.
getTypeMappingRegistry().getTypeMapping(encodingStyle);
}
public TypeMappingRegistry getTypeMappingRegistry()
{
if (msgContext == null)
return null;
return msgContext.getTypeMappingRegistry();
}
public String getPrefixForURI(String uri)
{
return getPrefixForURI(uri, null, false);
}
public String getPrefixForURI(String uri, String defaultPrefix)
{
return getPrefixForURI(uri, defaultPrefix, false);
}
public String getPrefixForURI(String uri, String defaultPrefix, boolean attribute)
{
if ((uri == null) || (uri.length() == 0))
return null;
String prefix = nsStack.getPrefix(uri, attribute);
if (prefix == null)
{
prefix = (String)preferredPrefixes.get(uri);
if (prefix == null)
{
if (defaultPrefix == null)
{
prefix = "ns" + lastPrefixIndex++;
}
else
{
prefix = defaultPrefix;
}
}
registerPrefixForURI(prefix, uri);
}
return prefix;
}
public void registerPrefixForURI(String prefix, String uri)
{
if (log.isDebugEnabled())
log.debug("registerPrefixForURI(" + prefix + ", " + uri + ")");
if ("".equals(uri))
{
log.warn("Ignoring invalid namespace mapping: [prefix=" + prefix + ",uri=" + uri + "]");
return;
}
if ((uri != null) && (prefix != null))
{
if (noNamespaceMappings)
{
nsStack.push();
noNamespaceMappings = false;
}
nsStack.add(uri, prefix);
}
}
public Message getCurrentMessage()
{
if (msgContext == null)
return null;
return msgContext.getCurrentMessage();
}
public MessageContext getMessageContext()
{
return msgContext;
}
public String qName2String(QName qName, boolean writeNS)
{
String prefix = null;
String namespaceURI = qName.getNamespaceURI();
if (namespaceURI.length() == 0)
{
if (writeNS)
{
String defaultNS = nsStack.getNamespaceURI("");
if (defaultNS != null && defaultNS.length() > 0)
{
registerPrefixForURI("", "");
}
}
}
else
{
prefix = getPrefixForURI(namespaceURI);
}
if ((prefix == null) || (prefix.length() == 0))
return qName.getLocalPart();
StringBuffer sb = new StringBuffer(prefix);
sb.append(':');
sb.append(qName.getLocalPart());
return sb.toString();
}
public String qName2String(QName qName)
{
return qName2String(qName, false);
}
public String attributeQName2String(QName qName)
{
String prefix = null;
if (qName.getNamespaceURI().length() > 0)
{
prefix = getPrefixForURI(qName.getNamespaceURI(), null, true);
}
if ((prefix == null) || (prefix.length() == 0))
return qName.getLocalPart();
StringBuffer sb = new StringBuffer(prefix);
sb.append(':');
sb.append(qName.getLocalPart());
return sb.toString();
}
public QName getQNameForClass(Class cls)
{
return getTypeMapping().getTypeQName(cls);
}
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;
if (javaType.isArray()) return true;
QName qName = getQNameForClass(javaType);
if (qName != null && Constants.isSchemaXSD(qName.getNamespaceURI()))
{
if (SchemaUtils.isSimpleSchemaType(qName))
{
return true;
}
}
return false;
}
public void serialize(QName elemQName,
Attributes attributes,
Object value)
throws IOException
{
serialize(elemQName, attributes, value, null, true, null);
}
public void serialize(QName elemQName,
Attributes attributes,
Object value,
QName xmlType,
boolean sendNull,
Boolean sendType)
throws IOException
{
boolean shouldSendType = (sendType == null) ? shouldSendXSIType() :
sendType.booleanValue();
if (value == null)
{
if (sendNull)
{
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", "1");
startElement(elemQName, attrs);
endElement();
}
return;
}
Message msg = getCurrentMessage();
if (null != msg)
{
Attachments attachments = msg.getAttachmentsImpl();
if (null != attachments)
{
if (attachments.isAttachment(value) || getSerializer(value.getClass(), xmlType, null) instanceof JAFDataHandlerSerializer)
{
serializeActual(elemQName, attributes, value, xmlType, sendType);
return;
}
}
}
if (doMultiRefs && (msgContext == null || msgContext.isEncoded()) &&
(value != forceSer) && !isPrimitive(value))
{
if (multiRefIndex == -1)
multiRefValues = new HashMap();
String id;
MultiRefItem mri = (MultiRefItem)multiRefValues.get(getIdentityKey(value));
if (mri == null)
{
multiRefIndex++;
id = "id" + multiRefIndex;
mri = new MultiRefItem(id, xmlType, sendType, value);
multiRefValues.put(getIdentityKey(value), mri);
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, sendType);
return;
}
if (outputMultiRefsFlag)
{
if (secondLevelObjects == null)
secondLevelObjects = new HashSet();
secondLevelObjects.add(getIdentityKey(value));
}
}
else
{
id = mri.id;
}
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();
return;
}
if (value == forceSer)
forceSer = null;
serializeActual(elemQName, attributes, value, xmlType, sendType);
}
private IDKey getIdentityKey(Object value)
{
return new IDKey(value);
}
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();
String prefix = getPrefixForURI(encodingURI);
String root = prefix + ":root";
attrs.addAttribute(encodingURI, Constants.ATTR_ROOT, root,
"CDATA", "0");
addEncodingStyleAttribute(attrs);
HashSet keys = new HashSet();
keys.addAll(multiRefValues.keySet());
Iterator i = keys.iterator();
while (i.hasNext())
{
while (i.hasNext())
{
Object val = i.next();
MultiRefItem mri = (MultiRefItem)multiRefValues.get(val);
attrs.setAttribute(0, "", Constants.ATTR_ID, "id", "CDATA",
mri.id);
forceSer = mri.value;
serialize(multirefQName, attrs, mri.value,
mri.xmlType,
true,
Boolean.TRUE); }
if (secondLevelObjects != null)
{
i = secondLevelObjects.iterator();
secondLevelObjects = null;
}
}
forceSer = null;
outputMultiRefsFlag = false;
multiRefValues = null;
multiRefIndex = -1;
secondLevelObjects = null;
}
private void addEncodingStyleAttribute(AttributesImpl attrs)
{
String encodingStyle;
if (msgContext != null)
{
encodingStyle = msgContext.getEncodingStyle();
}
else
{
encodingStyle = soapConstants.getEncodingURI();
}
String encStyle = getPrefixForURI(soapConstants.getEnvelopeURI()) + ':' + Constants.ATTR_ENCODING_STYLE;
attrs.addAttribute(soapConstants.getEnvelopeURI(), Constants.ATTR_ENCODING_STYLE, encStyle, "CDATA", encodingStyle);
}
public void startDocument() throws IOException
{
if (startOfDocument && sendXMLDecl)
{
writer.write("<?xml version=\"1.0\" encoding=\"");
String encoding = null;
if (msgContext != null)
{
encoding = (String)msgContext.getProperty(SOAPMessage.CHARACTER_SET_ENCODING);
}
if (encoding == null)
{
encoding = XMLUtils.getEncoding();
}
writer.write(encoding);
writer.write("\"?>\n");
startOfDocument = false;
}
}
public void endDocument() throws IOException
{
}
public void startElement(QName qName, Attributes attributes)
throws IOException
{
ArrayList vecQNames = new ArrayList();
if (log.isDebugEnabled())
{
log.debug(Messages.getMessage("startElem00",
"[" + qName.getNamespaceURI() + "]:" + qName.getLocalPart()));
}
if (writingStartTag)
{
writer.write('>');
if (pretty) writer.write(JavaUtils.LS);
indent++;
}
if (pretty) for (int i = 0; i < indent; i++) writer.write(' ');
String elementQName = qName2String(qName, true);
writer.write('<');
writer.write(elementQName);
if (attributes != null)
{
for (int i = 0; i < attributes.getLength(); i++)
{
String qname = attributes.getQName(i);
writer.write(' ');
String prefix = "";
String uri = attributes.getURI(i);
if (uri != null && uri.length() > 0)
{
if (qname.length() == 0)
{
prefix = getPrefixForURI(uri);
}
else
{
int idx = qname.indexOf(':');
if (idx > -1)
{
prefix = qname.substring(0, idx);
prefix = getPrefixForURI(uri, prefix, true);
}
if (uri.equals(Constants.NS_URI_XMLNS))
{
prefix = "xmlns";
}
}
if (prefix.length() > 0)
{
qname = prefix + ':' + attributes.getLocalName(i);
}
else
{
qname = attributes.getLocalName(i);
}
}
else
{
qname = attributes.getQName(i);
if (qname.length() == 0)
qname = attributes.getLocalName(i);
}
if (qname.startsWith("xmlns"))
{
vecQNames.add(qname);
}
writer.write(qname);
writer.write("=\"");
writer.write(XMLUtils.xmlEncodeString(attributes.getValue(i)));
writer.write('"');
}
}
if (noNamespaceMappings)
{
nsStack.push();
}
else
{
for (Mapping map = nsStack.topOfFrame(); map != null; map = nsStack.next())
{
StringBuffer sb = new StringBuffer("xmlns");
boolean isDefaultNS = map.getPrefix().equals("");
if (!isDefaultNS || !isNoDefaultNamespace())
{
if (!isDefaultNS)
{
sb.append(':');
sb.append(map.getPrefix());
}
if (vecQNames.indexOf(sb.toString()) == -1)
{
writer.write(' ');
sb.append("=\"");
sb.append(map.getNamespaceURI());
sb.append('"');
writer.write(sb.toString());
}
}
}
noNamespaceMappings = true;
}
writingStartTag = true;
elementStack.push(qName);
onlyXML = true;
}
public void endElement()
throws IOException
{
QName qname = (QName)elementStack.pop();
String elementQName = qName2String(qname, true);
if (log.isDebugEnabled())
{
log.debug(Messages.getMessage("endElem00", "" + elementQName));
}
nsStack.pop();
if (writingStartTag)
{
if (sendMinimizedElements)
{
writer.write("/>");
}
else
{
writer.write("></");
writer.write(elementQName);
writer.write('>');
}
if (pretty) writer.write(JavaUtils.LS);
writingStartTag = false;
return;
}
if (onlyXML)
{
indent--;
if (pretty) for (int i = 0; i < indent; i++) writer.write(' ');
}
writer.write("</");
writer.write(elementQName);
writer.write('>');
if (pretty) if (indent > 0) writer.write(JavaUtils.LS);
onlyXML = true;
}
public void writeChars(char[] p1, int p2, int p3)
throws IOException
{
if (writingStartTag)
{
writer.write('>');
writingStartTag = false;
}
writeSafeString(String.valueOf(p1, p2, p3));
onlyXML = false;
}
public void writeString(String string)
throws IOException
{
if (writingStartTag)
{
writer.write('>');
writingStartTag = false;
}
writer.write(string);
onlyXML = false;
}
public void writeSafeString(String string)
throws IOException
{
writeString(XMLUtils.xmlEncodeString(string));
}
public void writeDOMElement(Element el)
throws IOException
{
AttributesImpl attributes = null;
NamedNodeMap attrMap = el.getAttributes();
if (attrMap.getLength() > 0)
{
attributes = new AttributesImpl();
for (int i = 0; i < attrMap.getLength(); i++)
{
Attr attr = (Attr)attrMap.item(i);
String tmp = attr.getNamespaceURI();
if (tmp != null && tmp.equals(Constants.NS_URI_XMLNS))
{
String prefix = attr.getLocalName();
if (prefix != null)
{
if (prefix.equals("xmlns"))
prefix = "";
String nsURI = attr.getValue();
registerPrefixForURI(prefix, nsURI);
}
continue;
}
attributes.addAttribute(attr.getNamespaceURI(),
attr.getLocalName(),
attr.getName(),
"CDATA", attr.getValue());
}
}
String namespaceURI = el.getNamespaceURI();
String localPart = el.getLocalName();
if (localPart == null)
localPart = el.getNodeName();
QName qName = new QName(namespaceURI, localPart);
startElement(qName, attributes);
NodeList children = el.getChildNodes();
for (int i = 0; i < children.getLength(); i++)
{
Node child = children.item(i);
if (child instanceof Element)
{
writeDOMElement((Element)child);
}
else if (child instanceof CDATASection)
{
writeString("<![CDATA[");
writeString(((Text)child).getData());
writeString("]]>");
}
else if (child instanceof Comment)
{
writeString("<!--");
writeString(((CharacterData)child).getData());
writeString("-->");
}
else if (child instanceof Text)
{
writeSafeString(((Text)child).getData());
}
}
endElement();
}
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;
}
public Attributes setTypeAttribute(Attributes attributes, QName type)
{
if (type == null ||
type.getLocalPart().indexOf(SymbolTable.ANON_TOKEN) >= 0 ||
((attributes != null) &&
(attributes.getIndex(Constants.URI_DEFAULT_SCHEMA_XSI,
"type") != -1)))
return attributes;
AttributesImpl attrs = new AttributesImpl();
if (attributes != null && 0 < attributes.getLength())
attrs.setAttributes(attributes);
String prefix = getPrefixForURI(Constants.URI_DEFAULT_SCHEMA_XSI,
"xsi");
attrs.addAttribute(Constants.URI_DEFAULT_SCHEMA_XSI,
"type",
prefix + ":type",
"CDATA", attributeQName2String(type));
return attrs;
}
private void serializeActual(QName elemQName,
Attributes attributes,
Object value,
QName xmlType,
Boolean sendType)
throws IOException
{
boolean shouldSendType = (sendType == null) ? shouldSendXSIType() :
sendType.booleanValue();
if (value != null)
{
Class javaType = value.getClass();
TypeMapping tm = getTypeMapping();
if (tm == null)
{
throw new IOException(Messages.getMessage("noSerializer00",
value.getClass().getName(),
"" + this));
}
currentXMLType = xmlType;
if (Constants.equals(Constants.XSD_ANYTYPE, xmlType))
{
xmlType = null;
shouldSendType = true;
}
QNameHolder actualXMLType = new QNameHolder();
Serializer ser = getSerializer(javaType, xmlType, actualXMLType);
if (ser != null)
{
if (shouldSendType || (xmlType != null && !xmlType.equals(actualXMLType.value)))
{
if (msgContext != null && msgContext.isEncoded())
attributes = setTypeAttribute(attributes, actualXMLType.value);
}
ser.serialize(elemQName, attributes, value, this);
return;
}
try
{
Method method = value.getClass().getMethod("getSerializer", getSerializerClasses);
if (method != null)
{
Serializer serializer = (Serializer)method.invoke(value,
new Object[]{"", value.getClass(), elemQName});
TypeDesc typedesc = TypeDesc.getTypeDescForClass(value.getClass());
if (typedesc != null)
{
QName qname = typedesc.getXmlType();
if (qname != null)
{
attributes = setTypeAttribute(attributes,
qname);
}
}
serializer.serialize(elemQName, attributes, value, this);
return;
}
}
catch (Exception e)
{
}
throw new IOException(Messages.getMessage("noSerializer00",
value.getClass().getName(), "" + tm));
}
}
public QName getCurrentXMLType()
{
return currentXMLType;
}
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;
}
private Serializer getSerializer(Class javaType, QName xmlType, QNameHolder actualXMLType)
{
log.debug("Enter:getSerializer: [class=" + javaType + ",xmlType=" + xmlType + "]");
SerializerFactory serFactory = null;
TypeMapping tm = getTypeMapping();
if (actualXMLType != null)
{
actualXMLType.value = null;
}
while (javaType != null)
{
serFactory = (SerializerFactory)tm.getSerializer(javaType, xmlType);
if (serFactory != null)
break;
serFactory = getSerializerFactoryFromInterface(javaType, xmlType, tm);
if (serFactory != null)
break;
javaType = javaType.getSuperclass();
}
Serializer ser = null;
if (serFactory != null)
{
ser = (Serializer)serFactory.getSerializerAs(Constants.AXIS_SAX);
if (actualXMLType != null)
{
if (serFactory instanceof BaseSerializerFactory)
{
actualXMLType.value =
((BaseSerializerFactory)serFactory).getXMLType();
}
if (actualXMLType.value == null)
{
actualXMLType.value =
((TypeMappingImpl)tm).getXMLType(javaType,
xmlType);
}
}
}
log.debug("Exit:getSerializer: " + ser);
return ser;
}
public String getValueAsString(Object value, QName xmlType) throws IOException
{
Serializer ser = getSerializer(value.getClass(), xmlType, null);
if (!(ser instanceof SimpleValueSerializer))
{
throw new IOException(Messages.getMessage("needSimpleValueSer",
ser.getClass().getName()));
}
SimpleValueSerializer simpleSer = (SimpleValueSerializer)ser;
return simpleSer.getValueAsString(value, this);
}
public Stack getElementStack()
{
return elementStack;
}
}