Search in sources :

Example 26 with Field

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

the class XMLBinaryDataCollectionMappingNodeValue method startElement.

@Override
public boolean startElement(XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord, Attributes atts) {
    try {
        Field xmlField = (Field) xmlBinaryDataCollectionMapping.getField();
        XPathFragment lastFragment = xmlField.getLastXPathFragment();
        if (!lastFragment.isAttribute()) {
            // set a new content handler to deal with the Include element's event.
            BinaryMappingContentHandler handler = new BinaryMappingContentHandler(unmarshalRecord, this, this.xmlBinaryDataCollectionMapping);
            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);
        } else if (lastFragment.isAttribute()) {
            // handle swaRef and inline attribute cases here:
            String value = atts.getValue(lastFragment.getNamespaceURI(), lastFragment.getLocalName());
            Object fieldValue = null;
            if (xmlBinaryDataCollectionMapping.isSwaRef()) {
                if (unmarshalRecord.getUnmarshaller().getAttachmentUnmarshaller() != null) {
                    if (xmlBinaryDataCollectionMapping.getAttributeClassification() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
                        fieldValue = unmarshalRecord.getUnmarshaller().getAttachmentUnmarshaller().getAttachmentAsDataHandler(value);
                    } else {
                        fieldValue = unmarshalRecord.getUnmarshaller().getAttachmentUnmarshaller().getAttachmentAsByteArray(value);
                    }
                    xmlBinaryDataCollectionMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), XMLBinaryDataHelper.getXMLBinaryDataHelper().convertObject(fieldValue, xmlBinaryDataCollectionMapping.getAttributeClassification(), unmarshalRecord.getSession(), xmlBinaryDataCollectionMapping.getContainerPolicy()));
                }
            } else {
                // value should be base64 binary string
                fieldValue = ((ConversionManager) unmarshalRecord.getSession().getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(value);
                xmlBinaryDataCollectionMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), XMLBinaryDataHelper.getXMLBinaryDataHelper().convertObject(fieldValue, xmlBinaryDataCollectionMapping.getAttributeClassification(), unmarshalRecord.getSession(), xmlBinaryDataCollectionMapping.getContainerPolicy()));
            }
        }
        return true;
    } catch (SAXException ex) {
        throw XMLMarshalException.unmarshalException(ex);
    }
}
Also used : Field(org.eclipse.persistence.internal.oxm.mappings.Field) BinaryMappingContentHandler(org.eclipse.persistence.internal.oxm.record.deferred.BinaryMappingContentHandler) XMLReader(org.eclipse.persistence.internal.oxm.record.XMLReader) SAXException(org.xml.sax.SAXException)

Example 27 with Field

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

the class XMLChoiceCollectionMappingMarshalNodeValue method getNodeValueForValue.

private NodeValue getNodeValueForValue(Object value) {
    if (value == null) {
        Iterator<NodeValue> nodeValues = fieldToNodeValues.values().iterator();
        while (nodeValues.hasNext()) {
            XMLChoiceCollectionMappingUnmarshalNodeValue unmarshalNodeValue = (XMLChoiceCollectionMappingUnmarshalNodeValue) nodeValues.next();
            NodeValue nextNodeValue = unmarshalNodeValue.getChoiceElementMarshalNodeValue();
            if (nextNodeValue instanceof MappingNodeValue) {
                Mapping nextMapping = ((MappingNodeValue) nextNodeValue).getMapping();
                if (nextMapping.isAbstractCompositeCollectionMapping()) {
                    if (((CompositeCollectionMapping) nextMapping).getNullPolicy().isNullRepresentedByXsiNil()) {
                        return unmarshalNodeValue;
                    }
                } else if (nextMapping.isAbstractCompositeDirectCollectionMapping()) {
                    if (((DirectCollectionMapping) nextMapping).getNullPolicy().isNullRepresentedByXsiNil()) {
                        return unmarshalNodeValue;
                    }
                } else if (nextMapping instanceof BinaryDataCollectionMapping) {
                    if (((BinaryDataCollectionMapping) nextMapping).getNullPolicy().isNullRepresentedByXsiNil()) {
                        return unmarshalNodeValue;
                    }
                }
            }
        }
        return null;
    }
    Field associatedField = null;
    NodeValue nodeValue = null;
    if (value instanceof Root) {
        Root rootValue = (Root) value;
        String localName = rootValue.getLocalName();
        String namespaceUri = rootValue.getNamespaceURI();
        Object fieldValue = rootValue.getObject();
        associatedField = getFieldForName(localName, namespaceUri);
        if (associatedField == null) {
            if (xmlChoiceCollectionMapping.isAny()) {
                return this.anyNodeValue;
            }
            Class<?> theClass = fieldValue.getClass();
            while (associatedField == null) {
                associatedField = (Field) xmlChoiceCollectionMapping.getClassToFieldMappings().get(theClass);
                if (theClass.getSuperclass() != null) {
                    theClass = theClass.getSuperclass();
                } else {
                    break;
                }
            }
        }
        if (associatedField != null) {
            nodeValue = this.fieldToNodeValues.get(associatedField);
        }
    } else {
        Class<?> theClass = value.getClass();
        while (associatedField == null) {
            associatedField = (Field) xmlChoiceCollectionMapping.getClassToFieldMappings().get(theClass);
            List<FieldNodeValue> fieldNodes = classToNodeValues.get(theClass);
            nodeValue = null;
            if (null != fieldNodes) {
                // match also field
                if (null != associatedField && fieldNodes.size() > 1) {
                    for (FieldNodeValue fieldNode : fieldNodes) {
                        if (fieldNode.getField().equals(associatedField)) {
                            nodeValue = fieldNode.getNodeValue();
                        }
                    }
                }
                if (null == nodeValue && fieldNodes.size() > 0) {
                    nodeValue = fieldNodes.get(0).getNodeValue();
                }
            }
            if (theClass.getSuperclass() != null) {
                theClass = theClass.getSuperclass();
            } else {
                break;
            }
        }
    }
    if (associatedField == null) {
        // check the field associations
        List<Field> sourceFields = null;
        Class<?> theClass = value.getClass();
        while (theClass != null) {
            sourceFields = (List<Field>) xmlChoiceCollectionMapping.getClassToSourceFieldsMappings().get(theClass);
            if (sourceFields != null) {
                break;
            }
            theClass = theClass.getSuperclass();
        }
        if (sourceFields != null) {
            associatedField = sourceFields.get(0);
            nodeValue = fieldToNodeValues.get(associatedField);
        }
    }
    if (nodeValue != null) {
        return nodeValue;
    }
    if (associatedField != null) {
        return fieldToNodeValues.get(associatedField);
    }
    if (xmlChoiceCollectionMapping.isMixedContent() && value instanceof String) {
        // use this as a placeholder for the nodevalue for mixedcontent
        return this;
    }
    if (xmlChoiceCollectionMapping.isAny()) {
        return anyNodeValue;
    }
    return null;
}
Also used : BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) Field(org.eclipse.persistence.internal.oxm.mappings.Field)

Example 28 with Field

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

the class ReferenceResolver method getValue.

/**
 * Add java doc if you understand this code.
 */
private Object getValue(final CoreAbstractSession session, final Reference reference, final CacheId primaryKey, final ErrorHandler handler) {
    final Class<?> referenceTargetClass = reference.getTargetClass();
    if (null == referenceTargetClass || referenceTargetClass == CoreClassConstants.OBJECT) {
        for (Object entry : session.getDescriptors().values()) {
            Object value = null;
            final Descriptor targetDescriptor = (Descriptor) entry;
            final List pkFields = targetDescriptor.getPrimaryKeyFields();
            if (null != pkFields && 1 == pkFields.size()) {
                Field pkField = (Field) pkFields.get(0);
                pkField = (Field) targetDescriptor.getTypedField(pkField);
                final Class<?> targetType = pkField.getType();
                if (targetType == CoreClassConstants.STRING || targetType == CoreClassConstants.OBJECT) {
                    value = getValue(targetDescriptor.getJavaClass(), primaryKey);
                } else {
                    try {
                        final Object[] pkValues = primaryKey.getPrimaryKey();
                        final Object[] convertedPkValues = new Object[pkValues.length];
                        for (int x = 0; x < pkValues.length; x++) {
                            convertedPkValues[x] = session.getDatasourcePlatform().getConversionManager().convertObject(pkValues[x], targetType);
                        }
                        value = getValue(targetDescriptor.getJavaClass(), new CacheId(convertedPkValues));
                    } catch (ConversionException ignored) {
                    }
                }
                if (null != value) {
                    return value;
                }
            }
        }
        if (primaryKey.getPrimaryKey()[0] != null) {
            final XMLMarshalException e = XMLMarshalException.missingIDForIDRef(Object.class.getName(), primaryKey.getPrimaryKey());
            if (handler != null) {
                final SAXParseException saxParseException = new SAXParseException(e.getLocalizedMessage(), null, e);
                try {
                    handler.warning(saxParseException);
                } catch (SAXException saxException) {
                    throw e;
                }
            }
        }
        return null;
    } else {
        Object value = getValue(referenceTargetClass, primaryKey);
        if (null == value) {
            final CoreMapping mapping = (CoreMapping) reference.getMapping();
            final CoreDescriptor targetDescriptor = mapping.getReferenceDescriptor();
            if (targetDescriptor.hasInheritance()) {
                final CoreInheritancePolicy inheritancePolicy = targetDescriptor.getInheritancePolicy();
                final List<CoreDescriptor> childDescriptors = inheritancePolicy.getAllChildDescriptors();
                for (CoreDescriptor childDescriptor : childDescriptors) {
                    value = getValue(childDescriptor.getJavaClass(), primaryKey);
                    if (null != value) {
                        return value;
                    }
                }
            }
        }
        if (value == null && (primaryKey.getPrimaryKey()[0] != null)) {
            final XMLMarshalException e = XMLMarshalException.missingIDForIDRef(referenceTargetClass.getName(), primaryKey.getPrimaryKey());
            if (handler != null) {
                SAXParseException saxParseException = new SAXParseException(e.getLocalizedMessage(), null, e);
                try {
                    handler.warning(saxParseException);
                } catch (SAXException saxException) {
                    throw e;
                }
            }
        }
        return value;
    }
}
Also used : ConversionException(org.eclipse.persistence.exceptions.ConversionException) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) CoreInheritancePolicy(org.eclipse.persistence.core.descriptors.CoreInheritancePolicy) SAXException(org.xml.sax.SAXException) Field(org.eclipse.persistence.internal.oxm.mappings.Field) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) SAXParseException(org.xml.sax.SAXParseException) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) ArrayList(java.util.ArrayList) List(java.util.List) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Example 29 with Field

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

the class XMLDirectMappingNodeValue method marshalSingleValue.

@Override
public boolean marshalSingleValue(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, Object objectValue, CoreAbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) {
    Object fieldValue = xmlDirectMapping.getFieldValue(objectValue, session, marshalRecord);
    // Check for a null value
    if (null == fieldValue) {
        // Perform marshal operations based on the null policy
        return xmlDirectMapping.getNullPolicy().directMarshal(xPathFragment, marshalRecord, object, session, namespaceResolver);
    } else {
        QName schemaType = ((Field) xmlDirectMapping.getField()).getSchemaTypeForValue(fieldValue, session);
        XPathFragment groupingFragment = null;
        boolean isQName = false;
        if (Constants.QNAME_QNAME.equals(schemaType)) {
            // if marshalling a QName, handle grouping elements here in case namespace adjustments need
            // to happen
            groupingFragment = openGroupingElementsForQName((QName) fieldValue, marshalRecord);
            isQName = true;
        }
        if (groupingFragment == null) {
            groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
        }
        if (xPathFragment.hasAttribute) {
            marshalRecord.attribute(xPathFragment, namespaceResolver, fieldValue, schemaType);
            marshalRecord.closeStartGroupingElements(groupingFragment);
        } else {
            if (((Field) xmlDirectMapping.getField()).getXPathFragment().nameIsText) {
                XPathNode parentNode = xPathNode.getParent();
                if (parentNode.getAttributeChildren() != null) {
                    marshalRecord.forceValueWrapper();
                }
            }
            Field xmlField = (Field) xmlDirectMapping.getField();
            if (xmlField.isTypedTextField()) {
                updateNamespaces(schemaType, marshalRecord, xmlField);
            }
            marshalRecord.closeStartGroupingElements(groupingFragment);
            marshalRecord.characters(schemaType, fieldValue, null, xmlDirectMapping.isCDATA());
        }
        if (isQName) {
            // check to see if the last grouping fragment was swapped
            XPathFragment fragment = getLastGroupingFragment();
            if (fragment != groupingFragment) {
                marshalRecord.endElement(groupingFragment, namespaceResolver);
                return false;
            }
        }
        return true;
    }
}
Also used : Field(org.eclipse.persistence.internal.oxm.mappings.Field) QName(javax.xml.namespace.QName)

Example 30 with Field

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

the class XMLChoiceCollectionMapping method valueFromRow.

@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) throws DatabaseException {
    List<XMLEntry> values = ((DOMRecord) row).getValuesIndicatingNoEntry(this.getFields());
    Object container = getContainerPolicy().containerInstance(values.size());
    for (XMLEntry next : values) {
        Field valueField = next.getXMLField();
        DatabaseMapping nextMapping = (DatabaseMapping) this.choiceElementMappings.get(valueField);
        if (nextMapping.isAbstractCompositeCollectionMapping()) {
            XMLCompositeCollectionMapping xmlMapping = (XMLCompositeCollectionMapping) nextMapping;
            Object value = xmlMapping.buildObjectFromNestedRow((AbstractRecord) next.getValue(), joinManager, sourceQuery, executionSession, isTargetProtected);
            value = convertDataValueToObjectValue(value, executionSession, ((XMLRecord) row).getUnmarshaller());
            getContainerPolicy().addInto(value, container, executionSession);
        } else if (nextMapping instanceof XMLCompositeDirectCollectionMapping) {
            XMLCompositeDirectCollectionMapping xmlMapping = (XMLCompositeDirectCollectionMapping) nextMapping;
            Object value = next.getValue();
            value = convertDataValueToObjectValue(value, executionSession, ((XMLRecord) row).getUnmarshaller());
            getContainerPolicy().addInto(value, container, executionSession);
        }
    }
    ArrayList<XMLMapping> processedMappings = new ArrayList<>();
    for (XMLMapping mapping : choiceElementMappings.values()) {
        if (((DatabaseMapping) mapping).isObjectReferenceMapping() && ((DatabaseMapping) mapping).isCollectionMapping() && !(processedMappings.contains(mapping))) {
            ((XMLCollectionReferenceMapping) mapping).readFromRowIntoObject(row, joinManager, ((XMLRecord) row).getCurrentObject(), cacheKey, sourceQuery, executionSession, isTargetProtected, container);
            processedMappings.add(mapping);
        }
    }
    return container;
}
Also used : DOMRecord(org.eclipse.persistence.oxm.record.DOMRecord) ArrayList(java.util.ArrayList) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLEntry(org.eclipse.persistence.oxm.record.XMLEntry) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) Field(org.eclipse.persistence.internal.oxm.mappings.Field) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField)

Aggregations

Field (org.eclipse.persistence.internal.oxm.mappings.Field)65 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)20 XMLField (org.eclipse.persistence.oxm.XMLField)20 QName (javax.xml.namespace.QName)18 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)18 JavaField (org.eclipse.persistence.jaxb.javamodel.JavaField)16 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)14 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)13 CompositeObjectMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping)12 ArrayList (java.util.ArrayList)11 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)11 AbstractNullPolicy (org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy)11 List (java.util.List)10 BinaryDataMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping)10 CoreMapping (org.eclipse.persistence.core.mappings.CoreMapping)9 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)8 SAXException (org.xml.sax.SAXException)8 CoreConverter (org.eclipse.persistence.core.mappings.converters.CoreConverter)7 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)7 BinaryDataCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping)7