;
} else if (SchemaParticle.SEQUENCE == schemaType.getContentModel().getParticleType()
|| SchemaParticle.ALL == schemaType.getContentModel().getParticleType()) {
SchemaParticle[] properties = schemaType.getContentModel().getParticleChildren();
for (int i = 0; i < properties.length; i++) {
SchemaParticle parameter = properties[i];
paramNameToType.put(parameter.getName(), parameter);
}
} else if (SchemaParticle.ELEMENT == schemaType.getContentModel().getParticleType()) {
SchemaParticle parameter = schemaType.getContentModel();
paramNameToType.put(parameter.getName(), parameter);
} else {
throw new DeploymentException("Only element, sequence, and all particle types are supported." +
" SchemaType name =" + schemaType.getName());
}
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);
Class javaType = (Class) properties.get(fieldName);
if (javaType == null) {
//see if it is a public field
try {
Field field = javaClass.getField(fieldName);
javaType = field.getType();
} catch (NoSuchFieldException e) {
throw new DeploymentException("field name " + fieldName + " not found in " + properties, e);
}
}
QName xmlName = new QName("", variableMapping.getXmlElementName().getStringValue().trim());
SchemaParticle particle = (SchemaParticle) paramNameToType.get(xmlName);
if (null == particle) {
xmlName = new QName(ns, variableMapping.getXmlElementName().getStringValue().trim());
particle = (SchemaParticle) paramNameToType.get(xmlName);
if (null == particle) {
throw new DeploymentException("element " + xmlName + " not found in schema " + schemaType.getName());
}
} else if (SchemaParticle.ELEMENT != particle.getParticleType()) {
throw new DeploymentException(xmlName + " is not an element in schema " + schemaType.getName());
}
elementDesc.setNillable(particle.isNillable() || hasEncoded);
elementDesc.setXmlName(xmlName);
if (null != particle.getType().getName()) {
elementDesc.setXmlType(particle.getType().getName());
} else {
QName anonymousName;
if (key.isAnonymous()) {
anonymousName = new QName(key.getqName().getNamespaceURI(), key.getqName().getLocalPart() +
">" + particle.getName().getLocalPart());
} else {
anonymousName = new QName(key.getqName().getNamespaceURI(),
">" + key.getqName().getLocalPart() + ">" + particle.getName().getLocalPart());
}
elementDesc.setXmlType(anonymousName);
}
if (javaType.isArray()) {
elementDesc.setMinOccurs(particle.getIntMinOccurs());
elementDesc.setMaxOccurs(particle.getIntMaxOccurs());
//TODO axis seems to have the wrong name for this property based on how it is used
elementDesc.setMaxOccursUnbounded(particle.getIntMaxOccurs() > 1);
}
fields[i] = elementDesc;
}
}