Search in sources :

Example 16 with Descriptor

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

the class DOMUnmarshaller method xmlToObject.

/**
 * INTERNAL: Convert the Oracle XMLDocument to the reference-class.
 */
public Object xmlToObject(DOMRecord xmlRow, Class<?> referenceClass) throws XMLMarshalException {
    try {
        // Try to get the Encoding and Version from DOM3 APIs if available
        String xmlEncoding = "UTF-8";
        String xmlVersion = "1.0";
        try {
            xmlEncoding = xmlRow.getDocument().getXmlEncoding() != null ? xmlRow.getDocument().getXmlEncoding() : xmlEncoding;
            xmlVersion = xmlRow.getDocument().getXmlVersion() != null ? xmlRow.getDocument().getXmlVersion() : xmlVersion;
        } catch (Exception ex) {
        // if the methods aren't available, then just use the default values
        }
        XMLContext xmlContext = xmlUnmarshaller.getXMLContext();
        // populate and return an XMLRoot
        if (referenceClass != null && (XMLConversionManager.getDefaultJavaTypes().get(referenceClass) != null || CoreClassConstants.XML_GREGORIAN_CALENDAR.isAssignableFrom(referenceClass) || CoreClassConstants.DURATION.isAssignableFrom(referenceClass))) {
            // we're assuming that since we're unmarshalling to a primitive
            // wrapper, the root element has a single text node
            Object nodeVal;
            try {
                Text rootTxt = (Text) xmlRow.getDOM().getFirstChild();
                nodeVal = rootTxt.getNodeValue();
            } catch (Exception ex) {
                // here, either the root element doesn't have a text node as a
                // first child, or there is no first child at all - in any case,
                // try converting null
                nodeVal = null;
            }
            Object obj = xmlContext.getSession().getDatasourcePlatform().getConversionManager().convertObject(nodeVal, referenceClass);
            Root xmlRoot = new XMLRoot();
            xmlRoot.setObject(obj);
            String lName = xmlRow.getDOM().getLocalName();
            if (lName == null) {
                lName = xmlRow.getDOM().getNodeName();
            }
            xmlRoot.setLocalName(lName);
            xmlRoot.setNamespaceURI(xmlRow.getDOM().getNamespaceURI());
            xmlRoot.setEncoding(xmlEncoding);
            xmlRoot.setVersion(xmlVersion);
            return xmlRoot;
        }
        Descriptor descriptor = null;
        CoreAbstractSession readSession = null;
        boolean shouldWrap = true;
        if (referenceClass == null) {
            QName rootQName = new QName(xmlRow.getNamespaceURI(), xmlRow.getLocalName());
            descriptor = xmlContext.getDescriptor(rootQName);
            if (null == descriptor) {
                String type = ((Element) xmlRow.getDOM()).getAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type");
                if (null != type) {
                    XPathFragment typeFragment = new XPathFragment(type);
                    String namespaceURI = xmlRow.resolveNamespacePrefix(typeFragment.getPrefix());
                    typeFragment.setNamespaceURI(namespaceURI);
                    descriptor = xmlContext.getDescriptorByGlobalType(typeFragment);
                }
            } else {
                if (null != descriptor.getDefaultRootElementField() && !descriptor.isResultAlwaysXMLRoot() && !xmlUnmarshaller.isResultAlwaysXMLRoot()) {
                    String descLocalName = descriptor.getDefaultRootElementField().getXPathFragment().getLocalName();
                    String localName = xmlRow.getDOM().getLocalName();
                    if (localName == null) {
                        localName = xmlRow.getDOM().getNodeName();
                    }
                    String namespaceURI = xmlRow.getDOM().getNamespaceURI();
                    if (descLocalName != null && descLocalName.equals(localName)) {
                        String descUri = descriptor.getDefaultRootElementField().getXPathFragment().getNamespaceURI();
                        if ((namespaceURI == null && descUri == null) || (namespaceURI != null && namespaceURI.length() == 0 && descUri == null) || (namespaceURI != null && namespaceURI.equals(descUri))) {
                            // found a descriptor based on root element then know we won't need to wrap in an XMLRoot
                            shouldWrap = false;
                        }
                    }
                }
            }
            if (null == descriptor) {
                throw XMLMarshalException.noDescriptorWithMatchingRootElement(rootQName.toString());
            } else {
                readSession = xmlContext.getSession(descriptor.getJavaClass());
            }
        } else {
            // for XMLObjectReferenceMappings we need a non-shared cache, so
            // try and get a Unit Of Work from the XMLContext
            readSession = xmlContext.getSession(referenceClass);
            descriptor = (Descriptor) readSession.getDescriptor(referenceClass);
            if (descriptor == null) {
                throw XMLMarshalException.descriptorNotFoundInProject(referenceClass.getName());
            }
        }
        Object object = null;
        if (null == xmlRow.getDOM().getAttributes().getNamedItemNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_NIL_ATTRIBUTE)) {
            xmlRow.setUnmarshaller(xmlUnmarshaller);
            xmlRow.setDocPresPolicy(xmlContext.getDocumentPreservationPolicy((AbstractSession) readSession));
            XMLObjectBuilder objectBuilder = (XMLObjectBuilder) descriptor.getObjectBuilder();
            ReadObjectQuery query = new ReadObjectQuery();
            query.setReferenceClass(referenceClass);
            query.setSession((AbstractSession) readSession);
            object = objectBuilder.buildObject(query, xmlRow, null);
            // resolve mapping references
            xmlRow.resolveReferences(readSession, xmlUnmarshaller.getIDResolver());
        }
        String elementNamespaceUri = xmlRow.getDOM().getNamespaceURI();
        String elementLocalName = xmlRow.getDOM().getLocalName();
        if (elementLocalName == null) {
            elementLocalName = xmlRow.getDOM().getNodeName();
        }
        String elementPrefix = xmlRow.getDOM().getPrefix();
        if (shouldWrap || descriptor.isResultAlwaysXMLRoot() || isResultAlwaysXMLRoot) {
            return descriptor.wrapObjectInXMLRoot(object, elementNamespaceUri, elementLocalName, elementPrefix, xmlEncoding, xmlVersion, this.isResultAlwaysXMLRoot, true, xmlUnmarshaller);
        } else {
            return object;
        }
    } finally {
        xmlUnmarshaller.getStringBuffer().reset();
    }
}
Also used : CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession) Root(org.eclipse.persistence.internal.oxm.Root) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) ReadObjectQuery(org.eclipse.persistence.queries.ReadObjectQuery) XMLContext(org.eclipse.persistence.oxm.XMLContext) XMLRoot(org.eclipse.persistence.oxm.XMLRoot) QName(javax.xml.namespace.QName) Element(org.w3c.dom.Element) Text(org.w3c.dom.Text) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) IOException(java.io.IOException) XMLPlatformException(org.eclipse.persistence.platform.xml.XMLPlatformException) SAXException(org.xml.sax.SAXException) XMLObjectBuilder(org.eclipse.persistence.internal.oxm.XMLObjectBuilder) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)

Example 17 with Descriptor

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

the class XPathObjectBuilder method buildRow.

@Override
public XMLRecord buildRow(XMLRecord record, Object object, CoreAbstractSession session, Marshaller marshaller, XPathFragment rootFragment) {
    lazyInitialize();
    XPathNode textNode = rootXPathNode.getTextNode();
    List<XPathNode> nonAttributeChildren = rootXPathNode.getNonAttributeChildren();
    if (null == textNode && null == nonAttributeChildren) {
        return record;
    }
    Descriptor xmlDescriptor = (Descriptor) descriptor;
    XPathNode node = rootXPathNode;
    MarshalRecord marshalRecord = (MarshalRecord) record;
    QName schemaType = null;
    if (marshalRecord.getCycleDetectionStack().contains(object, marshaller.isEqualUsingIdenity())) {
        if (cycleRecoverableClass == null) {
            initCycleRecoverableClasses();
        }
        if (cycleRecoverableClass != null && cycleRecoverableClass.isAssignableFrom(object.getClass())) {
            try {
                Object jaxbMarshaller = marshaller.getProperty(Constants.JAXB_MARSHALLER);
                // Create a proxy instance of CycleRecoverable$Context, a parameter to
                // the onCycleDetected method
                Object contextProxy = CycleRecoverableContextProxy.getProxy(cycleRecoverableContextClass, jaxbMarshaller);
                // Invoke onCycleDetected method, passing in proxy, and reset
                // 'object' to the returned value
                Method onCycleDetectedMethod = object.getClass().getMethod(ON_CYCLE_DETECTED, cycleRecoverableContextClass);
                object = PrivilegedAccessHelper.invokeMethod(onCycleDetectedMethod, object, new Object[] { contextProxy });
            } catch (Exception e) {
                throw XMLMarshalException.marshalException(e);
            }
            // Returned object might have a different descriptor
            xmlDescriptor = (Descriptor) session.getDescriptor(object.getClass());
            if (xmlDescriptor != null) {
                node = ((ObjectBuilder) xmlDescriptor.getObjectBuilder()).getRootXPathNode();
            } else {
                node = null;
            }
            // Push new object
            marshalRecord.getCycleDetectionStack().push(object);
            // Write xsi:type if onCycleDetected returned an object of a type different than the one mapped
            if (xmlDescriptor != descriptor) {
                if (xmlDescriptor == null) {
                    schemaType = record.getConversionManager().schemaType(object.getClass());
                } else {
                    schemaType = xmlDescriptor.getSchemaReference().getSchemaContextAsQName();
                }
                marshalRecord.writeXsiTypeAttribute(xmlDescriptor, schemaType.getNamespaceURI(), schemaType.getLocalPart(), schemaType.getPrefix(), false);
            }
        } else {
            // Push the duplicate object anyway, so that we can get the complete cycle string
            marshalRecord.getCycleDetectionStack().push(object);
            throw XMLMarshalException.objectCycleDetected(marshalRecord.getCycleDetectionStack().getCycleString());
        }
    } else {
        marshalRecord.getCycleDetectionStack().push(object);
    }
    NamespaceResolver namespaceResolver = null;
    if (xmlDescriptor != null) {
        namespaceResolver = xmlDescriptor.getNamespaceResolver();
    }
    MarshalContext marshalContext = null;
    if (xmlDescriptor != null && xmlDescriptor.isSequencedObject()) {
        SequencedObject sequencedObject = (SequencedObject) object;
        marshalContext = new SequencedMarshalContext(sequencedObject.getSettings());
    } else {
        marshalContext = ObjectMarshalContext.getInstance();
    }
    if (null == nonAttributeChildren) {
        textNode.marshal((MarshalRecord) record, object, session, namespaceResolver, marshaller, marshalContext, rootFragment);
    } else {
        if (node == null) {
            // No descriptor for this object, so manually create a MappingNodeValue and marshal it
            XPathNode n = new XPathNode();
            CompositeObjectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, org.eclipse.persistence.oxm.record.XMLRecord> m = new XMLCompositeObjectMapping();
            m.setXPath(".");
            XMLCompositeObjectMappingNodeValue nv = new XMLCompositeObjectMappingNodeValue(m);
            n.setMarshalNodeValue(nv);
            nv.marshalSingleValue(new XPathFragment("."), marshalRecord, null, object, session, namespaceResolver, marshalContext);
        } else {
            for (int x = 0, size = marshalContext.getNonAttributeChildrenSize(node); x < size; x++) {
                XPathNode xPathNode = (XPathNode) marshalContext.getNonAttributeChild(x, node);
                xPathNode.marshal((MarshalRecord) record, object, session, namespaceResolver, marshaller, marshalContext.getMarshalContext(x), rootFragment);
            }
        }
    }
    marshalRecord.getCycleDetectionStack().pop();
    return record;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) AbstractMarshalRecord(org.eclipse.persistence.internal.oxm.record.AbstractMarshalRecord) MarshalRecord(org.eclipse.persistence.internal.oxm.record.MarshalRecord) SequencedObject(org.eclipse.persistence.oxm.sequenced.SequencedObject) Converter(org.eclipse.persistence.mappings.converters.Converter) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession) QName(javax.xml.namespace.QName) SequencedMarshalContext(org.eclipse.persistence.internal.oxm.record.SequencedMarshalContext) Method(java.lang.reflect.Method) XMLRecord(org.eclipse.persistence.internal.oxm.record.XMLRecord) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) ObjectMarshalContext(org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext) SequencedMarshalContext(org.eclipse.persistence.internal.oxm.record.SequencedMarshalContext) MarshalContext(org.eclipse.persistence.internal.oxm.record.MarshalContext) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) SequencedObject(org.eclipse.persistence.oxm.sequenced.SequencedObject) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)

Example 18 with Descriptor

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

the class XPathObjectBuilder method marshalAttributes.

@Override
public boolean marshalAttributes(MarshalRecord marshalRecord, Object object, CoreAbstractSession session) {
    lazyInitialize();
    boolean hasValue = false;
    NamespaceResolver namespaceResolver = ((Descriptor) descriptor).getNamespaceResolver();
    List<XPathNode> attributeChildren = rootXPathNode.getAttributeChildren();
    if (null != attributeChildren) {
        ObjectMarshalContext objectMarshalContext = ObjectMarshalContext.getInstance();
        for (XPathNode anAttributeChildren : attributeChildren) {
            hasValue = anAttributeChildren.marshal(marshalRecord, object, session, namespaceResolver, null, objectMarshalContext, null) || hasValue;
        }
    }
    if (rootXPathNode.getAnyAttributeNode() != null) {
        hasValue = rootXPathNode.getAnyAttributeNode().marshal(marshalRecord, object, session, namespaceResolver, null, ObjectMarshalContext.getInstance(), null) || hasValue;
    }
    List<XPathNode> selfChildren = rootXPathNode.getSelfChildren();
    if (null != selfChildren) {
        for (XPathNode selfXPathNode : selfChildren) {
            NodeValue marshalNodeValue = selfXPathNode.getMarshalNodeValue();
            if (marshalNodeValue instanceof MappingNodeValue) {
                Mapping selfMapping = ((MappingNodeValue) marshalNodeValue).getMapping();
                Object value = selfMapping.getAttributeValueFromObject(object);
                Descriptor referenceDescriptor = (Descriptor) selfMapping.getReferenceDescriptor();
                Descriptor valueDescriptor;
                if (value != null && (referenceDescriptor == null || referenceDescriptor.hasInheritance())) {
                    valueDescriptor = (Descriptor) session.getDescriptor(value.getClass());
                } else {
                    valueDescriptor = referenceDescriptor;
                }
                if (null != valueDescriptor) {
                    marshalRecord.addXsiTypeAndClassIndicatorIfRequired(valueDescriptor, referenceDescriptor, (Field) selfMapping.getField(), false);
                }
            }
            selfXPathNode.marshalSelfAttributes(marshalRecord, object, session, namespaceResolver, marshalRecord.getMarshaller());
        }
    }
    return hasValue;
}
Also used : ObjectMarshalContext(org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) TransformationMapping(org.eclipse.persistence.internal.oxm.mappings.TransformationMapping) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) AnyObjectMapping(org.eclipse.persistence.internal.oxm.mappings.AnyObjectMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) InverseReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.InverseReferenceMapping) VariableXPathCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathCollectionMapping) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) AnyCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.AnyCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) FragmentMapping(org.eclipse.persistence.internal.oxm.mappings.FragmentMapping) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping) FragmentCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.FragmentCollectionMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) AnyAttributeMapping(org.eclipse.persistence.internal.oxm.mappings.AnyAttributeMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) VariableXPathObjectMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathObjectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) SequencedObject(org.eclipse.persistence.oxm.sequenced.SequencedObject)

Example 19 with Descriptor

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

the class UnmarshalRecordImpl method endDocument.

@Override
public void endDocument() throws SAXException {
    if (unmarshaller.getIDResolver() != null && parentRecord == null) {
        unmarshaller.getIDResolver().endDocument();
    }
    if (null != selfRecords) {
        for (int x = 0, selfRecordsSize = selfRecords.size(); x < selfRecordsSize; x++) {
            UnmarshalRecord selfRecord = selfRecords.get(x);
            if (selfRecord != null) {
                selfRecord.endDocument();
            }
        }
    }
    if (null != xPathNode.getSelfChildren()) {
        int selfChildrenSize = xPathNode.getSelfChildren().size();
        for (int x = 0; x < selfChildrenSize; x++) {
            XPathNode selfNode = xPathNode.getSelfChildren().get(x);
            if (null != selfNode.getNodeValue()) {
                selfNode.getNodeValue().endSelfNodeValue(this, selfRecords.get(x), attributes);
            }
        }
    }
    CoreDescriptor xmlDescriptor = treeObjectBuilder.getDescriptor();
    try {
        // All populated containerValues need to be set on the object
        if (null != populatedContainerValues) {
            for (int populatedCVSize = populatedContainerValues.size(), i = populatedCVSize - 1; i >= 0; i--) {
                ContainerValue cv = (populatedContainerValues.get(i));
                cv.setContainerInstance(currentObject, getContainerInstance(cv, cv.isDefaultEmptyContainer()));
            }
        }
        // Additionally if any containerValues are defaultEmptyContainerValues they need to be set to a new empty container
        if (null != defaultEmptyContainerValues) {
            for (int defaultEmptyCVSize = defaultEmptyContainerValues.size(), i = defaultEmptyCVSize - 1; i >= 0; i--) {
                ContainerValue cv = (defaultEmptyContainerValues.get(i));
                cv.setContainerInstance(currentObject, getContainerInstance(cv, cv.isDefaultEmptyContainer()));
            }
        }
        // trigger the mapping.
        if (null != nullCapableValues) {
            for (int x = 0, nullValuesSize = nullCapableValues.size(); x < nullValuesSize; x++) {
                nullCapableValues.get(x).setNullValue(currentObject, session);
            }
        }
        // PROCESS TRANSFORMATION MAPPINGS
        List<TransformationMapping> transformationMappings = treeObjectBuilder.getTransformationMappings();
        if (null != transformationMappings) {
            for (int x = 0, transformationMappingsSize = transformationMappings.size(); x < transformationMappingsSize; x++) {
                TransformationMapping transformationMapping = transformationMappings.get(x);
                transformationMapping.readFromRowIntoObject((XMLRecord) transformationRecord, currentObject, session, true);
            }
        }
        Unmarshaller.Listener listener = unmarshaller.getUnmarshalListener();
        if (listener != null) {
            if (this.parentRecord != null) {
                listener.afterUnmarshal(currentObject, parentRecord.getCurrentObject());
            } else {
                listener.afterUnmarshal(currentObject, null);
            }
        }
        // HANDLE POST BUILD EVENTS
        if (xmlDescriptor.hasEventManager()) {
            CoreDescriptorEventManager eventManager = xmlDescriptor.getEventManager();
            if (null != eventManager && eventManager.hasAnyEventListeners()) {
                DescriptorEvent event = new DescriptorEvent(currentObject);
                event.setSession((AbstractSession) session);
                // this);
                event.setRecord(null);
                event.setEventCode(DescriptorEventManager.PostBuildEvent);
                eventManager.executeEvent(event);
            }
        }
    } catch (EclipseLinkException e) {
        if (null == xmlReader.getErrorHandler()) {
            throw e;
        } else {
            SAXParseException saxParseException = new SAXParseException(null, getDocumentLocator(), e);
            xmlReader.getErrorHandler().error(saxParseException);
        }
    }
    // if the object has any primary key fields set, add it to the cache
    if (null != referenceResolver) {
        if (null != xmlDescriptor) {
            List primaryKeyFields = xmlDescriptor.getPrimaryKeyFields();
            if (null != primaryKeyFields) {
                int primaryKeyFieldsSize = primaryKeyFields.size();
                if (primaryKeyFieldsSize > 0) {
                    CacheId pk = (CacheId) treeObjectBuilder.extractPrimaryKeyFromObject(currentObject, session);
                    for (int x = 0; x < primaryKeyFieldsSize; x++) {
                        Object value = pk.getPrimaryKey()[x];
                        if (null == value) {
                            Field pkField = (Field) xmlDescriptor.getPrimaryKeyFields().get(x);
                            pk.set(x, unmarshaller.getContext().getValueByXPath(currentObject, pkField.getXPath(), pkField.getNamespaceResolver(), Object.class));
                        }
                    }
                    referenceResolver.putValue(xmlDescriptor.getJavaClass(), pk, currentObject);
                    if (unmarshaller.getIDResolver() != null) {
                        try {
                            if (primaryKeyFieldsSize > 1) {
                                Map<String, Object> idWrapper = new HashMap<>();
                                for (int x = 0; x < primaryKeyFieldsSize; x++) {
                                    String idName = (String) xmlDescriptor.getPrimaryKeyFieldNames().get(x);
                                    Object idValue = pk.getPrimaryKey()[x];
                                    idWrapper.put(idName, idValue);
                                }
                                unmarshaller.getIDResolver().bind(idWrapper, currentObject);
                            } else {
                                unmarshaller.getIDResolver().bind(pk.getPrimaryKey()[0], currentObject);
                            }
                        } catch (SAXException e) {
                            throw XMLMarshalException.unmarshalException(e);
                        }
                    }
                }
            }
        }
    }
    if (null != parentRecord) {
        reset();
    }
    // Set XML Location if applicable
    if (xmlLocation != null && ((Descriptor) xmlDescriptor).getLocationAccessor() != null) {
        ((Descriptor) xmlDescriptor).getLocationAccessor().setAttributeValueInObject(getCurrentObject(), xmlLocation);
    }
}
Also used : CoreDescriptorEventManager(org.eclipse.persistence.core.descriptors.CoreDescriptorEventManager) TransformationMapping(org.eclipse.persistence.internal.oxm.mappings.TransformationMapping) HashMap(java.util.HashMap) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent) XPathNode(org.eclipse.persistence.internal.oxm.XPathNode) ContainerValue(org.eclipse.persistence.internal.oxm.ContainerValue) SAXException(org.xml.sax.SAXException) Field(org.eclipse.persistence.internal.oxm.mappings.Field) CoreField(org.eclipse.persistence.internal.core.helper.CoreField) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) SAXParseException(org.xml.sax.SAXParseException) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) List(java.util.List) ArrayList(java.util.ArrayList) Unmarshaller(org.eclipse.persistence.internal.oxm.Unmarshaller)

Example 20 with Descriptor

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

the class UnmarshalRecordImpl method initializeRecord.

@Override
public void initializeRecord(Mapping selfRecordMapping) throws SAXException {
    try {
        Descriptor xmlDescriptor = (Descriptor) treeObjectBuilder.getDescriptor();
        if (xmlDescriptor.isSequencedObject()) {
            unmarshalContext = new SequencedUnmarshalContext();
        } else {
            unmarshalContext = ObjectUnmarshalContext.getInstance();
        }
        currentObject = this.xmlReader.getCurrentObject(session, selfRecordMapping);
        if (currentObject == null) {
            currentObject = treeObjectBuilder.buildNewInstance();
        }
        if (xmlDescriptor.getLocationAccessor() != null && xmlReader.getLocator() != null) {
            // Check to see if this Descriptor isLocationAware
            // Store the snapshot of the current documentLocator
            xmlLocation = new Locator2Impl(xmlReader.getLocator());
        }
        Object parentRecordCurrentObject = null;
        if (null != this.parentRecord) {
            parentRecordCurrentObject = parentRecord.getCurrentObject();
        }
        Unmarshaller.Listener xmlUnmarshalListener = unmarshaller.getUnmarshalListener();
        if (null != xmlUnmarshalListener) {
            if (null == this.parentRecord) {
                xmlUnmarshalListener.beforeUnmarshal(currentObject, null);
            } else {
                xmlUnmarshalListener.beforeUnmarshal(currentObject, parentRecordCurrentObject);
            }
        }
        if (null == parentRecord) {
            this.xmlReader.newObjectEvent(currentObject, null, selfRecordMapping);
        } else {
            this.xmlReader.newObjectEvent(currentObject, parentRecordCurrentObject, selfRecordMapping);
        }
        List containerValues = treeObjectBuilder.getContainerValues();
        if (null != containerValues) {
            int containerSize = containerValues.size();
            containerInstances = new Object[containerSize];
            populatedContainerValues = new ArrayList(containerSize);
        }
        if (null != xPathNode.getSelfChildren()) {
            int selfChildrenSize = xPathNode.getSelfChildren().size();
            selfRecords = new ArrayList<>(selfChildrenSize);
            for (int x = 0; x < selfChildrenSize; x++) {
                NodeValue nv = xPathNode.getSelfChildren().get(x).getNodeValue();
                if (null != nv) {
                    selfRecords.add(nv.buildSelfRecord(this, attributes));
                }
            }
        }
    } catch (EclipseLinkException e) {
        if (null == xmlReader.getErrorHandler()) {
            throw e;
        } else {
            SAXParseException saxParseException = new SAXParseException(null, getDocumentLocator(), e);
            xmlReader.getErrorHandler().error(saxParseException);
        }
    }
}
Also used : MappingNodeValue(org.eclipse.persistence.internal.oxm.MappingNodeValue) NodeValue(org.eclipse.persistence.internal.oxm.NodeValue) ArrayList(java.util.ArrayList) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) SAXParseException(org.xml.sax.SAXParseException) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) List(java.util.List) ArrayList(java.util.ArrayList) Unmarshaller(org.eclipse.persistence.internal.oxm.Unmarshaller) Locator2Impl(org.xml.sax.ext.Locator2Impl)

Aggregations

Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)70 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)25 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)23 QName (javax.xml.namespace.QName)19 Schema (org.eclipse.persistence.internal.oxm.schema.model.Schema)18 Field (org.eclipse.persistence.internal.oxm.mappings.Field)17 Project (org.eclipse.persistence.sessions.Project)16 CoreDescriptor (org.eclipse.persistence.core.descriptors.CoreDescriptor)14 SAXException (org.xml.sax.SAXException)14 List (java.util.List)13 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)13 SchemaModelGeneratorProperties (org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties)13 SchemaModelProject (org.eclipse.persistence.internal.oxm.schema.SchemaModelProject)11 UnmarshalKeepAsElementPolicy (org.eclipse.persistence.internal.oxm.mappings.UnmarshalKeepAsElementPolicy)10 Document (org.w3c.dom.Document)10 ArrayList (java.util.ArrayList)9 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)9 CoreAbstractSession (org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)8 CompositeObjectMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping)8 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)7