XMLDescriptor refDesc = getDescriptorByName(refClassName, descriptors);
if (refDesc == null) {
throw DescriptorException.descriptorIsMissing(refClassName, mapping);
}
Element element = buildElement(((XMLField) mapping.getField()).getXPathFragment(), null, Occurs.ZERO, Occurs.UNBOUNDED);
ComplexType ctype = null;
if (refDesc.getSchemaReference() == null) {
ctype = buildComplexType(true, refDesc, schemaForNamespace, workingSchema, properties, descriptors);
} else {
element.setType(getSchemaTypeString(refDesc.getSchemaReference().getSchemaContextAsQName(workingSchema.getNamespaceResolver()), workingSchema));
}
XPathFragment frag = ((XMLField) mapping.getField()).getXPathFragment();
String fragUri = frag.getNamespaceURI();
if (fragUri != null) {
// may need to add a global element
Schema s = getSchema(fragUri, null, schemaForNamespace, properties);
String targetNS = workingSchema.getTargetNamespace();
if ((s.isElementFormDefault() && !fragUri.equals(targetNS)) || (!s.isElementFormDefault() && !fragUri.equals(""))) {
if (s.getTopLevelElements().get(frag.getShortName()) == null) {
Element globalElement = new Element();
globalElement.setName(frag.getLocalName());
if (ctype != null) {
globalElement.setComplexType(ctype);
} else {
globalElement.setType(getSchemaTypeString(refDesc.getSchemaReference().getSchemaContextAsQName(workingSchema.getNamespaceResolver()), workingSchema));
}
s.getTopLevelElements().put(frag.getShortName(), globalElement);
}
element = new Element();
element.setMinOccurs(Occurs.ZERO);
element.setMaxOccurs(Occurs.UNBOUNDED);
element.setRef(frag.getShortName());
} else {
element.setComplexType(ctype);