use of org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping in project eclipselink by eclipse-ee4j.
the class XmlValueCdnPricesTestCases method testContainerType.
public void testContainerType() {
XMLDescriptor xDesc = xmlContext.getDescriptor(new QName("canadian-price"));
assertNotNull("No descriptor was generated for CDNPricesNoAnnotation.", xDesc);
DatabaseMapping mapping = xDesc.getMappingForAttributeName("prices");
assertNotNull("No mapping exists on CDNPricesNoAnnotation for attribute [prices].", mapping);
assertTrue("Expected an XMLCompositeDirectCollectionMapping for attribute [prices], but was [" + mapping.toString() + "].", mapping instanceof XMLCompositeDirectCollectionMapping);
assertTrue("Expected container class [java.util.LinkedList] but was [" + mapping.getContainerPolicy().getContainerClassName() + "]", mapping.getContainerPolicy().getContainerClassName().equals("java.util.LinkedList"));
}
use of org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping 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.oxm.mappings.XMLCompositeDirectCollectionMapping in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateMappingForType.
private Mapping generateMappingForType(JavaClass theType, String attributeName) {
Mapping mapping;
boolean typeIsObject = theType.getRawName().equals(OBJECT_CLASS_NAME);
TypeInfo info = typeInfo.get(theType.getQualifiedName());
if ((info != null && !(info.isEnumerationType())) || typeIsObject) {
mapping = new XMLCompositeObjectMapping();
mapping.setAttributeName(attributeName);
((CompositeObjectMapping) mapping).setXPath(attributeName);
if (typeIsObject) {
((CompositeObjectMapping) mapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
setTypedTextField((Field) mapping.getField());
} else {
((CompositeObjectMapping) mapping).setReferenceClassName(theType.getQualifiedName());
}
} else if (theType.isArray() || helper.isCollectionType(theType)) {
DirectCollectionMapping directCollectionMapping;
mapping = directCollectionMapping = new XMLCompositeDirectCollectionMapping();
initializeXMLContainerMapping(directCollectionMapping, theType.isArray());
directCollectionMapping.setAttributeName(attributeName);
if (theType.isArray()) {
JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(directCollectionMapping.getAttributeAccessor(), directCollectionMapping.getContainerPolicy(), helper.getClassLoader());
String componentClassName = theType.getComponentType().getQualifiedName();
if (theType.getComponentType().isPrimitive()) {
Class<Object> primitiveClass = XMLConversionManager.getDefaultManager().convertClassNameToClass(componentClassName);
accessor.setComponentClass(primitiveClass);
directCollectionMapping.setAttributeAccessor(accessor);
Class<Object> declaredClass = XMLConversionManager.getObjectClass(primitiveClass);
directCollectionMapping.setAttributeElementClass(declaredClass);
} else {
accessor.setComponentClassName(componentClassName);
directCollectionMapping.setAttributeAccessor(accessor);
JavaClass componentType = theType.getComponentType();
Class<?> declaredClass = PrivilegedAccessHelper.callDoPrivilegedWithException(() -> PrivilegedAccessHelper.getClassForName(componentType.getRawName(), false, helper.getClassLoader()), (ex) -> JAXBException.classNotFoundException(componentType.getRawName()));
directCollectionMapping.setAttributeElementClass(declaredClass);
}
} else if (helper.isCollectionType(theType)) {
Collection args = theType.getActualTypeArguments();
if (args.size() > 0) {
JavaClass itemType = (JavaClass) args.iterator().next();
Class<?> declaredClass = PrivilegedAccessHelper.callDoPrivilegedWithException(() -> PrivilegedAccessHelper.getClassForName(itemType.getRawName(), false, helper.getClassLoader()), (ex) -> JAXBException.classNotFoundException(itemType.getRawName()));
if (declaredClass != String.class) {
directCollectionMapping.setAttributeElementClass(declaredClass);
}
}
}
theType = containerClassImpl(theType);
directCollectionMapping.useCollectionClassName(theType.getRawName());
directCollectionMapping.setXPath(attributeName + TXT);
} else {
mapping = new XMLDirectMapping();
mapping.setAttributeName(attributeName);
((DirectMapping) mapping).setNullValueMarshalled(true);
((DirectMapping) mapping).setXPath(attributeName + TXT);
QName schemaType = userDefinedSchemaTypes.get(theType.getQualifiedName());
if (schemaType == null) {
schemaType = helper.getXMLToJavaTypeMap().get(theType.getName());
}
((Field) mapping.getField()).setSchemaType(schemaType);
if (info != null && info.isEnumerationType()) {
((DirectMapping) mapping).setConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo) info));
}
}
return mapping;
}
use of org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping in project eclipselink by eclipse-ee4j.
the class PersistenceXMLMappings method buildPUInfoDescriptor.
/**
* INTERNAL:
*/
private static XMLDescriptor buildPUInfoDescriptor(NamespaceResolver resolver) {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setNamespaceResolver(resolver);
descriptor.setJavaClass(SEPersistenceUnitInfo.class);
descriptor.addDirectMapping("persistenceUnitName", "@name");
// For the canonical model generation we don't exclude the unlisted
// classes unless explicitly set by the user.
XMLDirectMapping mapping = new XMLDirectMapping();
mapping.setAttributeName("excludeUnlistedClasses");
mapping.setXPath("exclude-unlisted-classes/text()");
mapping.setNullValue(false);
descriptor.addMapping(mapping);
XMLCompositeDirectCollectionMapping classesMapping = new XMLCompositeDirectCollectionMapping();
classesMapping.setAttributeName("managedClassNames");
classesMapping.setXPath("class/text()");
descriptor.addMapping(classesMapping);
XMLCompositeDirectCollectionMapping mappingFilesMapping = new XMLCompositeDirectCollectionMapping();
mappingFilesMapping.setAttributeName("mappingFiles");
mappingFilesMapping.setXPath("mapping-file/text()");
descriptor.addMapping(mappingFilesMapping);
XMLCompositeCollectionMapping persistenceUnitPropertiesMapping = new XMLCompositeCollectionMapping();
persistenceUnitPropertiesMapping.setAttributeName("persistenceUnitProperties");
persistenceUnitPropertiesMapping.setGetMethodName("getPersistenceUnitProperties");
persistenceUnitPropertiesMapping.setSetMethodName("setPersistenceUnitProperties");
persistenceUnitPropertiesMapping.setReferenceClass(SEPersistenceUnitProperty.class);
persistenceUnitPropertiesMapping.setXPath("properties/property");
descriptor.addMapping(persistenceUnitPropertiesMapping);
return descriptor;
}
use of org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping in project eclipselink by eclipse-ee4j.
the class XMLEntityMappingsMappingProject method buildStructDescriptor.
/**
* INTERNAL:
* XSD: struct
*/
protected ClassDescriptor buildStructDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(StructMetadata.class);
// Element mappings - must remain in order of definition in XML.
XMLCompositeDirectCollectionMapping fieldsMapping = new XMLCompositeDirectCollectionMapping();
fieldsMapping.setAttributeName("m_fields");
fieldsMapping.setGetMethodName("getFields");
fieldsMapping.setSetMethodName("setFields");
fieldsMapping.setXPath("orm:field");
descriptor.addMapping(fieldsMapping);
// Attribute mappings.
descriptor.addMapping(getNameAttributeMapping());
return descriptor;
}
Aggregations