Map attNameToType = new HashMap();
if (null != schemaType.getAttributeModel()) {
SchemaLocalAttribute[] attributes = schemaType.getAttributeModel().getAttributes();
for (int i = 0; i < attributes.length; i++) {
SchemaLocalAttribute attribute = attributes[i];
Object old = attNameToType.put(attribute.getName().getLocalPart(), attribute);
if (old != null) {
throw new DeploymentException("Complain to your expert group member, spec does not support attributes with the same local name and differing namespaces: original: " + old + ", duplicate local name: " + attribute);
}
}
}
VariableMappingType[] variableMappings = javaXmlTypeMapping.getVariableMappingArray();
// short-circuit the processing of arrays as they should not define variable-mapping elements.
if (javaClass.isArray()) {
if (0 != variableMappings.length) {
// for portability reason we simply warn and not fail.
log.warn("Ignoring variable-mapping defined for class " + javaClass + " which is an array.");
}
typeInfo.setFields(new FieldDesc[0]);
return;
}
FieldDesc[] fields = new FieldDesc[variableMappings.length];
typeInfo.setFields(fields);
PropertyDescriptor[] propertyDescriptors = new PropertyDescriptor[0];
try {
propertyDescriptors = Introspector.getBeanInfo(javaClass).getPropertyDescriptors();
} catch (IntrospectionException e) {
throw new DeploymentException("Class " + javaClass + " is not a valid javabean", e);
}
Map properties = new HashMap();
for (int i = 0; i < propertyDescriptors.length; i++) {
PropertyDescriptor propertyDescriptor = propertyDescriptors[i];
properties.put(propertyDescriptor.getName(), propertyDescriptor.getPropertyType());
}
for (int i = 0; i < variableMappings.length; i++) {
VariableMappingType variableMapping = variableMappings[i];
String fieldName = variableMapping.getJavaVariableName().getStringValue().trim();
if (variableMapping.isSetXmlAttributeName()) {
AttributeDesc attributeDesc = new AttributeDesc();
attributeDesc.setFieldName(fieldName);
Class javaType = (Class) properties.get(fieldName);
if (javaType == null) {
throw new DeploymentException("field name " + fieldName + " not found in " + properties);
}
String attributeLocalName = variableMapping.getXmlAttributeName().getStringValue().trim();
QName xmlName = new QName("", attributeLocalName);
attributeDesc.setXmlName(xmlName);
SchemaLocalAttribute attribute = (SchemaLocalAttribute) attNameToType.get(attributeLocalName);
if (null == attribute) {
throw new DeploymentException("attribute " + xmlName + " not found in schema " + schemaType.getName());
}
attributeDesc.setXmlType(attribute.getType().getName());
fields[i] = attributeDesc;
} else {
ElementDesc elementDesc = new ElementDesc();
elementDesc.setFieldName(fieldName);