Search in sources :

Example 1 with UnmarshalKeepAsElementPolicy

use of org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy 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;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) MapValueAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) 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) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) 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) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) 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) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 2 with UnmarshalKeepAsElementPolicy

use of org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy 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 3 with UnmarshalKeepAsElementPolicy

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

the class XPathObjectBuilder method buildRow.

@Override
public XMLRecord buildRow(XMLRecord record, Object object, CoreAbstractSession session, Marshaller marshaller, XPathFragment rootFragment) {
    lazyInitialize();
    XPathNode textNode = rootXPathNode.getTextNode();
    List<XPathNode> nonAttributeChildren = rootXPathNode.getNonAttributeChildren();
    if (null == textNode && null == nonAttributeChildren) {
        return record;
    }
    Descriptor xmlDescriptor = (Descriptor) descriptor;
    XPathNode node = rootXPathNode;
    MarshalRecord marshalRecord = (MarshalRecord) record;
    QName schemaType = null;
    if (marshalRecord.getCycleDetectionStack().contains(object, marshaller.isEqualUsingIdenity())) {
        if (cycleRecoverableClass == null) {
            initCycleRecoverableClasses();
        }
        if (cycleRecoverableClass != null && cycleRecoverableClass.isAssignableFrom(object.getClass())) {
            try {
                Object jaxbMarshaller = marshaller.getProperty(Constants.JAXB_MARSHALLER);
                // Create a proxy instance of CycleRecoverable$Context, a parameter to
                // the onCycleDetected method
                Object contextProxy = CycleRecoverableContextProxy.getProxy(cycleRecoverableContextClass, jaxbMarshaller);
                // Invoke onCycleDetected method, passing in proxy, and reset
                // 'object' to the returned value
                Method onCycleDetectedMethod = object.getClass().getMethod(ON_CYCLE_DETECTED, cycleRecoverableContextClass);
                object = PrivilegedAccessHelper.invokeMethod(onCycleDetectedMethod, object, new Object[] { contextProxy });
            } catch (Exception e) {
                throw XMLMarshalException.marshalException(e);
            }
            // Returned object might have a different descriptor
            xmlDescriptor = (Descriptor) session.getDescriptor(object.getClass());
            if (xmlDescriptor != null) {
                node = ((ObjectBuilder) xmlDescriptor.getObjectBuilder()).getRootXPathNode();
            } else {
                node = null;
            }
            // Push new object
            marshalRecord.getCycleDetectionStack().push(object);
            // Write xsi:type if onCycleDetected returned an object of a type different than the one mapped
            if (xmlDescriptor != descriptor) {
                if (xmlDescriptor == null) {
                    schemaType = record.getConversionManager().schemaType(object.getClass());
                } else {
                    schemaType = xmlDescriptor.getSchemaReference().getSchemaContextAsQName();
                }
                marshalRecord.writeXsiTypeAttribute(xmlDescriptor, schemaType.getNamespaceURI(), schemaType.getLocalPart(), schemaType.getPrefix(), false);
            }
        } else {
            // Push the duplicate object anyway, so that we can get the complete cycle string
            marshalRecord.getCycleDetectionStack().push(object);
            throw XMLMarshalException.objectCycleDetected(marshalRecord.getCycleDetectionStack().getCycleString());
        }
    } else {
        marshalRecord.getCycleDetectionStack().push(object);
    }
    NamespaceResolver namespaceResolver = null;
    if (xmlDescriptor != null) {
        namespaceResolver = xmlDescriptor.getNamespaceResolver();
    }
    MarshalContext marshalContext = null;
    if (xmlDescriptor != null && xmlDescriptor.isSequencedObject()) {
        SequencedObject sequencedObject = (SequencedObject) object;
        marshalContext = new SequencedMarshalContext(sequencedObject.getSettings());
    } else {
        marshalContext = ObjectMarshalContext.getInstance();
    }
    if (null == nonAttributeChildren) {
        textNode.marshal((MarshalRecord) record, object, session, namespaceResolver, marshaller, marshalContext, rootFragment);
    } else {
        if (node == null) {
            // No descriptor for this object, so manually create a MappingNodeValue and marshal it
            XPathNode n = new XPathNode();
            CompositeObjectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, org.eclipse.persistence.oxm.record.XMLRecord> m = new XMLCompositeObjectMapping();
            m.setXPath(".");
            XMLCompositeObjectMappingNodeValue nv = new XMLCompositeObjectMappingNodeValue(m);
            n.setMarshalNodeValue(nv);
            nv.marshalSingleValue(new XPathFragment("."), marshalRecord, null, object, session, namespaceResolver, marshalContext);
        } else {
            for (int x = 0, size = marshalContext.getNonAttributeChildrenSize(node); x < size; x++) {
                XPathNode xPathNode = (XPathNode) marshalContext.getNonAttributeChild(x, node);
                xPathNode.marshal((MarshalRecord) record, object, session, namespaceResolver, marshaller, marshalContext.getMarshalContext(x), rootFragment);
            }
        }
    }
    marshalRecord.getCycleDetectionStack().pop();
    return record;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) AbstractMarshalRecord(org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecord) MarshalRecord(org.eclipse.persistence.internal.oxm.record.MarshalRecord) SequencedObject(org.eclipse.persistence.oxm.sequenced.SequencedObject) Converter(org.eclipse.persistence.mappings.converters.Converter) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession) QName(javax.xml.namespace.QName) SequencedMarshalContext(org.eclipse.persistence.internal.oxm.record.SequencedMarshalContext) Method(java.lang.reflect.Method) XMLRecord(org.eclipse.persistence.internal.oxm.record.XMLRecord) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) ObjectMarshalContext(org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext) SequencedMarshalContext(org.eclipse.persistence.internal.oxm.record.SequencedMarshalContext) MarshalContext(org.eclipse.persistence.internal.oxm.record.MarshalContext) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) SequencedObject(org.eclipse.persistence.oxm.sequenced.SequencedObject) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)

Example 4 with UnmarshalKeepAsElementPolicy

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

the class MappingsGenerator method generateAnyObjectMapping.

public AnyObjectMapping generateAnyObjectMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo) {
    AnyObjectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, XMLConverter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, XMLRecord> mapping = new XMLAnyObjectMapping();
    initializeXMLMapping((XMLMapping) mapping, property);
    // if the XPath is set (via xml-path) use it
    if (property.getXmlPath() != null) {
        mapping.setField(new XMLField(property.getXmlPath()));
    }
    Class<?> declaredType = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(property.getActualType().getQualifiedName(), helper.getClassLoader());
    JAXBElementRootConverter jaxbElementRootConverter = new JAXBElementRootConverter(declaredType);
    mapping.setConverter(jaxbElementRootConverter);
    if (property.getDomHandlerClassName() != null) {
        jaxbElementRootConverter.setNestedConverter(new DomHandlerConverter(property.getDomHandlerClassName()));
    }
    if (property.isLax()) {
        mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
    } else {
        mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT);
    }
    if (property.isMixedContent()) {
        mapping.setMixedContent(true);
    } else {
        mapping.setUseXMLRoot(true);
    }
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) XMLConverter(org.eclipse.persistence.oxm.mappings.converters.XMLConverter) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) DomHandlerConverter(org.eclipse.persistence.internal.jaxb.DomHandlerConverter) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) JAXBElementRootConverter(org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) 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) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) XMLAnyObjectMapping(org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping)

Aggregations

ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)4 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)4 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)4 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)4 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)4 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)4 XMLUnmarshaller (org.eclipse.persistence.oxm.XMLUnmarshaller)4 UnmarshalKeepAsElementPolicy (org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy)4 Session (org.eclipse.persistence.sessions.Session)4 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)3 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)3 VirtualAttributeAccessor (org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor)3 CustomAccessorAttributeAccessor (org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor)3 DomHandlerConverter (org.eclipse.persistence.internal.jaxb.DomHandlerConverter)3 JAXBElementRootConverter (org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter)3 JAXBSetMethodAttributeAccessor (org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor)3 JAXBArrayAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor)3 MapValueAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor)3 Converter (org.eclipse.persistence.mappings.converters.Converter)3 XMLField (org.eclipse.persistence.oxm.XMLField)3