if (targetSchema.getTopLevelElements().get(next.getLocalPart()) == null) {
Element element = new Element();
element.setName(next.getLocalPart());
element.setNillable(nextElement.isNillable());
JavaClass javaClass = nextElement.getJavaType();
//First check for built in type
QName schemaType = (QName) helper.getXMLToJavaTypeMap().get(javaClass.getRawName());
if (schemaType != null) {
element.setType(Constants.SCHEMA_PREFIX + COLON + schemaType.getLocalPart());
} else if (areEquals(javaClass, JAVAX_ACTIVATION_DATAHANDLER) || areEquals(javaClass, byte[].class) || areEquals(javaClass, Byte[].class) || areEquals(javaClass, Image.class) || areEquals(javaClass, Source.class) || areEquals(javaClass, JAVAX_MAIL_INTERNET_MIMEMULTIPART)) {
schemaType = Constants.BASE_64_BINARY_QNAME;
if(nextElement.getTypeMappingInfo() != null) {
if(nextElement.isXmlAttachmentRef()) {
schemaType = Constants.SWA_REF_QNAME;
}
if (nextElement.getXmlMimeType() != null) {
element.getAttributesMap().put(Constants.EXPECTED_CONTENT_TYPES_QNAME, nextElement.getXmlMimeType());
}
}
String prefix = getOrGeneratePrefixForNamespace(schemaType.getNamespaceURI(), targetSchema);
element.setType(prefix + COLON + schemaType.getLocalPart());
} else if (areEquals(javaClass, CoreClassConstants.XML_GREGORIAN_CALENDAR)) {
schemaType = Constants.ANY_SIMPLE_TYPE_QNAME;
element.setType(Constants.SCHEMA_PREFIX + COLON + schemaType.getLocalPart());
} else {
TypeInfo type = (TypeInfo) this.typeInfo.get(javaClass.getQualifiedName());
if (type != null) {
String typeName = null;
if (type.isComplexType()) {
typeName = type.getComplexType().getName();
} else {
typeName = type.getSimpleType().getName();
}
// may need an anonymous complex type
if (typeName == null) {
Schema schema = getSchemaForNamespace(next.getNamespaceURI());
ComplexType cType = createComplexTypeForClass(javaClass, type);
TypeDefParticle particle = null;
if(cType.getComplexContent() != null && cType.getComplexContent().getExtension() != null) {
particle = cType.getComplexContent().getExtension().getTypeDefParticle();
} else {
particle = cType.getTypeDefParticle();
}
addToSchemaType(type, type.getPropertyList(), particle, cType, schema);
targetSchema = schema;
element.setComplexType(cType);
} else {
// check namespace of schemaType
if (type.getClassNamespace().equals(namespaceURI)) {
//no need to prefix here
String prefix = targetSchema.getNamespaceResolver().resolveNamespaceURI(namespaceURI);
if(prefix != null && !(prefix.equals(EMPTY_STRING))) {
element.setType(prefix + COLON + typeName);
} else {
element.setType(typeName);
}
} else {
Schema complexTypeSchema = getSchemaForNamespace(type.getClassNamespace());
String complexTypeSchemaNS = type.getClassNamespace();
if (complexTypeSchemaNS == null) {
complexTypeSchemaNS = EMPTY_STRING;
}
addImportIfRequired(targetSchema, complexTypeSchema, type.getClassNamespace());
String prefix = targetSchema.getNamespaceResolver().resolveNamespaceURI(complexTypeSchemaNS);
if (prefix != null) {
element.setType(prefix + COLON + typeName);
} else {
element.setType(typeName);
}
}
}
}
}
if (nextElement.getSubstitutionHead() != null) {
String subLocal = nextElement.getSubstitutionHead().getLocalPart();
String subNamespace = nextElement.getSubstitutionHead().getNamespaceURI();
String prefix = getPrefixForNamespace(targetSchema, subNamespace);
if (prefix == null || prefix.equals(EMPTY_STRING)) {
element.setSubstitutionGroup(subLocal);
} else {
element.setSubstitutionGroup(prefix + COLON + subLocal);
}
}
targetSchema.addTopLevelElement(element);
SchemaTypeInfo info = this.schemaTypeInfo.get(javaClass.getQualifiedName());
if (info == null) {
// probably for a simple type, not generated
info = new SchemaTypeInfo();
info.setSchemaTypeName(schemaType);
schemaTypeInfo.put(javaClass.getQualifiedName(), info);
}
info.getGlobalElementDeclarations().add(next);
}
}
}