set the bean property with specified value
try {
// Set the value on the bean property.
// Use the indexed property method if the
// index is set.
if (index < 0) {
pd.set(object, value);
} else {
pd.set(object, index, value);
}
} catch (Exception e) {
try {
// If an exception occurred,
// see it the value can be converted into
// the expected type.
Class type = pd.getType();
if (value.getClass().isArray()
&& value.getClass().getComponentType().isPrimitive()
&& type.isArray()
&& type.getComponentType().equals(Object.class))
{
//we make our own array type here.
type = Array.newInstance(JavaUtils.getWrapperClass(value.getClass().getComponentType()),0).getClass();
}
if (JavaUtils.isConvertable(value, type)) {
value = JavaUtils.convert(value, type);
if (index < 0)
pd.set(object, value);
else
pd.set(object, index, value);
} else {
// It is possible that an indexed
// format was expected, but the
// entire array was sent. In such
// cases traverse the array and
// call the setter for each item.
if (index == 0 &&
value.getClass().isArray() &&
!type.getClass().isArray()) {
for (int i=0; i<Array.getLength(value); i++) {
Object item =
JavaUtils.convert(Array.get(value, i), type);
pd.set(object, i, item);
}
} else {
// Can't proceed. Throw an exception that
// will be caught in the catch block below.
throw e;
}
}
} catch (Exception ex) {
// Throw a SAX exception with an informative
// message.
String field= pd.getName();
if (index >=0) {
field += "[" + index + "]";
}
if (log.isErrorEnabled()) {
//TODO: why is this just logged on the server-side and not thrown back to the client???
String valueType = "null";
if (value != null)
valueType = value.getClass().getName();
log.error(Messages.getMessage("cantConvert02",
new String[]{
valueType,
field,
(index >= 0) ?
pd.getType().getComponentType().getName() :
pd.getType().getName()
}));
}
if(ex instanceof InvocationTargetException) {
Throwable t = ((InvocationTargetException)ex).getTargetException();
if( t != null) {
String classname = this.object.getClass().getName();
//show the context where this exception occured.
throw new SAXException(Messages.getMessage("cantConvert04",
new String[] {
classname,
field,
(value==null)?null:value.toString(),
t.getMessage()}));
}
}
throw new SAXException(ex);
}
}