Search in sources :

Example 1 with XMLConverter

use of org.eclipse.persistence.oxm.mappings.converters.XMLConverter in project eclipselink by eclipse-ee4j.

the class XMLCompositeObjectMapping method valueFromRow.

public Object valueFromRow(Object fieldValue, XMLRecord nestedRow, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, AbstractSession executionSession, boolean isTargetProtected) throws DatabaseException {
    // pretty sure we can ignore inheritance here:
    Object toReturn = null;
    // Use local descriptor - not the instance variable on DatabaseMapping
    ClassDescriptor aDescriptor = getReferenceDescriptor((DOMRecord) nestedRow);
    if (aDescriptor == null) {
        if ((getKeepAsElementPolicy() == UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT) || (getKeepAsElementPolicy() == UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT)) {
            XMLPlatformFactory.getInstance().getXMLPlatform().namespaceQualifyFragment((Element) nestedRow.getDOM());
            toReturn = nestedRow.getDOM();
            toReturn = convertDataValueToObjectValue(toReturn, executionSession, nestedRow.getUnmarshaller());
            // try simple case
            toReturn = convertToSimpleTypeIfPresent(toReturn, nestedRow, executionSession);
            return toReturn;
        } else {
            NodeList children = nestedRow.getDOM().getChildNodes();
            for (int i = 0, childrenLength = children.getLength(); i < childrenLength; i++) {
                Node nextNode = children.item(i);
                if (nextNode.getNodeType() == Node.ELEMENT_NODE) {
                    // complex child
                    String type = ((Element) nestedRow.getDOM()).getAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, XMLConstants.SCHEMA_TYPE_ATTRIBUTE);
                    if (type != null && type.length() > 0) {
                        throw XMLMarshalException.unknownXsiTypeValue(type, (CompositeObjectMapping) this);
                    } else {
                        throw XMLMarshalException.noDescriptorFound((CompositeObjectMapping) this);
                    }
                }
            }
            // simple case
            toReturn = convertToSimpleTypeIfPresent(toReturn, nestedRow, executionSession);
        }
    } else {
        if (aDescriptor.hasInheritance()) {
            Class<?> classValue = aDescriptor.getInheritancePolicy().classFromRow(nestedRow, executionSession);
            if (classValue == null) {
                // no xsi:type attribute - look for type indicator on the field
                QName leafElementType = ((XMLField) getField()).getLeafElementType();
                if (leafElementType != null) {
                    XPathQName leafElementXPathQName = new XPathQName(leafElementType, nestedRow.isNamespaceAware());
                    Object indicator = aDescriptor.getInheritancePolicy().getClassIndicatorMapping().get(leafElementXPathQName);
                    if (indicator != null) {
                        classValue = (Class) indicator;
                    }
                }
            }
            if (classValue != null) {
                aDescriptor = this.getReferenceDescriptor(classValue, executionSession);
            } else {
                // use the reference descriptor -  make sure it is non-abstract
                if (Modifier.isAbstract(aDescriptor.getJavaClass().getModifiers())) {
                    // throw an exception
                    throw DescriptorException.missingClassIndicatorField((org.eclipse.persistence.internal.oxm.record.XMLRecord) nestedRow, aDescriptor.getInheritancePolicy().getDescriptor());
                }
            }
        }
        ObjectBuilder objectBuilder = aDescriptor.getObjectBuilder();
        toReturn = buildCompositeObject(objectBuilder, nestedRow, sourceQuery, null, joinManager, executionSession);
    }
    if (getConverter() != null) {
        if (getConverter() instanceof XMLConverter) {
            toReturn = ((XMLConverter) getConverter()).convertDataValueToObjectValue(toReturn, executionSession, nestedRow.getUnmarshaller());
        } else {
            toReturn = getConverter().convertDataValueToObjectValue(toReturn, executionSession);
        }
    }
    return toReturn;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XPathQName(org.eclipse.persistence.internal.oxm.XPathQName) QName(javax.xml.namespace.QName) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Element(org.w3c.dom.Element) XPathQName(org.eclipse.persistence.internal.oxm.XPathQName) XMLConverter(org.eclipse.persistence.oxm.mappings.converters.XMLConverter) ObjectBuilder(org.eclipse.persistence.internal.descriptors.ObjectBuilder) XMLObjectBuilder(org.eclipse.persistence.internal.oxm.XMLObjectBuilder)

Example 2 with XMLConverter

use of org.eclipse.persistence.oxm.mappings.converters.XMLConverter in project eclipselink by eclipse-ee4j.

the class XMLChoiceCollectionMapping method initialize.

@Override
public void initialize(AbstractSession session) throws DescriptorException {
    super.initialize(session);
    if (this.converter != null) {
        this.converter.initialize(this, session);
    }
    ArrayList<XMLMapping> mappingsList = new ArrayList<>();
    mappingsList.addAll(getChoiceElementMappings().values());
    for (XMLMapping next : getChoiceElementMappingsByClass().values()) {
        if (!(mappingsList.contains(next))) {
            mappingsList.add(next);
        }
    }
    if (isAny) {
        // anyMapping = new XMLAnyCollectionMapping();
        mappingsList.add(anyMapping);
    }
    Iterator<XMLMapping> mappings = mappingsList.iterator();
    while (mappings.hasNext()) {
        DatabaseMapping nextMapping = (DatabaseMapping) mappings.next();
        Converter converter = null;
        if (fieldsToConverters != null) {
            converter = fieldsToConverters.get(nextMapping.getField());
        }
        if (nextMapping.isAbstractCompositeDirectCollectionMapping()) {
            XMLConversionManager xmlConversionManager = (XMLConversionManager) session.getDatasourcePlatform().getConversionManager();
            QName schemaType = xmlConversionManager.schemaType(((AbstractCompositeDirectCollectionMapping) nextMapping).getAttributeElementClass());
            if (schemaType != null) {
                ((XMLField) nextMapping.getField()).setSchemaType(schemaType);
            }
            if (converter != null) {
                ((AbstractCompositeDirectCollectionMapping) nextMapping).setValueConverter(converter);
            }
            ((AbstractCompositeDirectCollectionMapping) nextMapping).setContainerPolicy(getContainerPolicy());
        } else if (nextMapping.isAbstractCompositeCollectionMapping()) {
            if (converter != null) {
                ((AbstractCompositeCollectionMapping) nextMapping).setConverter(converter);
            }
            ((AbstractCompositeCollectionMapping) nextMapping).setContainerPolicy(getContainerPolicy());
        } else if (nextMapping instanceof XMLBinaryDataCollectionMapping) {
            ((XMLBinaryDataCollectionMapping) nextMapping).setContainerPolicy(getContainerPolicy());
            if (converter != null) {
                ((XMLBinaryDataCollectionMapping) nextMapping).setValueConverter(converter);
            }
        } else if (nextMapping instanceof XMLAnyCollectionMapping) {
            ((XMLAnyCollectionMapping) nextMapping).setContainerPolicy(getContainerPolicy());
            if (converter != null && converter instanceof XMLConverter) {
                ((XMLAnyCollectionMapping) nextMapping).setConverter((XMLConverter) converter);
            }
        } else {
            ((XMLCollectionReferenceMapping) nextMapping).setContainerPolicy(getContainerPolicy());
            ((XMLCollectionReferenceMapping) nextMapping).setReuseContainer(true);
        }
        nextMapping.initialize(session);
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) XMLConverter(org.eclipse.persistence.oxm.mappings.converters.XMLConverter) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) AbstractCompositeDirectCollectionMapping(org.eclipse.persistence.mappings.foundation.AbstractCompositeDirectCollectionMapping) Converter(org.eclipse.persistence.mappings.converters.Converter) XMLConverter(org.eclipse.persistence.oxm.mappings.converters.XMLConverter) XMLConversionManager(org.eclipse.persistence.internal.oxm.XMLConversionManager)

Example 3 with XMLConverter

use of org.eclipse.persistence.oxm.mappings.converters.XMLConverter in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method generateAnyObjectMapping.

public AnyObjectMapping generateAnyObjectMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo) {
    AnyObjectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, XMLConverter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, XMLRecord> mapping = new XMLAnyObjectMapping();
    initializeXMLMapping((XMLMapping) mapping, property);
    // if the XPath is set (via xml-path) use it
    if (property.getXmlPath() != null) {
        mapping.setField(new XMLField(property.getXmlPath()));
    }
    Class<?> declaredType = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(property.getActualType().getQualifiedName(), helper.getClassLoader());
    JAXBElementRootConverter jaxbElementRootConverter = new JAXBElementRootConverter(declaredType);
    mapping.setConverter(jaxbElementRootConverter);
    if (property.getDomHandlerClassName() != null) {
        jaxbElementRootConverter.setNestedConverter(new DomHandlerConverter(property.getDomHandlerClassName()));
    }
    if (property.isLax()) {
        mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
    } else {
        mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_ALL_AS_ELEMENT);
    }
    if (property.isMixedContent()) {
        mapping.setMixedContent(true);
    } else {
        mapping.setUseXMLRoot(true);
    }
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) XMLConverter(org.eclipse.persistence.oxm.mappings.converters.XMLConverter) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) DomHandlerConverter(org.eclipse.persistence.internal.jaxb.DomHandlerConverter) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) JAXBElementRootConverter(org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) VirtualAttributeAccessor(org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor) CustomAccessorAttributeAccessor(org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) MapValueAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) Session(org.eclipse.persistence.sessions.Session) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) XMLAnyObjectMapping(org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping)

Aggregations

XMLField (org.eclipse.persistence.oxm.XMLField)3 XMLConverter (org.eclipse.persistence.oxm.mappings.converters.XMLConverter)3 QName (javax.xml.namespace.QName)2 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)2 ArrayList (java.util.ArrayList)1 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)1 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)1 ObjectBuilder (org.eclipse.persistence.internal.descriptors.ObjectBuilder)1 VirtualAttributeAccessor (org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor)1 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)1 CustomAccessorAttributeAccessor (org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor)1 DomHandlerConverter (org.eclipse.persistence.internal.jaxb.DomHandlerConverter)1 JAXBElementRootConverter (org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter)1 JAXBSetMethodAttributeAccessor (org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor)1 JAXBArrayAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor)1 MapValueAttributeAccessor (org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor)1 XMLConversionManager (org.eclipse.persistence.internal.oxm.XMLConversionManager)1 XMLObjectBuilder (org.eclipse.persistence.internal.oxm.XMLObjectBuilder)1 XPathQName (org.eclipse.persistence.internal.oxm.XPathQName)1 ContainerPolicy (org.eclipse.persistence.internal.queries.ContainerPolicy)1