Search in sources :

Example 1 with XPathNode

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

the class UnmarshalRecordImpl method getAttributeChildNodeValue.

@Override
public NodeValue getAttributeChildNodeValue(String namespace, String localName) {
    Map<XPathFragment, XPathNode> attributeChildrenMap = xPathNode.getAttributeChildrenMap();
    if (attributeChildrenMap != null) {
        XPathNode resultNode;
        xPathFragment.setLocalName(localName);
        xPathFragment.setNamespaceURI(namespace);
        if (unmarshaller.isCaseInsensitive()) {
            resultNode = getNodeFromLookupTable(attributeChildrenMap, true);
        } else {
            resultNode = attributeChildrenMap.get(xPathFragment);
        }
        if (resultNode != null) {
            return resultNode.getUnmarshalNodeValue();
        }
    }
    return null;
}
Also used : XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) XPathNode(org.eclipse.persistence.internal.oxm.XPathNode)

Example 2 with XPathNode

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

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

the class UnmarshalRecordImpl method startUnmappedElement.

public void startUnmappedElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if (xmlReader.getMediaType().isApplicationXML() && null == selfRecords && !isSelfRecord) {
        ErrorHandler errorHandler = xmlReader.getErrorHandler();
        // Bug 452584 - check if a warning exception should be generated when an unmapped element is encountered
        if (null != errorHandler && unmarshaller.shouldWarnOnUnmappedElement()) {
            StringBuilder messageBuilder = new StringBuilder("unexpected element (uri:\"");
            if (null != namespaceURI) {
                messageBuilder.append(namespaceURI);
            }
            messageBuilder.append("\", local:\"");
            messageBuilder.append(localName);
            messageBuilder.append("\"). Expected elements are ");
            List<XPathNode> nonAttributeChildren = xPathNode.getNonAttributeChildren();
            if (nonAttributeChildren == null || nonAttributeChildren.size() == 0) {
                messageBuilder.append("(none)");
            } else {
                for (int x = 0, size = nonAttributeChildren.size(); x < size; x++) {
                    XPathFragment nonAttributeChildXPathFragment = nonAttributeChildren.get(x).getXPathFragment();
                    messageBuilder.append("<{");
                    String nonAttributeChildXPathFragmentNamespaceURI = nonAttributeChildXPathFragment.getNamespaceURI();
                    if (null != nonAttributeChildXPathFragmentNamespaceURI) {
                        messageBuilder.append(nonAttributeChildXPathFragmentNamespaceURI);
                    }
                    messageBuilder.append('}');
                    messageBuilder.append(nonAttributeChildXPathFragment.getLocalName());
                    messageBuilder.append('>');
                    if (x < size - 1) {
                        messageBuilder.append(',');
                    }
                }
            }
            errorHandler.warning(new SAXParseException(messageBuilder.toString(), getDocumentLocator()));
        }
    }
    if ((null != selfRecords) || (null == xmlReader) || isSelfRecord()) {
        if (-1 == unmappedLevel) {
            this.unmappedLevel = this.levelIndex;
        }
        return;
    }
    Class<?> unmappedContentHandlerClass = unmarshaller.getUnmappedContentHandlerClass();
    UnmappedContentHandler unmappedContentHandler;
    if (null == unmappedContentHandlerClass) {
        unmappedContentHandler = DEFAULT_UNMAPPED_CONTENT_HANDLER;
    } else {
        try {
            PrivilegedNewInstanceFromClass privilegedNewInstanceFromClass = new PrivilegedNewInstanceFromClass(unmappedContentHandlerClass);
            unmappedContentHandler = (UnmappedContentHandler) privilegedNewInstanceFromClass.run();
        } catch (ClassCastException e) {
            throw XMLMarshalException.unmappedContentHandlerDoesntImplement(e, unmappedContentHandlerClass.getName());
        } catch (IllegalAccessException e) {
            throw XMLMarshalException.errorInstantiatingUnmappedContentHandler(e, unmappedContentHandlerClass.getName());
        } catch (InstantiationException e) {
            throw XMLMarshalException.errorInstantiatingUnmappedContentHandler(e, unmappedContentHandlerClass.getName());
        }
    }
    UnmappedContentHandlerWrapper unmappedContentHandlerWrapper = new UnmappedContentHandlerWrapper(this, unmappedContentHandler);
    unmappedContentHandlerWrapper.startElement(namespaceURI, localName, qName, atts);
    xmlReader.setContentHandler(unmappedContentHandlerWrapper);
    xmlReader.setLexicalHandler(unmappedContentHandlerWrapper);
}
Also used : UnmappedContentHandler(org.eclipse.persistence.internal.oxm.unmapped.UnmappedContentHandler) ErrorHandler(org.xml.sax.ErrorHandler) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) XPathNode(org.eclipse.persistence.internal.oxm.XPathNode) PrivilegedNewInstanceFromClass(org.eclipse.persistence.internal.security.PrivilegedNewInstanceFromClass) SAXParseException(org.xml.sax.SAXParseException)

Example 4 with XPathNode

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

the class MarshalRecord method openStartGroupingElements.

/**
 * INTERNAL:
 * Trigger that the grouping elements should be written.  This is normally
 * done when something like a mapping has a non-null value that is
 * marshalled.
 * @param namespaceResolver The NamespaceResolver can be used to resolve the
 * namespace URI for the namespace prefix held by the XPathFragment (if
 * required).
 */
@Override
public XPathFragment openStartGroupingElements(NamespaceResolver namespaceResolver) {
    if (null == groupingElements) {
        return null;
    }
    XPathFragment xPathFragment = null;
    for (int x = 0, groupingElementsSize = groupingElements.size(); x < groupingElementsSize; x++) {
        XPathNode xPathNode = groupingElements.get(x);
        xPathFragment = xPathNode.getXPathFragment();
        openStartElement(xPathFragment, namespaceResolver);
        predicateAttribute(xPathFragment, namespaceResolver);
        if (x != (groupingElementsSize - 1)) {
            closeStartElement();
        }
    }
    groupingElements = null;
    return xPathFragment;
}
Also used : XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) XPathNode(org.eclipse.persistence.internal.oxm.XPathNode)

Example 5 with XPathNode

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

the class IsSetNullPolicy method xPathNode.

@Override
public void xPathNode(XPathNode xPathNode, NullCapableValue nullCapableValue) {
    // isset optional only
    if (!(isNullRepresentedByXsiNil() || marshalNullRepresentation == XMLNullRepresentationType.XSI_NIL)) {
        if (xPathNode.getXPathFragment().isAttribute()) {
            return;
        }
    }
    // get the parent above the text() node
    XPathNode parentNode = xPathNode.getParent();
    parentNode.setNullCapableValue(nullCapableValue);
}
Also used : XPathNode(org.eclipse.persistence.internal.oxm.XPathNode)

Aggregations

XPathNode (org.eclipse.persistence.internal.oxm.XPathNode)11 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)8 SAXParseException (org.xml.sax.SAXParseException)5 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)4 MappingNodeValue (org.eclipse.persistence.internal.oxm.MappingNodeValue)4 NodeValue (org.eclipse.persistence.internal.oxm.NodeValue)4 TransformationMapping (org.eclipse.persistence.internal.oxm.mappings.TransformationMapping)3 HashMap (java.util.HashMap)2 ContainerValue (org.eclipse.persistence.internal.oxm.ContainerValue)2 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)2 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)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 List (java.util.List)1 Map (java.util.Map)1