package org.jboss.axis.encoding.ser;
import org.jboss.axis.description.TypeDesc;
import org.jboss.axis.encoding.DeserializationContext;
import org.jboss.axis.encoding.Deserializer;
import org.jboss.axis.encoding.DeserializerImpl;
import org.jboss.axis.encoding.SimpleType;
import org.jboss.axis.encoding.TypeMapping;
import org.jboss.axis.message.SOAPHandler;
import org.jboss.axis.utils.BeanPropertyDescriptor;
import org.jboss.axis.utils.BeanUtils;
import org.jboss.axis.utils.Messages;
import org.jboss.logging.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import javax.xml.namespace.QName;
import java.io.CharArrayWriter;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
public class SimpleDeserializer extends DeserializerImpl
{
private static Logger log = Logger.getLogger(SimpleDeserializer.class.getName());
private final CharArrayWriter val = new CharArrayWriter();
private Constructor constructor = null;
private Map propertyMap = null;
private HashMap attributeMap = null;
public QName xmlType;
public Class javaType;
private TypeDesc typeDesc = null;
protected SimpleDeserializer cacheStringDSer = null;
protected QName cacheXMLType = null;
public SimpleDeserializer(Class javaType, QName xmlType)
{
this.xmlType = xmlType;
this.javaType = javaType;
init();
}
public SimpleDeserializer(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)
{
propertyMap = typeDesc.getPropertyDescriptorMap();
}
else
{
BeanPropertyDescriptor[] pd = BeanUtils.getPd(javaType, null);
propertyMap = new HashMap();
for (int i = 0; i < pd.length; i++)
{
BeanPropertyDescriptor descriptor = pd[i];
propertyMap.put(descriptor.getName(), descriptor);
}
}
}
}
public void reset()
{
val.reset();
attributeMap = null; isNil = false; isEnded = false; }
public void removeValueTargets()
{
if (targets != null)
{
targets.clear();
}
}
public void setConstructor(Constructor c)
{
constructor = c;
}
public SOAPHandler onStartChild(String namespace,
String localName,
String prefix,
Attributes attributes,
DeserializationContext context)
throws SAXException
{
throw new SAXException(Messages.getMessage("cantHandle00", "SimpleDeserializer"));
}
public void characters(char[] chars, int start, int end)
throws SAXException
{
val.write(chars, start, end);
}
public void onEndElement(String namespace, String localName,
DeserializationContext context)
throws SAXException
{
if (isNil)
{
value = null;
return;
}
try
{
value = makeValue(val.toString());
}
catch (InvocationTargetException ite)
{
Throwable realException = ite.getTargetException();
if (realException instanceof Exception)
throw new SAXException((Exception)realException);
else
throw new SAXException(ite.getMessage());
}
catch (Exception e)
{
throw new SAXException(e);
}
setSimpleTypeAttributes();
}
public Object makeValue(String source) throws Exception
{
log.debug("Making value [" + source + "] for javaType: " + javaType);
if (javaType == boolean.class || javaType == Boolean.class)
{
switch (source.charAt(0))
{
case '0':
case 'f':
case 'F':
return Boolean.FALSE;
case '1':
case 't':
case 'T':
return Boolean.TRUE;
default:
throw new NumberFormatException(Messages.getMessage("badBool00"));
}
}
if (javaType == float.class ||
javaType == java.lang.Float.class)
{
if (source.equals("NaN"))
{
return new Float(Float.NaN);
}
else if (source.equals("INF"))
{
return new Float(Float.POSITIVE_INFINITY);
}
else if (source.equals("-INF"))
{
return new Float(Float.NEGATIVE_INFINITY);
}
}
if (javaType == double.class ||
javaType == java.lang.Double.class)
{
if (source.equals("NaN"))
{
return new Double(Double.NaN);
}
else if (source.equals("INF"))
{
return new Double(Double.POSITIVE_INFINITY);
}
else if (source.equals("-INF"))
{
return new Double(Double.NEGATIVE_INFINITY);
}
}
return constructor.newInstance(new Object[]{source});
}
public void onStartElement(String namespace, String localName,
String prefix, Attributes attributes,
DeserializationContext context)
throws SAXException
{
if (typeDesc == null)
return;
for (int i = 0; i < attributes.getLength(); i++)
{
QName attrQName = new QName(attributes.getURI(i),
attributes.getLocalName(i));
String fieldName = typeDesc.getFieldNameForAttribute(attrQName);
if (fieldName == null)
continue;
BeanPropertyDescriptor bpd =
(BeanPropertyDescriptor)propertyMap.get(fieldName);
if (bpd != null)
{
if (!bpd.isWriteable() || bpd.isIndexed()) continue;
TypeMapping tm = context.getTypeMapping();
Class type = bpd.getType();
QName qn = tm.getTypeQName(type);
if (qn == null)
throw new SAXException(Messages.getMessage("unregistered00", type.toString()));
Deserializer dSer = context.getDeserializerForType(qn);
if (dSer == null)
throw new SAXException(Messages.getMessage("noDeser00", type.toString()));
if (!(dSer instanceof SimpleDeserializer))
throw new SAXException(Messages.getMessage("AttrNotSimpleType00",
bpd.getName(),
type.toString()));
if (attributeMap == null)
{
attributeMap = new HashMap();
}
try
{
Object val = ((SimpleDeserializer)dSer).
makeValue(attributes.getValue(i));
attributeMap.put(fieldName, val);
}
catch (Exception e)
{
throw new SAXException(e);
}
} } }
private void setSimpleTypeAttributes() throws SAXException
{
if (!SimpleType.class.isAssignableFrom(javaType) ||
attributeMap == null)
return;
Set entries = attributeMap.entrySet();
for (Iterator iterator = entries.iterator(); iterator.hasNext();)
{
Map.Entry entry = (Map.Entry)iterator.next();
String name = (String)entry.getKey();
Object val = entry.getValue();
BeanPropertyDescriptor bpd =
(BeanPropertyDescriptor)propertyMap.get(name);
if (!bpd.isWriteable() || bpd.isIndexed()) continue;
try
{
bpd.set(value, val);
}
catch (Exception e)
{
throw new SAXException(e);
}
}
}
}