Search in sources :

Example 1 with MappingNodeValue

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

the class UnmarshalRecordImpl method endElement.

@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    try {
        if (null != selfRecords) {
            for (int x = 0, selfRecordsSize = selfRecords.size(); x < selfRecordsSize; x++) {
                UnmarshalRecord selfRecord = selfRecords.get(x);
                if (selfRecord != null) {
                    selfRecord.endElement(namespaceURI, localName, qName);
                } else {
                    getFragmentBuilder().endSelfElement(namespaceURI, localName, qName);
                }
            }
        }
        if (-1 != unmappedLevel && unmappedLevel <= levelIndex) {
            if (levelIndex == unmappedLevel) {
                unmappedLevel = -1;
            }
            levelIndex--;
            return;
        }
        NodeValue unmarshalNodeValue = xPathNode.getUnmarshalNodeValue();
        if (null != unmarshalNodeValue) {
            boolean isIncludedInAttributeGroup = true;
            if (unmarshalNodeValue.isMappingNodeValue()) {
                Mapping mapping = ((MappingNodeValue) unmarshalNodeValue).getMapping();
                isIncludedInAttributeGroup = this.unmarshalAttributeGroup.containsAttributeInternal(mapping.getAttributeName());
            }
            try {
                if (isIncludedInAttributeGroup) {
                    unmarshalNodeValue.endElement(xPathFragment, this);
                } else {
                    resetStringBuffer();
                }
            } catch (EclipseLinkException e) {
                if ((null == xmlReader) || (null == xmlReader.getErrorHandler())) {
                    throw e;
                } else {
                    SAXParseException saxParseException = new SAXParseException(e.getLocalizedMessage(), getDocumentLocator(), e);
                    xmlReader.getErrorHandler().warning(saxParseException);
                }
            }
        } else {
            XPathNode textNode = xPathNode.getTextNode();
            if (null != textNode && getStringBuffer().length() == 0) {
                NodeValue textNodeUnmarshalNodeValue = textNode.getUnmarshalNodeValue();
                if (textNode.isWhitespaceAware()) {
                    if (textNodeUnmarshalNodeValue.isMappingNodeValue()) {
                        Mapping mapping = ((MappingNodeValue) textNodeUnmarshalNodeValue).getMapping();
                        if (mapping.isAbstractDirectMapping() && isNil()) {
                            Object nullValue = ((DirectMapping) mapping).getNullValue();
                            if (!(Constants.EMPTY_STRING.equals(nullValue))) {
                                setAttributeValue(null, mapping);
                                this.removeNullCapableValue((NullCapableValue) textNodeUnmarshalNodeValue);
                            }
                        } else {
                            textNodeUnmarshalNodeValue.endElement(xPathFragment, this);
                        }
                        setNil(false);
                    }
                } else {
                    // This means empty tag
                    if (textNodeUnmarshalNodeValue.isMappingNodeValue()) {
                        Mapping mapping = ((MappingNodeValue) textNodeUnmarshalNodeValue).getMapping();
                        if (mapping.isAbstractDirectMapping() && isNil()) {
                            Object nullValue = ((DirectMapping) mapping).getNullValue();
                            if (!(Constants.EMPTY_STRING.equals(nullValue))) {
                                setAttributeValue(null, mapping);
                            }
                        }
                        if (mapping.isAbstractDirectMapping() && !isNil() && ((DirectMapping) mapping).getNullPolicy().isNullRepresentedByXsiNil()) {
                            removeNullCapableValue((NullCapableValue) textNodeUnmarshalNodeValue);
                        }
                    }
                }
            }
        }
        XPathFragment xPathFragment = xPathNode.getXPathFragment();
        if ((null != xPathFragment && xPathFragment.nameIsText()) || (xpathNodeIsMixedContent && xPathNode.getParent() != null)) {
            xPathNode = xPathNode.getParent();
        }
        NodeValue xPathNodeUnmarshalNodeValue = xPathNode.getUnmarshalNodeValue();
        if (null != xPathNodeUnmarshalNodeValue && xPathNodeUnmarshalNodeValue.isContainerValue()) {
            predictedNextXPathNode = xPathNode;
        } else {
            predictedNextXPathNode = xPathNode.getNextNode();
        }
        if (null != xPathNode.getParent()) {
            xPathNode = xPathNode.getParent();
        }
        xpathNodeIsMixedContent = false;
        unmarshalContext.endElement(this);
        typeQName = null;
        levelIndex--;
        if (isNil() && levelIndex > 0) {
            setNil(false);
        }
        if ((0 == levelIndex) && (null != parentRecord) && !isSelfRecord()) {
            endDocument();
            // don't endElement on, or pass control to, a 'self' parent
            UnmarshalRecord pRec = parentRecord;
            while (pRec.isSelfRecord()) {
                pRec = pRec.getParentRecord();
            }
            pRec.endElement(namespaceURI, localName, qName);
            xmlReader.setContentHandler(pRec);
            xmlReader.setLexicalHandler(pRec);
        }
    } catch (EclipseLinkException e) {
        if ((null == xmlReader) || (null == xmlReader.getErrorHandler())) {
            throw e;
        } else {
            Locator locator = xmlReader.getLocator();
            SAXParseException saxParseException = new SAXParseException(null, getDocumentLocator(), e);
            xmlReader.getErrorHandler().warning(saxParseException);
        }
    }
}
Also used : MappingNodeValue(org.eclipse.persistence.internal.oxm.MappingNodeValue) NodeValue(org.eclipse.persistence.internal.oxm.NodeValue) TransformationMapping(org.eclipse.persistence.internal.oxm.mappings.TransformationMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) XPathNode(org.eclipse.persistence.internal.oxm.XPathNode) Locator(org.xml.sax.Locator) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) SAXParseException(org.xml.sax.SAXParseException) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) MappingNodeValue(org.eclipse.persistence.internal.oxm.MappingNodeValue)

Example 2 with MappingNodeValue

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

the class UnmarshalRecordImpl method startElement.

@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if (currentObject == null) {
        initializeRecord(atts);
    }
    XPathFragment xPathNodeXPathFragment = xPathNode.getXPathFragment();
    if ((null != xPathNodeXPathFragment && xPathNodeXPathFragment.nameIsText()) || xpathNodeIsMixedContent) {
        xpathNodeIsMixedContent = false;
        NodeValue xPathNodeUnmarshalNodeValue = xPathNode.getUnmarshalNodeValue();
        if (null != xPathNodeUnmarshalNodeValue) {
            boolean isIncludedInAttributeGroup = true;
            if (xPathNodeUnmarshalNodeValue.isMappingNodeValue()) {
                Mapping mapping = ((MappingNodeValue) xPathNodeUnmarshalNodeValue).getMapping();
                isIncludedInAttributeGroup = this.unmarshalAttributeGroup.containsAttributeInternal(mapping.getAttributeName());
            }
            if (isIncludedInAttributeGroup) {
                xPathNodeUnmarshalNodeValue.endElement(xPathFragment, this);
                if (xPathNode.getParent() != null) {
                    xPathNode = xPathNode.getParent();
                }
            }
        }
    }
    // schema locations etc.
    if (null == rootElementName && null == rootElementLocalName && parentRecord == null) {
        rootElementLocalName = localName;
        rootElementName = qName;
        rootElementNamespaceUri = namespaceURI;
        schemaLocation = atts.getValue(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_LOCATION);
        noNamespaceSchemaLocation = atts.getValue(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.NO_NS_SCHEMA_LOCATION);
    }
    try {
        if (null != selfRecords) {
            for (int x = 0, selfRecordsSize = selfRecords.size(); x < selfRecordsSize; x++) {
                UnmarshalRecord selfRecord = selfRecords.get(x);
                if (selfRecord == null) {
                    getFragmentBuilder().startElement(namespaceURI, localName, qName, atts);
                } else {
                    selfRecord.startElement(namespaceURI, localName, qName, atts);
                }
            }
        }
        if (unmappedLevel != -1 && unmappedLevel <= levelIndex) {
            levelIndex++;
            return;
        }
        XPathNode node = null;
        if (null != predictedNextXPathNode) {
            XPathFragment xpf = predictedNextXPathNode.getXPathFragment();
            if (null != xpf && xPathNode == predictedNextXPathNode.getParent() && (localName == xpf.getLocalName() || localName.equals(xpf.getLocalName())) && (namespaceURI == xpf.getNamespaceURI() || namespaceURI.equals(xpf.getNamespaceURI())) && null == xpf.getPredicate() && !xpf.containsIndex()) {
                updateXPathFragment(qName, localName, namespaceURI);
                node = predictedNextXPathNode;
            }
        }
        if (null == node) {
            node = getNonAttributeXPathNode(namespaceURI, localName, qName, atts);
        }
        if (null == node) {
            NodeValue parentNodeValue = xPathNode.getUnmarshalNodeValue();
            if ((null == xPathNode.getXPathFragment()) && (parentNodeValue != null)) {
                XPathFragment parentFragment = new XPathFragment();
                parentFragment.setNamespaceAware(isNamespaceAware());
                if (namespaceURI != null && namespaceURI.length() == 0) {
                    parentFragment.setLocalName(qName);
                    parentFragment.setNamespaceURI(null);
                } else {
                    parentFragment.setLocalName(localName);
                    parentFragment.setNamespaceURI(namespaceURI);
                }
                if (parentNodeValue.startElement(parentFragment, this, atts)) {
                    levelIndex++;
                } else {
                    // UNMAPPED CONTENT
                    startUnmappedElement(namespaceURI, localName, qName, atts);
                    return;
                }
            } else {
                // UNMAPPED CONTENT
                levelIndex++;
                startUnmappedElement(namespaceURI, localName, qName, atts);
                return;
            }
        } else {
            xPathNode = node;
            unmarshalContext.startElement(this);
            levelIndex++;
            if (xmlReader.getMediaType().isApplicationXML()) {
                String xsiNilValue = atts.getValue(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_NIL_ATTRIBUTE);
                if (xsiNilValue != null) {
                    setNil(xsiNilValue.equals(Constants.BOOLEAN_STRING_TRUE) || xsiNilValue.equals("1"));
                }
            }
            if (node.getNullCapableValue() != null) {
                getNullCapableValues().add(node.getNullCapableValue());
            }
            NodeValue nodeValue = node.getUnmarshalNodeValue();
            if (null != nodeValue) {
                boolean isIncludedInAttributeGroup = true;
                if (nodeValue.isMappingNodeValue()) {
                    Mapping mapping = ((MappingNodeValue) nodeValue).getMapping();
                    isIncludedInAttributeGroup = this.unmarshalAttributeGroup.containsAttributeInternal(mapping.getAttributeName());
                }
                if (!isIncludedInAttributeGroup || !nodeValue.startElement(xPathFragment, this, atts)) {
                    // UNMAPPED CONTENT
                    startUnmappedElement(namespaceURI, localName, qName, atts);
                    return;
                }
            }
            // Handle Attributes
            if (xPathNode.getAttributeChildren() != null || xPathNode.getAnyAttributeNodeValue() != null || selfRecords != null) {
                for (int i = 0, size = atts.getLength(); i < size; i++) {
                    String attNamespace = atts.getURI(i);
                    String attLocalName = atts.getLocalName(i);
                    String value = atts.getValue(i);
                    NodeValue attributeNodeValue = null;
                    // attributes
                    if ((attLocalName == null) || (attLocalName.length() == 0)) {
                        String qname = atts.getQName(i);
                        if (qname != null) {
                            int qnameLength = qname.length();
                            if (qnameLength > 0) {
                                int idx = qname.indexOf(Constants.COLON);
                                if (idx > 0) {
                                    attLocalName = qname.substring(idx + 1, qnameLength);
                                    String attPrefix = qname.substring(0, idx);
                                    if (attPrefix.equals(javax.xml.XMLConstants.XMLNS_ATTRIBUTE)) {
                                        attNamespace = javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
                                    }
                                } else {
                                    attLocalName = qname;
                                    if (attLocalName.equals(javax.xml.XMLConstants.XMLNS_ATTRIBUTE)) {
                                        attNamespace = javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI;
                                    }
                                }
                            }
                        }
                    }
                    // Look for any Self-Mapping nodes that may want this attribute.
                    if (this.selfRecords != null) {
                        for (int j = 0; j < selfRecords.size(); j++) {
                            UnmarshalRecord nestedRecord = selfRecords.get(j);
                            if (nestedRecord != null) {
                                attributeNodeValue = nestedRecord.getAttributeChildNodeValue(attNamespace, attLocalName);
                                if (attributeNodeValue != null) {
                                    attributeNodeValue.attribute(nestedRecord, attNamespace, attLocalName, value);
                                }
                            }
                        }
                    }
                    if (attributeNodeValue == null) {
                        attributeNodeValue = this.getAttributeChildNodeValue(attNamespace, attLocalName);
                        try {
                            if (attributeNodeValue != null) {
                                if (attributeNodeValue.isMappingNodeValue()) {
                                    Mapping mapping = ((MappingNodeValue) attributeNodeValue).getMapping();
                                    if (!unmarshalAttributeGroup.containsAttributeInternal(mapping.getAttributeName())) {
                                        continue;
                                    }
                                }
                                attributeNodeValue.attribute(this, attNamespace, attLocalName, value);
                            } else {
                                if (xPathNode.getAnyAttributeNodeValue() != null) {
                                    xPathNode.getAnyAttributeNodeValue().attribute(this, attNamespace, attLocalName, value);
                                }
                            }
                        } catch (EclipseLinkException e) {
                            if ((null == xmlReader) || (null == xmlReader.getErrorHandler())) {
                                throw e;
                            } else {
                                SAXParseException saxParseException = new SAXParseException(e.getLocalizedMessage(), getDocumentLocator(), e);
                                xmlReader.getErrorHandler().warning(saxParseException);
                            }
                        }
                    }
                }
            }
        }
        if (prefixesForFragment != null) {
            this.prefixesForFragment.clear();
        }
    } catch (EclipseLinkException e) {
        if ((null == xmlReader) || (null == xmlReader.getErrorHandler())) {
            throw e;
        } else {
            SAXParseException saxParseException = new SAXParseException(e.getLocalizedMessage(), getDocumentLocator(), e);
            xmlReader.getErrorHandler().error(saxParseException);
        }
    }
}
Also used : MappingNodeValue(org.eclipse.persistence.internal.oxm.MappingNodeValue) NodeValue(org.eclipse.persistence.internal.oxm.NodeValue) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) SAXParseException(org.xml.sax.SAXParseException) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) TransformationMapping(org.eclipse.persistence.internal.oxm.mappings.TransformationMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) MappingNodeValue(org.eclipse.persistence.internal.oxm.MappingNodeValue) XPathNode(org.eclipse.persistence.internal.oxm.XPathNode)

Example 3 with MappingNodeValue

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

the class JAXBValueStore method getJAXBMappingForProperty.

/**
 * Return the JAXB mapping for the SDO property.  They are matched
 * on their XML schema representation.
 */
Mapping getJAXBMappingForProperty(SDOProperty sdoProperty) {
    DatabaseMapping sdoMapping = sdoProperty.getXmlMapping();
    XMLField field;
    if (sdoMapping instanceof XMLObjectReferenceMapping) {
        XMLObjectReferenceMapping referenceMapping = (XMLObjectReferenceMapping) sdoMapping;
        field = (XMLField) referenceMapping.getFields().get(0);
    } else {
        field = (XMLField) sdoMapping.getField();
    }
    TreeObjectBuilder treeObjectBuilder = (TreeObjectBuilder) descriptor.getObjectBuilder();
    XPathNode xPathNode = treeObjectBuilder.getRootXPathNode();
    XPathFragment xPathFragment = field.getXPathFragment();
    while (xPathNode != null && xPathFragment != null) {
        if (xPathFragment.isAttribute()) {
            if (sdoProperty.isMany() && !sdoProperty.isContainment() && !sdoProperty.getType().isDataType()) {
                xPathFragment = null;
                break;
            }
            Map<XPathFragment, XPathNode> attributeChildrenMap = xPathNode.getAttributeChildrenMap();
            if (null == attributeChildrenMap) {
                xPathNode = null;
            } else {
                xPathNode = attributeChildrenMap.get(xPathFragment);
            }
        } else if (xPathFragment.nameIsText()) {
            xPathNode = xPathNode.getTextNode();
        } else {
            Map<XPathFragment, XPathNode> nonAttributeChildrenMap = xPathNode.getNonAttributeChildrenMap();
            if (null == nonAttributeChildrenMap) {
                xPathNode = null;
            } else {
                xPathNode = nonAttributeChildrenMap.get(xPathFragment);
            }
        }
        xPathFragment = xPathFragment.getNextFragment();
        if (xPathFragment != null && xPathFragment.nameIsText()) {
            if (sdoProperty.isMany() && !sdoProperty.isContainment()) {
                xPathFragment = null;
                break;
            }
        }
    }
    if (null == xPathFragment && xPathNode != null) {
        if (xPathNode.getNodeValue().isMappingNodeValue()) {
            MappingNodeValue mappingNodeValue = (MappingNodeValue) xPathNode.getNodeValue();
            return mappingNodeValue.getMapping();
        }
    }
    throw SDOException.sdoJaxbNoMappingForProperty(sdoProperty.getName(), field.getXPath());
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) TreeObjectBuilder(org.eclipse.persistence.internal.oxm.TreeObjectBuilder) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) MappingNodeValue(org.eclipse.persistence.internal.oxm.MappingNodeValue) Map(java.util.Map) WeakHashMap(java.util.WeakHashMap) XPathNode(org.eclipse.persistence.internal.oxm.XPathNode)

Aggregations

MappingNodeValue (org.eclipse.persistence.internal.oxm.MappingNodeValue)3 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)3 XPathNode (org.eclipse.persistence.internal.oxm.XPathNode)3 EclipseLinkException (org.eclipse.persistence.exceptions.EclipseLinkException)2 NodeValue (org.eclipse.persistence.internal.oxm.NodeValue)2 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)2 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)2 TransformationMapping (org.eclipse.persistence.internal.oxm.mappings.TransformationMapping)2 SAXParseException (org.xml.sax.SAXParseException)2 Map (java.util.Map)1 WeakHashMap (java.util.WeakHashMap)1 TreeObjectBuilder (org.eclipse.persistence.internal.oxm.TreeObjectBuilder)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1 XMLField (org.eclipse.persistence.oxm.XMLField)1 XMLObjectReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping)1 Locator (org.xml.sax.Locator)1