use of org.eclipse.persistence.jaxb.TypeMappingInfo in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateWrapperClassAndDescriptor.
private Class<?> generateWrapperClassAndDescriptor(TypeInfo type, QName next, ElementDeclaration nextElement, String nextClassName, String attributeTypeName) {
String namespaceUri = null;
if (next != null) {
// generate a class/descriptor for this element
namespaceUri = next.getNamespaceURI();
if (namespaceUri == null || namespaceUri.equals(XMLProcessor.DEFAULT)) {
namespaceUri = "";
}
}
TypeMappingInfo tmi = nextElement.getTypeMappingInfo();
Class<?> generatedClass = null;
JaxbClassLoader loader = getJaxbClassLoader();
if (tmi != null) {
generatedClass = CompilerHelper.getExisitingGeneratedClass(tmi, typeMappingInfoToGeneratedClasses, typeMappingInfoToAdapterClasses, helper.getClassLoader());
if (generatedClass == null) {
generatedClass = this.generateWrapperClass(loader.nextAvailableGeneratedClassName(), attributeTypeName, nextElement.isList(), next);
}
typeMappingInfoToGeneratedClasses.put(tmi, generatedClass);
} else {
generatedClass = this.generateWrapperClass(loader.nextAvailableGeneratedClassName(), attributeTypeName, nextElement.isList(), next);
}
this.qNamesToGeneratedClasses.put(next, generatedClass);
try {
Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
this.qNamesToDeclaredClasses.put(next, declaredClass);
} catch (Exception e) {
}
Descriptor desc = (Descriptor) project.getDescriptor(generatedClass);
if (desc == null) {
desc = new XMLDescriptor();
desc.setJavaClass(generatedClass);
if (nextElement.isList()) {
DirectCollectionMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLCompositeDirectCollectionMapping();
mapping.setAttributeName("value");
mapping.setXPath("text()");
mapping.setUsesSingleNode(true);
mapping.setReuseContainer(true);
if (type != null && type.isEnumerationType()) {
mapping.setValueConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo) type));
} else {
try {
Class<Object> fieldElementClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
mapping.setFieldElementClass(fieldElementClass);
} catch (ClassNotFoundException e) {
}
}
if (nextClassName.equals("[B") || nextClassName.equals("[Ljava.lang.Byte;")) {
((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
} else if (nextClassName.equals("javax.xml.namespace.QName")) {
((Field) mapping.getField()).setSchemaType(Constants.QNAME_QNAME);
}
desc.addMapping((CoreMapping) mapping);
} else {
if (nextElement.getJavaTypeName().equals(OBJECT_CLASS_NAME)) {
CompositeObjectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, XMLRecord> mapping = new XMLCompositeObjectMapping();
mapping.setAttributeName("value");
mapping.setSetMethodName("setValue");
mapping.setGetMethodName("getValue");
mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
mapping.setXPath(".");
setTypedTextField((Field) mapping.getField());
desc.addMapping((CoreMapping) mapping);
} else if (isBinaryData(nextElement.getJavaType())) {
BinaryDataMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, MimeTypePolicy, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLBinaryDataMapping();
mapping.setAttributeName("value");
mapping.setXPath(".");
((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
mapping.setSetMethodName("setValue");
mapping.setGetMethodName("getValue");
mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
mapping.getNullPolicy().setNullRepresentedByEmptyNode(false);
Class<?> attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(attributeTypeName, helper.getClassLoader());
mapping.setAttributeClassification(attributeClassification);
mapping.setShouldInlineBinaryData(false);
// if(nextElement.getTypeMappingInfo() != null) {
mapping.setSwaRef(nextElement.isXmlAttachmentRef());
mapping.setMimeType(nextElement.getXmlMimeType());
// }
desc.addMapping((CoreMapping) mapping);
} else {
DirectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLDirectMapping();
mapping.setNullValueMarshalled(true);
mapping.setAttributeName("value");
mapping.setXPath("text()");
mapping.setSetMethodName("setValue");
mapping.setGetMethodName("getValue");
if (nextElement.getDefaultValue() != null) {
mapping.setNullValue(nextElement.getDefaultValue());
mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
}
if (helper.isBuiltInJavaType(nextElement.getJavaType())) {
Class<?> attributeClassification = null;
if (nextElement.getJavaType().isPrimitive()) {
attributeClassification = XMLConversionManager.getDefaultManager().convertClassNameToClass(attributeTypeName);
} else {
attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(attributeTypeName, helper.getClassLoader());
}
mapping.setAttributeClassification(attributeClassification);
}
IsSetNullPolicy nullPolicy = new IsSetNullPolicy("isSetValue", false, true, XMLNullRepresentationType.ABSENT_NODE);
// nullPolicy.setNullRepresentedByEmptyNode(true);
mapping.setNullPolicy(nullPolicy);
if (type != null && type.isEnumerationType()) {
mapping.setConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo) type));
}
if (nextClassName.equals("[B") || nextClassName.equals("[Ljava.lang.Byte;")) {
((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
} else if (nextClassName.equals("javax.xml.namespace.QName")) {
((Field) mapping.getField()).setSchemaType(Constants.QNAME_QNAME);
}
if (nextElement.getJavaTypeAdapterClass() != null) {
mapping.setConverter(new XMLJavaTypeConverter(nextElement.getJavaTypeAdapterClass()));
}
desc.addMapping((CoreMapping) mapping);
}
}
if (next != null) {
NamespaceInfo info = getNamespaceInfoForURI(namespaceUri);
if (info != null) {
NamespaceResolver resolver = getNamespaceResolverForDescriptor(info);
String prefix = null;
if (namespaceUri != Constants.EMPTY_STRING) {
prefix = resolver.resolveNamespaceURI(namespaceUri);
if (prefix == null) {
prefix = getPrefixForNamespace(namespaceUri, resolver);
}
}
desc.setNamespaceResolver(resolver);
if (nextElement.isXmlRootElement()) {
desc.setDefaultRootElement(getQualifiedString(prefix, next.getLocalPart()));
} else {
desc.setDefaultRootElement("");
desc.addRootElement(getQualifiedString(prefix, next.getLocalPart()));
desc.setResultAlwaysXMLRoot(true);
}
} else {
if (namespaceUri.equals("")) {
desc.setDefaultRootElement(next.getLocalPart());
} else {
NamespaceResolver resolver = new org.eclipse.persistence.oxm.NamespaceResolver();
String prefix = getPrefixForNamespace(namespaceUri, resolver);
desc.setNamespaceResolver(resolver);
if (nextElement.isXmlRootElement()) {
desc.setDefaultRootElement(getQualifiedString(prefix, next.getLocalPart()));
} else {
desc.setDefaultRootElement("");
desc.addRootElement(getQualifiedString(prefix, next.getLocalPart()));
desc.setResultAlwaysXMLRoot(true);
}
}
}
}
project.addDescriptor((CoreDescriptor) desc);
}
return generatedClass;
}
use of org.eclipse.persistence.jaxb.TypeMappingInfo in project eclipselink by eclipse-ee4j.
the class AnnotationsProcessor method init.
/**
* Initialize maps, lists, etc. Typically called prior to processing a set
* of classes via preBuildTypeInfo, postBuildTypeInfo, processJavaClasses.
*/
void init(JavaClass[] classes, TypeMappingInfo[] typeMappingInfos) {
typeInfoClasses = new ArrayList<>();
referencedByTransformer = new ArrayList<>();
typeInfos = new HashMap<>();
typeQNames = new ArrayList<>();
classesToProcessPropertyTypes = new ArrayList<>();
objectFactoryClassNames = new ArrayList<>();
userDefinedSchemaTypes = new HashMap<>();
if (packageToPackageInfoMappings == null) {
packageToPackageInfoMappings = new HashMap<>();
}
this.factoryMethods = new HashMap<>();
this.xmlRegistries = new HashMap<>();
this.xmlRootElements = new HashMap<>();
arrayClassesToGeneratedClasses = new HashMap<>();
collectionClassesToGeneratedClasses = new HashMap<>();
generatedClassesToArrayClasses = new HashMap<>();
generatedClassesToCollectionClasses = new HashMap<>();
typeMappingInfosToGeneratedClasses = new HashMap<>();
typeMappingInfosToSchemaTypes = new HashMap<>();
elementDeclarations = new HashMap<>();
Map<QName, ElementDeclaration> globalElements = new HashMap<>();
elementDeclarations.put(XmlElementDecl.GLOBAL.class.getName(), globalElements);
localElements = new ArrayList<>();
javaClassToTypeMappingInfos = new HashMap<>();
typeMappingInfoToAdapterClasses = new HashMap<>();
if (typeMappingInfos != null) {
for (int i = 0; i < typeMappingInfos.length; i++) {
List<TypeMappingInfo> infos = javaClassToTypeMappingInfos.get(classes[i]);
if (infos == null) {
infos = new ArrayList<>();
javaClassToTypeMappingInfos.put(classes[i], infos);
}
infos.add(typeMappingInfos[i]);
java.lang.annotation.Annotation[] annotations = getAnnotations(typeMappingInfos[i]);
if (annotations != null) {
for (java.lang.annotation.Annotation nextAnnotation : annotations) {
if (nextAnnotation instanceof XmlJavaTypeAdapter) {
typeMappingInfoToAdapterClasses.put(typeMappingInfos[i], ((XmlJavaTypeAdapter) nextAnnotation).value());
}
}
}
}
}
}
use of org.eclipse.persistence.jaxb.TypeMappingInfo in project eclipselink by eclipse-ee4j.
the class AnonymousTypeTestCases method testAnonymousComplexType.
public void testAnonymousComplexType() throws Exception {
String TNS = "http://xmlns.oracle.com/Test";
String TYPES_TNS = "http://xmlns.oracle.com/Test/types";
String TNS_XSD = "org/eclipse/persistence/testing/jaxb/schemagen/anonymoustype/test_ns.xsd";
String TYPES_XSD = "org/eclipse/persistence/testing/jaxb/schemagen/anonymoustype/target_ns.xsd";
TypeMappingInfo t1 = new TypeMappingInfo();
t1.setAnnotations(new Annotation[0]);
t1.setType(Process.class);
t1.setElementScope(TypeMappingInfo.ElementScope.Global);
t1.setXmlTagName(new QName(TNS, "process"));
TypeMappingInfo[] types = { t1 };
Map<String, Object> properties = new HashMap<String, Object>();
properties.put(JAXBContextProperties.DEFAULT_TARGET_NAMESPACE, TYPES_TNS);
JAXBContext cxt = JAXBContextFactory.createContext(types, properties, Thread.currentThread().getContextClassLoader());
MySchemaOutputResolver mysr = new MySchemaOutputResolver();
cxt.generateSchema(mysr);
assertTrue("Expected two schemas to be generated, but there were [" + mysr.schemaFiles.size() + "]", mysr.schemaFiles.size() == 2);
ExternalizedMetadataTestCases.compareSchemas(mysr.schemaFiles.get(TNS), getFile(TNS_XSD));
ExternalizedMetadataTestCases.compareSchemas(mysr.schemaFiles.get(TYPES_TNS), getFile(TYPES_XSD));
}
use of org.eclipse.persistence.jaxb.TypeMappingInfo in project eclipselink by eclipse-ee4j.
the class ListOfCustomerNoXmlXsiTypeTestCases method getTypeMappingInfos.
protected TypeMappingInfo[] getTypeMappingInfos() throws Exception {
if (typeMappingInfos == null) {
typeMappingInfos = new TypeMappingInfo[1];
TypeMappingInfo tpi = new TypeMappingInfo();
tpi.setXmlTagName(new QName("someUri", "testListOfCustomer"));
tpi.setElementScope(ElementScope.Global);
tpi.setType(this.getClass().getField("testField").getGenericType());
typeMappingInfos[0] = tpi;
}
return typeMappingInfos;
}
use of org.eclipse.persistence.jaxb.TypeMappingInfo in project eclipselink by eclipse-ee4j.
the class JaxbTypeToSchemaTypeTestCases method testObject.
public void testObject() throws Exception {
TypeMappingInfo tmi = new TypeMappingInfo();
tmi.setType(Object.class);
TypeMappingInfo[] tmis = new TypeMappingInfo[] { tmi };
JAXBContext ctx = (JAXBContext) JAXBContextFactory.createContext(tmis, null, Thread.currentThread().getContextClassLoader());
Map<Type, QName> typeMap = ctx.getTypeToSchemaType();
assertEquals(0, typeMap.size());
Map<TypeMappingInfo, QName> tmiMap = ctx.getTypeMappingInfoToSchemaType();
assertEquals(1, tmiMap.size());
assertNotNull(tmiMap.get(tmi));
}
Aggregations