Search in sources :

Example 1 with FixedMimeTypePolicy

use of org.eclipse.persistence.oxm.mappings.FixedMimeTypePolicy in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method generateDescriptorForJAXBElementSubclass.

public void generateDescriptorForJAXBElementSubclass(JavaClass javaClass, CoreProject project, NamespaceResolver nsr) {
    String jClassName = javaClass.getQualifiedName();
    TypeInfo info = typeInfo.get(jClassName);
    Descriptor xmlDescriptor = new XMLDescriptor();
    xmlDescriptor.setJavaClassName(jClassName);
    String[] factoryMethodParamTypes = info.getFactoryMethodParamTypes();
    MultiArgInstantiationPolicy policy = new MultiArgInstantiationPolicy();
    policy.useFactoryInstantiationPolicy(info.getObjectFactoryClassName(), info.getFactoryMethodName());
    policy.setParameterTypeNames(factoryMethodParamTypes);
    policy.setDefaultValues(new String[] { null });
    xmlDescriptor.setInstantiationPolicy(policy);
    JavaClass paramClass = helper.getJavaClass(factoryMethodParamTypes[0]);
    boolean isObject = paramClass.getName().equals("java.lang.Object");
    if (helper.isBuiltInJavaType(paramClass) && !isObject) {
        if (isBinaryData(paramClass)) {
            BinaryDataMapping mapping = new XMLBinaryDataMapping();
            mapping.setAttributeName("value");
            mapping.setXPath(".");
            ((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
            mapping.setSetMethodName("setValue");
            mapping.setGetMethodName("getValue");
            Class<?> attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(factoryMethodParamTypes[0], helper.getClassLoader());
            mapping.setAttributeClassification(attributeClassification);
            mapping.getNullPolicy().setNullRepresentedByEmptyNode(false);
            mapping.setShouldInlineBinaryData(false);
            if (mapping.getMimeType() == null) {
                if (areEquals(paramClass, javax.xml.transform.Source.class)) {
                    mapping.setMimeTypePolicy(new FixedMimeTypePolicy("application/xml"));
                } else {
                    mapping.setMimeTypePolicy(new FixedMimeTypePolicy("application/octet-stream"));
                }
            }
            xmlDescriptor.addMapping((CoreMapping) mapping);
        } else {
            DirectMapping mapping = new XMLDirectMapping();
            mapping.setNullValueMarshalled(true);
            mapping.setAttributeName("value");
            mapping.setGetMethodName("getValue");
            mapping.setSetMethodName("setValue");
            mapping.setXPath("text()");
            Class<?> attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(factoryMethodParamTypes[0], helper.getClassLoader());
            mapping.setAttributeClassification(attributeClassification);
            xmlDescriptor.addMapping((CoreMapping) mapping);
        }
    } else if (paramClass.isEnum()) {
        EnumTypeInfo enumInfo = (EnumTypeInfo) typeInfo.get(paramClass.getQualifiedName());
        DirectMapping mapping = new XMLDirectMapping();
        mapping.setConverter(buildJAXBEnumTypeConverter(mapping, enumInfo));
        mapping.setNullValueMarshalled(true);
        mapping.setAttributeName("value");
        mapping.setGetMethodName("getValue");
        mapping.setSetMethodName("setValue");
        mapping.setXPath("text()");
        Class<?> attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(factoryMethodParamTypes[0], helper.getClassLoader());
        mapping.setAttributeClassification(attributeClassification);
        xmlDescriptor.addMapping((CoreMapping) mapping);
    } else {
        CompositeObjectMapping mapping = new XMLCompositeObjectMapping();
        mapping.setAttributeName("value");
        mapping.setGetMethodName("getValue");
        mapping.setSetMethodName("setValue");
        mapping.setXPath(".");
        if (isObject) {
            mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
        } else {
            mapping.setReferenceClassName(factoryMethodParamTypes[0]);
        }
        xmlDescriptor.addMapping((CoreMapping) mapping);
    }
    xmlDescriptor.setNamespaceResolver(nsr);
    setSchemaContext(xmlDescriptor, info);
    project.addDescriptor((CoreDescriptor) xmlDescriptor);
    info.setDescriptor(xmlDescriptor);
}
Also used : FixedMimeTypePolicy(org.eclipse.persistence.oxm.mappings.FixedMimeTypePolicy) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) MultiArgInstantiationPolicy(org.eclipse.persistence.internal.jaxb.MultiArgInstantiationPolicy) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) 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) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) 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) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

Example 2 with FixedMimeTypePolicy

use of org.eclipse.persistence.oxm.mappings.FixedMimeTypePolicy in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method generateBinaryMapping.

public BinaryDataMapping generateBinaryMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo) {
    BinaryDataMapping mapping = new XMLBinaryDataMapping();
    initializeXMLMapping((XMLMapping) mapping, property);
    // if the XPath is set (via xml-path) use it
    mapping.setField(getXPathForField(property, namespaceInfo, false, false));
    if (property.isSwaAttachmentRef()) {
        ((Field) mapping.getField()).setSchemaType(Constants.SWA_REF_QNAME);
        mapping.setSwaRef(true);
    } else if (property.isMtomAttachment()) {
        Field f = ((Field) mapping.getField());
        if (!f.getSchemaType().equals(Constants.HEX_BINARY_QNAME)) {
            f.setSchemaType(Constants.BASE_64_BINARY_QNAME);
        }
    }
    if (property.isInlineBinaryData()) {
        mapping.setShouldInlineBinaryData(true);
    }
    // use a non-dynamic implementation of MimeTypePolicy to wrap the MIME string
    if (property.getMimeType() != null) {
        mapping.setMimeTypePolicy(new FixedMimeTypePolicy(property.getMimeType(), (DatabaseMapping) mapping));
    } else {
        if (areEquals(property.getType(), javax.xml.transform.Source.class)) {
            mapping.setMimeTypePolicy(new FixedMimeTypePolicy("application/xml", (DatabaseMapping) mapping));
        } else if (areEquals(property.getType(), java.awt.Image.class)) {
            mapping.setMimeTypePolicy(new FixedMimeTypePolicy("image/png", (DatabaseMapping) mapping));
        } else {
            mapping.setMimeTypePolicy(new FixedMimeTypePolicy("application/octet-stream", (DatabaseMapping) mapping));
        }
    }
    if (property.isSetNullPolicy()) {
        mapping.setNullPolicy(getNullPolicyFromProperty(property, getNamespaceResolverForDescriptor(namespaceInfo)));
    } else {
        if (property.isNillable()) {
            mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
            mapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
        }
        mapping.getNullPolicy().setNullRepresentedByEmptyNode(false);
        if (!mapping.getXPath().equals("text()")) {
            ((NullPolicy) mapping.getNullPolicy()).setSetPerformedForAbsentNode(false);
        }
    }
    mapping.setAttributeClassificationName(property.getActualType().getQualifiedName());
    return mapping;
}
Also used : XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) IsSetNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.IsSetNullPolicy) XmlIsSetNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlIsSetNullPolicy) NullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.NullPolicy) XmlNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlNullPolicy) XmlAbstractNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlAbstractNullPolicy) AbstractNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy) 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) FixedMimeTypePolicy(org.eclipse.persistence.oxm.mappings.FixedMimeTypePolicy) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) Image(java.awt.Image) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping)

Example 3 with FixedMimeTypePolicy

use of org.eclipse.persistence.oxm.mappings.FixedMimeTypePolicy in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method generateBinaryDataCollectionMapping.

public BinaryDataCollectionMapping generateBinaryDataCollectionMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo) {
    BinaryDataCollectionMapping mapping = new XMLBinaryDataCollectionMapping();
    initializeXMLMapping((XMLMapping) mapping, property);
    initializeXMLContainerMapping(mapping, property.getType().isArray());
    if (property.isSetXmlElementWrapper()) {
        mapping.setWrapperNullPolicy(getWrapperNullPolicyFromProperty(property));
    }
    // 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 the XPath is set (via xml-path) use it
    mapping.setField(getXPathForField(property, namespaceInfo, false, false));
    if (property.isSwaAttachmentRef()) {
        ((Field) mapping.getField()).setSchemaType(Constants.SWA_REF_QNAME);
        mapping.setSwaRef(true);
    } else if (property.isMtomAttachment()) {
        Field f = (Field) mapping.getField();
        if (!f.getSchemaType().equals(Constants.HEX_BINARY_QNAME)) {
            f.setSchemaType(Constants.BASE_64_BINARY_QNAME);
        }
    }
    if (property.isInlineBinaryData()) {
        mapping.setShouldInlineBinaryData(true);
    }
    // use a non-dynamic implementation of MimeTypePolicy to wrap the MIME string
    if (property.getMimeType() != null) {
        mapping.setMimeTypePolicy(new FixedMimeTypePolicy(property.getMimeType()));
    } else {
        if (areEquals(property.getType(), javax.xml.transform.Source.class)) {
            mapping.setMimeTypePolicy(new FixedMimeTypePolicy("application/xml"));
        } else {
            mapping.setMimeTypePolicy(new FixedMimeTypePolicy("application/octet-stream"));
        }
    }
    JavaClass collectionType = property.getType();
    JavaClass itemType = property.getActualType();
    if (collectionType != null && helper.isCollectionType(collectionType)) {
        try {
            Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(itemType.getQualifiedName(), false, helper.getClassLoader());
            mapping.setAttributeElementClass(declaredClass);
        } catch (Exception e) {
        }
    }
    collectionType = containerClassImpl(collectionType);
    mapping.useCollectionClassName(collectionType.getRawName());
    return mapping;
}
Also used : 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) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) FixedMimeTypePolicy(org.eclipse.persistence.oxm.mappings.FixedMimeTypePolicy) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) JAXBException(org.eclipse.persistence.exceptions.JAXBException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException)

Aggregations

DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)3 Field (org.eclipse.persistence.internal.oxm.mappings.Field)3 JavaField (org.eclipse.persistence.jaxb.javamodel.JavaField)3 XMLField (org.eclipse.persistence.oxm.XMLField)3 FixedMimeTypePolicy (org.eclipse.persistence.oxm.mappings.FixedMimeTypePolicy)3 BinaryDataMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping)2 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)2 XMLBinaryDataMapping (org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping)2 Image (java.awt.Image)1 CoreDescriptor (org.eclipse.persistence.core.descriptors.CoreDescriptor)1 CoreMapping (org.eclipse.persistence.core.mappings.CoreMapping)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)1 JAXBException (org.eclipse.persistence.exceptions.JAXBException)1 MultiArgInstantiationPolicy (org.eclipse.persistence.internal.jaxb.MultiArgInstantiationPolicy)1 BinaryDataCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping)1 CompositeObjectMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping)1 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)1 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)1 XmlAbstractNullPolicy (org.eclipse.persistence.jaxb.xmlmodel.XmlAbstractNullPolicy)1