package org.jboss.axis.encoding.ser;
import org.jboss.axis.AxisFault;
import org.jboss.axis.Constants;
import org.jboss.axis.MessageContext;
import org.jboss.axis.description.ElementDesc;
import org.jboss.axis.description.FieldDesc;
import org.jboss.axis.description.OperationDesc;
import org.jboss.axis.description.TypeDesc;
import org.jboss.axis.encoding.SerializationContext;
import org.jboss.axis.encoding.Serializer;
import org.jboss.axis.enums.Style;
import org.jboss.axis.message.SOAPElementAxisImpl;
import org.jboss.axis.soap.SOAPConstants;
import org.jboss.axis.utils.BeanPropertyDescriptor;
import org.jboss.axis.utils.BeanUtils;
import org.jboss.axis.utils.JavaUtils;
import org.jboss.axis.utils.Messages;
import org.jboss.axis.wsdl.fromJava.Types;
import org.jboss.logging.Logger;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;
import javax.xml.namespace.QName;
import java.io.IOException;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Modifier;
import java.util.List;
public class BeanSerializer implements Serializer
{
private static Logger log = Logger.getLogger(BeanSerializer.class.getName());
QName xmlType;
Class javaType;
protected BeanPropertyDescriptor[] propertyDescriptor;
protected TypeDesc typeDesc;
public BeanSerializer(Class javaType, QName xmlType)
{
this(javaType, xmlType, TypeDesc.getTypeDescForClass(javaType));
}
public BeanSerializer(Class javaType, QName xmlType, TypeDesc typeDesc)
{
this.xmlType = xmlType;
this.javaType = javaType;
this.typeDesc = typeDesc;
if (typeDesc != null)
{
propertyDescriptor = typeDesc.getPropertyDescriptors();
}
else
{
propertyDescriptor = BeanUtils.getPd(javaType, null);
}
}
public BeanSerializer(Class javaType, QName xmlType, TypeDesc typeDesc,
BeanPropertyDescriptor[] propertyDescriptor)
{
this.xmlType = xmlType;
this.javaType = javaType;
this.typeDesc = typeDesc;
this.propertyDescriptor = propertyDescriptor;
}
public void serialize(QName name, Attributes attributes,
Object value, SerializationContext context)
throws IOException
{
log.debug("serialize [name=" + name + ",value=" + value + "]");
MessageContext messageContext = context.getMessageContext();
OperationDesc operation = messageContext.getOperation();
if (operation != null && operation.getStyle() == Style.DOCUMENT)
{
String uri = name.getNamespaceURI();
String prefix = name.getPrefix();
if (prefix.length() == 0 && uri.length() > 0)
{
prefix = context.getPrefixForURI(uri);
if (prefix.equals("")) prefix = "ns1";
name = new QName(uri, name.getLocalPart(), prefix);
context.registerPrefixForURI(prefix, uri);
context.setNoDefaultNamespace(true);
}
}
Attributes beanAttrs = getObjectAttributes(value, attributes, context);
String encodingStyle = messageContext.getEncodingStyle();
boolean isEncoded = Constants.isSOAP_ENC(encodingStyle);
boolean suppressElement = !context.getMessageContext().isEncoded() &&
name.getNamespaceURI().equals("") &&
name.getLocalPart().equals("any");
if (!suppressElement)
{
context.startElement(name, beanAttrs);
}
try
{
for (int i = 0; i < propertyDescriptor.length; i++)
{
BeanPropertyDescriptor bpDesc = propertyDescriptor[i];
String propName = bpDesc.getName();
if (propName.equals("class"))
continue;
QName qname = null;
QName xmlType = null;
QName itemXmlType = null;
boolean isOmittable = false;
if (typeDesc != null)
{
FieldDesc field = typeDesc.getFieldByName(propName);
if (field != null)
{
if (!field.isElement())
continue;
if (((ElementDesc)field).isAsContent())
{
Object propValue = bpDesc.get(value);
String strval = context.getValueAsString(propValue, xmlType);
context.writeSafeString(strval);
continue;
}
itemXmlType = ((ElementDesc)field).getItemXmlType();
if (isEncoded)
{
qname = new QName(field.getXmlName().getLocalPart());
}
else
{
qname = field.getXmlName();
}
isOmittable = field.isMinOccursZero();
xmlType = field.getXmlType();
}
}
if (qname == null)
{
qname = new QName(propName);
}
if (xmlType == null)
{
xmlType = context.getQNameForClass(bpDesc.getType());
}
if (bpDesc.isReadable())
{
if (!bpDesc.isIndexed())
{
Object propValue = bpDesc.get(value);
if (propValue == null && isOmittable && !isEncoded)
continue;
if (!isEncoded && propValue != null && JavaUtils.isArrayClass(propValue.getClass())
&& !Constants.XSD_BASE64.equals(xmlType) && !Constants.XSD_HEXBIN.equals(xmlType))
{
QName itemType;
if (itemXmlType == null)
{
Class componentType = propValue.getClass().getComponentType();
itemType = context.getQNameForClass(componentType);
}
else
{
itemType = itemXmlType;
}
log.debug("Item type is " + itemType);
for (int j = 0; j < Array.getLength(propValue); j++)
{
Object item = Array.get(propValue, j);
context.serialize(qname, null, item, itemType, true, new Boolean(false));
}
}
else
{
context.serialize(qname, null, propValue, xmlType, true, new Boolean(isEncoded));
}
}
else
{
int j = 0;
while (j >= 0)
{
Object propValue = null;
try
{
propValue = bpDesc.get(value, j);
j++;
}
catch (Exception e)
{
j = -1;
}
if (j >= 0)
{
context.serialize(qname, null,
propValue, xmlType,
true, null);
}
}
}
}
}
BeanPropertyDescriptor anyDesc = typeDesc == null ? null : typeDesc.getAnyDesc();
if (anyDesc != null)
{
Object anyVal = anyDesc.get(value);
if (anyVal != null && anyVal instanceof SOAPElementAxisImpl[])
{
SOAPElementAxisImpl[] anyContent = (SOAPElementAxisImpl[])anyVal;
for (int i = 0; i < anyContent.length; i++)
{
SOAPElementAxisImpl element = anyContent[i];
element.output(context);
}
}
}
}
catch (InvocationTargetException ite)
{
Throwable target = ite.getTargetException();
log.error(Messages.getMessage("exception00"), target);
throw new IOException(target.toString());
}
catch (Exception e)
{
log.error(Messages.getMessage("exception00"), e);
throw new IOException(e.toString());
}
if (!suppressElement)
context.endElement();
}
public String getMechanismType()
{
return Constants.AXIS_SAX;
}
public Element writeSchema(Class javaType, Types types) throws Exception
{
Element complexType = types.createElement("complexType");
Element e = null;
Class superClass = javaType.getSuperclass();
BeanPropertyDescriptor[] superPd = null;
List stopClasses = types.getStopClasses();
if (superClass != null &&
superClass != java.lang.Object.class &&
superClass != java.lang.Exception.class &&
superClass != java.lang.Throwable.class &&
superClass != java.rmi.RemoteException.class &&
superClass != org.jboss.axis.AxisFault.class &&
(stopClasses == null ||
!(stopClasses.contains(superClass.getName()))))
{
String base = types.writeType(superClass);
Element complexContent = types.createElement("complexContent");
complexType.appendChild(complexContent);
Element extension = types.createElement("extension");
complexContent.appendChild(extension);
extension.setAttribute("base", base);
e = extension;
TypeDesc superTypeDesc = TypeDesc.getTypeDescForClass(superClass);
if (superTypeDesc != null)
{
superPd = superTypeDesc.getPropertyDescriptors();
}
else
{
superPd = BeanUtils.getPd(superClass, null);
}
}
else
{
e = complexType;
}
Element all = types.createElement("sequence");
e.appendChild(all);
if (Modifier.isAbstract(javaType.getModifiers()))
{
complexType.setAttribute("abstract", "true");
}
for (int i = 0; i < propertyDescriptor.length; i++)
{
String propName = propertyDescriptor[i].getName();
boolean writeProperty = true;
if (propName.equals("class"))
{
writeProperty = false;
}
if (superPd != null && writeProperty)
{
for (int j = 0; j < superPd.length && writeProperty; j++)
{
if (propName.equals(superPd[j].getName()))
{
writeProperty = false;
}
}
}
if (!writeProperty)
{
continue;
}
Class fieldType = propertyDescriptor[i].getType();
if (typeDesc != null)
{
FieldDesc field = typeDesc.getFieldByName(propName);
if (field != null)
{
QName qname = field.getXmlName();
QName fieldXmlType = field.getXmlType();
boolean isAnonymous = fieldXmlType != null && fieldXmlType.getLocalPart().startsWith(">");
if (qname != null)
{
propName = qname.getLocalPart();
}
if (!field.isElement())
{
writeAttribute(types,
propName,
fieldType,
fieldXmlType,
complexType);
}
else
{
writeField(types,
propName,
fieldType,
propertyDescriptor[i].isIndexed(),
field.isMinOccursZero(),
all, isAnonymous);
}
}
else
{
writeField(types,
propName,
fieldType,
propertyDescriptor[i].isIndexed(), false, all, false);
}
}
else
{
writeField(types,
propName,
fieldType,
propertyDescriptor[i].isIndexed(), false, all, false);
}
}
return complexType;
}
protected void writeField(Types types, String fieldName,
Class fieldType,
boolean isUnbounded,
boolean isOmittable,
Element where,
boolean isAnonymous) throws Exception
{
Element elem;
if (isAnonymous)
{
elem = types.createElementWithAnonymousType(fieldName,
fieldType, isOmittable, where.getOwnerDocument());
}
else
{
String elementType = types.writeType(fieldType);
if (elementType == null)
{
QName anyQN = Constants.XSD_ANYTYPE;
String prefix = types.getNamespaces().getCreatePrefix(anyQN.getNamespaceURI());
elementType = prefix + ":" + anyQN.getLocalPart();
}
elem = types.createElement(fieldName,
elementType,
types.isNullable(fieldType),
isOmittable,
where.getOwnerDocument());
}
if (isUnbounded)
{
elem.setAttribute("maxOccurs", "unbounded");
}
where.appendChild(elem);
}
protected void writeAttribute(Types types,
String fieldName,
Class fieldType,
QName fieldXmlType,
Element where) throws Exception
{
if (!types.isAcceptableAsAttribute(fieldType))
{
throw new AxisFault(Messages.getMessage("AttrNotSimpleType00",
fieldName,
fieldType.getName()));
}
Element elem = types.createAttributeElement(fieldName,
fieldType, fieldXmlType,
false,
where.getOwnerDocument());
where.appendChild(elem);
}
protected Attributes getObjectAttributes(Object value,
Attributes attributes,
SerializationContext context)
{
if (typeDesc == null || !typeDesc.hasAttributes())
return attributes;
AttributesImpl attrs;
if (attributes == null)
{
attrs = new AttributesImpl();
}
else if (attributes instanceof AttributesImpl)
{
attrs = (AttributesImpl)attributes;
}
else
{
attrs = new AttributesImpl(attributes);
}
try
{
for (int i = 0; i < propertyDescriptor.length; i++)
{
String propName = propertyDescriptor[i].getName();
if (propName.equals("class"))
continue;
FieldDesc field = typeDesc.getFieldByName(propName);
if (field == null || field.isElement())
continue;
QName qname = field.getXmlName();
if (qname == null)
{
qname = new QName("", propName);
}
if (propertyDescriptor[i].isReadable() &&
!propertyDescriptor[i].isIndexed())
{
Object propValue = propertyDescriptor[i].get(value);
if (propValue != null)
{
setAttributeProperty(propValue, qname, attrs, context);
}
}
}
}
catch (Exception e)
{
return attrs;
}
return attrs;
}
private void setAttributeProperty(Object propValue,
QName qname,
AttributesImpl attrs,
SerializationContext context) throws Exception
{
String propString = context.getValueAsString(propValue, null);
String namespace = qname.getNamespaceURI();
String localName = qname.getLocalPart();
SOAPConstants soapConstants = context.getMessageContext().getSOAPConstants();
QName parentElementQName = (QName)context.getElementStack().peek();
QName headerQName = soapConstants.getHeaderQName();
if (headerQName.equals(parentElementQName))
{
if (localName.equals("mustUnderstand") || localName.equals("role"))
{
namespace = soapConstants.getEnvelopeURI();
}
}
attrs.addAttribute(namespace,
localName,
context.attributeQName2String(qname),
"CDATA",
propString);
}
}