Search in sources :

Example 1 with CoreInheritancePolicy

use of org.eclipse.persistence.core.descriptors.CoreInheritancePolicy in project eclipselink by eclipse-ee4j.

the class SchemaModelGenerator method buildComplexType.

/**
 * Create and return a ComplexType for a given XMLDescriptor.  Assumes that the descriptor has a schema context
 * set.
 */
protected ComplexType buildComplexType(boolean anonymous, Descriptor desc, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties, List<Descriptor> descriptors) {
    ComplexType ct = new ComplexType();
    if (!anonymous) {
        ct.setName(desc.getSchemaReference().getSchemaContextAsQName(workingSchema.getNamespaceResolver()).getLocalPart());
    }
    CoreInheritancePolicy inheritancePolicy = desc.getInheritancePolicyOrNull();
    Extension extension = null;
    if (inheritancePolicy != null && inheritancePolicy.getParentClass() != null) {
        extension = new Extension();
        extension.setBaseType(desc.getSchemaReference().getSchemaContextAsQName(workingSchema.getNamespaceResolver()).getLocalPart());
        ComplexContent complexContent = new ComplexContent();
        complexContent.setExtension(extension);
        ct.setComplexContent(complexContent);
    }
    Sequence seq = new Sequence();
    for (CoreMapping mapping : (Vector<CoreMapping>) desc.getMappings()) {
        processMapping(mapping, seq, ct, schemaForNamespace, workingSchema, properties, descriptors);
    }
    if (extension != null) {
        extension.setSequence(seq);
    } else {
        ct.setSequence(seq);
    }
    return ct;
}
Also used : Extension(org.eclipse.persistence.internal.oxm.schema.model.Extension) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) Sequence(org.eclipse.persistence.internal.oxm.schema.model.Sequence) CoreInheritancePolicy(org.eclipse.persistence.core.descriptors.CoreInheritancePolicy) ComplexContent(org.eclipse.persistence.internal.oxm.schema.model.ComplexContent) ComplexType(org.eclipse.persistence.internal.oxm.schema.model.ComplexType) Vector(java.util.Vector)

Example 2 with CoreInheritancePolicy

use of org.eclipse.persistence.core.descriptors.CoreInheritancePolicy 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 3 with CoreInheritancePolicy

use of org.eclipse.persistence.core.descriptors.CoreInheritancePolicy in project eclipselink by eclipse-ee4j.

the class UnmarshalRecordImpl method initializeRecord.

private void initializeRecord(Attributes attrs) throws SAXException {
    this.setAttributes(attrs);
    Descriptor xmlDescriptor = (Descriptor) treeObjectBuilder.getDescriptor();
    if (!xmlDescriptor.hasInheritance() || xmlDescriptor.getInheritancePolicy().getClassIndicatorField() == null) {
        initialize((ObjectBuilder) xmlDescriptor.getObjectBuilder());
        initializeRecord((Mapping) null);
        return;
    }
    CoreInheritancePolicy inheritancePolicy = xmlDescriptor.getInheritancePolicy();
    Class<?> classValue = treeObjectBuilder.classFromRow(this, 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) {
            XPathQName xpathQName = new XPathQName(leafElementType, isNamespaceAware());
            Object indicator = inheritancePolicy.getClassIndicatorMapping().get(xpathQName);
            if (indicator != null) {
                classValue = (Class) indicator;
            }
        }
    }
    if (classValue != null) {
        xmlDescriptor = (Descriptor) session.getDescriptor(classValue);
    }
    initialize((ObjectBuilder) xmlDescriptor.getObjectBuilder());
    initializeRecord((Mapping) null);
}
Also used : XPathQName(org.eclipse.persistence.internal.oxm.XPathQName) QName(javax.xml.namespace.QName) XPathQName(org.eclipse.persistence.internal.oxm.XPathQName) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) CoreInheritancePolicy(org.eclipse.persistence.core.descriptors.CoreInheritancePolicy)

Example 4 with CoreInheritancePolicy

use of org.eclipse.persistence.core.descriptors.CoreInheritancePolicy in project eclipselink by eclipse-ee4j.

the class AbstractMarshalRecordImpl method addXsiTypeAndClassIndicatorIfRequired.

@Override
public boolean addXsiTypeAndClassIndicatorIfRequired(Descriptor descriptor, Descriptor referenceDescriptor, Field xmlField, boolean isRootElement) {
    ObjectBuilder objectBuilder = (ObjectBuilder) descriptor.getObjectBuilder();
    boolean xsiTypeIndicatorField = objectBuilder.isXsiTypeIndicatorField();
    if (objectBuilder.addClassIndicatorFieldToRow(this)) {
        return true;
    }
    QName leafType = null;
    if (xmlField != null) {
        leafType = xmlField.getLeafElementType();
        XMLSchemaReference xmlRef = descriptor.getSchemaReference();
        if (xmlRef != null) {
            if (leafType == null) {
                if (xmlRef.getType() == XMLSchemaReference.ELEMENT) {
                    return false;
                }
                if (referenceDescriptor == null) {
                    writeXsiTypeAttribute(descriptor, xmlRef, isRootElement);
                    return true;
                }
            } else if (((xmlRef.getType() == XMLSchemaReference.COMPLEX_TYPE) || (xmlRef.getType() == XMLSchemaReference.SIMPLE_TYPE)) && xmlRef.getSchemaContext() != null && xmlRef.isGlobalDefinition()) {
                QName ctxQName = xmlRef.getSchemaContextAsQName(descriptor.getNamespaceResolver());
                if (!ctxQName.equals(leafType)) {
                    writeXsiTypeAttribute(descriptor, xmlRef, isRootElement);
                    return true;
                }
            }
        }
    }
    if (referenceDescriptor != null && referenceDescriptor == descriptor) {
        return false;
    }
    if (descriptor.hasInheritance() && !descriptor.getInheritancePolicy().isRootParentDescriptor()) {
        CoreInheritancePolicy inheritancePolicy = descriptor.getInheritancePolicy();
        Field indicatorField = (Field) inheritancePolicy.getClassIndicatorField();
        if (indicatorField != null && xsiTypeIndicatorField) {
            Object classIndicatorValueObject = inheritancePolicy.getClassIndicatorMapping().get(descriptor.getJavaClass());
            String classIndicatorUri = null;
            String classIndicatorLocal = null;
            String classIndicatorPrefix = null;
            if (classIndicatorValueObject instanceof QName) {
                QName classIndicatorQName = (QName) classIndicatorValueObject;
                classIndicatorUri = classIndicatorQName.getNamespaceURI();
                classIndicatorLocal = classIndicatorQName.getLocalPart();
                classIndicatorPrefix = classIndicatorQName.getPrefix();
            } else {
                String classIndicatorValue = (String) inheritancePolicy.getClassIndicatorMapping().get(descriptor.getJavaClass());
                int nsindex = classIndicatorValue.indexOf(Constants.COLON);
                String prefix = null;
                if (nsindex != -1) {
                    classIndicatorLocal = classIndicatorValue.substring(nsindex + 1);
                    prefix = classIndicatorValue.substring(0, nsindex);
                } else {
                    classIndicatorLocal = classIndicatorValue;
                }
                classIndicatorUri = descriptor.getNonNullNamespaceResolver().resolveNamespacePrefix(prefix);
            }
            if (leafType == null || isRootElement && marshaller.isApplicationJSON() && !marshaller.isIncludeRoot() || !(leafType.getLocalPart().equals(classIndicatorLocal)) || (classIndicatorUri == null && (leafType.getNamespaceURI() != null && leafType.getNamespaceURI().length() > 0)) || (classIndicatorUri != null && !classIndicatorUri.equals(leafType.getNamespaceURI()))) {
                if (inheritancePolicy.hasClassExtractor()) {
                    objectBuilder.addClassIndicatorFieldToRow(this);
                } else {
                    writeXsiTypeAttribute(descriptor, classIndicatorUri, classIndicatorLocal, classIndicatorPrefix, isRootElement);
                }
                return true;
            }
            return false;
        }
    }
    return false;
}
Also used : CoreField(org.eclipse.persistence.internal.core.helper.CoreField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) XMLField(org.eclipse.persistence.oxm.XMLField) XMLSchemaReference(org.eclipse.persistence.oxm.schema.XMLSchemaReference) XPathQName(org.eclipse.persistence.internal.oxm.XPathQName) QName(javax.xml.namespace.QName) ObjectBuilder(org.eclipse.persistence.internal.oxm.ObjectBuilder) CoreInheritancePolicy(org.eclipse.persistence.core.descriptors.CoreInheritancePolicy)

Aggregations

CoreInheritancePolicy (org.eclipse.persistence.core.descriptors.CoreInheritancePolicy)4 QName (javax.xml.namespace.QName)2 CoreDescriptor (org.eclipse.persistence.core.descriptors.CoreDescriptor)2 CoreMapping (org.eclipse.persistence.core.mappings.CoreMapping)2 XPathQName (org.eclipse.persistence.internal.oxm.XPathQName)2 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)2 Field (org.eclipse.persistence.internal.oxm.mappings.Field)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Vector (java.util.Vector)1 ConversionException (org.eclipse.persistence.exceptions.ConversionException)1 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)1 CoreField (org.eclipse.persistence.internal.core.helper.CoreField)1 CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)1 ObjectBuilder (org.eclipse.persistence.internal.oxm.ObjectBuilder)1 ComplexContent (org.eclipse.persistence.internal.oxm.schema.model.ComplexContent)1 ComplexType (org.eclipse.persistence.internal.oxm.schema.model.ComplexType)1 Extension (org.eclipse.persistence.internal.oxm.schema.model.Extension)1 Sequence (org.eclipse.persistence.internal.oxm.schema.model.Sequence)1 XMLField (org.eclipse.persistence.oxm.XMLField)1