Search in sources :

Example 16 with XPathFragment

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

the class XMLAnyObjectMapping method writeSingleValue.

@Override
public void writeSingleValue(Object value, Object parent, XMLRecord row, AbstractSession session) {
    DOMRecord record = (DOMRecord) row;
    Node root = record.getDOM();
    Object objectValue = value;
    objectValue = convertObjectValueToDataValue(objectValue, session, row.getMarshaller());
    if (field != null) {
        root = XPathEngine.getInstance().create((XMLField) getField(), root, session);
    }
    org.w3c.dom.Document doc = record.getDocument();
    XMLField xmlRootField = null;
    boolean wasXMLRoot = false;
    Object originalObject = objectValue;
    Node toReplace = getNodeToReplace(root);
    if (usesXMLRoot() && (objectValue instanceof XMLRoot)) {
        xmlRootField = new XMLField();
        wasXMLRoot = true;
        XPathFragment frag = new XPathFragment();
        if ((((XMLRoot) objectValue)).getNamespaceURI() != null) {
            frag.setNamespaceURI(((XMLRoot) objectValue).getNamespaceURI());
        }
        frag.setXPath(((XMLRoot) objectValue).getLocalName());
        xmlRootField.setXPathFragment(frag);
        xmlRootField.setNamespaceResolver(row.getNamespaceResolver());
        objectValue = ((XMLRoot) objectValue).getObject();
    }
    if (objectValue instanceof String) {
        writeSimpleValue(xmlRootField, record, session, originalObject, objectValue, root, toReplace, wasXMLRoot);
    } else if (objectValue instanceof org.w3c.dom.Node) {
        Node importedCopy = doc.importNode((Node) objectValue, true);
        root.appendChild(importedCopy);
    } else {
        XMLDescriptor referenceDescriptor = (XMLDescriptor) session.getDescriptor(objectValue.getClass());
        if (referenceDescriptor == null) {
            writeSimpleValue(xmlRootField, record, session, originalObject, objectValue, root, toReplace, wasXMLRoot);
            return;
        }
        if (wasXMLRoot) {
            if (((XMLRoot) originalObject).getNamespaceURI() != null) {
                String prefix = referenceDescriptor.getNonNullNamespaceResolver().resolveNamespaceURI(((XMLRoot) originalObject).getNamespaceURI());
                if ((prefix == null) || prefix.length() == 0) {
                    prefix = row.getNamespaceResolver().resolveNamespaceURI(((XMLRoot) originalObject).getNamespaceURI());
                }
                if ((prefix == null) || prefix.length() == 0) {
                    xmlRootField.getXPathFragment().setGeneratedPrefix(true);
                    prefix = row.getNamespaceResolver().generatePrefix();
                }
                xmlRootField.getXPathFragment().setXPath(prefix + XMLConstants.COLON + ((XMLRoot) originalObject).getLocalName());
            }
        }
        DOMRecord nestedRecord = (DOMRecord) buildCompositeRow(objectValue, session, referenceDescriptor, row, xmlRootField, originalObject, wasXMLRoot);
        if ((nestedRecord != null) && (toReplace != null)) {
            if (nestedRecord.getDOM() != toReplace) {
                root.replaceChild(nestedRecord.getDOM(), toReplace);
            }
        } else if (nestedRecord != null) {
            root.appendChild(nestedRecord.getDOM());
        } else if (toReplace != null) {
            root.removeChild(toReplace);
        }
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) Node(org.w3c.dom.Node) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Node(org.w3c.dom.Node)

Example 17 with XPathFragment

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

the class XMLCompositeObjectMapping method convertToSimpleTypeIfPresent.

private Object convertToSimpleTypeIfPresent(Object toReturn, XMLRecord nestedRow, AbstractSession executionSession) {
    Node textchild = nestedRow.getDOM().getFirstChild();
    String stringValue = null;
    if ((textchild != null) && (textchild.getNodeType() == Node.TEXT_NODE)) {
        stringValue = textchild.getNodeValue();
        if (stringValue != null && getKeepAsElementPolicy() != UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT && getKeepAsElementPolicy() != UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT) {
            toReturn = stringValue;
        }
    }
    if ((stringValue == null) || stringValue.length() == 0) {
        return toReturn;
    }
    String type = ((Element) nestedRow.getDOM()).getAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, XMLConstants.SCHEMA_TYPE_ATTRIBUTE);
    if ((null != type) && type.length() > 0) {
        XPathFragment typeFragment = new XPathFragment(type);
        String namespaceURI = nestedRow.resolveNamespacePrefix(typeFragment.getPrefix());
        QName schemaTypeQName = new QName(namespaceURI, typeFragment.getLocalName());
        ConversionManager conversionManager = (ConversionManager) executionSession.getDatasourcePlatform().getConversionManager();
        Class<Object> theClass = conversionManager.javaType(schemaTypeQName);
        if (theClass != null) {
            toReturn = conversionManager.convertObject(stringValue, theClass, schemaTypeQName);
        }
    }
    return toReturn;
}
Also used : XPathQName(org.eclipse.persistence.internal.oxm.XPathQName) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) ConversionManager(org.eclipse.persistence.internal.oxm.ConversionManager) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment)

Example 18 with XPathFragment

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

the class XMLAnyCollectionMapping method buildObjectValuesFromDOMRecord.

private Object buildObjectValuesFromDOMRecord(DOMRecord record, AbstractSession session, ObjectBuildingQuery query, JoinedAttributeManager joinManager) {
    // This DOMRecord represents the root node of the AnyType instance
    // Grab ALL children to populate the collection.
    Node root = record.getDOM();
    NodeList children = root.getChildNodes();
    ContainerPolicy cp = getContainerPolicy();
    Object container = null;
    if (reuseContainer) {
        Object currentObject = record.getCurrentObject();
        Object value = getAttributeAccessor().getAttributeValueFromObject(currentObject);
        container = value != null ? value : cp.containerInstance();
    } else {
        container = cp.containerInstance();
    }
    int length = children.getLength();
    Node next = null;
    if (length > 0) {
        next = record.getDOM().getFirstChild();
    }
    while (next != null) {
        Object objectValue = null;
        if (isUnmappedContent(next)) {
            if ((next.getNodeType() == Node.TEXT_NODE) && this.isMixedContent()) {
                if (next.getNodeValue().trim().length() > 0) {
                    objectValue = next.getNodeValue();
                    objectValue = convertDataValueToObjectValue(objectValue, session, record.getUnmarshaller());
                    cp.addInto(objectValue, container, session);
                }
            } else if (next.getNodeType() == Node.ELEMENT_NODE) {
                XMLDescriptor referenceDescriptor = null;
                // In this case it must be an element so we need to dig up the descriptor
                // make a nested record and build an object from it.
                DOMRecord nestedRecord = (DOMRecord) record.buildNestedRow((Element) next);
                if (!useXMLRoot) {
                    referenceDescriptor = getDescriptor(nestedRecord, session, null);
                    objectValue = buildObjectForNonXMLRoot(referenceDescriptor, getConverter(), query, record, nestedRecord, joinManager, session, next, container, cp);
                } else {
                    String schemaType = ((Element) next).getAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, XMLConstants.SCHEMA_TYPE_ATTRIBUTE);
                    QName schemaTypeQName = null;
                    XPathFragment frag = new XPathFragment();
                    if ((null != schemaType) && (schemaType.length() > 0)) {
                        frag.setXPath(schemaType);
                        if (frag.hasNamespace()) {
                            String prefix = frag.getPrefix();
                            XMLPlatform xmlPlatform = XMLPlatformFactory.getInstance().getXMLPlatform();
                            String url = xmlPlatform.resolveNamespacePrefix(next, prefix);
                            frag.setNamespaceURI(url);
                            schemaTypeQName = new QName(url, frag.getLocalName());
                        }
                        XMLContext xmlContext = nestedRecord.getUnmarshaller().getXMLContext();
                        referenceDescriptor = xmlContext.getDescriptorByGlobalType(frag);
                    }
                    if (referenceDescriptor == null) {
                        try {
                            QName qname = new QName(nestedRecord.getNamespaceURI(), nestedRecord.getLocalName());
                            referenceDescriptor = getDescriptor(nestedRecord, session, qname);
                        } catch (XMLMarshalException e) {
                            referenceDescriptor = null;
                        }
                        // Check if descriptor is for a wrapper, if it is null it out and let continue
                        XMLDescriptor xmlReferenceDescriptor = referenceDescriptor;
                        if (referenceDescriptor != null && xmlReferenceDescriptor.isWrapper()) {
                            referenceDescriptor = null;
                        }
                    }
                    // is set, then we want to return either an Element or an XMLRoot wrapping an Element
                    if (getKeepAsElementPolicy() == UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT || (referenceDescriptor == null && getKeepAsElementPolicy() == UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT)) {
                        Object objVal = buildObjectNoReferenceDescriptor(record, getConverter(), session, next, null, null);
                        // if we know the descriptor use it to wrap the Element in an XMLRoot (if necessary)
                        if (referenceDescriptor != null) {
                            objVal = referenceDescriptor.wrapObjectInXMLRoot(objVal, next.getNamespaceURI(), next.getLocalName(), next.getPrefix(), false, record.isNamespaceAware(), record.getUnmarshaller());
                            cp.addInto(objVal, container, session);
                        } else {
                            // no descriptor, so manually build the XMLRoot
                            cp.addInto(buildXMLRoot(next, objVal), container, session);
                        }
                    } else // if we have a descriptor use it to build the object and wrap it in an XMLRoot
                    if (referenceDescriptor != null) {
                        buildObjectAndWrapInXMLRoot(referenceDescriptor, getConverter(), query, record, nestedRecord, joinManager, session, next, container, cp);
                    } else {
                        // no descriptor, but could be TEXT to wrap and return
                        XMLRoot rootValue;
                        if ((rootValue = buildXMLRootForText(next, schemaTypeQName, getConverter(), session, record)) != null) {
                            cp.addInto(rootValue, container, session);
                        }
                    }
                }
            }
        }
        next = next.getNextSibling();
    }
    return container;
}
Also used : XMLContext(org.eclipse.persistence.oxm.XMLContext) DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) QName(javax.xml.namespace.QName) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) Node(org.w3c.dom.Node) NodeList(org.w3c.dom.NodeList) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) ListContainerPolicy(org.eclipse.persistence.internal.queries.ListContainerPolicy) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLPlatform(org.eclipse.persistence.platform.xml.XMLPlatform) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Example 19 with XPathFragment

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

the class XMLChoiceCollectionMapping method getFieldForName.

private XMLField getFieldForName(String localName, String namespaceUri) {
    Iterator<DatabaseField> fields = getFields().iterator();
    while (fields.hasNext()) {
        XMLField nextField = (XMLField) fields.next();
        XPathFragment fragment = nextField.getXPathFragment();
        while (fragment != null && (!fragment.nameIsText())) {
            if (fragment.getNextFragment() == null || fragment.getHasText()) {
                if (fragment.getLocalName().equals(localName)) {
                    String fragUri = fragment.getNamespaceURI();
                    if ((namespaceUri == null && fragUri == null) || (namespaceUri != null && fragUri != null && namespaceUri.equals(fragUri))) {
                        return nextField;
                    }
                }
            }
            fragment = fragment.getNextFragment();
        }
    }
    return null;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment)

Example 20 with XPathFragment

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

the class XMLVariableXPathCollectionMapping method getXPathFragmentForValue.

@Override
public XPathFragment getXPathFragmentForValue(Object obj, NamespaceResolver nr, boolean isNamespaceAware, char sep) {
    Object value = getVariableAttributeAccessor().getAttributeValueFromObject(obj);
    if (value == null) {
        throw XMLMarshalException.nullValueNotAllowed(getVariableAttributeName(), getReferenceClassName());
    }
    String returnString;
    String uri = null;
    if (value instanceof QName) {
        returnString = ((QName) value).getLocalPart();
        uri = ((QName) value).getNamespaceURI();
    } else {
        returnString = (String) value;
    }
    XPathFragment frag = new XPathFragment();
    frag.setLocalName(returnString);
    if (isNamespaceAware && uri != null && uri.length() > 0) {
        String prefix = nr.resolveNamespaceURI(uri);
        if (prefix == null) {
            prefix = nr.generatePrefix();
            // marshalRecord.namespaceDeclaration(prefix, uri);
            frag.setGeneratedPrefix(true);
        }
        if (prefix != null && prefix.length() > 0) {
            frag.setPrefix(prefix);
        // returnString = prefix + sep + returnString;
        }
    }
    // frag.setXPath(returnString);
    // frag.setLocalName(localName);
    frag.setNamespaceURI(uri);
    return frag;
}
Also used : QName(javax.xml.namespace.QName) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment)

Aggregations

XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)76 QName (javax.xml.namespace.QName)15 XMLField (org.eclipse.persistence.oxm.XMLField)15 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)13 Field (org.eclipse.persistence.internal.oxm.mappings.Field)9 XPathNode (org.eclipse.persistence.internal.oxm.XPathNode)8 Element (org.eclipse.persistence.internal.oxm.schema.model.Element)8 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)8 XMLRoot (org.eclipse.persistence.oxm.XMLRoot)7 Node (org.w3c.dom.Node)7 XPathQName (org.eclipse.persistence.internal.oxm.XPathQName)6 DOMRecord (org.eclipse.persistence.oxm.record.DOMRecord)6 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)5 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)5 Element (org.w3c.dom.Element)5 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)4 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)4 AnyAttribute (org.eclipse.persistence.internal.oxm.schema.model.AnyAttribute)4 Attribute (org.eclipse.persistence.internal.oxm.schema.model.Attribute)4 ComplexType (org.eclipse.persistence.internal.oxm.schema.model.ComplexType)4