package org.jboss.axis.encoding.ser;
import org.jboss.axis.AxisFault;
import org.jboss.axis.Constants;
import org.jboss.axis.description.FieldDesc;
import org.jboss.axis.description.TypeDesc;
import org.jboss.axis.encoding.SerializationContext;
import org.jboss.axis.encoding.SimpleType;
import org.jboss.axis.encoding.SimpleValueSerializer;
import org.jboss.axis.utils.BeanPropertyDescriptor;
import org.jboss.axis.utils.BeanUtils;
import org.jboss.axis.utils.Messages;
import org.jboss.axis.wsdl.fromJava.Types;
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;
public class SimpleSerializer implements SimpleValueSerializer
{
public QName xmlType;
public Class javaType;
private BeanPropertyDescriptor[] propertyDescriptor = null;
private TypeDesc typeDesc = null;
public SimpleSerializer(Class javaType, QName xmlType)
{
this.xmlType = xmlType;
this.javaType = javaType;
init();
}
public SimpleSerializer(Class javaType, QName xmlType, TypeDesc typeDesc)
{
this.xmlType = xmlType;
this.javaType = javaType;
this.typeDesc = typeDesc;
init();
}
private void init()
{
if (SimpleType.class.isAssignableFrom(javaType))
{
if (typeDesc == null)
{
typeDesc = TypeDesc.getTypeDescForClass(javaType);
}
if (typeDesc != null)
{
propertyDescriptor = typeDesc.getPropertyDescriptors();
}
else
{
propertyDescriptor = BeanUtils.getPd(javaType, null);
}
}
}
public void serialize(QName name, Attributes attributes,
Object value, SerializationContext context)
throws IOException
{
if (value != null && value.getClass() == java.lang.Object.class)
{
throw new IOException(Messages.getMessage("cantSerialize02"));
}
if (value instanceof SimpleType)
attributes = getObjectAttributes(value, attributes, context);
if (name != null)
context.startElement(name, attributes);
if (value != null)
context.writeSafeString(getValueAsString(value, context));
if (name != null)
context.endElement();
}
public String getValueAsString(Object value, SerializationContext context)
{
if (value instanceof Float ||
value instanceof Double)
{
double data = 0.0;
if (value instanceof Float)
{
data = ((Float)value).doubleValue();
}
else
{
data = ((Double)value).doubleValue();
}
if (Double.isNaN(data))
{
return "NaN";
}
else if (data == Double.POSITIVE_INFINITY)
{
return "INF";
}
else if (data == Double.NEGATIVE_INFINITY)
{
return "-INF";
}
}
return value.toString();
}
private 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)
{
String propString = getValueAsString(propValue, context);
String namespace = qname.getNamespaceURI();
String localName = qname.getLocalPart();
attrs.addAttribute(namespace,
localName,
context.qName2String(qname),
"CDATA",
propString);
}
}
}
}
catch (Exception e)
{
return attrs;
}
return attrs;
}
public String getMechanismType()
{
return Constants.AXIS_SAX;
}
public Element writeSchema(Class javaType, Types types) throws Exception
{
if (!SimpleType.class.isAssignableFrom(javaType))
return null;
Element complexType = types.createElement("complexType");
types.writeSchemaElement(xmlType, complexType);
complexType.setAttribute("name", xmlType.getLocalPart());
Element simpleContent = types.createElement("simpleContent");
complexType.appendChild(simpleContent);
Element extension = types.createElement("extension");
simpleContent.appendChild(extension);
String base = "string";
for (int i = 0; i < propertyDescriptor.length; i++)
{
String propName = propertyDescriptor[i].getName();
if (!propName.equals("value"))
{
if (typeDesc != null)
{
FieldDesc field = typeDesc.getFieldByName(propName);
if (field != null)
{
if (field.isElement())
{
}
QName qname = field.getXmlName();
if (qname == null)
{
qname = new QName("", propName);
}
Class fieldType = propertyDescriptor[i].getType();
if (!types.isAcceptableAsAttribute(fieldType))
{
throw new AxisFault(Messages.getMessage("AttrNotSimpleType00",
propName,
fieldType.getName()));
}
Element elem = types.createAttributeElement(propName,
fieldType,
field.getXmlType(),
false,
extension.getOwnerDocument());
extension.appendChild(elem);
}
}
continue;
}
BeanPropertyDescriptor bpd = propertyDescriptor[i];
Class type = bpd.getType();
if (!types.isAcceptableAsAttribute(type))
{
throw new AxisFault(Messages.getMessage("AttrNotSimpleType01",
type.getName()));
}
base = types.writeType(type);
extension.setAttribute("base", base);
}
return complexType;
}
}