Search in sources :

Example 6 with Descriptor

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

the class MappingsGenerator method setupInheritance.

/**
 * Setup inheritance for abstract superclass.
 *
 * NOTE: We currently only handle one level of inheritance in this case.
 * For multiple levels the code will need to be modified. The logic in
 * generateMappings() that determines when to copy down inherited
 * methods from the parent class will need to be changed as well.
 */
private void setupInheritance(JavaClass jClass) {
    TypeInfo tInfo = typeInfo.get(jClass.getName());
    Descriptor descriptor = tInfo.getDescriptor();
    if (descriptor == null) {
        return;
    }
    JavaClass superClass = CompilerHelper.getNextMappedSuperClass(jClass, typeInfo, helper);
    if (superClass == null) {
        return;
    }
    TypeInfo superTypeInfo = typeInfo.get(superClass.getName());
    if (superTypeInfo == null) {
        return;
    }
    Descriptor superDescriptor = superTypeInfo.getDescriptor();
    if (superDescriptor != null) {
        XMLSchemaReference sRef = descriptor.getSchemaReference();
        if (sRef == null || sRef.getSchemaContext() == null) {
            return;
        }
        JavaClass rootMappedSuperClass = getRootMappedSuperClass(superClass);
        TypeInfo rootTypeInfo = typeInfo.get(rootMappedSuperClass.getName());
        Descriptor rootDescriptor = rootTypeInfo.getDescriptor();
        if (rootDescriptor.getNamespaceResolver() == null) {
            rootDescriptor.setNamespaceResolver(new org.eclipse.persistence.oxm.NamespaceResolver());
        }
        if (rootDescriptor.getInheritancePolicy().getClassIndicatorField() == null) {
            Field classIndicatorField;
            if (rootTypeInfo.isSetXmlDiscriminatorNode()) {
                classIndicatorField = new XMLField(rootTypeInfo.getXmlDiscriminatorNode());
            } else {
                classIndicatorField = XMLConstants.DEFAULT_XML_TYPE_ATTRIBUTE;
                classIndicatorField.getXPathFragment().setNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
            }
            rootDescriptor.getInheritancePolicy().setClassIndicatorField(classIndicatorField);
        }
        Object sCtx = null;
        // TypeInfo tInfo = typeInfo.get(jClass.getName());
        if (tInfo.isSetXmlDiscriminatorValue()) {
            sCtx = tInfo.getXmlDiscriminatorValue();
        } else if (!tInfo.isAnonymousComplexType()) {
            sCtx = sRef.getSchemaContextAsQName();
        }
        if (sCtx != null) {
            descriptor.getInheritancePolicy().setParentClassName(superClass.getName());
            rootDescriptor.getInheritancePolicy().addClassNameIndicator(jClass.getName(), sCtx);
        }
        Object value = rootDescriptor.getInheritancePolicy().getClassNameIndicatorMapping().get(rootDescriptor.getJavaClassName());
        if (value == null) {
            if (rootTypeInfo.isSetXmlDiscriminatorValue()) {
                rootDescriptor.getInheritancePolicy().addClassNameIndicator(rootDescriptor.getJavaClassName(), rootTypeInfo.getXmlDiscriminatorValue());
            } else {
                XMLSchemaReference rootSRef = rootDescriptor.getSchemaReference();
                if (rootSRef != null && rootSRef.getSchemaContext() != null) {
                    QName rootSCtx = rootSRef.getSchemaContextAsQName();
                    rootDescriptor.getInheritancePolicy().addClassNameIndicator(rootDescriptor.getJavaClassName(), rootSCtx);
                }
            }
        }
        rootDescriptor.getInheritancePolicy().setShouldReadSubclasses(true);
        // Check for attributeGroups
        Map<String, AttributeGroup> childGroups = ((XMLDescriptor) descriptor).getAttributeGroups();
        Map<String, AttributeGroup> parentGroups = ((XMLDescriptor) rootDescriptor).getAttributeGroups();
        if (childGroups != null && !(childGroups.isEmpty()) && parentGroups != null && !(parentGroups.isEmpty())) {
            for (String nextKey : childGroups.keySet()) {
                AttributeGroup parentGroup = parentGroups.get(nextKey);
                if (parentGroup != null) {
                    AttributeGroup childGroup = childGroups.get(nextKey);
                    parentGroup.getSubClassGroups().put(descriptor.getJavaClassName(), childGroup);
                }
            }
        }
    }
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) QName(javax.xml.namespace.QName) AttributeGroup(org.eclipse.persistence.queries.AttributeGroup) CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) Field(org.eclipse.persistence.internal.oxm.mappings.Field) JavaField(org.eclipse.persistence.jaxb.javamodel.JavaField) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XMLSchemaReference(org.eclipse.persistence.oxm.schema.XMLSchemaReference) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor)

Example 7 with Descriptor

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

the class MappingsGenerator method generateDescriptor.

public void generateDescriptor(JavaClass javaClass, CoreProject project) {
    String jClassName = javaClass.getQualifiedName();
    TypeInfo info = typeInfo.get(jClassName);
    if (info.isTransient()) {
        return;
    }
    NamespaceInfo namespaceInfo = this.packageToPackageInfoMappings.get(javaClass.getPackageName()).getNamespaceInfo();
    String packageNamespace = namespaceInfo.getNamespace();
    String elementName;
    String namespace;
    if (javaClass.getSuperclass() != null && javaClass.getSuperclass().getName().equals("jakarta.xml.bind.JAXBElement")) {
        generateDescriptorForJAXBElementSubclass(javaClass, project, getNamespaceResolverForDescriptor(namespaceInfo));
        return;
    }
    Descriptor descriptor = new XMLDescriptor();
    org.eclipse.persistence.jaxb.xmlmodel.XmlRootElement rootElem = info.getXmlRootElement();
    if (rootElem == null) {
        try {
            elementName = info.getXmlNameTransformer().transformRootElementName(javaClass.getName());
        } catch (Exception ex) {
            throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(javaClass.getName(), info.getXmlNameTransformer().getClass().getName(), ex);
        }
        namespace = packageNamespace;
    } else {
        elementName = rootElem.getName();
        if (elementName.equals(XMLProcessor.DEFAULT)) {
            try {
                elementName = info.getXmlNameTransformer().transformRootElementName(javaClass.getName());
            } catch (Exception ex) {
                throw org.eclipse.persistence.exceptions.JAXBException.exceptionDuringNameTransformation(javaClass.getName(), info.getXmlNameTransformer().getClass().getName(), ex);
            }
        }
        namespace = rootElem.getNamespace();
    }
    descriptor.setJavaClassName(jClassName);
    if (info.getFactoryMethodName() != null) {
        descriptor.getInstantiationPolicy().useFactoryInstantiationPolicy(info.getObjectFactoryClassName(), info.getFactoryMethodName());
    }
    if (namespace.equals(XMLProcessor.DEFAULT)) {
        namespace = namespaceInfo.getNamespace();
    }
    NamespaceResolver resolverForDescriptor = getNamespaceResolverForDescriptor(namespaceInfo);
    JavaClass manyValueJavaClass = helper.getJavaClass(ManyValue.class);
    if (!manyValueJavaClass.isAssignableFrom(javaClass)) {
        if (isDefaultNamespaceAllowed && namespace.length() != 0 && globalNamespaceResolver.getDefaultNamespaceURI() == null && !resolverForDescriptor.getPrefixesToNamespaces().containsValue(namespace)) {
            globalNamespaceResolver.setDefaultNamespaceURI(namespace);
            resolverForDescriptor.setDefaultNamespaceURI(namespace);
        }
        if (rootElem == null) {
            descriptor.setDefaultRootElement("");
        } else {
            if (namespace.length() == 0) {
                descriptor.setDefaultRootElement(elementName);
            } else {
                descriptor.setDefaultRootElement(getQualifiedString(getPrefixForNamespace(namespace, resolverForDescriptor), elementName));
            }
        }
    }
    descriptor.setNamespaceResolver(resolverForDescriptor);
    setSchemaContext(descriptor, info);
    // set the ClassExtractor class name if necessary
    if (info.isSetClassExtractorName()) {
        descriptor.getInheritancePolicy().setClassExtractorName(info.getClassExtractorName());
    }
    // set any user-defined properties
    if (info.getUserProperties() != null) {
        descriptor.setProperties(info.getUserProperties());
    }
    if (info.isLocationAware()) {
        Property locProp = null;
        Iterator<Property> i = info.getPropertyList().iterator();
        while (i.hasNext()) {
            Property p = i.next();
            if (p.getType().getName().equals(Constants.LOCATOR_CLASS_NAME)) {
                locProp = p;
            }
        }
        if (locProp != null && locProp.isTransient()) {
            // don't make a mapping
            if (locProp.isMethodProperty()) {
                MethodAttributeAccessor aa = new MethodAttributeAccessor();
                aa.setAttributeName(locProp.getPropertyName());
                aa.setSetMethodName(locProp.getSetMethodName());
                aa.setGetMethodName(locProp.getGetMethodName());
                descriptor.setLocationAccessor(aa);
            } else {
                // instance variable property
                InstanceVariableAttributeAccessor aa = new InstanceVariableAttributeAccessor();
                aa.setAttributeName(locProp.getPropertyName());
                descriptor.setLocationAccessor(aa);
            }
        }
    }
    if (!info.getObjectGraphs().isEmpty()) {
        // these will be populated later to allow for linking
        for (XmlNamedObjectGraph next : info.getObjectGraphs()) {
            AttributeGroup attributeGroup = new AttributeGroup(next.getName(), info.getJavaClassName(), false);
            ((XMLDescriptor) descriptor).addAttributeGroup(attributeGroup);
        // process subclass graphs for inheritance
        // for(NamedSubgraph nextSubclass:next.getNamedSubclassGraph()) {
        // attributeGroup.insertSubClass(new AttributeGroup(next.getName(), nextSubclass.getType()));
        // }
        }
    }
    project.addDescriptor((CoreDescriptor) descriptor);
    info.setDescriptor(descriptor);
}
Also used : InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) XmlNamedObjectGraph(org.eclipse.persistence.jaxb.xmlmodel.XmlNamedObjectGraph) AttributeGroup(org.eclipse.persistence.queries.AttributeGroup) CoreAttributeGroup(org.eclipse.persistence.core.queries.CoreAttributeGroup) JAXBException(org.eclipse.persistence.exceptions.JAXBException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) NamespaceResolver(org.eclipse.persistence.internal.oxm.NamespaceResolver) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)

Example 8 with Descriptor

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

the class MappingsGenerator method generateMappings.

public void generateMappings() {
    Iterator<String> javaClasses = this.typeInfo.keySet().iterator();
    while (javaClasses.hasNext()) {
        String next = javaClasses.next();
        JavaClass javaClass = helper.getJavaClass(next);
        TypeInfo info = this.typeInfo.get(next);
        if (info.isEnumerationType()) {
            continue;
        }
        NamespaceInfo namespaceInfo = this.packageToPackageInfoMappings.get(javaClass.getPackageName()).getNamespaceInfo();
        Descriptor descriptor = info.getDescriptor();
        if (descriptor != null) {
            generateMappings(info, descriptor, javaClass, namespaceInfo);
            // set primary key fields (if necessary)
            CoreMapping mapping;
            // handle XmlID
            if (info.isIDSet()) {
                mapping = descriptor.getMappingForAttributeName(info.getIDProperty().getPropertyName());
                if (mapping != null) {
                    descriptor.addPrimaryKeyField(mapping.getField());
                }
            }
            // handle XmlKey
            if (info.hasXmlKeyProperties()) {
                for (Property keyProp : info.getXmlKeyProperties()) {
                    mapping = descriptor.getMappingForAttributeName(keyProp.getPropertyName());
                    if (mapping != null) {
                        descriptor.addPrimaryKeyField(mapping.getField());
                    }
                }
            }
        }
        info.postInitialize();
        if (descriptor != null) {
            logMappingGeneration(descriptor);
        }
    }
}
Also used : JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor)

Example 9 with Descriptor

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

the class SchemaModelGenerator method generateSchemas.

/**
 * Generates a Map of EclipseLink schema model Schema objects for a given list of XMLDescriptors.
 * The descriptors are assumed to have been initialized.  One Schema  object will be generated
 * per namespace.
 *
 * @param descriptorsToProcess list of XMLDescriptors which will be used to generate Schema objects
 * @param properties holds a namespace to Properties map containing schema settings, such as elementFormDefault
 * @return a map of namespaces to EclipseLink schema model Schema objects
 * @throws DescriptorException if the reference descriptor for a composite mapping is not in the list of descriptors
 * @see Schema
 */
public Map<String, Schema> generateSchemas(List<Descriptor> descriptorsToProcess, SchemaModelGeneratorProperties properties) throws DescriptorException {
    HashMap<String, Schema> schemaForNamespace = new HashMap<>();
    Schema workingSchema = null;
    if (properties == null) {
        properties = new SchemaModelGeneratorProperties();
    }
    // set up schemas for the descriptors
    for (Descriptor desc : descriptorsToProcess) {
        String namespace;
        XMLSchemaReference schemaRef = desc.getSchemaReference();
        if (schemaRef != null) {
            namespace = schemaRef.getSchemaContextAsQName(desc.getNamespaceResolver()).getNamespaceURI();
            workingSchema = getSchema(namespace, desc.getNamespaceResolver(), schemaForNamespace, properties);
            addNamespacesToWorkingSchema(desc.getNamespaceResolver(), workingSchema);
        } else {
            // default root element set we will need to generate a global element for it
            for (DatabaseTable table : (Vector<DatabaseTable>) desc.getTables()) {
                namespace = getDefaultRootElementAsQName(desc, table.getName()).getNamespaceURI();
                workingSchema = getSchema(namespace, desc.getNamespaceResolver(), schemaForNamespace, properties);
                addNamespacesToWorkingSchema(desc.getNamespaceResolver(), workingSchema);
            }
        }
    }
    // process the descriptors
    for (Descriptor xdesc : descriptorsToProcess) {
        processDescriptor(xdesc, schemaForNamespace, workingSchema, properties, descriptorsToProcess);
    }
    // return the generated schema(s)
    return schemaForNamespace;
}
Also used : HashMap(java.util.HashMap) XMLSchemaReference(org.eclipse.persistence.oxm.schema.XMLSchemaReference) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) Vector(java.util.Vector)

Example 10 with Descriptor

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

the class SchemaModelGenerator method processXMLObjectReferenceMapping.

/**
 * Process a given XMLObjectReferenceMapping.  In the case of an XMLCollectionReferenceMapping,
 * i.e. the isCollection flag is set to true, maxOccurs will be set to 'unbounded' on any
 * source elements
 */
protected void processXMLObjectReferenceMapping(ObjectReferenceMapping mapping, Sequence seq, ComplexType ct, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties, List<Descriptor> descriptors, boolean isCollection) {
    String tgtClassName = mapping.getReferenceClassName();
    Descriptor tgtDesc = getDescriptorByName(tgtClassName, descriptors);
    if (tgtDesc == null) {
        throw DescriptorException.descriptorIsMissing(tgtClassName, (DatabaseMapping) mapping);
    }
    // get the target mapping(s) to determine the appropriate type(s)
    String schemaTypeString = null;
    Map<Field, Field> associations = mapping.getSourceToTargetKeyFieldAssociations();
    for (Entry<Field, Field> entry : associations.entrySet()) {
        Field tgtField = entry.getValue();
        Vector mappings = tgtDesc.getMappings();
        // schemaTypeString = Constants.SCHEMA_PREFIX + COLON + IDREF;
        for (Enumeration mappingsNum = mappings.elements(); mappingsNum.hasMoreElements(); ) {
            Mapping nextMapping = (Mapping) mappingsNum.nextElement();
            if (nextMapping.getField() != null && nextMapping.getField() instanceof Field) {
                Field xFld = (Field) nextMapping.getField();
                if (xFld == tgtField) {
                    schemaTypeString = getSchemaTypeForElement(tgtField, nextMapping.getAttributeClassification(), workingSchema);
                }
            }
        }
        if (schemaTypeString == null) {
            schemaTypeString = getSchemaTypeString(Constants.STRING_QNAME, workingSchema);
        }
        XPathFragment frag = entry.getKey().getXPathFragment();
        if (frag.isAttribute()) {
            Attribute attr = buildAttribute(frag, schemaTypeString);
            ct.getOrderedAttributes().add(attr);
        } else {
            Element elem = buildElement(frag, schemaTypeString, Occurs.ZERO, null);
            if (isCollection) {
                elem.setMaxOccurs(Occurs.UNBOUNDED);
            }
            seq.addElement(elem);
        }
    }
}
Also used : Field(org.eclipse.persistence.internal.oxm.mappings.Field) Enumeration(java.util.Enumeration) AnyAttribute(org.eclipse.persistence.internal.oxm.schema.model.AnyAttribute) Attribute(org.eclipse.persistence.internal.oxm.schema.model.Attribute) Element(org.eclipse.persistence.internal.oxm.schema.model.Element) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) AnyObjectMapping(org.eclipse.persistence.internal.oxm.mappings.AnyObjectMapping) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) AnyCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.AnyCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) AnyAttributeMapping(org.eclipse.persistence.internal.oxm.mappings.AnyAttributeMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) Vector(java.util.Vector)

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