Search in sources :

Example 1 with XMLReader

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

the class DeferredContentHandler method endElement.

@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
    levelIndex--;
    EndElementEvent event = new EndElementEvent(uri, localName, qName);
    events.add(event);
    if (charactersOccurred) {
        // we know it is a simple element
        processSimpleElement();
    } else if (startOccurred) {
        // we know it is an empty element
        if (attributesOccurred) {
            processEmptyElementWithAttributes();
        } else {
            processEmptyElement();
        }
    }
    if ((levelIndex == 0) && (parent != null)) {
        XMLReader xmlReader = parent.getXMLReader();
        xmlReader.setContentHandler(parent);
        xmlReader.setLexicalHandler(parent);
    }
}
Also used : XMLReader(org.eclipse.persistence.internal.oxm.record.XMLReader)

Example 2 with XMLReader

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

the class XMLFragmentMappingNodeValue method startElement.

@Override
public boolean startElement(XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord, Attributes atts) {
    unmarshalRecord.removeNullCapableValue(this);
    SAXFragmentBuilder builder = unmarshalRecord.getFragmentBuilder();
    builder.setOwningRecord(unmarshalRecord);
    try {
        String namespaceURI = Constants.EMPTY_STRING;
        if (xPathFragment.getNamespaceURI() != null) {
            namespaceURI = xPathFragment.getNamespaceURI();
        }
        String qName = xPathFragment.getLocalName();
        if (xPathFragment.getPrefix() != null) {
            qName = xPathFragment.getPrefix() + Constants.COLON + qName;
        }
        if (!(unmarshalRecord.getPrefixesForFragment().isEmpty())) {
            for (Entry<String, String> next : ((Map<String, String>) unmarshalRecord.getPrefixesForFragment()).entrySet()) {
                builder.startPrefixMapping(next.getKey(), next.getValue());
            }
        }
        builder.startElement(namespaceURI, xPathFragment.getLocalName(), qName, atts);
        XMLReader xmlReader = unmarshalRecord.getXMLReader();
        xmlReader.setContentHandler(builder);
        xmlReader.setLexicalHandler(null);
    } catch (SAXException ex) {
    // Do nothing.
    }
    return true;
}
Also used : Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) XMLReader(org.eclipse.persistence.internal.oxm.record.XMLReader) SAXException(org.xml.sax.SAXException)

Example 3 with XMLReader

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

the class XMLRelationshipMappingNodeValue method processChild.

// Protected to public
public void processChild(XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord, Attributes atts, Descriptor xmlDescriptor, Mapping mapping) throws SAXException {
    if (xmlDescriptor == null) {
        // Use the DescriptorNotFoundContentHandler to "look ahead" and determine if this is a simple or complex element
        // if it is complex the exception should be thrown
        DescriptorNotFoundContentHandler handler = new DescriptorNotFoundContentHandler(unmarshalRecord, mapping);
        String qnameString = xPathFragment.getLocalName();
        if (xPathFragment.getPrefix() != null) {
            qnameString = xPathFragment.getPrefix() + Constants.COLON + qnameString;
        }
        handler.startElement(xPathFragment.getNamespaceURI(), xPathFragment.getLocalName(), qnameString, atts);
        XMLReader xmlReader = unmarshalRecord.getXMLReader();
        xmlReader.setContentHandler(handler);
        xmlReader.setLexicalHandler(handler);
        return;
    }
    if (xmlDescriptor.hasInheritance()) {
        unmarshalRecord.setAttributes(atts);
        CoreAbstractSession session = unmarshalRecord.getSession();
        Class<?> classValue = ((ObjectBuilder) xmlDescriptor.getObjectBuilder()).classFromRow(unmarshalRecord, session);
        if (classValue == null) {
            // no xsi:type attribute - look for type indicator on the default root element
            XPathQName leafElementType = unmarshalRecord.getLeafElementType();
            // if we have a user-set type, try to get the class from the inheritance policy
            if (leafElementType != null) {
                Object indicator = xmlDescriptor.getInheritancePolicy().getClassIndicatorMapping().get(leafElementType);
                if (indicator != null) {
                    classValue = (Class) indicator;
                }
            }
            // if xsi:type is overriden by JSON_TYPE_ATTRIBUTE_NAME unmarshall property
            if (classValue == null && unmarshalRecord.getUnmarshaller().isApplicationJSON() && unmarshalRecord.getUnmarshaller().getJsonTypeConfiguration().getJsonTypeAttributeName() != null && atts.getValue(unmarshalRecord.getUnmarshaller().getJsonTypeConfiguration().getJsonTypeAttributeName()) != null) {
                QName qname = new QName(xmlDescriptor.getSchemaReference().getSchemaContextAsQName().getNamespaceURI(), atts.getValue(unmarshalRecord.getUnmarshaller().getJsonTypeConfiguration().getJsonTypeAttributeName()));
                classValue = (Class) xmlDescriptor.getInheritancePolicy().getClassIndicatorMapping().get(qname);
            }
        }
        if (classValue != null) {
            xmlDescriptor = (Descriptor) session.getDescriptor(classValue);
        } else {
            // on the mapping -  make sure it is non-abstract
            if (Modifier.isAbstract(xmlDescriptor.getJavaClass().getModifiers())) {
                // need to throw an exception here
                throw DescriptorException.missingClassIndicatorField(unmarshalRecord, (org.eclipse.persistence.oxm.XMLDescriptor) xmlDescriptor.getInheritancePolicy().getDescriptor());
            }
        }
    }
    ObjectBuilder targetObjectBuilder = (ObjectBuilder) xmlDescriptor.getObjectBuilder();
    CoreAttributeGroup group = unmarshalRecord.getUnmarshalAttributeGroup();
    CoreAttributeGroup nestedGroup = null;
    if (group == XMLRecord.DEFAULT_ATTRIBUTE_GROUP) {
        nestedGroup = group;
    }
    if (nestedGroup == null) {
        CoreAttributeItem item = group.getItem(getMapping().getAttributeName());
        nestedGroup = item.getGroup(xmlDescriptor.getJavaClass());
        if (nestedGroup == null) {
            if (item.getGroup() == null) {
                nestedGroup = XMLRecord.DEFAULT_ATTRIBUTE_GROUP;
            } else {
                nestedGroup = item.getGroup();
            }
        }
    }
    UnmarshalRecord childRecord = unmarshalRecord.getChildUnmarshalRecord(targetObjectBuilder);
    childRecord.setAttributes(atts);
    childRecord.startDocument();
    childRecord.initializeRecord(null);
    childRecord.setUnmarshalAttributeGroup(nestedGroup);
    childRecord.startElement(xPathFragment.getNamespaceURI(), xPathFragment.getLocalName(), xPathFragment.getShortName(), atts);
    XMLReader xmlReader = unmarshalRecord.getXMLReader();
    xmlReader.setContentHandler(childRecord);
    xmlReader.setLexicalHandler(childRecord);
}
Also used : CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession) CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) QName(javax.xml.namespace.QName) CoreAttributeItem(org.eclipse.persistence.core.queries.CoreAttributeItem) UnmarshalRecord(org.eclipse.persistence.internal.oxm.record.UnmarshalRecord) XMLReader(org.eclipse.persistence.internal.oxm.record.XMLReader) DescriptorNotFoundContentHandler(org.eclipse.persistence.internal.oxm.record.deferred.DescriptorNotFoundContentHandler)

Example 4 with XMLReader

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

the class XMLCompositeObjectMappingNodeValue method startElement.

@Override
public boolean startElement(XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord, Attributes atts) {
    try {
        unmarshalRecord.removeNullCapableValue(this);
        Descriptor xmlDescriptor = (Descriptor) xmlCompositeObjectMapping.getReferenceDescriptor();
        if (null == xmlDescriptor) {
            xmlDescriptor = findReferenceDescriptor(xPathFragment, unmarshalRecord, atts, xmlCompositeObjectMapping, xmlCompositeObjectMapping.getKeepAsElementPolicy());
            if (xmlDescriptor == null) {
                if (xmlCompositeObjectMapping.getField() != null) {
                    // try leaf element type
                    QName leafType = ((Field) xmlCompositeObjectMapping.getField()).getLastXPathFragment().getLeafElementType();
                    if (leafType != null) {
                        XPathFragment frag = new XPathFragment();
                        frag.setNamespaceAware(unmarshalRecord.isNamespaceAware());
                        String xpath = leafType.getLocalPart();
                        String uri = leafType.getNamespaceURI();
                        if (uri != null && uri.length() > 0) {
                            frag.setNamespaceURI(uri);
                            String prefix = ((Descriptor) xmlCompositeObjectMapping.getDescriptor()).getNonNullNamespaceResolver().resolveNamespaceURI(uri);
                            if (prefix != null && prefix.length() > 0) {
                                xpath = prefix + Constants.COLON + xpath;
                            }
                        }
                        frag.setXPath(xpath);
                        Context xmlContext = unmarshalRecord.getUnmarshaller().getContext();
                        xmlDescriptor = xmlContext.getDescriptorByGlobalType(frag);
                    }
                }
            }
            UnmarshalKeepAsElementPolicy policy = xmlCompositeObjectMapping.getKeepAsElementPolicy();
            if (null != policy && ((xmlDescriptor == null && policy.isKeepUnknownAsElement()) || policy.isKeepAllAsElement())) {
                QName schemaType = unmarshalRecord.getTypeQName();
                if (schemaType == null) {
                    schemaType = ((Field) xmlCompositeObjectMapping.getField()).getSchemaType();
                    unmarshalRecord.setTypeQName(schemaType);
                }
                if (schemaType != null) {
                    Class<Object> theClass = unmarshalRecord.getConversionManager().javaType(schemaType);
                    if (theClass == null) {
                        setupHandlerForKeepAsElementPolicy(unmarshalRecord, xPathFragment, atts);
                        return true;
                    }
                } else {
                    setupHandlerForKeepAsElementPolicy(unmarshalRecord, xPathFragment, atts);
                    return true;
                }
            }
        }
        // 
        // Null Composite Objects are marshalled in 2 ways when the input XML node is empty.
        // (1) as null
        // - isNullRepresentedByEmptyNode = true
        // (2) as empty object
        // - isNullRepresentedByEmptyNode = false
        // A deferred contentHandler is used to queue events until we are able to determine
        // whether we are in one of empty/simple/complex state.
        // Control is returned to the UnmarshalHandler after creation of (1) or (2) above is started.
        // Object creation was deferred to the DeferredContentHandler
        // 
        // Check if we need to create the DeferredContentHandler based on policy state
        AbstractNullPolicy nullPolicy = xmlCompositeObjectMapping.getNullPolicy();
        if (nullPolicy.isNullRepresentedByEmptyNode()) {
            String qnameString = xPathFragment.getLocalName();
            if (xPathFragment.getPrefix() != null) {
                qnameString = xPathFragment.getPrefix() + Constants.COLON + qnameString;
            }
            if (null != xmlDescriptor) {
                // Process null capable value
                CompositeObjectMappingContentHandler aHandler = new // 
                CompositeObjectMappingContentHandler(unmarshalRecord, this, xmlCompositeObjectMapping, atts, xPathFragment, xmlDescriptor);
                // Send control to the handler
                aHandler.startElement(xPathFragment.getNamespaceURI(), xPathFragment.getLocalName(), qnameString, atts);
                XMLReader xmlReader = unmarshalRecord.getXMLReader();
                xmlReader.setContentHandler(aHandler);
                xmlReader.setLexicalHandler(aHandler);
            }
        } else {
            if (unmarshalRecord.getXMLReader().isNullRecord(nullPolicy, atts, unmarshalRecord) && nullPolicy.ignoreAttributesForNil()) {
                xmlCompositeObjectMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), null);
            } else {
                Field xmlFld = (Field) this.xmlCompositeObjectMapping.getField();
                if (xmlFld.hasLastXPathFragment()) {
                    unmarshalRecord.setLeafElementType(xmlFld.getLastXPathFragment().getLeafElementType());
                }
                processChild(xPathFragment, unmarshalRecord, atts, xmlDescriptor, xmlCompositeObjectMapping);
            }
        }
    } catch (SAXException e) {
        throw XMLMarshalException.unmarshalException(e);
    }
    return true;
}
Also used : ObjectMarshalContext(org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext) MarshalContext(org.eclipse.persistence.internal.oxm.record.MarshalContext) AbstractNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy) QName(javax.xml.namespace.QName) CompositeObjectMappingContentHandler(org.eclipse.persistence.internal.oxm.record.deferred.CompositeObjectMappingContentHandler) SAXException(org.xml.sax.SAXException) Field(org.eclipse.persistence.internal.oxm.mappings.Field) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.internal.oxm.mappings.UnmarshalKeepAsElementPolicy) XMLReader(org.eclipse.persistence.internal.oxm.record.XMLReader)

Example 5 with XMLReader

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

the class XMLAnyCollectionMappingNodeValue method startElement.

@Override
public boolean startElement(XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord, Attributes atts) {
    try {
        // Mixed Content
        Object collection = unmarshalRecord.getContainerInstance(this);
        startElementProcessText(unmarshalRecord, collection);
        Context xmlContext = unmarshalRecord.getUnmarshaller().getContext();
        // used to only check xsitype when usesXMLRoot was true???
        Descriptor workingDescriptor = findReferenceDescriptor(xPathFragment, unmarshalRecord, atts, xmlAnyCollectionMapping, xmlAnyCollectionMapping.getKeepAsElementPolicy());
        if (workingDescriptor == null) {
            XPathQName qname = new XPathQName(xPathFragment.getNamespaceURI(), xPathFragment.getLocalName(), unmarshalRecord.isNamespaceAware());
            workingDescriptor = xmlContext.getDescriptor(qname);
            // Check if descriptor is for a wrapper, if it is null it out and let continue
            if (workingDescriptor != null && workingDescriptor.isWrapper()) {
                workingDescriptor = null;
            }
        }
        UnmarshalKeepAsElementPolicy policy = xmlAnyCollectionMapping.getKeepAsElementPolicy();
        if (null != policy && (workingDescriptor == null && policy.isKeepUnknownAsElement()) || policy.isKeepAllAsElement()) {
            if (!(xmlAnyCollectionMapping.isMixedContent() && unmarshalRecord.getTextWrapperFragment() != null && unmarshalRecord.getTextWrapperFragment().equals(xPathFragment))) {
                setupHandlerForKeepAsElementPolicy(unmarshalRecord, xPathFragment, atts);
            }
        } else if (workingDescriptor != null) {
            processChild(xPathFragment, unmarshalRecord, atts, workingDescriptor, xmlAnyCollectionMapping);
        } else {
            // need to give to special handler, let it find out what to do depending on if this is simple or complex content
            AnyMappingContentHandler handler = new AnyMappingContentHandler(unmarshalRecord, xmlAnyCollectionMapping.usesXMLRoot());
            String qnameString = xPathFragment.getLocalName();
            if (xPathFragment.getPrefix() != null) {
                qnameString = xPathFragment.getPrefix() + Constants.COLON + qnameString;
            }
            handler.startElement(xPathFragment.getNamespaceURI(), xPathFragment.getLocalName(), qnameString, atts);
            XMLReader xmlReader = unmarshalRecord.getXMLReader();
            xmlReader.setContentHandler(handler);
            xmlReader.setLexicalHandler(handler);
            return true;
        }
    } catch (SAXException e) {
        throw XMLMarshalException.unmarshalException(e);
    }
    return true;
}
Also used : ObjectMarshalContext(org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext) MarshalContext(org.eclipse.persistence.internal.oxm.record.MarshalContext) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.internal.oxm.mappings.UnmarshalKeepAsElementPolicy) XMLReader(org.eclipse.persistence.internal.oxm.record.XMLReader) AnyMappingContentHandler(org.eclipse.persistence.internal.oxm.record.deferred.AnyMappingContentHandler) SAXException(org.xml.sax.SAXException)

Aggregations

XMLReader (org.eclipse.persistence.internal.oxm.record.XMLReader)13 SAXException (org.xml.sax.SAXException)9 Field (org.eclipse.persistence.internal.oxm.mappings.Field)5 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)4 UnmarshalKeepAsElementPolicy (org.eclipse.persistence.internal.oxm.mappings.UnmarshalKeepAsElementPolicy)4 Map (java.util.Map)3 QName (javax.xml.namespace.QName)3 MarshalContext (org.eclipse.persistence.internal.oxm.record.MarshalContext)3 ObjectMarshalContext (org.eclipse.persistence.internal.oxm.record.ObjectMarshalContext)3 AnyMappingContentHandler (org.eclipse.persistence.internal.oxm.record.deferred.AnyMappingContentHandler)2 BinaryMappingContentHandler (org.eclipse.persistence.internal.oxm.record.deferred.BinaryMappingContentHandler)2 AbstractNullPolicy (org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy)2 CoreAttributeGroup (org.eclipse.persistence.core.queries.CoreAttributeGroup)1 CoreAttributeItem (org.eclipse.persistence.core.queries.CoreAttributeItem)1 CoreContainerPolicy (org.eclipse.persistence.internal.core.queries.CoreContainerPolicy)1 CoreAbstractSession (org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)1 UnmarshalRecord (org.eclipse.persistence.internal.oxm.record.UnmarshalRecord)1 CompositeCollectionMappingContentHandler (org.eclipse.persistence.internal.oxm.record.deferred.CompositeCollectionMappingContentHandler)1 CompositeObjectMappingContentHandler (org.eclipse.persistence.internal.oxm.record.deferred.CompositeObjectMappingContentHandler)1 DescriptorNotFoundContentHandler (org.eclipse.persistence.internal.oxm.record.deferred.DescriptorNotFoundContentHandler)1