use of org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateCompositeCollectionMapping.
public CompositeCollectionMapping generateCompositeCollectionMapping(Property property, Descriptor descriptor, JavaClass javaClass, NamespaceInfo namespaceInfo, String referenceClassName) {
boolean nestedArray = false;
CompositeCollectionMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, XMLRecord> mapping = new XMLCompositeCollectionMapping();
initializeXMLMapping((XMLMapping) mapping, property);
initializeXMLContainerMapping(mapping, property.getType().isArray());
JavaClass manyValueJavaClass = helper.getJavaClass(ManyValue.class);
if (manyValueJavaClass.isAssignableFrom(javaClass)) {
mapping.setReuseContainer(false);
}
// handle null policy set via xml metadata
if (property.isSetNullPolicy()) {
mapping.setNullPolicy(getNullPolicyFromProperty(property, getNamespaceResolverForDescriptor(namespaceInfo)));
} else if (property.isNillable()) {
mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
mapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
}
if (property.isSetXmlElementWrapper()) {
mapping.setWrapperNullPolicy(getWrapperNullPolicyFromProperty(property));
}
JavaClass collectionType = property.getType();
if (collectionType.isArray()) {
JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), helper.getClassLoader());
JavaClass componentType = collectionType.getComponentType();
if (componentType.isArray()) {
Class<?> adaptedClass = classToGeneratedClasses.get(componentType.getName());
referenceClassName = adaptedClass.getName();
accessor.setAdaptedClassName(referenceClassName);
JavaClass baseComponentType = getBaseComponentType(componentType);
if (baseComponentType.isPrimitive()) {
Class<Object> primitiveClass = XMLConversionManager.getDefaultManager().convertClassNameToClass(baseComponentType.getRawName());
accessor.setComponentClass(primitiveClass);
} else {
accessor.setComponentClassName(baseComponentType.getQualifiedName());
}
} else {
accessor.setComponentClassName(componentType.getQualifiedName());
}
mapping.setAttributeAccessor(accessor);
} else if (helper.isMapType(property.getType())) {
Class<?> generatedClass = generateMapEntryClassAndDescriptor(property, descriptor.getNonNullNamespaceResolver());
referenceClassName = generatedClass.getName();
String mapClassName = property.getType().getRawName();
mapping.setAttributeAccessor(new MapValueAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), generatedClass, mapClassName, helper.getClassLoader()));
}
// Nested array check (used in JSON marshalling)
if (collectionType.getComponentType() == null) {
if ((collectionType.isArray() || helper.isCollectionType(collectionType)) && (referenceClassName != null && referenceClassName.contains(AnnotationsProcessor.ARRAY_PACKAGE_NAME))) {
nestedArray = true;
}
} else if ((collectionType.isArray() || helper.isCollectionType(collectionType)) && (collectionType.getComponentType().isArray() || helper.isCollectionType(collectionType.getComponentType()))) {
nestedArray = true;
}
collectionType = containerClassImpl(collectionType);
mapping.useCollectionClassName(collectionType.getRawName());
// if the XPath is set (via xml-path) use it; otherwise figure it out
Field xmlField = getXPathForField(property, namespaceInfo, false, false);
if (helper.isMapType(property.getType())) {
JavaClass mapValueClass = helper.getJavaClass(MapValue.class);
if (mapValueClass.isAssignableFrom(javaClass)) {
mapping.setXPath("entry");
} else {
mapping.setXPath(xmlField.getXPath() + "/entry");
}
} else {
mapping.setXPath(xmlField.getXPath());
}
if (referenceClassName == null) {
setTypedTextField((Field) mapping.getField());
} else {
mapping.setReferenceClassName(referenceClassName);
}
if (property.isTransientType()) {
mapping.setReferenceClassName(Constants.UNKNOWN_OR_TRANSIENT_CLASS);
}
if (property.isRequired()) {
((Field) mapping.getField()).setRequired(true);
}
((Field) mapping.getField()).setNestedArray(nestedArray);
return mapping;
}
use of org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateMappings.
/**
* Generate mappings for a given TypeInfo.
*/
public void generateMappings(TypeInfo info, Descriptor descriptor, JavaClass descriptorJavaClass, NamespaceInfo namespaceInfo) {
if (info.isAnonymousComplexType()) {
// may need to generate inherited mappings
generateInheritedMappingsForAnonymousType(info, descriptor, descriptorJavaClass, namespaceInfo);
}
List<Property> propertiesInOrder = info.getNonTransientPropertiesInPropOrder();
for (int i = 0; i < propertiesInOrder.size(); i++) {
Property next = propertiesInOrder.get(i);
if (next != null && (!next.isTransient() || (next.isTransient() && next.isXmlLocation()))) {
Mapping mapping = generateMapping(next, descriptor, descriptorJavaClass, namespaceInfo);
if (next.isVirtual()) {
VirtualAttributeAccessor accessor = new VirtualAttributeAccessor();
accessor.setAttributeName(mapping.getAttributeName());
String getMethod = info.getXmlVirtualAccessMethods().getGetMethod();
String setMethod = info.getXmlVirtualAccessMethods().getSetMethod();
// Check to see if get/set were overridden in the mapping
if (mapping.getAttributeAccessor().isMethodAttributeAccessor()) {
getMethod = ((MethodAttributeAccessor) mapping.getAttributeAccessor()).getGetMethodName();
setMethod = ((MethodAttributeAccessor) mapping.getAttributeAccessor()).getSetMethodName();
accessor.setValueType(mapping.getAttributeClassification());
}
accessor.setGetMethodName(getMethod);
accessor.setSetMethodName(setMethod);
if (mapping.getAttributeAccessor() instanceof JAXBArrayAttributeAccessor) {
JAXBArrayAttributeAccessor jaa = (JAXBArrayAttributeAccessor) mapping.getAttributeAccessor();
jaa.setNestedAccessor(accessor);
} else {
mapping.setAttributeAccessor(accessor);
}
}
if (mapping != null) {
descriptor.addMapping((CoreMapping) mapping);
}
// set user-defined properties if necessary
if (next.isSetUserProperties()) {
mapping.setProperties(next.getUserProperties());
}
// get package info
AccessorFactoryWrapper accessorFactory = info.getXmlAccessorFactory();
if (accessorFactory == null) {
accessorFactory = info.getPackageLevelXmlAccessorFactory();
}
if (accessorFactory != null) {
try {
Object accessor = CompilerHelper.createAccessorFor(descriptorJavaClass, next, helper, accessorFactory);
if (accessor != null) {
CustomAccessorAttributeAccessor attributeAccessor = new CustomAccessorAttributeAccessor(accessor);
mapping.setAttributeAccessor(attributeAccessor);
}
} catch (Exception ex) {
}
}
}
next.postInitialize();
}
}
use of org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateVariableXPathCollectionMapping.
private Mapping generateVariableXPathCollectionMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo, JavaClass actualType) {
XMLVariableXPathCollectionMapping mapping = new XMLVariableXPathCollectionMapping();
mapping.setAttributeName(property.getPropertyName());
if (property.isMap()) {
actualType = property.getValueType();
}
initializeXMLContainerMapping(mapping, property.getType().isArray());
initializeXMLMapping(mapping, property);
initializeVariableXPathMapping(mapping, property, actualType);
if (property.getXmlPath() != null) {
mapping.setField(new XMLField(property.getXmlPath()));
} else {
if (property.isSetXmlElementWrapper()) {
mapping.setField((XMLField) getXPathForField(property, namespaceInfo, false, true));
}
}
if (property.isSetXmlElementWrapper()) {
mapping.setWrapperNullPolicy(getWrapperNullPolicyFromProperty(property));
}
JavaClass collectionType = property.getType();
if (collectionType.isArray()) {
JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), helper.getClassLoader());
JavaClass componentType = collectionType.getComponentType();
if (componentType.isArray()) {
JavaClass baseComponentType = getBaseComponentType(componentType);
if (baseComponentType.isPrimitive()) {
Class<Object> primitiveClass = XMLConversionManager.getDefaultManager().convertClassNameToClass(baseComponentType.getRawName());
accessor.setComponentClass(primitiveClass);
} else {
accessor.setComponentClassName(baseComponentType.getQualifiedName());
}
} else {
accessor.setComponentClassName(componentType.getQualifiedName());
}
mapping.setAttributeAccessor(accessor);
}
if (property.isMap()) {
JavaClass mapType = property.getType();
if (mapType.isInterface()) {
mapping.useMapClass("java.util.HashMap");
} else {
mapping.useMapClass(property.getType().getName());
}
} else {
collectionType = containerClassImpl(collectionType);
mapping.useCollectionClass(helper.getClassForJavaClass(collectionType));
}
return mapping;
}
use of org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor 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.internal.jaxb.many.JAXBArrayAttributeAccessor in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateMappingForReferenceProperty.
public Mapping generateMappingForReferenceProperty(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo) {
boolean isCollection = helper.isCollectionType(property.getType()) || property.getType().isArray();
Mapping mapping;
if (isCollection) {
mapping = new XMLChoiceCollectionMapping();
initializeXMLContainerMapping((ChoiceCollectionMapping) mapping, property.getType().isArray());
JavaClass collectionType = property.getType();
collectionType = containerClassImpl(collectionType);
((ChoiceCollectionMapping) mapping).useCollectionClassName(collectionType.getRawName());
JAXBElementRootConverter jaxbERConverter = new JAXBElementRootConverter(Object.class);
if (property.isSetXmlJavaTypeAdapter()) {
JavaClass adapterClass = helper.getJavaClass(property.getXmlJavaTypeAdapter().getValue());
jaxbERConverter.setNestedConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
}
((ChoiceCollectionMapping) mapping).setConverter(jaxbERConverter);
if (property.isSetWriteOnly()) {
((ChoiceCollectionMapping) mapping).setIsWriteOnly(property.isWriteOnly());
}
if (property.isSetXmlElementWrapper()) {
((ChoiceCollectionMapping) mapping).setWrapperNullPolicy(getWrapperNullPolicyFromProperty(property));
}
} else {
mapping = new XMLChoiceObjectMapping();
JAXBElementRootConverter jaxbERConverter = new JAXBElementRootConverter(Object.class);
if (property.isSetXmlJavaTypeAdapter()) {
JavaClass adapterClass = helper.getJavaClass(property.getXmlJavaTypeAdapter().getValue());
jaxbERConverter.setNestedConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
}
((ChoiceObjectMapping) mapping).setConverter(jaxbERConverter);
if (property.isSetWriteOnly()) {
((ChoiceObjectMapping) mapping).setIsWriteOnly(property.isWriteOnly());
}
}
initializeXMLMapping((XMLMapping) mapping, property);
List<ElementDeclaration> referencedElements = property.getReferencedElements();
JavaClass propertyType = property.getType();
if (propertyType.isArray()) {
JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), helper.getClassLoader());
accessor.setComponentClassName(property.getType().getComponentType().getQualifiedName());
JavaClass componentType = propertyType.getComponentType();
if (componentType.isArray()) {
Class<?> adaptedClass = classToGeneratedClasses.get(componentType.getQualifiedName());
accessor.setAdaptedClassName(adaptedClass.getName());
}
mapping.setAttributeAccessor(accessor);
}
String wrapperXPath = "";
// handle XmlElementWrapper
if (property.isSetXmlElementWrapper()) {
XmlElementWrapper wrapper = property.getXmlElementWrapper();
String namespace = wrapper.getNamespace();
if (namespace.equals(XMLProcessor.DEFAULT)) {
if (namespaceInfo.isElementFormQualified()) {
namespace = namespaceInfo.getNamespace();
} else {
namespace = "";
}
}
if (namespace.equals("")) {
wrapperXPath += (wrapper.getName() + "/");
} else {
String prefix = getPrefixForNamespace(namespace, getNamespaceResolverForDescriptor(namespaceInfo));
wrapperXPath += getQualifiedString(prefix, wrapper.getName() + "/");
}
}
if (property.isMixedContent() && isCollection) {
if (wrapperXPath.length() == 0) {
((ChoiceCollectionMapping) mapping).setMixedContent(true);
} else {
((ChoiceCollectionMapping) mapping).setMixedContent(wrapperXPath.substring(0, wrapperXPath.length() - 1));
}
}
for (ElementDeclaration element : referencedElements) {
QName elementName = element.getElementName();
JavaClass pType = element.getJavaType();
String pTypeName = element.getJavaTypeName();
boolean isBinaryType = (areEquals(pType, AnnotationsProcessor.JAVAX_ACTIVATION_DATAHANDLER) || areEquals(pType, byte[].class) || areEquals(pType, Image.class) || areEquals(pType, Source.class) || areEquals(pType, AnnotationsProcessor.JAVAX_MAIL_INTERNET_MIMEMULTIPART));
boolean isText = pType.isEnum() || (!isBinaryType && !(this.typeInfo.containsKey(element.getJavaTypeName())) && !(element.getJavaTypeName().equals(OBJECT_CLASS_NAME)));
String xPath = wrapperXPath;
Field xmlField = this.getXPathForElement(xPath, elementName, namespaceInfo, isText);
// ensure byte[] goes to base64 instead of the default hex.
if (helper.getXMLToJavaTypeMap().get(pType.getRawName()) == Constants.BASE_64_BINARY_QNAME) {
xmlField.setSchemaType(Constants.BASE_64_BINARY_QNAME);
}
if (areEquals(pType, Object.class)) {
setTypedTextField(xmlField);
}
Mapping nestedMapping;
AbstractNullPolicy nullPolicy = null;
if (isCollection) {
ChoiceCollectionMapping xmlChoiceCollectionMapping = (ChoiceCollectionMapping) mapping;
xmlChoiceCollectionMapping.addChoiceElement(xmlField, pTypeName);
nestedMapping = (Mapping) xmlChoiceCollectionMapping.getChoiceElementMappings().get(xmlField);
if (nestedMapping.isAbstractCompositeCollectionMapping()) {
((CompositeCollectionMapping) nestedMapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
nullPolicy = ((CompositeCollectionMapping) nestedMapping).getNullPolicy();
}
if (nestedMapping.isAbstractCompositeDirectCollectionMapping()) {
DirectCollectionMapping nestedCompositeDirectCollectionMapping = (DirectCollectionMapping) nestedMapping;
nullPolicy = nestedCompositeDirectCollectionMapping.getNullPolicy();
if (pType.isEnum()) {
TypeInfo enumTypeInfo = typeInfo.get(pType.getQualifiedName());
nestedCompositeDirectCollectionMapping.setValueConverter(buildJAXBEnumTypeConverter(nestedCompositeDirectCollectionMapping, (EnumTypeInfo) enumTypeInfo));
}
if (element.isList()) {
XMLListConverter listConverter = new XMLListConverter();
listConverter.setObjectClassName(pType.getQualifiedName());
((DirectCollectionMapping) nestedMapping).setValueConverter(listConverter);
}
} else if (nestedMapping instanceof BinaryDataCollectionMapping) {
nullPolicy = ((BinaryDataCollectionMapping) nestedMapping).getNullPolicy();
if (element.isList()) {
((XMLField) nestedMapping.getField()).setUsesSingleNode(true);
}
}
} else {
ChoiceObjectMapping xmlChoiceObjectMapping = (ChoiceObjectMapping) mapping;
xmlChoiceObjectMapping.addChoiceElement(xmlField, pTypeName);
nestedMapping = (Mapping) xmlChoiceObjectMapping.getChoiceElementMappings().get(xmlField);
if (pType.isEnum()) {
TypeInfo enumTypeInfo = typeInfo.get(pType.getQualifiedName());
((DirectMapping) nestedMapping).setConverter(buildJAXBEnumTypeConverter(nestedMapping, (EnumTypeInfo) enumTypeInfo));
}
if (nestedMapping.isAbstractCompositeObjectMapping()) {
((CompositeObjectMapping) nestedMapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
nullPolicy = ((CompositeObjectMapping) nestedMapping).getNullPolicy();
} else if (nestedMapping instanceof BinaryDataMapping) {
nullPolicy = ((BinaryDataMapping) nestedMapping).getNullPolicy();
} else if (nestedMapping instanceof DirectMapping) {
nullPolicy = ((DirectMapping) nestedMapping).getNullPolicy();
}
}
if (nullPolicy != null) {
nullPolicy.setNullRepresentedByEmptyNode(false);
nullPolicy.setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
nullPolicy.setNullRepresentedByXsiNil(true);
nullPolicy.setIgnoreAttributesForNil(false);
}
if (!element.isXmlRootElement()) {
Class<?> scopeClass = element.getScopeClass();
if (scopeClass == jakarta.xml.bind.annotation.XmlElementDecl.GLOBAL.class) {
scopeClass = JAXBElement.GlobalScope.class;
}
Class<?> declaredType = null;
if (element.getAdaptedJavaType() != null) {
declaredType = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(element.getAdaptedJavaType().getQualifiedName(), helper.getClassLoader());
} else {
declaredType = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(pType.getQualifiedName(), helper.getClassLoader());
}
JAXBElementConverter converter = new JAXBElementConverter(xmlField, declaredType, scopeClass);
if (isCollection) {
ChoiceCollectionMapping xmlChoiceCollectionMapping = (ChoiceCollectionMapping) mapping;
if (element.getJavaTypeAdapterClass() != null) {
converter.setNestedConverter(new XMLJavaTypeConverter(element.getJavaTypeAdapterClass().getName()));
} else {
CoreConverter originalConverter = xmlChoiceCollectionMapping.getConverter(xmlField);
converter.setNestedConverter(originalConverter);
}
xmlChoiceCollectionMapping.addConverter(xmlField, converter);
} else {
ChoiceObjectMapping xmlChoiceObjectMapping = (ChoiceObjectMapping) mapping;
if (element.getJavaTypeAdapterClass() != null) {
converter.setNestedConverter(new XMLJavaTypeConverter(element.getJavaTypeAdapterClass().getName()));
} else {
CoreConverter originalConverter = xmlChoiceObjectMapping.getConverter(xmlField);
converter.setNestedConverter(originalConverter);
}
xmlChoiceObjectMapping.addConverter(xmlField, converter);
}
}
}
if (property.isAny()) {
if (isCollection) {
XMLChoiceCollectionMapping xmlChoiceCollectionMapping = (XMLChoiceCollectionMapping) mapping;
xmlChoiceCollectionMapping.setIsAny(true);
}
}
return mapping;
}
Aggregations