Search in sources :

Example 1 with DOMRecord

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

the class SDOMarshalListener method beforeMarshal.

@Override
public void beforeMarshal(Object obj) {
    if (obj instanceof SDOChangeSummary) {
        SDOChangeSummary changeSummary = ((SDOChangeSummary) obj);
        // CREATED - build a list of xpaths to write to the created attribute
        // this must be done dynamically because the xpath is relative to the marshalledObject
        // so it can't be calculated until we know what object is being marshalled
        List createdSet = changeSummary.getCreated();
        List xpaths = new ArrayList(createdSet.size());
        String rootElementName = this.marshalledObjectRootQName.getLocalPart();
        String rootNamespaceUri = this.marshalledObjectRootQName.getNamespaceURI();
        if (rootNamespaceUri != null && !rootNamespaceUri.equals(XMLConstants.EMPTY_STRING)) {
            org.eclipse.persistence.internal.oxm.NamespaceResolver resolver = getRootMarshalRecord().getNamespaceResolver();
            if (resolver != null) {
                String prefix = resolver.resolveNamespaceURI(this.marshalledObjectRootQName.getNamespaceURI());
                if (prefix != null) {
                    rootElementName = prefix + XMLConstants.COLON + rootElementName;
                }
            }
        }
        if ((createdSet != null) && (createdSet.size() > 0)) {
            Iterator anIterator = createdSet.iterator();
            SDODataObject nextCreatedDO = null;
            while (anIterator.hasNext()) {
                // get path to the changeSummaryRoot (may not be the root marshalled object - may be internal)
                nextCreatedDO = ((SDODataObject) anIterator.next());
                String nextPath = getPathFromAncestor(nextCreatedDO, (SDODataObject) marshalledObject, changeSummary);
                // Add sdoRef attribute...all modified objects written should have this
                if (nextPath == SDOConstants.EMPTY_STRING) {
                    // if this is the root, just put the root element
                    xpaths.add(SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + // 
                    SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName);
                } else {
                    xpaths.add(SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + // 
                    SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + nextPath);
                }
            }
        }
        changeSummary.setCreatedXPaths(xpaths);
        // Build xpathToCS
        String xpathMarshalledObjToCS = getPathFromAncestor(changeSummary.getRootObject(), (SDODataObject) marshalledObject, changeSummary);
        String xpathChangeSumProp = getXPathForProperty(changeSummary.getRootObject().getType().getChangeSummaryProperty());
        String xpathToCS = SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName;
        // check if the CS is at the local-cs-root or is in a child property
        if ((xpathMarshalledObjToCS != null) && !xpathMarshalledObjToCS.equals(SDOConstants.EMPTY_STRING)) {
            // SDO_XPATH_TO_ROOT)) {
            // CS is not on the root
            xpathToCS = xpathToCS + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + xpathMarshalledObjToCS;
        }
        xpathToCS = // 
        xpathToCS + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + xpathChangeSumProp + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT;
        // MODIFIED && DELETED
        List deletedXPaths = new ArrayList();
        Document document = XMLPlatformFactory.getInstance().getXMLPlatform().createDocument();
        Element csNode = null;
        List modifiedItems = changeSummary.getModified();
        int modifiedSize = modifiedItems.size();
        List newNodes = new ArrayList(modifiedSize);
        SDODataObject nextModifiedDO = null;
        // Iterate through CS modified items
        for (int i = 0; i < modifiedSize; i++) {
            nextModifiedDO = (SDODataObject) modifiedItems.get(i);
            String sdoPrefix = typeHelper.getPrefix(SDOConstants.SDO_URL);
            // List unsetPropNames = new ArrayList();
            String uri = getURI(nextModifiedDO);
            String qualifiedName = getQualifiedName(nextModifiedDO);
            String sdoRefPrefix = SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT;
            if (uri == null) {
                csNode = document.createElement(qualifiedName);
            } else {
                csNode = document.createElementNS(uri, qualifiedName);
            }
            String nextPath = getPathFromAncestor(nextModifiedDO, (SDODataObject) marshalledObject, changeSummary);
            // Add sdoRef attribute...all modified objects written should have this
            if (nextPath == SDOConstants.EMPTY_STRING) {
                // if this is the root, just put the root element
                csNode.setAttributeNS(SDOConstants.SDO_URL, // 
                sdoPrefix + // 
                SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + // 
                SDOConstants.CHANGESUMMARY_REF, sdoRefPrefix + rootElementName);
            } else {
                csNode.setAttributeNS(SDOConstants.SDO_URL, // 
                sdoPrefix + // 
                SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + // 
                SDOConstants.CHANGESUMMARY_REF, sdoRefPrefix + rootElementName + "/" + nextPath);
            }
            // Bug6346754 Add all namespaces if they are not yet declared above.
            Vector namespaces = nextModifiedDO.getType().getXmlDescriptor().getNonNullNamespaceResolver().getNamespaces();
            for (int j = 0; j < namespaces.size(); j++) {
                Namespace next = (Namespace) namespaces.get(j);
                if (declareNamespace(next.getNamespaceURI(), next.getPrefix(), changeSummary.getRootObject())) {
                    csNode.setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + XMLConstants.COLON + next.getPrefix(), next.getNamespaceURI());
                }
            }
            List nextDOSettings = changeSummary.getOldValues(nextModifiedDO);
            DOMRecord row = new DOMRecord(csNode);
            Session session = ((SDOXMLHelper) typeHelper.getHelperContext().getXMLHelper()).getXmlContext().getSession();
            row.setSession((AbstractSession) session);
            // Iterate through SDOSettings for the current modified Object
            SDOSetting nextSetting = null;
            for (int j = 0; j < nextDOSettings.size(); j++) {
                nextSetting = (SDOSetting) nextDOSettings.get(j);
                if (nextSetting.isSet()) {
                    if (!nextSetting.getProperty().getType().isDataType()) {
                        if (nextSetting.getProperty().isMany()) {
                            List values = (List) nextSetting.getValue();
                            for (int k = 0; k < values.size(); k++) {
                                doMarshal(// 
                                nextSetting.getProperty(), // 
                                (DataObject) values.get(k), changeSummary, csNode, nextModifiedDO, deletedXPaths, xpathToCS, sdoPrefix, rootElementName);
                            }
                        } else {
                            doMarshal(// 
                            nextSetting.getProperty(), // 
                            (DataObject) nextSetting.getValue(), changeSummary, csNode, nextModifiedDO, deletedXPaths, xpathToCS, sdoPrefix, rootElementName);
                        }
                    } else {
                        // This writes out simple values
                        Object value = nextSetting.getValue();
                        if (value == null) {
                            // Marshal out xsi:nil=true
                            marshalNilAttribute(nextSetting.getProperty(), row);
                        } else {
                            String xPath = getXPathForProperty(nextSetting.getProperty());
                            XMLField field = new XMLField(xPath);
                            field.setNamespaceResolver(typeHelper.getNamespaceResolver());
                            row.put(field, value);
                        }
                    }
                }
            }
            List unsetPropNames = changeSummary.getUnsetProps(nextModifiedDO);
            if (!unsetPropNames.isEmpty()) {
                XMLConversionManager xmlConversionManager = ((SDOXMLHelper) typeHelper.getHelperContext().getXMLHelper()).getXmlConversionManager();
                String unsetPropsString = xmlConversionManager.convertObject(unsetPropNames, String.class);
                csNode.setAttributeNS(SDOConstants.SDO_URL, // 
                sdoPrefix + // 
                SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + SDOConstants.CHANGESUMMARY_UNSET, unsetPropsString);
            }
            newNodes.add(csNode);
        }
        changeSummary.setDeletedXPaths(deletedXPaths);
        changeSummary.setModifiedDoms(newNodes);
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) SDOSetting(org.eclipse.persistence.sdo.SDOSetting) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) Namespace(org.eclipse.persistence.internal.oxm.Namespace) SDOChangeSummary(org.eclipse.persistence.sdo.SDOChangeSummary) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) Vector(java.util.Vector) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 2 with DOMRecord

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

the class SDOMarshalListener method doMarshal.

private // 
void doMarshal(// 
SDOProperty prop, // 
DataObject value, // 
SDOChangeSummary cs, Element csNode, SDODataObject modifiedObject, List deletedXPaths, String xpathToCS, String sdoPrefix, String rootElementName) {
    if (value == null) {
        // Marshal out xsi:nil=true
        DOMRecord row = new DOMRecord(csNode);
        Session session = ((SDOXMLHelper) typeHelper.getHelperContext().getXMLHelper()).getXmlContext().getSession();
        row.setSession((AbstractSession) session);
        marshalNilAttribute(prop, row);
        return;
    }
    boolean isDeleted = false;
    // Do we need a second map or can we just check the values of the deleted map
    Object original = cs.getReverseDeletedMap().get(value);
    if ((original != null) && cs.isDeleted((DataObject) original)) {
        isDeleted = true;
    }
    String qualifiedName = getXPathForProperty(prop);
    String uri = null;
    if (prop.isOpenContent()) {
        uri = prop.getUri();
    } else {
        uri = ((XMLField) prop.getXmlMapping().getField()).getXPathFragment().getNamespaceURI();
    }
    if (isDeleted) {
        String pathToNode = getPathFromAncestor(((SDODataObject) original), modifiedObject, cs);
        String containerPath = null;
        containerPath = getQualifiedName(modifiedObject);
        deletedXPaths.add(xpathToCS + containerPath + SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + pathToNode);
        XMLRoot xmlroot = new XMLRoot();
        // set the object to the deep copy
        xmlroot.setObject(value);
        xmlroot.setNamespaceURI(uri);
        xmlroot.setLocalName(qualifiedName);
        xmlMarshaller.marshal(xmlroot, csNode);
    } else {
        // need sdoref
        Element modifiedElement = null;
        if (uri == null) {
            modifiedElement = csNode.getOwnerDocument().createElement(qualifiedName);
        } else {
            modifiedElement = csNode.getOwnerDocument().createElementNS(uri, qualifiedName);
        }
        csNode.appendChild(modifiedElement);
        String nextPath = getPathFromAncestor((SDODataObject) original, (SDODataObject) marshalledObject, cs);
        // Add sdoRef attribute...all modified objects written should have this
        if (nextPath == SDOConstants.EMPTY_STRING) {
            // if this is the root, just put the root element
            modifiedElement.setAttributeNS(SDOConstants.SDO_URL, // 
            sdoPrefix + // 
            SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + // 
            SDOConstants.CHANGESUMMARY_REF, SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + // 
            SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName);
        } else {
            modifiedElement.setAttributeNS(SDOConstants.SDO_URL, // 
            sdoPrefix + // 
            SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + // 
            SDOConstants.CHANGESUMMARY_REF, SDOConstants.SDO_CHANGESUMMARY_REF_PATH_PREFIX + // 
            SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + rootElementName + "/" + nextPath);
        }
        // Added for bug 6346754
        if ((((SDODataObject) original).getContainmentProperty() != null) && ((SDODataObject) original).getContainmentProperty().getType().isDataObjectType()) {
            // may also need xsi:type
            String schemaContext = ((SDOType) value.getType()).getXmlDescriptor().getSchemaReference().getSchemaContext();
            QName schemaContextQName = ((SDOType) value.getType()).getXmlDescriptor().getSchemaReference().getSchemaContextAsQName(((SDOType) value.getType()).getXmlDescriptor().getNonNullNamespaceResolver());
            if (schemaContext != null) {
                String typeValue = schemaContext.substring(1, schemaContext.length());
                String schemaInstancePrefix = ((SDOType) value.getType()).getXmlDescriptor().getNonNullNamespaceResolver().resolveNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
                // may or may not need the xmlns declaration added.
                String schemaContextUri = schemaContextQName.getNamespaceURI();
                String schemaContextPrefix = ((SDOType) value.getType()).getXmlDescriptor().getNonNullNamespaceResolver().resolveNamespaceURI(schemaContextUri);
                if (schemaContextPrefix != null) {
                    modifiedElement.setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + XMLConstants.COLON + schemaContextPrefix, schemaContextQName.getNamespaceURI());
                }
                modifiedElement.setAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, schemaInstancePrefix + XMLConstants.COLON + XMLConstants.SCHEMA_TYPE_ATTRIBUTE, typeValue);
            }
        }
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) SDOType(org.eclipse.persistence.sdo.SDOType) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) SDODataObject(org.eclipse.persistence.sdo.SDODataObject)

Example 3 with DOMRecord

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

the class EISOneToManyMapping method extractKeyRowFromReferenceObject.

/**
 * Build and return a database row that contains a foreign key for the specified reference
 * object.  This will be stored in the nested row(s).
 */
protected AbstractRecord extractKeyRowFromReferenceObject(Object object, AbstractSession session, AbstractRecord parentRecord) {
    int size = this.sourceForeignKeyFields.size();
    AbstractRecord result;
    if (((EISDescriptor) this.getDescriptor()).isXMLFormat()) {
        Element newNode = XPathEngine.getInstance().createUnownedElement(((XMLRecord) parentRecord).getDOM(), (XMLField) getForeignKeyGroupingElement());
        result = new DOMRecord(newNode);
        ((DOMRecord) result).setSession(session);
    } else {
        result = this.descriptor.getObjectBuilder().createRecord(size, session);
    }
    for (int index = 0; index < size; index++) {
        DatabaseField fkField = this.sourceForeignKeyFields.get(index);
        if (object == null) {
            result.add(fkField, null);
        } else {
            DatabaseField pkField = this.sourceForeignKeysToTargetKeys.get(fkField);
            Object value = this.referenceDescriptor.getObjectBuilder().extractValueFromObjectForField(object, pkField, session);
            result.add(fkField, value);
        }
    }
    return result;
}
Also used : DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Element(org.w3c.dom.Element) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) AbstractRecord(org.eclipse.persistence.internal.sessions.AbstractRecord) EISDescriptor(org.eclipse.persistence.eis.EISDescriptor)

Example 4 with DOMRecord

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

the class DOMUnmarshaller method unmarshal.

@Override
public Object unmarshal(Source source, Class<?> clazz) {
    if (!xmlUnmarshaller.isApplicationXML()) {
        throw XMLMarshalException.unsupportedMediaTypeForPlatform();
    }
    try {
        Document document = null;
        document = getParser().parse(source);
        return xmlToObject(new DOMRecord(document), clazz);
    } catch (XMLPlatformException e) {
        throw XMLMarshalException.unmarshalException(e);
    } finally {
        xmlUnmarshaller.getStringBuffer().reset();
    }
}
Also used : DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Document(org.w3c.dom.Document) XMLPlatformException(org.eclipse.persistence.platform.xml.XMLPlatformException)

Example 5 with DOMRecord

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

Aggregations

DOMRecord (org.eclipse.persistence.oxm.record.DOMRecord)38 Element (org.w3c.dom.Element)16 XMLField (org.eclipse.persistence.oxm.XMLField)14 XMLRecord (org.eclipse.persistence.oxm.record.XMLRecord)14 Node (org.w3c.dom.Node)12 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)8 QName (javax.xml.namespace.QName)7 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)6 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)6 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)6 Vector (java.util.Vector)5 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)5 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)5 XMLRoot (org.eclipse.persistence.oxm.XMLRoot)5 Document (org.w3c.dom.Document)5 ArrayList (java.util.ArrayList)4 Iterator (java.util.Iterator)3 CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)3 XMLConversionManager (org.eclipse.persistence.internal.oxm.XMLConversionManager)3 CollectionContainerPolicy (org.eclipse.persistence.internal.queries.CollectionContainerPolicy)3