Search in sources :

Example 21 with Descriptor

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

the class SAXUnmarshaller method unmarshal.

public Object unmarshal(InputSource inputSource, Class<?> clazz, XMLReader xmlReader) {
    if (inputSource != null && null == inputSource.getSystemId()) {
        inputSource.setSystemId(this.systemId);
    }
    UnmarshalRecord unmarshalRecord = null;
    Descriptor xmlDescriptor = null;
    // for XMLObjectReferenceMappings we need a non-shared cache, so
    // try and get a Unit Of Work from the XMLContext
    CoreAbstractSession session = null;
    // check for case where the reference class is a primitive wrapper - in this case, we
    // need to use the conversion manager to convert the node's value to the primitive
    // wrapper class, then create, populate and return an XMLRoot. This will be done
    // via XMLRootRecord.
    boolean isPrimitiveWrapper = false;
    if (clazz == CoreClassConstants.OBJECT) {
        try {
            SAXUnmarshallerHandler saxUnmarshallerHandler = new SAXUnmarshallerHandler(xmlUnmarshaller.getContext());
            saxUnmarshallerHandler.setXMLReader(xmlReader);
            saxUnmarshallerHandler.setUnmarshaller(xmlUnmarshaller);
            saxUnmarshallerHandler.setKeepAsElementPolicy(KEEP_UNKNOWN_AS_ELEMENT);
            setContentHandler(xmlReader, saxUnmarshallerHandler);
            xmlReader.parse(inputSource);
            // resolve any mapping references
            saxUnmarshallerHandler.resolveReferences();
            return saxUnmarshallerHandler.getObject();
        } catch (IOException e) {
            throw XMLMarshalException.unmarshalException(e);
        } catch (SAXException e) {
            throw convertSAXException(e);
        }
    } else {
        // try and get a Unit Of Work from the XMLContext
        try {
            session = xmlUnmarshaller.getContext().getSession(clazz);
            xmlDescriptor = (Descriptor) session.getDescriptor(clazz);
            unmarshalRecord = xmlUnmarshaller.createUnmarshalRecord(xmlDescriptor, session);
        } catch (XMLMarshalException xme) {
            if (xme.getErrorCode() == XMLMarshalException.DESCRIPTOR_NOT_FOUND_IN_PROJECT) {
                isPrimitiveWrapper = isPrimitiveWrapper(clazz);
                if (isPrimitiveWrapper) {
                    unmarshalRecord = xmlUnmarshaller.createRootUnmarshalRecord(clazz);
                } else {
                    throw xme;
                }
            } else {
                throw xme;
            }
        }
    }
    try {
        unmarshalRecord.setXMLReader(xmlReader);
        unmarshalRecord.setUnmarshaller(xmlUnmarshaller);
        setContentHandler(xmlReader, unmarshalRecord);
        xmlReader.setLexicalHandler(unmarshalRecord);
        xmlReader.parse(inputSource);
    } catch (IOException e) {
        throw XMLMarshalException.unmarshalException(e);
    } catch (SAXException e) {
        throw convertSAXException(e);
    } finally {
        xmlUnmarshaller.getStringBuffer().reset();
    }
    // resolve mapping references
    unmarshalRecord.resolveReferences(session, xmlUnmarshaller.getIDResolver());
    if (isPrimitiveWrapper) {
        return unmarshalRecord.getCurrentObject();
    }
    return xmlDescriptor.wrapObjectInXMLRoot(unmarshalRecord, this.isResultAlwaysXMLRoot);
}
Also used : CoreAbstractSession(org.eclipse.persistence.internal.core.sessions.CoreAbstractSession) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) IOException(java.io.IOException) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) SAXException(org.xml.sax.SAXException)

Example 22 with Descriptor

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

the class XMLObjectBuilder method buildIntoNestedRow.

public AbstractRecord buildIntoNestedRow(AbstractRecord row, Object originalObject, Object object, AbstractSession session, Descriptor refDesc, Field xmlField, boolean wasXMLRoot) {
    // PERF: Avoid synchronized enumerator as is concurrency bottleneck.
    XMLRecord record = (XMLRecord) row;
    record.setSession(session);
    XMLMarshaller marshaller = record.getMarshaller();
    if ((marshaller != null) && (marshaller.getMarshalListener() != null)) {
        marshaller.getMarshalListener().beforeMarshal(object);
    }
    List extraNamespaces = null;
    if (isXmlDescriptor()) {
        Descriptor xmlDescriptor = (Descriptor) getDescriptor();
        extraNamespaces = addExtraNamespacesToNamespaceResolver(xmlDescriptor, record, session, false, false);
        writeExtraNamespaces(extraNamespaces, record);
        record.addXsiTypeAndClassIndicatorIfRequired(xmlDescriptor, refDesc, xmlField, originalObject, object, wasXMLRoot, false);
    }
    writeOutMappings(record, object, session);
    // If this descriptor is involved in inheritance add the class type.
    if (isXmlDescriptor()) {
        record.removeExtraNamespacesFromNamespaceResolver(extraNamespaces, session);
    }
    // the non default tables.
    if (!getDescriptor().isAggregateDescriptor()) {
        addPrimaryKeyForNonDefaultTable(row);
    }
    if ((marshaller != null) && (marshaller.getMarshalListener() != null)) {
        marshaller.getMarshalListener().afterMarshal(object);
    }
    return row;
}
Also used : XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord)

Example 23 with Descriptor

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

the class XMLObjectReferenceMappingNodeValue method marshalSingleValue.

@Override
public boolean marshalSingleValue(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, Object targetObject, CoreAbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) {
    Object fieldValue = xmlObjectReferenceMapping.buildFieldValue(targetObject, xmlField, session);
    if (fieldValue == null) {
        if (null != targetObject) {
            Field fkField = (Field) xmlObjectReferenceMapping.getSourceToTargetKeyFieldAssociations().get(xmlField);
            if (null == fkField) {
                Descriptor targetDescriptor = (Descriptor) session.getDescriptor(targetObject);
                fieldValue = marshalRecord.getMarshaller().getContext().getValueByXPath(targetObject, ((CoreField) targetDescriptor.getPrimaryKeyFields().get(0)).getName(), targetDescriptor.getNamespaceResolver(), Object.class);
            } else {
                fieldValue = marshalRecord.getMarshaller().getContext().getValueByXPath(targetObject, fkField.getXPath(), fkField.getNamespaceResolver(), Object.class);
            }
        }
        if (null == fieldValue) {
            return false;
        }
    }
    QName schemaType = xmlField.getSchemaTypeForValue(fieldValue, session);
    XPathFragment groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
    if (xPathFragment != null && xPathFragment.isAttribute()) {
        marshalRecord.attribute(xPathFragment, namespaceResolver, fieldValue, schemaType);
        marshalRecord.closeStartGroupingElements(groupingFragment);
    } else {
        marshalRecord.closeStartGroupingElements(groupingFragment);
        marshalRecord.characters(schemaType, fieldValue, null, false);
    }
    return true;
}
Also used : CoreField(org.eclipse.persistence.internal.core.helper.CoreField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) QName(javax.xml.namespace.QName) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) CoreField(org.eclipse.persistence.internal.core.helper.CoreField)

Example 24 with Descriptor

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

the class XMLCompositeCollectionMappingNodeValue method marshalSingleValue.

@Override
public boolean marshalSingleValue(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, Object value, CoreAbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) {
    Marshaller marshaller = marshalRecord.getMarshaller();
    // convert the value - if necessary
    boolean isNil = false;
    if (value instanceof Root) {
        isNil = ((Root) value).nil;
        value = ((Root) value).getObject();
    }
    value = xmlCompositeCollectionMapping.convertObjectValueToDataValue(value, session, marshaller);
    if (null == value) {
        return xmlCompositeCollectionMapping.getNullPolicy().compositeObjectMarshal(xPathFragment, marshalRecord, object, session, namespaceResolver);
    }
    Descriptor descriptor = (Descriptor) xmlCompositeCollectionMapping.getReferenceDescriptor();
    if (descriptor == null) {
        descriptor = (Descriptor) session.getDescriptor(value.getClass());
    } else if (descriptor.hasInheritance()) {
        Class<?> objectValueClass = value.getClass();
        if (!(objectValueClass == descriptor.getJavaClass())) {
            descriptor = (Descriptor) session.getDescriptor(objectValueClass);
        }
    }
    UnmarshalKeepAsElementPolicy keepAsElementPolicy = xmlCompositeCollectionMapping.getKeepAsElementPolicy();
    if (null != keepAsElementPolicy && (keepAsElementPolicy.isKeepUnknownAsElement() || keepAsElementPolicy.isKeepAllAsElement()) && value instanceof org.w3c.dom.Node) {
        marshalRecord.node((org.w3c.dom.Node) value, marshalRecord.getNamespaceResolver());
        return true;
    }
    if (descriptor != null) {
        marshalRecord.beforeContainmentMarshal(value);
        ObjectBuilder objectBuilder = (ObjectBuilder) descriptor.getObjectBuilder();
        CoreAttributeGroup group = marshalRecord.getCurrentAttributeGroup();
        CoreAttributeGroup nestedGroup = XMLRecord.DEFAULT_ATTRIBUTE_GROUP;
        CoreAttributeItem item = group.getItem(getMapping().getAttributeName());
        if (item != null) {
            if (item.getGroups() != null) {
                nestedGroup = item.getGroup(descriptor.getJavaClass());
            }
            if (nestedGroup == null) {
                nestedGroup = item.getGroup() == null ? XMLRecord.DEFAULT_ATTRIBUTE_GROUP : item.getGroup();
            }
        }
        marshalRecord.pushAttributeGroup(nestedGroup);
        xPathNode.startElement(marshalRecord, xPathFragment, object, session, namespaceResolver, objectBuilder, value);
        if (isNil) {
            marshalRecord.nilSimple(namespaceResolver);
        }
        List extraNamespaces = objectBuilder.addExtraNamespacesToNamespaceResolver(descriptor, marshalRecord, session, true, false);
        writeExtraNamespaces(extraNamespaces, marshalRecord, session);
        marshalRecord.addXsiTypeAndClassIndicatorIfRequired(descriptor, (Descriptor) xmlCompositeCollectionMapping.getReferenceDescriptor(), (Field) xmlCompositeCollectionMapping.getField(), false);
        objectBuilder.buildRow(marshalRecord, value, session, marshaller, xPathFragment);
        marshalRecord.afterContainmentMarshal(object, value);
        marshalRecord.popAttributeGroup();
        marshalRecord.endElement(xPathFragment, namespaceResolver);
        marshalRecord.removeExtraNamespacesFromNamespaceResolver(extraNamespaces, session);
    } else {
        if (Constants.UNKNOWN_OR_TRANSIENT_CLASS.equals(xmlCompositeCollectionMapping.getReferenceClassName())) {
            throw XMLMarshalException.descriptorNotFoundInProject(value.getClass().getName());
        }
        xPathNode.startElement(marshalRecord, xPathFragment, object, session, namespaceResolver, null, value);
        QName schemaType = ((Field) xmlCompositeCollectionMapping.getField()).getSchemaTypeForValue(value, session);
        updateNamespaces(schemaType, marshalRecord, ((Field) xmlCompositeCollectionMapping.getField()));
        marshalRecord.characters(schemaType, value, null, false);
        marshalRecord.endElement(xPathFragment, namespaceResolver);
    }
    return true;
}
Also used : CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) QName(javax.xml.namespace.QName) Field(org.eclipse.persistence.internal.oxm.mappings.Field) CoreAttributeItem(org.eclipse.persistence.core.queries.CoreAttributeItem) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) List(java.util.List) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.internal.oxm.mappings.UnmarshalKeepAsElementPolicy)

Example 25 with Descriptor

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

the class XMLCompositeObjectMappingNodeValue method buildSelfRecord.

@Override
public UnmarshalRecord buildSelfRecord(UnmarshalRecord unmarshalRecord, Attributes atts) {
    try {
        Descriptor xmlDescriptor = (Descriptor) xmlCompositeObjectMapping.getReferenceDescriptor();
        if (null == xmlDescriptor) {
            xmlDescriptor = findReferenceDescriptor(null, unmarshalRecord, atts, xmlCompositeObjectMapping, xmlCompositeObjectMapping.getKeepAsElementPolicy());
        }
        if (xmlDescriptor != null) {
            if (xmlDescriptor.hasInheritance()) {
                unmarshalRecord.setAttributes(atts);
                Class<?> clazz = ((ObjectBuilder) xmlDescriptor.getObjectBuilder()).classFromRow(unmarshalRecord, unmarshalRecord.getSession());
                if (clazz == 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) {
                            clazz = (Class) indicator;
                        }
                    }
                }
                if (clazz != null) {
                    xmlDescriptor = (Descriptor) unmarshalRecord.getSession().getDescriptor(clazz);
                } 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());
                    }
                }
            }
            UnmarshalRecord childRecord = unmarshalRecord.getChildUnmarshalRecord((ObjectBuilder) xmlDescriptor.getObjectBuilder());
            childRecord.setSelfRecord(true);
            unmarshalRecord.setChildRecord(childRecord);
            childRecord.startDocument();
            childRecord.initializeRecord(this.xmlCompositeObjectMapping);
            return childRecord;
        } else {
            return null;
        }
    } catch (SAXException e) {
        throw XMLMarshalException.unmarshalException(e);
    }
}
Also used : UnmarshalRecord(org.eclipse.persistence.internal.oxm.record.UnmarshalRecord) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) SAXException(org.xml.sax.SAXException)

Aggregations

Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)70 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)25 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)23 QName (javax.xml.namespace.QName)19 Schema (org.eclipse.persistence.internal.oxm.schema.model.Schema)18 Field (org.eclipse.persistence.internal.oxm.mappings.Field)17 Project (org.eclipse.persistence.sessions.Project)16 CoreDescriptor (org.eclipse.persistence.core.descriptors.CoreDescriptor)14 SAXException (org.xml.sax.SAXException)14 List (java.util.List)13 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)13 SchemaModelGeneratorProperties (org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties)13 SchemaModelProject (org.eclipse.persistence.internal.oxm.schema.SchemaModelProject)11 UnmarshalKeepAsElementPolicy (org.eclipse.persistence.internal.oxm.mappings.UnmarshalKeepAsElementPolicy)10 Document (org.w3c.dom.Document)10 ArrayList (java.util.ArrayList)9 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)9 CoreAbstractSession (org.eclipse.persistence.internal.core.sessions.CoreAbstractSession)8 CompositeObjectMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping)8 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)7