Search in sources :

Example 1 with XMLJavaTypeConverter

use of org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter 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;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) IsSetNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.IsSetNullPolicy) XmlIsSetNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlIsSetNullPolicy) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) CoreConverter(org.eclipse.persistence.core.mappings.converters.CoreConverter) JAXBEnumTypeConverter(org.eclipse.persistence.jaxb.JAXBEnumTypeConverter) JAXBElementConverter(org.eclipse.persistence.internal.jaxb.JAXBElementConverter) XMLListConverter(org.eclipse.persistence.oxm.mappings.converters.XMLListConverter) XMLConverter(org.eclipse.persistence.oxm.mappings.converters.XMLConverter) Converter(org.eclipse.persistence.mappings.converters.Converter) DefaultElementConverter(org.eclipse.persistence.internal.jaxb.DefaultElementConverter) DomHandlerConverter(org.eclipse.persistence.internal.jaxb.DomHandlerConverter) JAXBElementRootConverter(org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) JAXBException(org.eclipse.persistence.exceptions.JAXBException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) VirtualAttributeAccessor(org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor) CustomAccessorAttributeAccessor(org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) MapValueAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) Session(org.eclipse.persistence.sessions.Session) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 2 with XMLJavaTypeConverter

use of org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method generateMapping.

/**
 * Generate a mapping for a given Property.
 *
 * @return newly created mapping
 */
public Mapping generateMapping(Property property, Descriptor descriptor, JavaClass descriptorJavaClass, NamespaceInfo namespaceInfo) {
    if (property.isSetXmlJavaTypeAdapter()) {
        // if we are dealing with a reference, generate mapping and return
        if (property.isReference()) {
            return generateMappingForReferenceProperty(property, descriptor, namespaceInfo);
        }
        XmlJavaTypeAdapter xja = property.getXmlJavaTypeAdapter();
        JavaClass adapterClass = helper.getJavaClass(xja.getValue());
        JavaClass valueType = null;
        String sValType = xja.getValueType();
        if (sValType.equals("jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT")) {
            valueType = property.getActualType();
        } else {
            valueType = helper.getJavaClass(xja.getValueType());
        }
        Mapping mapping;
        boolean isArray = property.getType().isArray() && !property.getType().getRawName().equals("byte[]");
        // a composite mapping
        if (property.isChoice()) {
            if (helper.isCollectionType(property.getType()) || property.getType().isArray()) {
                mapping = generateChoiceCollectionMapping(property, descriptor, namespaceInfo);
                ((ChoiceCollectionMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
            } else {
                mapping = generateChoiceMapping(property, descriptor, namespaceInfo);
                ((ChoiceObjectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
            }
        } else if (typeInfo.containsKey(valueType.getQualifiedName())) {
            TypeInfo reference = typeInfo.get(valueType.getQualifiedName());
            if (helper.isCollectionType(property.getType())) {
                if (reference.isEnumerationType()) {
                    mapping = generateEnumCollectionMapping(property, descriptor, namespaceInfo, (EnumTypeInfo) reference);
                    XMLJavaTypeConverter converter = new XMLJavaTypeConverter(adapterClass.getQualifiedName());
                    converter.setNestedConverter(((DirectCollectionMapping) mapping).getValueConverter());
                    ((DirectCollectionMapping) mapping).setValueConverter(converter);
                } else {
                    if (property.getVariableAttributeName() != null) {
                        mapping = generateVariableXPathCollectionMapping(property, descriptor, namespaceInfo, valueType);
                        ((VariableXPathCollectionMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
                    } else {
                        mapping = generateCompositeCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo, valueType.getQualifiedName());
                        ((CompositeCollectionMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
                    }
                }
            } else {
                if (reference.isEnumerationType()) {
                    mapping = generateDirectEnumerationMapping(property, descriptor, namespaceInfo, (EnumTypeInfo) reference);
                    XMLJavaTypeConverter converter = new XMLJavaTypeConverter(adapterClass.getQualifiedName());
                    converter.setNestedConverter(((DirectMapping) mapping).getConverter());
                    ((DirectMapping) mapping).setConverter(converter);
                } else if (property.isInverseReference()) {
                    mapping = generateInverseReferenceMapping(property, descriptor, namespaceInfo);
                } else {
                    if (property.getVariableAttributeName() != null) {
                        mapping = generateVariableXPathObjectMapping(property, descriptor, namespaceInfo, valueType);
                        ((VariableXPathObjectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
                    } else {
                        mapping = generateCompositeObjectMapping(property, descriptor, namespaceInfo, valueType.getQualifiedName());
                        ((CompositeObjectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
                    }
                }
            }
        } else {
            // no descriptor for value type
            if (property.isAny()) {
                if (helper.isCollectionType(property.getType())) {
                    mapping = generateAnyCollectionMapping(property, descriptor, namespaceInfo, property.isMixedContent());
                    ((AnyCollectionMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
                } else {
                    mapping = generateAnyObjectMapping(property, descriptor, namespaceInfo);
                    ((AnyObjectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
                }
            } else if (helper.isCollectionType(property.getType()) || isArray) {
                if (property.isSwaAttachmentRef() || property.isMtomAttachment()) {
                    mapping = generateBinaryDataCollectionMapping(property, descriptor, namespaceInfo);
                    ((BinaryDataCollectionMapping) mapping).setValueConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
                } else {
                    mapping = generateDirectCollectionMapping(property, descriptor, namespaceInfo);
                    if (adapterClass.getQualifiedName().equals(CollapsedStringAdapter.class.getName())) {
                        ((DirectCollectionMapping) mapping).setCollapsingStringValues(true);
                    } else if (adapterClass.getQualifiedName().equals(NormalizedStringAdapter.class.getName())) {
                        ((DirectCollectionMapping) mapping).setNormalizingStringValues(true);
                    } else {
                        ((DirectCollectionMapping) mapping).setValueConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
                    }
                }
            } else if (property.isSwaAttachmentRef() || property.isMtomAttachment()) {
                mapping = generateBinaryMapping(property, descriptor, namespaceInfo);
                ((BinaryDataMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
            } else {
                if (!property.isAttribute() && areEquals(valueType, Object.class) || property.isTyped()) {
                    mapping = generateCompositeObjectMapping(property, descriptor, namespaceInfo, null);
                    ((CompositeObjectMapping) mapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
                    ((CompositeObjectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
                    return mapping;
                }
                mapping = generateDirectMapping(property, descriptor, namespaceInfo);
                if (adapterClass.getQualifiedName().equals(CollapsedStringAdapter.class.getName())) {
                    ((DirectMapping) mapping).setCollapsingStringValues(true);
                } else if (adapterClass.getQualifiedName().equals(NormalizedStringAdapter.class.getName())) {
                    ((DirectMapping) mapping).setNormalizingStringValues(true);
                } else {
                    ((DirectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
                }
            }
        }
        return mapping;
    }
    if (property.getVariableAttributeName() != null) {
        if (helper.isCollectionType(property.getType()) || property.getType().isArray() || property.isMap()) {
            return generateVariableXPathCollectionMapping(property, descriptor, namespaceInfo, property.getActualType());
        } else {
            return generateVariableXPathObjectMapping(property, descriptor, namespaceInfo, property.getActualType());
        }
    }
    if (property.isSetXmlJoinNodes()) {
        if (helper.isCollectionType(property.getType())) {
            return generateXMLCollectionReferenceMapping(property, descriptor, namespaceInfo, property.getActualType());
        }
        return generateXMLObjectReferenceMapping(property, descriptor, namespaceInfo, property.getType());
    }
    if (property.isXmlTransformation()) {
        return generateTransformationMapping(property, descriptor, namespaceInfo);
    }
    if (property.isChoice()) {
        if (helper.isCollectionType(property.getType()) || property.getType().isArray()) {
            return generateChoiceCollectionMapping(property, descriptor, namespaceInfo);
        }
        return generateChoiceMapping(property, descriptor, namespaceInfo);
    }
    if (property.isInverseReference()) {
        return generateInverseReferenceMapping(property, descriptor, namespaceInfo);
    }
    if (property.isReference()) {
        return generateMappingForReferenceProperty(property, descriptor, namespaceInfo);
    }
    if (property.isAny()) {
        if (helper.isCollectionType(property.getType()) || property.getType().isArray()) {
            return generateAnyCollectionMapping(property, descriptor, namespaceInfo, property.isMixedContent());
        }
        return generateAnyObjectMapping(property, descriptor, namespaceInfo);
    }
    if (property.isMap()) {
        if (property.isAnyAttribute()) {
            return generateAnyAttributeMapping(property, descriptor, namespaceInfo);
        }
        return generateCompositeCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo, null);
    }
    if (helper.isCollectionType(property.getType())) {
        return generateCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo);
    }
    JavaClass referenceClass = property.getType();
    String referenceClassName = referenceClass.getRawName();
    if (referenceClass.isArray() && !referenceClassName.equals("byte[]")) {
        JavaClass componentType = referenceClass.getComponentType();
        TypeInfo reference = typeInfo.get(componentType.getName());
        if (reference != null && reference.isEnumerationType()) {
            return generateEnumCollectionMapping(property, descriptor, namespaceInfo, (EnumTypeInfo) reference);
        }
        if (areEquals(componentType, Object.class)) {
            CompositeCollectionMapping mapping = generateCompositeCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo, null);
            mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
            return mapping;
        }
        if (reference != null || componentType.isArray()) {
            if (property.isXmlIdRef() || property.isSetXmlJoinNodes()) {
                return generateXMLCollectionReferenceMapping(property, descriptor, namespaceInfo, componentType);
            }
            return generateCompositeCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo, componentType.getQualifiedName());
        }
        return generateDirectCollectionMapping(property, descriptor, namespaceInfo);
    }
    if (property.isXmlIdRef()) {
        return generateXMLObjectReferenceMapping(property, descriptor, namespaceInfo, referenceClass);
    }
    TypeInfo reference = typeInfo.get(referenceClass.getQualifiedName());
    if (reference != null) {
        if (reference.isEnumerationType()) {
            return generateDirectEnumerationMapping(property, descriptor, namespaceInfo, (EnumTypeInfo) reference);
        }
        if (property.isXmlLocation()) {
            CompositeObjectMapping locationMapping = generateCompositeObjectMapping(property, descriptor, namespaceInfo, referenceClass.getQualifiedName());
            reference.getDescriptor().setInstantiationPolicy(new NullInstantiationPolicy());
            descriptor.setLocationAccessor(locationMapping.getAttributeAccessor());
            return locationMapping;
        } else {
            return generateCompositeObjectMapping(property, descriptor, namespaceInfo, referenceClass.getQualifiedName());
        }
    }
    if (property.isSwaAttachmentRef() || property.isMtomAttachment()) {
        return generateBinaryMapping(property, descriptor, namespaceInfo);
    }
    if (referenceClass.getQualifiedName().equals(OBJECT_CLASS_NAME) && !property.isAttribute() || property.isTyped()) {
        CompositeObjectMapping coMapping = generateCompositeObjectMapping(property, descriptor, namespaceInfo, null);
        coMapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
        return coMapping;
    }
    if (property.isXmlLocation()) {
        return null;
    }
    return generateDirectMapping(property, descriptor, namespaceInfo);
}
Also used : CollapsedStringAdapter(jakarta.xml.bind.annotation.adapters.CollapsedStringAdapter) XmlJavaTypeAdapter(org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter) XMLVariableXPathObjectMapping(org.eclipse.persistence.oxm.mappings.XMLVariableXPathObjectMapping) AnyObjectMapping(org.eclipse.persistence.internal.oxm.mappings.AnyObjectMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) VariableXPathCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping) XMLVariableXPathCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLVariableXPathCollectionMapping) XMLAnyObjectMapping(org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) XMLMapping(org.eclipse.persistence.oxm.mappings.XMLMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) VariableXPathObjectMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathObjectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) TransformationMapping(org.eclipse.persistence.internal.oxm.mappings.TransformationMapping) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) XMLAnyAttributeMapping(org.eclipse.persistence.oxm.mappings.XMLAnyAttributeMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) InverseReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.InverseReferenceMapping) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) AnyCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.AnyCollectionMapping) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLAnyCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) AnyAttributeMapping(org.eclipse.persistence.internal.oxm.mappings.AnyAttributeMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLContainerMapping(org.eclipse.persistence.internal.oxm.mappings.XMLContainerMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) NormalizedStringAdapter(jakarta.xml.bind.annotation.adapters.NormalizedStringAdapter) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLVariableXPathObjectMapping(org.eclipse.persistence.oxm.mappings.XMLVariableXPathObjectMapping) VariableXPathObjectMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathObjectMapping) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping)

Example 3 with XMLJavaTypeConverter

use of org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method generateChoiceMapping.

public ChoiceObjectMapping generateChoiceMapping(Property property, Descriptor descriptor, NamespaceInfo namespace) {
    XMLChoiceObjectMapping mapping = new XMLChoiceObjectMapping();
    initializeXMLMapping(mapping, property);
    boolean isIdRef = property.isXmlIdRef();
    Iterator<Property> choiceProperties = property.getChoiceProperties().iterator();
    while (choiceProperties.hasNext()) {
        Property next = choiceProperties.next();
        JavaClass type = next.getType();
        JavaClass originalType = next.getType();
        Converter converter = null;
        TypeInfo info = typeInfo.get(type.getName());
        if (info != null) {
            XmlJavaTypeAdapter adapter = info.getXmlJavaTypeAdapter();
            if (adapter != null) {
                String adapterValue = adapter.getValue();
                JavaClass adapterClass = helper.getJavaClass(adapterValue);
                JavaClass theClass = CompilerHelper.getTypeFromAdapterClass(adapterClass, helper);
                type = theClass;
                converter = new XMLJavaTypeConverter(adapterClass.getQualifiedName());
            }
        }
        if (next.getXmlJoinNodes() != null) {
            // handle XmlJoinNodes
            List<XMLField> srcFlds = new ArrayList<>();
            List<XMLField> tgtFlds = new ArrayList<>();
            for (XmlJoinNode xmlJoinNode : next.getXmlJoinNodes().getXmlJoinNode()) {
                srcFlds.add(new XMLField(xmlJoinNode.getXmlPath()));
                tgtFlds.add(new XMLField(xmlJoinNode.getReferencedXmlPath()));
            }
            mapping.addChoiceElement(srcFlds, type.getQualifiedName(), tgtFlds);
        } else if (isIdRef) {
            // handle IDREF
            String tgtXPath = null;
            TypeInfo referenceType = typeInfo.get(type.getQualifiedName());
            if (null != referenceType && referenceType.isIDSet()) {
                Property prop = referenceType.getIDProperty();
                tgtXPath = getXPathForField(prop, namespace, !prop.isAttribute(), false).getXPath();
            }
            // if the XPath is set (via xml-path) use it, otherwise figure it out
            Field srcXPath;
            if (next.getXmlPath() != null) {
                srcXPath = new XMLField(next.getXmlPath());
            } else {
                srcXPath = getXPathForField(next, namespace, true, false);
            }
            mapping.addChoiceElement(srcXPath.getXPath(), type.getQualifiedName(), tgtXPath);
        } else {
            XMLField xpath;
            if (next.getXmlPath() != null) {
                xpath = new XMLField(next.getXmlPath());
            } else {
                xpath = (XMLField) getXPathForField(next, namespace, (!(this.typeInfo.containsKey(type.getQualifiedName()))) || next.isMtomAttachment() || type.isEnum(), false);
            }
            mapping.addChoiceElement(xpath, type.getQualifiedName());
            if (!originalType.getQualifiedName().equals(type.getQualifiedName())) {
                mapping.getClassNameToFieldMappings().putIfAbsent(originalType.getQualifiedName(), xpath);
                mapping.addConverter(xpath, converter);
            }
            XMLMapping nestedMapping = mapping.getChoiceElementMappings().get(xpath);
            if (nestedMapping instanceof BinaryDataMapping) {
                ((BinaryDataMapping) nestedMapping).getNullPolicy().setNullRepresentedByEmptyNode(false);
            }
            if (type.isEnum()) {
                if (nestedMapping.isAbstractDirectMapping()) {
                    ((DirectMapping) nestedMapping).setConverter(buildJAXBEnumTypeConverter(nestedMapping, (EnumTypeInfo) info));
                }
            }
        }
    }
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) XMLMapping(org.eclipse.persistence.oxm.mappings.XMLMapping) XmlJavaTypeAdapter(org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter) ArrayList(java.util.ArrayList) XmlJoinNode(org.eclipse.persistence.jaxb.xmlmodel.XmlJoinNodes.XmlJoinNode) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) CoreConverter(org.eclipse.persistence.core.mappings.converters.CoreConverter) JAXBEnumTypeConverter(org.eclipse.persistence.jaxb.JAXBEnumTypeConverter) JAXBElementConverter(org.eclipse.persistence.internal.jaxb.JAXBElementConverter) XMLListConverter(org.eclipse.persistence.oxm.mappings.converters.XMLListConverter) XMLConverter(org.eclipse.persistence.oxm.mappings.converters.XMLConverter) Converter(org.eclipse.persistence.mappings.converters.Converter) DefaultElementConverter(org.eclipse.persistence.internal.jaxb.DefaultElementConverter) DomHandlerConverter(org.eclipse.persistence.internal.jaxb.DomHandlerConverter) JAXBElementRootConverter(org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter)

Example 4 with XMLJavaTypeConverter

use of org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter in project eclipselink by eclipse-ee4j.

the class PreLoginMappingAdapter method preLogin.

/* (non-Javadoc)
     * @see org.eclipse.persistence.internal.jaxb.SessionEventListener#preLogin(org.eclipse.persistence.sessions.SessionEvent)
     */
@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public void preLogin(SessionEvent event) {
    Project project = event.getSession().getProject();
    ClassLoader cl = jpaSession.getDatasourcePlatform().getConversionManager().getLoader();
    DefaultXMLNameTransformer xmlNameTransformer = new DefaultXMLNameTransformer();
    for (Object descriptorAlias : project.getAliasDescriptors().keySet()) {
        ClassDescriptor descriptor = project.getAliasDescriptors().get(descriptorAlias);
        if (!PersistenceWeavedRest.class.isAssignableFrom(descriptor.getJavaClass())) {
            continue;
        }
        if (descriptor.isXMLDescriptor()) {
            XMLDescriptor xmlDescriptor = (XMLDescriptor) project.getAliasDescriptors().get(descriptorAlias);
            if (null != xmlDescriptor) {
                if (null == xmlDescriptor.getDefaultRootElement()) {
                    // set root element
                    xmlDescriptor.setDefaultRootElement(xmlNameTransformer.transformRootElementName(xmlDescriptor.getJavaClass().getName()));
                    // set resultAlwaysXMLRoot to false, so that the elements are not wrapped in JAXBElements when unmarshalling.
                    xmlDescriptor.setResultAlwaysXMLRoot(false);
                }
            }
        }
        XMLCompositeCollectionMapping relationshipMapping = new XMLCompositeCollectionMapping();
        relationshipMapping.setAttributeName("_persistence_relationshipInfo");
        relationshipMapping.setGetMethodName("_persistence_getRelationships");
        relationshipMapping.setSetMethodName("_persistence_setRelationships");
        relationshipMapping.setDescriptor(descriptor);
        CollectionContainerPolicy containerPolicy = new CollectionContainerPolicy(ArrayList.class);
        relationshipMapping.setContainerPolicy(containerPolicy);
        relationshipMapping.setField(new XMLField("_relationships"));
        relationshipMapping.setReferenceClass(Link.class);
        XMLJavaTypeConverter converter = new XMLJavaTypeConverter(RelationshipLinkAdapter.class);
        converter.initialize(relationshipMapping, event.getSession());
        relationshipMapping.setConverter(converter);
        descriptor.addMapping(relationshipMapping);
        XMLCompositeObjectMapping hrefMapping = new XMLCompositeObjectMapping();
        hrefMapping.setAttributeName("_persistence_href");
        hrefMapping.setGetMethodName("_persistence_getHref");
        hrefMapping.setSetMethodName("_persistence_setHref");
        hrefMapping.setDescriptor(descriptor);
        hrefMapping.setField(new XMLField("_link"));
        hrefMapping.setReferenceClass(Link.class);
        hrefMapping.setXPath(".");
        descriptor.addMapping(hrefMapping);
        XMLCompositeObjectMapping itemLinksMapping = new XMLCompositeObjectMapping();
        itemLinksMapping.setAttributeName("_persistence_links");
        itemLinksMapping.setGetMethodName("_persistence_getLinks");
        itemLinksMapping.setSetMethodName("_persistence_setLinks");
        itemLinksMapping.setDescriptor(descriptor);
        itemLinksMapping.setReferenceClass(ItemLinks.class);
        itemLinksMapping.setXPath(".");
        descriptor.addMapping(itemLinksMapping);
        ClassDescriptor jpaDescriptor = jpaSession.getDescriptorForAlias(descriptor.getAlias());
        Vector<DatabaseMapping> descriptorMappings = (Vector<DatabaseMapping>) descriptor.getMappings().clone();
        for (DatabaseMapping mapping : descriptorMappings) {
            if (mapping.isXMLMapping()) {
                if (mapping.isAbstractCompositeObjectMapping() || mapping.isAbstractCompositeCollectionMapping()) {
                    if (mapping.isAbstractCompositeCollectionMapping()) {
                        XMLInverseReferenceMapping inverseMapping = ((XMLCompositeCollectionMapping) mapping).getInverseReferenceMapping();
                        if (inverseMapping != null) {
                            break;
                        }
                    } else if (mapping.isAbstractCompositeObjectMapping()) {
                        XMLInverseReferenceMapping inverseMapping = ((XMLCompositeObjectMapping) mapping).getInverseReferenceMapping();
                        if (inverseMapping != null) {
                            break;
                        }
                    }
                    if (jpaDescriptor != null) {
                        DatabaseMapping dbMapping = jpaDescriptor.getMappingForAttributeName(mapping.getAttributeName());
                        if ((dbMapping != null) && (dbMapping instanceof ForeignReferenceMapping)) {
                            ForeignReferenceMapping jpaMapping = (ForeignReferenceMapping) dbMapping;
                            if (jpaMapping.getMappedBy() != null) {
                                ClassDescriptor inverseDescriptor = project.getDescriptorForAlias(jpaMapping.getReferenceDescriptor().getAlias());
                                if (inverseDescriptor != null) {
                                    DatabaseMapping inverseMapping = inverseDescriptor.getMappingForAttributeName(jpaMapping.getMappedBy());
                                    if (inverseMapping != null) {
                                        convertMappingToXMLInverseReferenceMapping(inverseDescriptor, inverseMapping, jpaMapping);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        InheritancePolicy inheritancePolicy = descriptor.getInheritancePolicyOrNull();
        if ((inheritancePolicy != null) && (inheritancePolicy.isRootParentDescriptor())) {
            boolean isAbstract = Modifier.isAbstract(descriptor.getJavaClass().getModifiers());
            if (isAbstract) {
                Class subClassToInstantiate = null;
                Map<?, ?> classIndicatorMapping = inheritancePolicy.getClassIndicatorMapping();
                // get one of subclasses that extends this abstract class
                for (Map.Entry<?, ?> entry : classIndicatorMapping.entrySet()) {
                    Object value = entry.getValue();
                    if (value instanceof Class) {
                        subClassToInstantiate = (Class) value;
                        isAbstract = Modifier.isAbstract(subClassToInstantiate.getModifiers());
                        if (!isAbstract) {
                            InstantiationPolicy instantiationPolicy = new InstantiationPolicy();
                            instantiationPolicy.useFactoryInstantiationPolicy(new ConcreteSubclassFactory(subClassToInstantiate), "createConcreteSubclass");
                            descriptor.setInstantiationPolicy(instantiationPolicy);
                            break;
                        }
                    }
                }
            }
        }
    }
    for (Object descriptorAlias : project.getAliasDescriptors().keySet()) {
        ClassDescriptor descriptor = project.getAliasDescriptors().get(descriptorAlias);
        ClassDescriptor jpaDescriptor = jpaSession.getDescriptorForAlias(descriptor.getAlias());
        Vector<DatabaseMapping> descriptorMappings = (Vector<DatabaseMapping>) descriptor.getMappings().clone();
        for (DatabaseMapping mapping : descriptorMappings) {
            if (mapping.isXMLMapping()) {
                if (mapping.isAbstractCompositeObjectMapping() || mapping.isAbstractCompositeCollectionMapping()) {
                    if (jpaDescriptor != null) {
                        DatabaseMapping dbMapping = jpaDescriptor.getMappingForAttributeName(mapping.getAttributeName());
                        if ((dbMapping instanceof ForeignReferenceMapping)) {
                            ForeignReferenceMapping jpaMapping = (ForeignReferenceMapping) dbMapping;
                            ClassDescriptor jaxbDescriptor = project.getDescriptorForAlias(jpaMapping.getDescriptor().getAlias());
                            convertMappingToXMLChoiceMapping(jaxbDescriptor, jpaMapping, cl, jpaSession);
                        }
                    } else if (mapping instanceof XMLCompositeObjectMapping) {
                        // Fix for Bug 403113 - JPA-RS Isn't Serializing an Embeddable defined in an ElementCollection to JSON Correctly
                        // add choice mapping for one-to-one relationships within embeddables
                        // Based on (http://wiki.eclipse.org/EclipseLink/Examples/JPA/NoSQL#Step_2_:_Map_the_data),
                        // the mappedBy option on relationships is not supported for NoSQL data, so no need to add inverse mapping
                        XMLCompositeObjectMapping jpaMapping = (XMLCompositeObjectMapping) mapping;
                        ClassDescriptor jaxbDescriptor = project.getDescriptorForAlias(jpaMapping.getDescriptor().getAlias());
                        if (jaxbDescriptor != null) {
                            Class clazz = jpaMapping.getReferenceClass();
                            if (clazz != null) {
                                if ((jpaSession.getDescriptor(clazz) != null) && (jpaSession.getDescriptor(clazz).isEISDescriptor()))
                                    convertMappingToXMLChoiceMapping(jaxbDescriptor, jpaMapping, cl, jpaSession);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) InstantiationPolicy(org.eclipse.persistence.internal.descriptors.InstantiationPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ForeignReferenceMapping(org.eclipse.persistence.mappings.ForeignReferenceMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) Vector(java.util.Vector) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) XMLField(org.eclipse.persistence.oxm.XMLField) PersistenceWeavedRest(org.eclipse.persistence.internal.jpa.rs.weaving.PersistenceWeavedRest) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Project(org.eclipse.persistence.sessions.Project) InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) DefaultXMLNameTransformer(org.eclipse.persistence.jaxb.DefaultXMLNameTransformer) Map(java.util.Map) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 5 with XMLJavaTypeConverter

use of org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter 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;
}
Also used : XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) XMLVariableXPathObjectMapping(org.eclipse.persistence.oxm.mappings.XMLVariableXPathObjectMapping) AnyObjectMapping(org.eclipse.persistence.internal.oxm.mappings.AnyObjectMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) VariableXPathCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping) XMLVariableXPathCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLVariableXPathCollectionMapping) XMLAnyObjectMapping(org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) XMLMapping(org.eclipse.persistence.oxm.mappings.XMLMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) VariableXPathObjectMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathObjectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) TransformationMapping(org.eclipse.persistence.internal.oxm.mappings.TransformationMapping) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) XMLAnyAttributeMapping(org.eclipse.persistence.oxm.mappings.XMLAnyAttributeMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) InverseReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.InverseReferenceMapping) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) AnyCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.AnyCollectionMapping) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLAnyCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) AnyAttributeMapping(org.eclipse.persistence.internal.oxm.mappings.AnyAttributeMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLContainerMapping(org.eclipse.persistence.internal.oxm.mappings.XMLContainerMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) Source(javax.xml.transform.Source) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) CoreConverter(org.eclipse.persistence.core.mappings.converters.CoreConverter) XmlElementWrapper(org.eclipse.persistence.jaxb.xmlmodel.XmlElementWrapper) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) XmlAbstractNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlAbstractNullPolicy) AbstractNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy) QName(javax.xml.namespace.QName) JAXBElementConverter(org.eclipse.persistence.internal.jaxb.JAXBElementConverter) JAXBElement(jakarta.xml.bind.JAXBElement) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLListConverter(org.eclipse.persistence.oxm.mappings.converters.XMLListConverter) JAXBElementRootConverter(org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping)

Aggregations

XMLJavaTypeConverter (org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter)7 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)6 BinaryDataMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping)5 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)5 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)5 XMLField (org.eclipse.persistence.oxm.XMLField)5 XMLBinaryDataMapping (org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping)5 XMLChoiceObjectMapping (org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping)5 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)5 CoreMapping (org.eclipse.persistence.core.mappings.CoreMapping)4 CoreConverter (org.eclipse.persistence.core.mappings.converters.CoreConverter)4 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)4 JAXBElementConverter (org.eclipse.persistence.internal.jaxb.JAXBElementConverter)4 JAXBElementRootConverter (org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter)4 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)4 Field (org.eclipse.persistence.internal.oxm.mappings.Field)4 JavaField (org.eclipse.persistence.jaxb.javamodel.JavaField)4 XMLChoiceCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 DefaultElementConverter (org.eclipse.persistence.internal.jaxb.DefaultElementConverter)2