Search in sources :

Example 21 with XMLRoot

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

the class XMLAnyCollectionMapping method writeFromObjectIntoRow.

@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) throws DescriptorException {
    if (this.isReadOnly()) {
        return;
    }
    DOMRecord record = (DOMRecord) row;
    Object attributeValue = this.getAttributeValueFromObject(object);
    Node root = record.getDOM();
    org.w3c.dom.Document doc = record.getDocument();
    XMLField xmlRootField = null;
    if (attributeValue == null) {
        return;
    }
    if (field != null) {
        root = XPathEngine.getInstance().create((XMLField) getField(), record.getDOM(), session);
        DOMRecord rootRecord = new DOMRecord(root);
        rootRecord.setSession(session);
        rootRecord.setDocPresPolicy(record.getDocPresPolicy());
        rootRecord.setNamespaceResolver(record.getNamespaceResolver());
        rootRecord.setMarshaller(record.getMarshaller());
        record = rootRecord;
    }
    ContainerPolicy cp = this.getContainerPolicy();
    ArrayList childNodes = getUnmappedChildNodes(record.getDOM().getChildNodes());
    Object iter = cp.iteratorFor(attributeValue);
    int childNodeCount = 0;
    boolean wasXMLRoot = false;
    if (null != iter) {
        while (cp.hasNext(iter) && (childNodeCount < childNodes.size())) {
            Object element = cp.next(iter, session);
            element = convertObjectValueToDataValue(element, session, record.getMarshaller());
            Object originalObject = element;
            Node nextChild = null;
            while (childNodeCount < childNodes.size()) {
                Node nextPossible = (Node) childNodes.get(childNodeCount);
                if ((nextPossible.getNodeType() == Node.ELEMENT_NODE) || (nextPossible.getNodeType() == Node.TEXT_NODE) || (nextPossible.getNodeType() == Node.CDATA_SECTION_NODE)) {
                    nextChild = nextPossible;
                    childNodeCount++;
                    break;
                }
                childNodeCount++;
            }
            if (nextChild == null) {
                break;
            }
            if (usesXMLRoot() && (element instanceof XMLRoot)) {
                xmlRootField = new XMLField();
                wasXMLRoot = true;
                XPathFragment frag = new XPathFragment();
                if ((((XMLRoot) element)).getNamespaceURI() != null) {
                    frag.setNamespaceURI(((XMLRoot) element).getNamespaceURI());
                }
                frag.setXPath(((XMLRoot) element).getLocalName());
                xmlRootField.setXPathFragment(frag);
                xmlRootField.setNamespaceResolver(record.getNamespaceResolver());
                element = ((XMLRoot) element).getObject();
            }
            if (element instanceof String) {
                if (wasXMLRoot) {
                    if (((XMLRoot) originalObject).getNamespaceURI() != null) {
                        String prefix = record.getNamespaceResolver().resolveNamespaceURI(((XMLRoot) originalObject).getNamespaceURI());
                        if ((prefix == null) || prefix.length() == 0) {
                            xmlRootField.getXPathFragment().setGeneratedPrefix(true);
                            prefix = record.getNamespaceResolver().generatePrefix();
                        }
                        xmlRootField.getXPathFragment().setXPath(prefix + XMLConstants.COLON + ((XMLRoot) originalObject).getLocalName());
                    }
                }
                if (xmlRootField != null) {
                    XPathEngine.getInstance().create(xmlRootField, root, element, session);
                } else {
                    Text textNode = doc.createTextNode((String) element);
                    root.replaceChild(textNode, nextChild);
                }
            } else if (element instanceof org.w3c.dom.Node) {
                Node importedCopy = doc.importNode((Node) element, true);
                root.replaceChild(importedCopy, nextChild);
            } else {
                XMLDescriptor referenceDescriptor = (XMLDescriptor) session.getDescriptor(element.getClass());
                if (wasXMLRoot) {
                    if (((XMLRoot) originalObject).getNamespaceURI() != null) {
                        String prefix = referenceDescriptor.getNonNullNamespaceResolver().resolveNamespaceURI(((XMLRoot) originalObject).getNamespaceURI());
                        if ((prefix == null) || prefix.length() == 0) {
                            prefix = record.getNamespaceResolver().resolveNamespaceURI(((XMLRoot) originalObject).getNamespaceURI());
                        }
                        if ((prefix == null) || prefix.length() == 0) {
                            xmlRootField.getXPathFragment().setGeneratedPrefix(true);
                            prefix = record.getNamespaceResolver().generatePrefix();
                        }
                        xmlRootField.getXPathFragment().setXPath(prefix + XMLConstants.COLON + ((XMLRoot) originalObject).getLocalName());
                    }
                }
                DOMRecord nestedRecord = (DOMRecord) buildCompositeRow(element, session, referenceDescriptor, row, xmlRootField, element, wasXMLRoot);
                if (nestedRecord != null) {
                    if (nestedRecord.getDOM() != nextChild) {
                        root.replaceChild(nestedRecord.getDOM(), nextChild);
                    }
                }
            }
        }
    }
    if (childNodeCount < childNodes.size()) {
        // Remove whatever's left
        for (int i = childNodeCount; i < childNodes.size(); i++) {
            Node child = (Node) childNodes.get(i);
            if ((child.getNodeType() == Node.ELEMENT_NODE) || (child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE)) {
                root.removeChild(child);
            }
        }
    }
    if (null != iter && cp.hasNext(iter)) {
        while (cp.hasNext(iter)) {
            // add in extras
            Object element = cp.next(iter, session);
            writeSingleValue(element, object, record, session);
        }
    }
}
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) ArrayList(java.util.ArrayList) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) Text(org.w3c.dom.Text) 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) Node(org.w3c.dom.Node)

Example 22 with XMLRoot

use of org.eclipse.persistence.oxm.XMLRoot 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 23 with XMLRoot

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

the class XMLAnyObjectMapping method writeSimpleValue.

private void writeSimpleValue(XMLField xmlRootField, DOMRecord row, AbstractSession session, Object originalObject, Object value, Node root, Node toReplace, boolean wasXMLRoot) {
    org.w3c.dom.Document doc = row.getDocument();
    if (wasXMLRoot) {
        if (((XMLRoot) originalObject).getNamespaceURI() != null) {
            String 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());
        }
    }
    if (null == xmlRootField) {
        Text textNode = doc.createTextNode((String) value);
        if (toReplace != null) {
            root.replaceChild(textNode, toReplace);
        } else {
            root.appendChild(textNode);
        }
    } else {
        QName qname = ((XMLRoot) originalObject).getSchemaType();
        if (qname != null && !qname.equals(XMLConstants.STRING_QNAME)) {
            xmlRootField.setSchemaType(qname);
            xmlRootField.setIsTypedTextField(true);
            xmlRootField.addJavaConversion(value.getClass(), qname);
        }
        Node newNode = XPathEngine.getInstance().create(xmlRootField, root, value, session);
    }
}
Also used : XMLRoot(org.eclipse.persistence.oxm.XMLRoot) QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) Text(org.w3c.dom.Text)

Example 24 with XMLRoot

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

the class XMLAbstractAnyMapping method buildXMLRoot.

/**
 * Convenience method that builds an XMLRoot wrapping a given object.
 * The local name and uri are set using the given Node.
 */
protected XMLRoot buildXMLRoot(Node node, Object object) {
    XMLRoot xmlRoot = new XMLRoot();
    xmlRoot.setLocalName(node.getLocalName());
    xmlRoot.setNamespaceURI(node.getNamespaceURI());
    xmlRoot.setObject(object);
    return xmlRoot;
}
Also used : XMLRoot(org.eclipse.persistence.oxm.XMLRoot)

Example 25 with XMLRoot

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

the class XMLAbstractAnyMapping method buildXMLRootForText.

/**
 * Convenience method that takes a given node and checks the first child for
 * TEXT_NODE.  If the first child is a text node containing a non-empty
 * String, the String value will be processed and wrapped in an XMLRoot.
 * If schemaTypeQName represents a default XML type (boolean, dateTime, etc)
 * the String value will be converted to that type.  If a converter is
 * provided it will be applied before wrapping in an XMLRoot.
 */
protected XMLRoot buildXMLRootForText(Node node, QName schemaTypeQName, XMLConverter converter, AbstractSession session, DOMRecord record) {
    XMLRoot rootValue = null;
    Node textchild = node.getFirstChild();
    if ((textchild != null) && (textchild.getNodeType() == Node.TEXT_NODE)) {
        String stringValue = textchild.getNodeValue();
        if ((stringValue != null) && stringValue.length() > 0) {
            Object convertedValue = stringValue;
            if (schemaTypeQName != null) {
                ConversionManager conversionManager = (ConversionManager) session.getDatasourcePlatform().getConversionManager();
                Class<Object> theClass = conversionManager.javaType(schemaTypeQName);
                if (theClass != null) {
                    convertedValue = conversionManager.convertObject(convertedValue, theClass, schemaTypeQName);
                }
            }
            if (converter != null) {
                convertedValue = converter.convertDataValueToObjectValue(convertedValue, session, record.getUnmarshaller());
            }
            rootValue = new XMLRoot();
            rootValue.setLocalName(node.getLocalName());
            rootValue.setSchemaType(schemaTypeQName);
            rootValue.setNamespaceURI(node.getNamespaceURI());
            rootValue.setObject(convertedValue);
        }
    }
    return rootValue;
}
Also used : XMLRoot(org.eclipse.persistence.oxm.XMLRoot) Node(org.w3c.dom.Node) ConversionManager(org.eclipse.persistence.internal.oxm.ConversionManager)

Aggregations

XMLRoot (org.eclipse.persistence.oxm.XMLRoot)126 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)34 Document (org.w3c.dom.Document)28 SAXException (org.xml.sax.SAXException)17 StringWriter (java.io.StringWriter)16 Person (org.eclipse.persistence.testing.oxm.xmlroot.Person)13 IOException (java.io.IOException)12 ArrayList (java.util.ArrayList)11 Vector (java.util.Vector)11 Node (org.w3c.dom.Node)11 QName (javax.xml.namespace.QName)10 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)10 Element (org.w3c.dom.Element)10 StringReader (java.io.StringReader)9 InputSource (org.xml.sax.InputSource)9 DataObject (commonj.sdo.DataObject)8 JAXBException (jakarta.xml.bind.JAXBException)8 InputStream (java.io.InputStream)8 MalformedURLException (java.net.MalformedURLException)8 XMLStreamException (javax.xml.stream.XMLStreamException)8