{
ElementToFieldMapping fieldMapping = elementToFieldMapping.get(
new ElementToFieldMappingKey(localName, o.getClass())
);
FieldInfo fieldInfo;
if(fieldMapping != null)
{
fieldInfo = fieldMapping.fieldInfo;
}
else
{
String fieldName = Util.xmlNameToFieldName(localName, true);
fieldInfo = FieldInfo.getFieldInfo(o.getClass(), fieldName, true);
}
child = get(o, localName, fieldInfo);
}
if(child == null)
{
child = newInstance(mapping.cls);
}
if(attrs != null)
{
for(int i = 0; i < attrs.getLength(); ++i)
{
if(attrs.getLocalName(i).length() > 0)
{
if(!attrs.getQName(i).startsWith("xsi:")) //todo horrible
{
setAttribute(child, attrs.getLocalName(i), attrs.getValue(i), ctx);
}
}
}
}
}
catch(RuntimeException e)
{
throw e;
}
catch(Exception e)
{
throw new NestedRuntimeException("newChild failed for o=" +
o +
", uri=" +
namespaceURI +
", local="
+ localName + ", attrs=" + attrs, e
);
}
}
else
{
if(o instanceof Collection)
{
child = create(namespaceURI, localName, type);
}
else
{
Class<?> oCls;
if(o instanceof Immutable)
{
oCls = ((Immutable)o).cls;
}
else
{
oCls = o.getClass();
}
String fieldName = Util.xmlNameToFieldName(localName, true);
FieldInfo fieldInfo = FieldInfo.getFieldInfo(oCls, fieldName, true);
if(Collection.class.isAssignableFrom(fieldInfo.getType()))
{
child = get(o, localName, fieldInfo);
// now does this element really represent a Java collection or is it an element that can appear more than once?
// try to load the class and create an instance
Object item = null;
if(type == null || type != null && type.getTypeCategory() == XSTypeDefinition.COMPLEX_TYPE)
{
item = create(namespaceURI, localName, type);
}
if(item != null)
{
if(child == null)
{
setChild(new ArrayList(), o, localName);
}
child = item;
}
else
{
if(child == null)
{
child = new ArrayList<Object>();
}
}
}
else if(!Util.isAttributeType(fieldInfo.getType()))
{
// id there is no field mapping
ElementToFieldMapping fieldMapping = elementToFieldMapping.get(
new ElementToFieldMappingKey(localName, o.getClass())
);
TypeBinding converter = fieldMapping == null ? null : fieldMapping.converter;
// if converter != null it will be used in setValue
if(converter == null)
{
child = newInstance(fieldInfo.getType());
}
}
}
}