Search in sources :

Example 1 with DocumentPreservationPolicy

use of org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy in project eclipselink by eclipse-ee4j.

the class XMLObjectBuilder method createRecordFor.

public AbstractRecord createRecordFor(Object attributeValue, Field xmlField, XMLRecord parentRecord, Mapping mapping) {
    DocumentPreservationPolicy policy = parentRecord.getDocPresPolicy();
    Element newNode = null;
    if (policy != null) {
        newNode = (Element) policy.getNodeForObject(attributeValue);
    }
    if (newNode == null) {
        newNode = XPathEngine.getInstance().createUnownedElement(parentRecord.getDOM(), xmlField);
        if (policy != null) {
            if (xmlField.isSelfField()) {
                policy.addObjectToCache(attributeValue, newNode, mapping);
            } else {
                policy.addObjectToCache(attributeValue, newNode);
            }
        }
    }
    DOMRecord nestedRecord = new DOMRecord(newNode);
    nestedRecord.setMarshaller(parentRecord.getMarshaller());
    nestedRecord.setLeafElementType(parentRecord.getLeafElementType());
    parentRecord.setLeafElementType((XPathQName) null);
    nestedRecord.setDocPresPolicy(policy);
    nestedRecord.setXOPPackage(parentRecord.isXOPPackage());
    nestedRecord.setReferenceResolver(((DOMRecord) parentRecord).getReferenceResolver());
    return nestedRecord;
}
Also used : DocumentPreservationPolicy(org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Element(org.w3c.dom.Element)

Example 2 with DocumentPreservationPolicy

use of org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy in project eclipselink by eclipse-ee4j.

the class XMLMarshaller method objectToXML.

/**
 * INTERNAL:
 * Convert the given object to an XML Document
 * @param object the object to marshal
 * @return the document which the specified object has been marshalled to
 * @param descriptor the XMLDescriptor for the object being marshalled
 * @throws XMLMarshalException if an error occurred during marshalling
 */
@Override
protected Document objectToXML(Object object, XMLDescriptor descriptor, boolean isXMLRoot) throws XMLMarshalException {
    AbstractSession session = context.getSession(descriptor);
    DocumentPreservationPolicy docPresPolicy = context.getDocumentPreservationPolicy(session);
    if (docPresPolicy != null && docPresPolicy.shouldPreserveDocument()) {
        XMLRecord xmlRow = null;
        if (!isXMLRoot) {
            xmlRow = (XMLRecord) ((XMLObjectBuilder) descriptor.getObjectBuilder()).createRecordFor(object, context.getDocumentPreservationPolicy(session));
            xmlRow.setMarshaller(this);
            if (this.attachmentMarshaller != null) {
                xmlRow.setXOPPackage(this.attachmentMarshaller.isXOPPackage());
            }
            addDescriptorNamespacesToXMLRecord(descriptor, xmlRow);
        }
        return objectToXML(object, descriptor, xmlRow, isXMLRoot, docPresPolicy);
    }
    return super.objectToXML(object, descriptor, isXMLRoot);
}
Also used : DocumentPreservationPolicy(org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy) XMLObjectBuilder(org.eclipse.persistence.internal.oxm.XMLObjectBuilder) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)

Example 3 with DocumentPreservationPolicy

use of org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy in project eclipselink by eclipse-ee4j.

the class XMLMarshaller method objectToXMLNode.

@Override
protected Node objectToXMLNode(Object object, Node rootNode, AbstractSession session, XMLDescriptor descriptor, boolean isXMLRoot) throws XMLMarshalException {
    DocumentPreservationPolicy docPresPolicy = context.getDocumentPreservationPolicy(session);
    if (docPresPolicy != null && docPresPolicy.shouldPreserveDocument()) {
        XMLRecord xmlRow = null;
        if (!isXMLRoot) {
            xmlRow = (XMLRecord) ((XMLObjectBuilder) descriptor.getObjectBuilder()).createRecordFor(object, context.getDocumentPreservationPolicy(session));
            xmlRow.setMarshaller(this);
            if (this.attachmentMarshaller != null) {
                xmlRow.setXOPPackage(this.attachmentMarshaller.isXOPPackage());
            }
            if (xmlRow.getDOM().getNodeType() == Node.ELEMENT_NODE) {
                addDescriptorNamespacesToXMLRecord(descriptor, xmlRow);
            }
        }
        Document doc = objectToXML(object, rootNode, descriptor, xmlRow, isXMLRoot, docPresPolicy);
        if ((xmlRow != null) && (xmlRow.getDOM().getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)) {
            return xmlRow.getDOM();
        } else {
            return doc;
        }
    }
    return super.objectToXMLNode(object, rootNode, session, descriptor, isXMLRoot);
}
Also used : DocumentPreservationPolicy(org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy) XMLObjectBuilder(org.eclipse.persistence.internal.oxm.XMLObjectBuilder) Document(org.w3c.dom.Document) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord)

Example 4 with DocumentPreservationPolicy

use of org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy in project eclipselink by eclipse-ee4j.

the class XMLObjectBuilder method buildObject.

/**
 * INTERNAL: Override the parent's buildObject to allow for the caching of
 * aggregate objects in OX. By caching aggregates along with XML Nodes that
 * they were created from, we are able to preserve the structure and
 * unmapped content of the document that was used to create these objects.
 */
@Override
public Object buildObject(ObjectBuildingQuery query, AbstractRecord databaseRow, JoinedAttributeManager joinManager) throws DatabaseException, QueryException {
    XMLRecord row = (XMLRecord) databaseRow;
    row.setSession(query.getSession());
    XMLUnmarshaller unmarshaller = row.getUnmarshaller();
    Object parent = row.getOwningObject();
    if (!(isXmlDescriptor() || getDescriptor().isDescriptorTypeAggregate())) {
        return super.buildObject(query, databaseRow, joinManager);
    }
    query.getSession().startOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
    ClassDescriptor concreteDescriptor = getDescriptor();
    Object domainObject = null;
    // in the mapping
    if (concreteDescriptor.hasInheritance() && (parent == null)) {
        // look for an xsi:type attribute in the xml document
        InheritancePolicy inheritancePolicy = concreteDescriptor.getInheritancePolicy();
        Class<?> classValue = inheritancePolicy.classFromRow(databaseRow, query.getSession());
        if ((classValue == null) && isXmlDescriptor()) {
            // no xsi:type attribute - look for type indicator on the
            // default root element
            QName leafElementType = ((Descriptor) concreteDescriptor).getDefaultRootElementType();
            // inheritance policy
            if (leafElementType != null) {
                XPathQName xpathQName = new XPathQName(leafElementType, row.isNamespaceAware());
                Object indicator = inheritancePolicy.getClassIndicatorMapping().get(xpathQName);
                if (indicator != null) {
                    classValue = (Class) indicator;
                }
            }
        }
        // class, if non-abstract
        if (classValue != null) {
            concreteDescriptor = query.getSession().getDescriptor(classValue);
            if ((concreteDescriptor == null) && query.hasPartialAttributeExpressions()) {
                concreteDescriptor = getDescriptor();
            }
            if (concreteDescriptor == null) {
                throw QueryException.noDescriptorForClassFromInheritancePolicy(query, classValue);
            }
        } else {
            // make sure the class is non-abstract
            if (Modifier.isAbstract(concreteDescriptor.getJavaClass().getModifiers())) {
                // throw an exception
                throw DescriptorException.missingClassIndicatorField(databaseRow, inheritancePolicy.getDescriptor());
            }
        }
    }
    domainObject = concreteDescriptor.getObjectBuilder().buildNewInstance();
    row.setCurrentObject(domainObject);
    if ((unmarshaller != null) && (unmarshaller.getUnmarshalListener() != null)) {
        unmarshaller.getUnmarshalListener().beforeUnmarshal(domainObject, parent);
    }
    concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject, null, databaseRow, query, joinManager, null, false, query.getSession());
    if (isXmlDescriptor() && ((Descriptor) concreteDescriptor).getPrimaryKeyFieldNames().size() > 0) {
        Object pk = extractPrimaryKeyFromRow(databaseRow, query.getSession());
        if ((pk != null) && (((CacheId) pk).getPrimaryKey().length > 0)) {
            DOMRecord domRecord = (DOMRecord) databaseRow;
            domRecord.getReferenceResolver().putValue(concreteDescriptor.getJavaClass(), pk, domainObject);
        }
    }
    DocumentPreservationPolicy docPresPolicy = row.getDocPresPolicy();
    if (docPresPolicy != null) {
        // EIS XML Cases won't have a doc pres policy set
        row.getDocPresPolicy().addObjectToCache(domainObject, row.getDOM());
    }
    query.getSession().endOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
    if ((unmarshaller != null) && (unmarshaller.getUnmarshalListener() != null)) {
        unmarshaller.getUnmarshalListener().afterUnmarshal(domainObject, parent);
    }
    return domainObject;
}
Also used : InheritancePolicy(org.eclipse.persistence.descriptors.InheritancePolicy) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) QName(javax.xml.namespace.QName) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) DocumentPreservationPolicy(org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord)

Aggregations

DocumentPreservationPolicy (org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy)4 XMLRecord (org.eclipse.persistence.oxm.record.XMLRecord)3 XMLObjectBuilder (org.eclipse.persistence.internal.oxm.XMLObjectBuilder)2 DOMRecord (org.eclipse.persistence.oxm.record.DOMRecord)2 QName (javax.xml.namespace.QName)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 InheritancePolicy (org.eclipse.persistence.descriptors.InheritancePolicy)1 CoreAbstractSession (org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)1 CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)1 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)1 AbstractSession (org.eclipse.persistence.internal.sessions.AbstractSession)1 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)1 XMLUnmarshaller (org.eclipse.persistence.oxm.XMLUnmarshaller)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1