Search in sources :

Example 1 with MarshalContext

use of org.eclipse.persistence.internal.oxm.record.MarshalContext in project eclipselink by eclipse-ee4j.

the class XPathNode method marshal.

public boolean marshal(MarshalRecord marshalRecord, Object object, CoreAbstractSession session, NamespaceResolver namespaceResolver, Marshaller marshaller, MarshalContext marshalContext, XPathFragment rootFragment) {
    if ((null == marshalNodeValue) || isMarshalOnlyNodeValue) {
        if (marshalRecord.isWrapperAsCollectionName() && null != nonAttributeChildren && nonAttributeChildren.size() == 1) {
            XPathNode childXPathNode = nonAttributeChildren.get(0);
            NodeValue childXPathNodeUnmarshalNodeValue = childXPathNode.getUnmarshalNodeValue();
            if (childXPathNodeUnmarshalNodeValue != null && childXPathNodeUnmarshalNodeValue.isContainerValue()) {
                ContainerValue containerValue = (ContainerValue) childXPathNodeUnmarshalNodeValue;
                if (containerValue.isWrapperAllowedAsCollectionName()) {
                    XPathNode wrapperXPathNode = new XPathNode();
                    wrapperXPathNode.setXPathFragment(this.getXPathFragment());
                    wrapperXPathNode.setMarshalNodeValue(childXPathNode.getMarshalNodeValue());
                    return wrapperXPathNode.marshal(marshalRecord, object, session, namespaceResolver, marshaller, marshalContext, rootFragment);
                }
            }
        }
        marshalRecord.addGroupingElement(this);
        boolean hasValue = false;
        if (null != attributeChildren) {
            for (int x = 0, size = attributeChildren.size(); x < size; x++) {
                XPathNode xPathNode = attributeChildren.get(x);
                hasValue = xPathNode.marshal(marshalRecord, object, session, namespaceResolver, marshaller, ObjectMarshalContext.getInstance(), this.xPathFragment) || hasValue;
            }
        }
        if (anyAttributeNode != null) {
            hasValue = anyAttributeNode.marshal(marshalRecord, object, session, namespaceResolver, marshaller, ObjectMarshalContext.getInstance(), null) || hasValue;
        }
        if (null == nonAttributeChildren) {
            if (textNode != null) {
                hasValue = textNode.marshal(marshalRecord, object, session, namespaceResolver, marshaller, ObjectMarshalContext.getInstance(), null) || hasValue;
            }
        } else {
            for (int x = 0, size = marshalContext.getNonAttributeChildrenSize(this); x < size; x++) {
                XPathNode xPathNode = (XPathNode) marshalContext.getNonAttributeChild(x, this);
                MarshalContext childMarshalContext = marshalContext.getMarshalContext(x);
                hasValue = xPathNode.marshal(marshalRecord, object, session, namespaceResolver, marshaller, childMarshalContext, this.xPathFragment) || hasValue;
            }
        }
        if (hasValue) {
            marshalRecord.endElement(xPathFragment, namespaceResolver);
        } else {
            marshalRecord.removeGroupingElement(this);
        }
        return hasValue;
    } else {
        if (marshalNodeValue.isMappingNodeValue()) {
            Mapping mapping = ((MappingNodeValue) marshalNodeValue).getMapping();
            CoreAttributeGroup currentGroup = marshalRecord.getCurrentAttributeGroup();
            if (!(currentGroup.containsAttributeInternal(mapping.getAttributeName()))) {
                return false;
            }
        }
        return marshalContext.marshal(marshalNodeValue, xPathFragment, marshalRecord, object, session, namespaceResolver, rootFragment);
    }
}
Also used : ObjectMarshalContext(org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext) MarshalContext(org.eclipse.persistence.internal.oxm.record.MarshalContext) CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping)

Example 2 with MarshalContext

use of org.eclipse.persistence.internal.oxm.record.MarshalContext 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)

Aggregations

MarshalContext (org.eclipse.persistence.internal.oxm.record.MarshalContext)2 ObjectMarshalContext (org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext)2 Method (java.lang.reflect.Method)1 QName (javax.xml.namespace.QName)1 CoreDescriptor (org.eclipse.persistence.core.descriptors.CoreDescriptor)1 CoreAttributeGroup (org.eclipse.persistence.core.queries.CoreAttributeGroup)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)1 CoreAbstractSession (org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)1 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)1 AbstractMarshalRecord (org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecord)1 MarshalRecord (org.eclipse.persistence.internal.oxm.record.MarshalRecord)1 SequencedMarshalContext (org.eclipse.persistence.internal.oxm.record.SequencedMarshalContext)1 XMLRecord (org.eclipse.persistence.internal.oxm.record.XMLRecord)1 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)1 Converter (org.eclipse.persistence.mappings.converters.Converter)1