use of org.apache.axis.utils.BeanPropertyDescriptor in project Lucee by lucee.
the class BeanDeserializer method onStartElement.
/**
* Set the bean properties that correspond to element attributes.
*
* This method is invoked after startElement when the element requires
* deserialization (i.e. the element is not an href and the value is not
* nil.)
* @param namespace is the namespace of the element
* @param localName is the name of the element
* @param prefix is the prefix of the element
* @param attributes are the attributes on the element...used to get the
* type
* @param context is the DeserializationContext
*/
@Override
public void onStartElement(String namespace, String localName, String prefix, Attributes attributes, DeserializationContext context) throws SAXException {
// This code may no longer be needed.
if (value == null && constructorToUse == null) {
// create a value
try {
value = javaType.newInstance();
} catch (Exception e) {
throw new SAXException(Messages.getMessage("cantCreateBean00", javaType.getName(), e.toString()));
}
}
// so we are done.
if (typeDesc == null)
return;
// correspond to attributes
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;
FieldDesc fieldDesc = typeDesc.getFieldByName(fieldName);
// look for the attribute property
BeanPropertyDescriptor bpd = (BeanPropertyDescriptor) propertyMap.get(fieldName);
if (bpd != null) {
if (constructorToUse == null) {
// check only if default constructor
if (!bpd.isWriteable() || bpd.isIndexed())
continue;
}
// Get the Deserializer for the attribute
Deserializer dSer = getDeserializer(fieldDesc.getXmlType(), bpd.getType(), null, context);
if (dSer == null) {
dSer = context.getDeserializerForClass(bpd.getType());
// simple types..
if (dSer instanceof ArrayDeserializer) {
SimpleListDeserializerFactory factory = new SimpleListDeserializerFactory(bpd.getType(), fieldDesc.getXmlType());
dSer = (Deserializer) factory.getDeserializerAs(dSer.getMechanismType());
}
}
if (dSer == null)
throw new SAXException(Messages.getMessage("unregistered00", bpd.getType().toString()));
if (!(dSer instanceof SimpleDeserializer))
throw new SAXException(Messages.getMessage("AttrNotSimpleType00", bpd.getName(), bpd.getType().toString()));
// it in the bean
try {
dSer.onStartElement(namespace, localName, prefix, attributes, context);
Object val = ((SimpleDeserializer) dSer).makeValue(attributes.getValue(i));
if (constructorToUse == null) {
bpd.set(value, val);
} else {
// add value for our constructor
if (constructorTarget == null) {
constructorTarget = new ConstructorTarget(constructorToUse, this);
}
constructorTarget.set(val);
}
} catch (Exception e) {
throw new SAXException(e);
}
}
// if
}
// attribute loop
}
use of org.apache.axis.utils.BeanPropertyDescriptor in project Lucee by lucee.
the class BeanDeserializer method handleMixedContent.
protected void handleMixedContent() throws SAXException {
BeanPropertyDescriptor propDesc = getAnyPropertyDesc();
if (propDesc == null || val.size() == 0) {
return;
}
String textValue = val.toString().trim();
val.reset();
if (textValue.length() == 0) {
return;
}
try {
MessageElement[] curElements = (MessageElement[]) propDesc.get(value);
int length = 0;
if (curElements != null) {
length = curElements.length;
}
MessageElement[] newElements = new MessageElement[length + 1];
if (curElements != null) {
System.arraycopy(curElements, 0, newElements, 0, length);
}
MessageElement thisEl = new MessageElement(new org.apache.axis.message.Text(textValue));
newElements[length] = thisEl;
propDesc.set(value, newElements);
} catch (Exception e) {
throw new SAXException(e);
}
}
use of org.apache.axis.utils.BeanPropertyDescriptor in project Lucee by lucee.
the class BeanDeserializer method onStartChild.
/**
* Deserializer interface called on each child element encountered in
* the XML stream.
* @param namespace is the namespace of the child element
* @param localName is the local name of the child element
* @param prefix is the prefix used on the name of the child element
* @param attributes are the attributes of the child element
* @param context is the deserialization context.
* @return is a Deserializer to use to deserialize a child (must be
* a derived class of SOAPHandler) or null if no deserialization should
* be performed.
*/
@Override
public SOAPHandler onStartChild(String namespace, String localName, String prefix, Attributes attributes, DeserializationContext context) throws SAXException {
handleMixedContent();
BeanPropertyDescriptor propDesc = null;
FieldDesc fieldDesc = null;
SOAPConstants soapConstants = context.getSOAPConstants();
String encodingStyle = context.getEncodingStyle();
boolean isEncoded = Constants.isSOAP_ENC(encodingStyle);
QName elemQName = new QName(namespace, localName);
// The collectionIndex needs to be reset for Beans with multiple arrays
if ((prevQName == null) || (!prevQName.equals(elemQName))) {
collectionIndex = -1;
}
boolean isArray = false;
QName itemQName = null;
if (typeDesc != null) {
// Lookup the name appropriately (assuming an unqualified
// name for SOAP encoding, using the namespace otherwise)
String fieldName = typeDesc.getFieldNameForElement(elemQName, isEncoded);
propDesc = (BeanPropertyDescriptor) propertyMap.get(fieldName);
fieldDesc = typeDesc.getFieldByName(fieldName);
if (fieldDesc != null) {
ElementDesc element = (ElementDesc) fieldDesc;
isArray = element.isMaxOccursUnbounded();
itemQName = element.getItemQName();
}
}
if (propDesc == null) {
// look for a field by this name.
propDesc = (BeanPropertyDescriptor) propertyMap.get(localName);
}
// Workaround
if (propDesc == null) {
StringBuilder sb = new StringBuilder();
sb.append(Character.toLowerCase(localName.charAt(0)));
if (localName.length() > 1)
sb.append(localName.substring(1));
// look for a field by this name.
propDesc = (BeanPropertyDescriptor) propertyMap.get(sb.toString());
}
// reporting a problem
if (propDesc == null || (((prevQName != null) && prevQName.equals(elemQName) && !(propDesc.isIndexed() || isArray) && getAnyPropertyDesc() != null))) {
// try to put unknown elements into a SOAPElement property, if
// appropriate
prevQName = elemQName;
propDesc = getAnyPropertyDesc();
if (propDesc != null) {
try {
MessageElement[] curElements = (MessageElement[]) propDesc.get(value);
int length = 0;
if (curElements != null) {
length = curElements.length;
}
MessageElement[] newElements = new MessageElement[length + 1];
if (curElements != null) {
System.arraycopy(curElements, 0, newElements, 0, length);
}
MessageElement thisEl = context.getCurElement();
newElements[length] = thisEl;
propDesc.set(value, newElements);
// defer it to the child SOAPHandler
if (!localName.equals(thisEl.getName())) {
return new SOAPHandler(newElements, length);
}
return new SOAPHandler();
} catch (Exception e) {
throw new SAXException(e);
}
}
}
if (propDesc == null) {
// No such field
throw new SAXException(Messages.getMessage("badElem00", javaType.getName(), localName));
}
prevQName = elemQName;
// Get the child's xsi:type if available
QName childXMLType = context.getTypeFromAttributes(namespace, localName, attributes);
String href = attributes.getValue(soapConstants.getAttrHref());
Class fieldType = propDesc.getType();
// If no xsi:type or href, check the meta-data for the field
if (childXMLType == null && fieldDesc != null && href == null) {
childXMLType = fieldDesc.getXmlType();
if (itemQName != null) {
// This is actually a wrapped literal array and should be
// deserialized with the ArrayDeserializer
childXMLType = Constants.SOAP_ARRAY;
fieldType = propDesc.getActualType();
} else {
childXMLType = fieldDesc.getXmlType();
}
}
// Get Deserializer for child, default to using DeserializerImpl
Deserializer dSer = getDeserializer(childXMLType, fieldType, href, context);
// no good metadata).
if (dSer == null) {
dSer = context.getDeserializerForClass(propDesc.getType());
}
// Fastpath nil checks...
if (context.isNil(attributes)) {
if ((propDesc.isIndexed() || isArray)) {
if (!((dSer != null) && (dSer instanceof ArrayDeserializer))) {
collectionIndex++;
dSer.registerValueTarget(new BeanPropertyTarget(value, propDesc, collectionIndex));
addChildDeserializer(dSer);
return (SOAPHandler) dSer;
}
}
return null;
}
if (dSer == null) {
throw new SAXException(Messages.getMessage("noDeser00", childXMLType.toString()));
}
if (constructorToUse != null) {
if (constructorTarget == null) {
constructorTarget = new ConstructorTarget(constructorToUse, this);
}
dSer.registerValueTarget(constructorTarget);
} else if (propDesc.isWriteable()) {
// Register value target
if ((itemQName != null || propDesc.isIndexed() || isArray) && !(dSer instanceof ArrayDeserializer)) {
collectionIndex++;
dSer.registerValueTarget(new BeanPropertyTarget(value, propDesc, collectionIndex));
} else {
// If we're here, the element maps to a single field value,
// whether that be a "basic" type or an array, so use the
// normal (non-indexed) BeanPropertyTarget form.
collectionIndex = -1;
dSer.registerValueTarget(new BeanPropertyTarget(value, propDesc));
}
}
// Let the framework know that we need this deserializer to complete
// for the bean to complete.
addChildDeserializer(dSer);
return (SOAPHandler) dSer;
}
use of org.apache.axis.utils.BeanPropertyDescriptor in project Lucee by lucee.
the class BeanDeserializerFactory method getProperties.
/**
* Get a list of the bean properties
*/
public static Map getProperties(Class javaType, TypeDesc typeDesc) {
Map propertyMap = null;
if (typeDesc != null) {
propertyMap = typeDesc.getPropertyDescriptorMap();
} else {
BeanPropertyDescriptor[] pd = BeanUtils.getPd(javaType, null);
// use this to get rid of case sensitivity
propertyMap = new StructImpl();
// loop through properties and grab the names for later
for (int i = 0; i < pd.length; i++) {
BeanPropertyDescriptor descriptor = pd[i];
propertyMap.put(descriptor.getName(), descriptor);
}
}
return propertyMap;
}
Aggregations