Search in sources :

Example 1 with UnmarshalRecord

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

the class JAXBUnmarshaller method unmarshal.

/**
 * Unmarshal the object based on the binding metadata associated with the
 * TypeMappingInfo.
 */
public JAXBElement unmarshal(XMLStreamReader streamReader, TypeMappingInfo type) throws JAXBException {
    try {
        Descriptor xmlDescriptor = type.getXmlDescriptor();
        if (type.getType() instanceof Class) {
            Class<?> javaClass = (Class) type.getType();
            Class<?> componentClass = javaClass.getComponentType();
            if (javaClass.isArray() && javaClass != CoreClassConstants.APBYTE && javaClass != CoreClassConstants.ABYTE && XMLConversionManager.getDefaultJavaTypes().get(componentClass) != null) {
                // Top-level array.  Descriptor will be for an EL-generated class, containing one DirectCollection mapping.
                DirectCollectionMapping mapping = (DirectCollectionMapping) xmlDescriptor.getMappings().get(0);
                XMLStreamReaderReader staxReader = new XMLStreamReaderReader(xmlUnmarshaller);
                staxReader.setErrorHandler(xmlUnmarshaller.getErrorHandler());
                PrimitiveArrayContentHandler primitiveArrayContentHandler = new PrimitiveArrayContentHandler(javaClass, componentClass, mapping.usesSingleNode());
                staxReader.setContentHandler(primitiveArrayContentHandler);
                XMLStreamReaderInputSource inputSource = new XMLStreamReaderInputSource(streamReader);
                staxReader.parse(inputSource);
                return primitiveArrayContentHandler.getJaxbElement();
            }
        }
        if (null != xmlDescriptor && null == getSchema()) {
            RootLevelXmlAdapter adapter = null;
            if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
                adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
            }
            UnmarshalRecord wrapper = (UnmarshalRecord) xmlDescriptor.getObjectBuilder().createRecordFromXMLContext(xmlUnmarshaller.getXMLContext());
            org.eclipse.persistence.internal.oxm.record.UnmarshalRecord unmarshalRecord = wrapper.getUnmarshalRecord();
            XMLStreamReaderReader staxReader = new XMLStreamReaderReader(xmlUnmarshaller);
            unmarshalRecord.setUnmarshaller(xmlUnmarshaller);
            unmarshalRecord.setXMLReader(staxReader);
            staxReader.setContentHandler(unmarshalRecord);
            staxReader.parse(streamReader);
            Object value = null;
            if (unmarshalRecord.isNil()) {
                value = null;
            } else {
                value = unmarshalRecord.getCurrentObject();
            }
            if (value instanceof WrappedValue) {
                value = ((WrappedValue) value).getValue();
            }
            if (value instanceof ManyValue) {
                value = ((ManyValue) value).getItem();
            }
            if (adapter != null) {
                try {
                    value = adapter.getXmlAdapter().unmarshal(value);
                } catch (Exception ex) {
                    throw new JAXBException(XMLMarshalException.marshalException(ex));
                }
            }
            Class<?> declaredClass = null;
            if (type.getType() instanceof Class) {
                declaredClass = (Class) type.getType();
            } else {
                declaredClass = Object.class;
            }
            return new JAXBElement(new QName(unmarshalRecord.getRootElementNamespaceUri(), unmarshalRecord.getLocalName()), declaredClass, value);
        }
        if (jaxbContext.getTypeMappingInfoToGeneratedType() == null) {
            return unmarshal(streamReader, type.getType());
        }
        RootLevelXmlAdapter adapter = null;
        if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
            adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
        }
        Class<?> unmarshalClass = null;
        if (jaxbContext.getTypeMappingInfoToGeneratedType().size() > 0) {
            unmarshalClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(type);
        }
        if (unmarshalClass != null) {
            JAXBElement unmarshalled = unmarshal(streamReader, unmarshalClass);
            Class<?> declaredClass = null;
            if (type.getType() instanceof Class) {
                declaredClass = (Class) type.getType();
            } else {
                declaredClass = Object.class;
            }
            Object value = unmarshalled.getValue();
            if (adapter != null) {
                try {
                    value = adapter.getXmlAdapter().unmarshal(value);
                } catch (Exception ex) {
                    throw new JAXBException(XMLMarshalException.marshalException(ex));
                }
            }
            JAXBElement returnVal = new JAXBElement(unmarshalled.getName(), declaredClass, unmarshalled.getScope(), value);
            return returnVal;
        } else if (type.getType() instanceof Class) {
            if (adapter != null) {
                JAXBElement element = unmarshal(streamReader, adapter.getBoundType());
                try {
                    Object value = adapter.getXmlAdapter().unmarshal(element.getValue());
                    element.setValue(value);
                    return element;
                } catch (Exception ex) {
                    throw new JAXBException(XMLMarshalException.marshalException(ex));
                }
            }
            return unmarshal(streamReader, (Class) type.getType());
        } else if (type.getType() instanceof ParameterizedType) {
            return unmarshal(streamReader, ((ParameterizedType) type.getType()).getRawType());
        }
        return null;
    } catch (XMLMarshalException xmlMarshalException) {
        throw handleXMLMarshalException(xmlMarshalException);
    } catch (SAXException e) {
        throw new JAXBException(e);
    }
}
Also used : QName(javax.xml.namespace.QName) JAXBException(jakarta.xml.bind.JAXBException) ManyValue(org.eclipse.persistence.internal.jaxb.many.ManyValue) JAXBElement(jakarta.xml.bind.JAXBElement) WrappedValue(org.eclipse.persistence.internal.jaxb.WrappedValue) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) BeanValidationException(org.eclipse.persistence.exceptions.BeanValidationException) SAXException(org.xml.sax.SAXException) UnmarshalException(jakarta.xml.bind.UnmarshalException) PropertyException(jakarta.xml.bind.PropertyException) JAXBException(jakarta.xml.bind.JAXBException) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) RootLevelXmlAdapter(org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter) SAXException(org.xml.sax.SAXException) ParameterizedType(java.lang.reflect.ParameterizedType) XMLStreamReaderReader(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader) UnmarshalRecord(org.eclipse.persistence.oxm.record.UnmarshalRecord) XMLStreamReaderInputSource(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Example 2 with UnmarshalRecord

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

the class DBWSModelProject method buildResultDescriptor.

// result
// 
// type|attachment
protected XMLDescriptor buildResultDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(Result.class);
    descriptor.setDefaultRootElement("result");
    XMLTransformationMapping type = new XMLTransformationMapping();
    type.setAttributeName("type");
    QNameTransformer qNameTransformer = new QNameTransformer("type/text()");
    type.addFieldTransformer("type/text()", qNameTransformer);
    type.setAttributeTransformer(qNameTransformer);
    descriptor.addMapping(type);
    XMLCompositeObjectMapping attachment = new XMLCompositeObjectMapping();
    attachment.setAttributeName("attachment");
    attachment.setXPath("attachment");
    attachment.setReferenceClass(Attachment.class);
    descriptor.addMapping(attachment);
    XMLCompositeObjectMapping sxf = new XMLCompositeObjectMapping();
    sxf.setAttributeName("simpleXMLFormat");
    sxf.setXPath("simple-xml-format");
    sxf.setReferenceClass(SimpleXMLFormat.class);
    descriptor.addMapping(sxf);
    XMLDirectMapping isCollection = new XMLDirectMapping();
    isCollection.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public String getAttributeName() {
            return "isCollection";
        }

        @Override
        public Object getAttributeValueFromObject(Object object) throws DescriptorException {
            if (object instanceof CollectionResult) {
                return Boolean.TRUE;
            }
            return null;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) throws DescriptorException {
        }
    });
    isCollection.setXPath("@isCollection");
    descriptor.addMapping(isCollection);
    XMLField isColl = new XMLField("@isCollection");
    isColl.setSchemaType(BOOLEAN_QNAME);
    descriptor.getInheritancePolicy().setClassIndicatorField(isColl);
    descriptor.getInheritancePolicy().setClassExtractor(new ClassExtractor() {

        @Override
        @SuppressWarnings({ "unchecked" })
        public <T> Class<T> extractClassFromRow(DataRecord dataRecord, Session session) {
            Class<?> clz = Result.class;
            UnmarshalRecord uRecord = (UnmarshalRecord) dataRecord;
            Attributes attrs = uRecord.getAttributes();
            if (attrs != null) {
                for (int i = 0, l = attrs.getLength(); i < l; i++) {
                    String attributeName = attrs.getQName(i);
                    if (attributeName.equals("isCollection")) {
                        String value = attrs.getValue(i);
                        if (value.equalsIgnoreCase("true")) {
                            clz = CollectionResult.class;
                            break;
                        }
                    }
                }
            }
            return (Class<T>) clz;
        }
    });
    XMLDirectMapping jdbcTypeMapping = new XMLDirectMapping();
    jdbcTypeMapping.setAttributeName("jdbcType");
    jdbcTypeMapping.setXPath("@jdbcType");
    descriptor.addMapping(jdbcTypeMapping);
    return descriptor;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) Attributes(org.xml.sax.Attributes) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) ClassExtractor(org.eclipse.persistence.descriptors.ClassExtractor) CollectionResult(org.eclipse.persistence.internal.xr.CollectionResult) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) UnmarshalRecord(org.eclipse.persistence.oxm.record.UnmarshalRecord) QNameTransformer(org.eclipse.persistence.internal.xr.QNameTransformer) DataRecord(org.eclipse.persistence.sessions.DataRecord) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) Session(org.eclipse.persistence.sessions.Session)

Example 3 with UnmarshalRecord

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

the class PLSQLCallModelTestProject method buildDatabaseTypeWrapperDescriptor.

protected ClassDescriptor buildDatabaseTypeWrapperDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(DatabaseTypeWrapper.class);
    descriptor.getInheritancePolicy().setClassExtractor(new ClassExtractor() {

        @Override
        public Class<?> extractClassFromRow(DataRecord databaseRow, Session session) {
            if (databaseRow instanceof UnmarshalRecord) {
                UnmarshalRecord unmarshalRecord = (UnmarshalRecord) databaseRow;
                Attributes attributes = unmarshalRecord.getAttributes();
                for (int i = 0, l = attributes.getLength(); i < l; i++) {
                    String qName = attributes.getQName(i);
                    if (PLSQL_RECORD_INDICATOR.equals(qName)) {
                        return ComplexPLSQLTypeWrapper.class;
                    }
                    if (PLSQL_TYPE_INDICATOR.equals(qName)) {
                        return SimplePLSQLTypeWrapper.class;
                    }
                    return JDBCTypeWrapper.class;
                }
            }
            Class<?> clz = null;
            DOMRecord domRecord = (DOMRecord) databaseRow;
            Object o = domRecord.get("@" + PLSQL_RECORD_INDICATOR);
            if (o != null) {
                clz = ComplexPLSQLTypeWrapper.class;
            } else {
                o = domRecord.get("@" + PLSQL_TYPE_INDICATOR);
                if (o != null) {
                    clz = SimplePLSQLTypeWrapper.class;
                } else {
                    o = domRecord.get("@" + JDBC_TYPE_INDICATOR);
                    if (o != null) {
                        clz = JDBCTypeWrapper.class;
                    }
                }
            }
            return clz;
        }
    });
    descriptor.descriptorIsAggregate();
    return descriptor;
}
Also used : DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) Attributes(org.xml.sax.Attributes) ClassExtractor(org.eclipse.persistence.descriptors.ClassExtractor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) UnmarshalRecord(org.eclipse.persistence.oxm.record.UnmarshalRecord) DataRecord(org.eclipse.persistence.sessions.DataRecord) Session(org.eclipse.persistence.sessions.Session)

Example 4 with UnmarshalRecord

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

the class SDOUnmappedContentHandler method giveToOXToProcess.

private void giveToOXToProcess(String namespaceURI, String localName, String qName, Attributes atts, XMLDescriptor xmlDescriptor) throws SAXException {
    AbstractSession session = ((SDOXMLHelper) aHelperContext.getXMLHelper()).getXmlContext().getReadSession(xmlDescriptor);
    // taken from SAXUnmarshallerHandler start Element
    UnmarshalRecord unmarshalRecord;
    if (xmlDescriptor.hasInheritance()) {
        unmarshalRecord = new UnmarshalRecord((TreeObjectBuilder) xmlDescriptor.getObjectBuilder());
        unmarshalRecord.setUnmarshalNamespaceResolver(unmarshalNamespaceResolver);
        unmarshalRecord.setAttributes(atts);
        if (parentRecord != null) {
            unmarshalRecord.setXMLReader(parentRecord.getXMLReader());
        }
        Class<?> classValue = xmlDescriptor.getInheritancePolicy().classFromRow(unmarshalRecord, session);
        if (classValue == null) {
            // no xsi:type attribute - look for type indicator on the default root element
            QName leafElementType = xmlDescriptor.getDefaultRootElementType();
            // 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 (classValue != null) {
            xmlDescriptor = (XMLDescriptor) session.getDescriptor(classValue);
        } else {
            // sure it is non-abstract
            if (Modifier.isAbstract(xmlDescriptor.getJavaClass().getModifiers())) {
                // need to throw an exception here
                throw DescriptorException.missingClassIndicatorField((XMLRecord) unmarshalRecord, xmlDescriptor.getInheritancePolicy().getDescriptor());
            }
        }
    }
    unmarshalRecord = (UnmarshalRecord) xmlDescriptor.getObjectBuilder().createRecord(session);
    unmarshalRecord.setParentRecord(parentRecord);
    unmarshalRecord.setUnmarshaller(parentRecord.getUnmarshaller());
    unmarshalRecord.setXMLReader(parentRecord.getXMLReader());
    unmarshalRecord.startDocument();
    unmarshalRecord.setUnmarshalNamespaceResolver(unmarshalNamespaceResolver);
    unmarshalRecord.startElement(namespaceURI, localName, qName, atts);
    parentRecord.getXMLReader().setContentHandler(unmarshalRecord);
    try {
        unmarshalRecord.getXMLReader().setProperty("http://xml.org/sax/properties/lexical-handler", unmarshalRecord);
    } catch (SAXNotRecognizedException ex) {
    } catch (SAXNotSupportedException ex) {
    // if lexical handling is not supported by this parser, just ignore.
    }
    currentDataObjects.push(unmarshalRecord.getCurrentObject());
    depth++;
}
Also used : SAXNotSupportedException(org.xml.sax.SAXNotSupportedException) TreeObjectBuilder(org.eclipse.persistence.internal.oxm.TreeObjectBuilder) QName(javax.xml.namespace.QName) UnmarshalRecord(org.eclipse.persistence.oxm.record.UnmarshalRecord) SDODataObject(org.eclipse.persistence.sdo.SDODataObject) DataObject(commonj.sdo.DataObject) SAXNotRecognizedException(org.xml.sax.SAXNotRecognizedException) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Aggregations

UnmarshalRecord (org.eclipse.persistence.oxm.record.UnmarshalRecord)4 QName (javax.xml.namespace.QName)2 ClassExtractor (org.eclipse.persistence.descriptors.ClassExtractor)2 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)2 DataRecord (org.eclipse.persistence.sessions.DataRecord)2 Session (org.eclipse.persistence.sessions.Session)2 Attributes (org.xml.sax.Attributes)2 DataObject (commonj.sdo.DataObject)1 JAXBElement (jakarta.xml.bind.JAXBElement)1 JAXBException (jakarta.xml.bind.JAXBException)1 PropertyException (jakarta.xml.bind.PropertyException)1 UnmarshalException (jakarta.xml.bind.UnmarshalException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 BeanValidationException (org.eclipse.persistence.exceptions.BeanValidationException)1 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)1 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)1 WrappedValue (org.eclipse.persistence.internal.jaxb.WrappedValue)1 ManyValue (org.eclipse.persistence.internal.jaxb.many.ManyValue)1 TreeObjectBuilder (org.eclipse.persistence.internal.oxm.TreeObjectBuilder)1 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)1