Search in sources :

Example 26 with XMLRoot

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

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

the class XMLChoiceCollectionMapping method writeFromObjectIntoRow.

@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord row, AbstractSession session, WriteType writeType) throws DescriptorException {
    if (this.isReadOnly()) {
        return;
    }
    Object attributeValue = getAttributeValueFromObject(object);
    List<XMLEntry> nestedRows = new ArrayList<>();
    XMLRecord record = (XMLRecord) row;
    // First determine which Field is associated with each value:
    if (null != attributeValue) {
        ContainerPolicy cp = getContainerPolicy();
        Object iterator = cp.iteratorFor(attributeValue);
        if (null != iterator) {
            while (cp.hasNext(iterator)) {
                Object value = cp.next(iterator, session);
                value = convertObjectValueToDataValue(value, session, record.getMarshaller());
                NodeValue associatedNodeValue = null;
                XMLField associatedField = null;
                Object fieldValue = value;
                if (value instanceof XMLRoot) {
                    XMLRoot rootValue = (XMLRoot) value;
                    String localName = rootValue.getLocalName();
                    String namespaceUri = rootValue.getNamespaceURI();
                    fieldValue = rootValue.getObject();
                    associatedField = getFieldForName(localName, namespaceUri);
                    if (associatedField == null) {
                        associatedField = getClassToFieldMappings().get(fieldValue.getClass());
                    }
                } else {
                    associatedField = getClassToFieldMappings().get(value.getClass());
                }
                if (associatedField == null) {
                    // this may be a reference mapping
                    List<XMLField> sourceFields = classToSourceFieldsMappings.get(value.getClass());
                    if (sourceFields != null && sourceFields.size() > 0) {
                        DatabaseMapping xmlMapping = (DatabaseMapping) this.choiceElementMappings.get(sourceFields.get(0));
                        for (XMLField next : sourceFields) {
                            fieldValue = ((XMLCollectionReferenceMapping) xmlMapping).buildFieldValue(value, next, session);
                            XMLEntry entry = new XMLEntry();
                            entry.setValue(fieldValue);
                            entry.setXMLField(next);
                            nestedRows.add(entry);
                        }
                    }
                } else {
                    DatabaseMapping xmlMapping = (DatabaseMapping) this.choiceElementMappings.get(associatedField);
                    if (xmlMapping.isAbstractCompositeCollectionMapping()) {
                        fieldValue = ((XMLCompositeCollectionMapping) xmlMapping).buildCompositeRow(fieldValue, session, row, writeType);
                    }
                    XMLEntry entry = new XMLEntry();
                    entry.setValue(fieldValue);
                    entry.setXMLField(associatedField);
                    nestedRows.add(entry);
                }
            }
        }
    }
    row.put(getFields(), nestedRows);
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) NodeValue(org.eclipse.persistence.internal.oxm.NodeValue) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) CollectionContainerPolicy(org.eclipse.persistence.internal.queries.CollectionContainerPolicy) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) ArrayList(java.util.ArrayList) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLEntry(org.eclipse.persistence.oxm.record.XMLEntry) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord)

Example 28 with XMLRoot

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

the class ChoiceCollectionReuseTestCases method getControlObject.

@Override
protected Object getControlObject() {
    Employee employee = new Employee();
    employee.name = "Jane Doe";
    employee.choice = new LinkedList<Object>();
    employee.choice.add("123 Fake Street");
    employee.choice.add(12);
    Address addr = new Address();
    addr.city = "Ottawa";
    addr.street = "45 O'Connor";
    employee.choice.add(addr);
    employee.choice.add(14);
    XMLRoot xmlRoot = new XMLRoot();
    xmlRoot.setLocalName("simpleAddress");
    xmlRoot.setObject("addressString");
    employee.choice.add(xmlRoot);
    employee.phone = "123-4567";
    return employee;
}
Also used : XMLRoot(org.eclipse.persistence.oxm.XMLRoot)

Example 29 with XMLRoot

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

the class XMLChoiceCollectionMappingMixedTestCases method getControlObject.

@Override
protected Object getControlObject() {
    Employee employee = new Employee();
    employee.name = "Jane Doe";
    employee.choice = new java.util.Vector<Object>();
    employee.choice.add("123 Fake Street");
    employee.choice.add(12);
    Address addr = new Address();
    addr.city = "Ottawa";
    addr.street = "45 O'Connor";
    employee.choice.add(addr);
    employee.choice.add(14);
    XMLRoot xmlRoot = new XMLRoot();
    xmlRoot.setLocalName("simpleAddress");
    xmlRoot.setObject("addressString");
    employee.choice.add(xmlRoot);
    employee.phone = "123-4567";
    return employee;
}
Also used : XMLRoot(org.eclipse.persistence.oxm.XMLRoot)

Example 30 with XMLRoot

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

the class Root method equals.

public boolean equals(Object o) {
    try {
        Root theRoot = (Root) o;
        if (theRoot.getCompanies().size() != getCompanies().size()) {
            return false;
        }
        /*if ((!theRoot.getCompanies().containsAll(getCompanies())) || (!getCompanies().containsAll(theRoot.getCompanies()))) {
                return false;
            }*/
        for (int i = 0; i < getCompanies().size(); i++) {
            Object first = getCompanies().get(i);
            Object second = theRoot.getCompanies().get(i);
            if (first instanceof Company && second instanceof Company) {
                if (!first.equals(second)) {
                    return false;
                }
            } else if (first instanceof XMLRoot && second instanceof XMLRoot) {
                if (!(((XMLRoot) first).getLocalName().equals(((XMLRoot) second).getLocalName()))) {
                    return false;
                }
                if (!(((XMLRoot) first).getNamespaceURI().equals(((XMLRoot) second).getNamespaceURI()))) {
                    return false;
                }
            } else {
                return false;
            }
        }
    } catch (ClassCastException e) {
        return false;
    }
    return true;
}
Also used : XMLRoot(org.eclipse.persistence.oxm.XMLRoot) XMLRoot(org.eclipse.persistence.oxm.XMLRoot)

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