Search in sources :

Example 1 with ContainerValue

use of org.eclipse.persistence.internal.oxm.ContainerValue 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 2 with ContainerValue

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

the class JsonStructureReader method parsePair.

private void parsePair(String name, JsonValue jsonValue) throws SAXException {
    if (jsonValue == null) {
        return;
    }
    ValueType valueType = jsonValue.getValueType();
    if (valueType == ValueType.ARRAY) {
        JsonArray jsonArray = (JsonArray) jsonValue;
        String parentLocalName = name;
        if (attributePrefix != null && parentLocalName.startsWith(attributePrefix)) {
            // do nothing;
            return;
        }
        String uri = Constants.EMPTY_STRING;
        if (isNamespaceAware() && namespaces != null) {
            if (parentLocalName.length() > 2) {
                int nsIndex = parentLocalName.indexOf(getNamespaceSeparator(), 1);
                if (nsIndex > -1) {
                    String prefix = parentLocalName.substring(0, nsIndex);
                    uri = namespaces.resolveNamespacePrefix(prefix);
                }
                if (uri == null || uri == Constants.EMPTY_STRING) {
                    uri = namespaces.getDefaultNamespaceURI();
                } else {
                    parentLocalName = parentLocalName.substring(nsIndex + 1);
                }
            } else {
                uri = namespaces.getDefaultNamespaceURI();
            }
        }
        boolean isTextValue;
        int arraySize = jsonArray.size();
        if (arraySize == 0) {
            if (contentHandler instanceof UnmarshalRecord || isUnmarshalRecordWithinAdapter()) {
                final UnmarshalRecord ur = this.contentHandler instanceof UnmarshalRecord ? (UnmarshalRecord) this.contentHandler : getUnmarshalRecordFromAdapter();
                XPathNode node = ur.getNonAttributeXPathNode(uri, parentLocalName, parentLocalName, null);
                if (node != null) {
                    NodeValue nv = node.getNodeValue();
                    if (nv == null && node.getTextNode() != null) {
                        nv = node.getTextNode().getUnmarshalNodeValue();
                    }
                    if (nv != null && nv.isContainerValue()) {
                        ur.getContainerInstance(((ContainerValue) nv));
                    }
                }
            }
        }
        startCollection();
        XPathFragment groupingXPathFragment = null;
        XPathFragment itemXPathFragment = null;
        if (contentHandler instanceof UnmarshalRecord || isUnmarshalRecordWithinAdapter()) {
            final UnmarshalRecord contentHandler_ = contentHandler instanceof UnmarshalRecord ? (UnmarshalRecord) contentHandler : getUnmarshalRecordFromAdapter();
            isTextValue = isTextValue(parentLocalName, contentHandler_);
            UnmarshalRecord unmarshalRecord = contentHandler_;
            if (unmarshalRecord.getUnmarshaller().isWrapperAsCollectionName()) {
                XPathNode unmarshalRecordXPathNode = unmarshalRecord.getXPathNode();
                if (null != unmarshalRecordXPathNode) {
                    XPathFragment currentFragment = new XPathFragment();
                    currentFragment.setLocalName(parentLocalName);
                    currentFragment.setNamespaceURI(uri);
                    currentFragment.setNamespaceAware(isNamespaceAware());
                    XPathNode groupingXPathNode = unmarshalRecordXPathNode.getNonAttributeChildrenMap().get(currentFragment);
                    if (groupingXPathNode != null) {
                        if (groupingXPathNode.getUnmarshalNodeValue() instanceof CollectionGroupingElementNodeValue) {
                            groupingXPathFragment = groupingXPathNode.getXPathFragment();
                            contentHandler_.startElement(uri, parentLocalName, parentLocalName, new AttributesImpl());
                            XPathNode itemXPathNode = groupingXPathNode.getNonAttributeChildren().get(0);
                            itemXPathFragment = itemXPathNode.getXPathFragment();
                        } else if (groupingXPathNode.getUnmarshalNodeValue() == null) {
                            XPathNode itemXPathNode = groupingXPathNode.getNonAttributeChildren().get(0);
                            if (itemXPathNode != null) {
                                if ((itemXPathNode.getUnmarshalNodeValue()).isContainerValue()) {
                                    groupingXPathFragment = groupingXPathNode.getXPathFragment();
                                    contentHandler_.startElement(uri, parentLocalName, parentLocalName, new AttributesImpl());
                                    itemXPathFragment = itemXPathNode.getXPathFragment();
                                }
                            }
                        }
                    }
                }
            }
            for (JsonValue nextArrayValue : jsonArray) {
                if (nextArrayValue.getValueType() == ValueType.NULL) {
                    contentHandler.setNil(true);
                }
                if (!isTextValue) {
                    if (null != itemXPathFragment) {
                        contentHandler.startElement(itemXPathFragment.getNamespaceURI(), itemXPathFragment.getLocalName(), itemXPathFragment.getLocalName(), attributes.setValue(nextArrayValue, attributePrefix, namespaces, getNamespaceSeparator(), isNamespaceAware()));
                    } else {
                        contentHandler.startElement(uri, parentLocalName, parentLocalName, attributes.setValue(nextArrayValue, attributePrefix, namespaces, getNamespaceSeparator(), isNamespaceAware()));
                    }
                }
                // Internally store each nested array it as JsonObject with name: "item"
                if (valueType == nextArrayValue.getValueType()) {
                    JsonBuilderFactory factory = Json.createBuilderFactory(null);
                    JsonObjectBuilder jsonObjectBuilder = factory.createObjectBuilder();
                    jsonObjectBuilder.add("item", nextArrayValue);
                    nextArrayValue = jsonObjectBuilder.build();
                }
                parseValue(nextArrayValue);
                if (!isTextValue) {
                    if (null != itemXPathFragment) {
                        contentHandler.endElement(itemXPathFragment.getNamespaceURI(), itemXPathFragment.getLocalName(), itemXPathFragment.getLocalName());
                    } else {
                        contentHandler.endElement(uri, parentLocalName, parentLocalName);
                    }
                }
            }
        }
        if (null != groupingXPathFragment) {
            contentHandler.endElement(uri, groupingXPathFragment.getLocalName(), groupingXPathFragment.getLocalName());
        }
        endCollection();
    } else {
        if (attributePrefix != null && name.startsWith(attributePrefix)) {
            return;
        }
        String localName = name;
        String uri = Constants.EMPTY_STRING;
        if (isNamespaceAware() && namespaces != null) {
            if (localName.length() > 2) {
                int nsIndex = localName.indexOf(getNamespaceSeparator(), 1);
                String prefix = Constants.EMPTY_STRING;
                if (nsIndex > -1) {
                    prefix = localName.substring(0, nsIndex);
                }
                uri = namespaces.resolveNamespacePrefix(prefix);
                if (uri == null || uri == Constants.EMPTY_STRING) {
                    uri = namespaces.getDefaultNamespaceURI();
                } else {
                    localName = localName.substring(nsIndex + 1);
                }
                if (localName.equals(Constants.SCHEMA_TYPE_ATTRIBUTE) && uri != null && uri.equals(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI)) {
                    return;
                }
            } else {
                uri = namespaces.getDefaultNamespaceURI();
            }
        }
        if (contentHandler instanceof XMLRootRecord || contentHandler instanceof DeferredContentHandler) {
            if (jsonTypeCompatibility) {
                // is will be read by the xsi:type lookup
                if (!isNamespaceAware() && localName.equals(Constants.SCHEMA_TYPE_ATTRIBUTE)) {
                    return;
                }
            }
            if (textWrapper != null && textWrapper.equals(localName)) {
                parseValue(jsonValue);
                return;
            }
        } else if (contentHandler instanceof UnmarshalRecord && ((UnmarshalRecord) contentHandler).getXPathNode() != null) {
            if (jsonTypeCompatibility) {
                if (!isNamespaceAware() && localName.equals(Constants.SCHEMA_TYPE_ATTRIBUTE) && !((UnmarshalRecord) contentHandler).getXPathNode().hasTypeChild()) {
                    return;
                }
            }
            boolean isTextValue = isTextValue(localName, (UnmarshalRecord) contentHandler);
            if (isTextValue) {
                parseValue(jsonValue);
                return;
            }
            NodeValue nv = ((UnmarshalRecord) contentHandler).getAttributeChildNodeValue(uri, localName);
            if (attributePrefix == null && nv != null) {
                return;
            }
        } else if (isUnmarshalRecordWithinAdapter()) {
            @SuppressWarnings("rawtypes") final UnmarshalRecord contentHandler_ = getUnmarshalRecordFromAdapter();
            if (jsonTypeCompatibility) {
                if (!isNamespaceAware() && localName.equals(Constants.SCHEMA_TYPE_ATTRIBUTE) && !contentHandler_.getXPathNode().hasTypeChild()) {
                    return;
                }
            }
            boolean isTextValue = isTextValue(localName, contentHandler_);
            if (isTextValue) {
                parseValue(jsonValue);
                return;
            }
            NodeValue nv = contentHandler_.getAttributeChildNodeValue(uri, localName);
            if (attributePrefix == null && nv != null) {
                return;
            }
        }
        if (jsonValue.getValueType() == ValueType.NULL) {
            contentHandler.setNil(true);
        }
        contentHandler.startElement(uri, localName, localName, attributes.setValue(jsonValue, attributePrefix, namespaces, getNamespaceSeparator(), isNamespaceAware()));
        parseValue(jsonValue);
        contentHandler.endElement(uri, localName, localName);
    }
}
Also used : CollectionGroupingElementNodeValue(org.eclipse.persistence.internal.oxm.CollectionGroupingElementNodeValue) NodeValue(org.eclipse.persistence.internal.oxm.NodeValue) ValueType(jakarta.json.JsonValue.ValueType) JsonValue(jakarta.json.JsonValue) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) JsonString(jakarta.json.JsonString) CollectionGroupingElementNodeValue(org.eclipse.persistence.internal.oxm.CollectionGroupingElementNodeValue) XPathNode(org.eclipse.persistence.internal.oxm.XPathNode) ContainerValue(org.eclipse.persistence.internal.oxm.ContainerValue) JsonArray(jakarta.json.JsonArray) AttributesImpl(org.xml.sax.helpers.AttributesImpl) JsonBuilderFactory(jakarta.json.JsonBuilderFactory) UnmarshalRecord(org.eclipse.persistence.internal.oxm.record.UnmarshalRecord) AbstractUnmarshalRecord(org.eclipse.persistence.internal.oxm.record.AbstractUnmarshalRecord) XMLRootRecord(org.eclipse.persistence.oxm.record.XMLRootRecord) JsonObjectBuilder(jakarta.json.JsonObjectBuilder) DeferredContentHandler(org.eclipse.persistence.internal.oxm.record.deferred.DeferredContentHandler)

Aggregations

ContainerValue (org.eclipse.persistence.internal.oxm.ContainerValue)2 XPathNode (org.eclipse.persistence.internal.oxm.XPathNode)2 JsonArray (jakarta.json.JsonArray)1 JsonBuilderFactory (jakarta.json.JsonBuilderFactory)1 JsonObjectBuilder (jakarta.json.JsonObjectBuilder)1 JsonString (jakarta.json.JsonString)1 JsonValue (jakarta.json.JsonValue)1 ValueType (jakarta.json.JsonValue.ValueType)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 CoreDescriptor (org.eclipse.persistence.core.descriptors.CoreDescriptor)1 CoreDescriptorEventManager (org.eclipse.persistence.core.descriptors.CoreDescriptorEventManager)1 DescriptorEvent (org.eclipse.persistence.descriptors.DescriptorEvent)1 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)1 CoreField (org.eclipse.persistence.internal.core.helper.CoreField)1 CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)1 CollectionGroupingElementNodeValue (org.eclipse.persistence.internal.oxm.CollectionGroupingElementNodeValue)1 NodeValue (org.eclipse.persistence.internal.oxm.NodeValue)1 Unmarshaller (org.eclipse.persistence.internal.oxm.Unmarshaller)1