Search in sources :

Example 1 with XMLSchemaReference

use of org.eclipse.persistence.oxm.schema.XMLSchemaReference 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 2 with XMLSchemaReference

use of org.eclipse.persistence.oxm.schema.XMLSchemaReference in project eclipselink by eclipse-ee4j.

the class SDOType method preInitialize.

/**
 * INTERNAL:
 * For this Type generate classes
 * @param packageName
 * @param namespaceResolvers
 */
public void preInitialize(String packageName, List namespaceResolvers) {
    String instanceClassName = getInstanceClassName();
    if (null == instanceClassName) {
        if (null == packageName) {
            String uri = getURI();
            if (null == uri) {
                packageName = SDOUtil.getDefaultPackageName() + SDOConstants.JAVA_PACKAGE_NAME_SEPARATOR;
            } else {
                packageName = SDOUtil.getPackageNameFromURI(uri) + SDOConstants.JAVA_PACKAGE_NAME_SEPARATOR;
            }
        }
        // Verify and fix any Class name that does not conform to conventions
        // run the class name through the JAXB mangler
        String mangledClassName = SDOUtil.className(getName(), false, true, true);
        // we will not fix any type collision at this time as a result of class renaming
        // write fully qualified java class name
        StringBuffer fullClassName = new StringBuffer(packageName);
        fullClassName.append(mangledClassName);
        setInstanceClassName(fullClassName.toString());
    }
    // 
    AbstractSessionLog.getLog().log(// 
    AbstractSessionLog.FINER, // 
    "sdo_type_generation_processing_type", new Object[] { Helper.getShortClassName(getClass()), getInstanceClassName() });
    initializeNamespaces(namespaceResolvers);
    getXmlDescriptor().setJavaClassName(getImplClassName());
    // See SDOResolvable enhancement
    String schemaContext = getName();
    if (getXmlDescriptor().getNamespaceResolver() != null) {
        String prefix = getXmlDescriptor().getNamespaceResolver().resolveNamespaceURI(getURI());
        if ((prefix != null) && !prefix.equals(SDOConstants.EMPTY_STRING)) {
            schemaContext = prefix + SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + schemaContext;
        }
    }
    String schemaContextWithSlash = SDOConstants.SDO_XPATH_SEPARATOR_FRAGMENT + schemaContext;
    XMLSchemaReference schemaRef = new XMLSchemaClassPathReference();
    schemaRef.setSchemaContext(schemaContextWithSlash);
    schemaRef.setType(XMLSchemaReference.COMPLEX_TYPE);
    getXmlDescriptor().setSchemaReference(schemaRef);
}
Also used : XMLSchemaReference(org.eclipse.persistence.oxm.schema.XMLSchemaReference) XMLSchemaClassPathReference(org.eclipse.persistence.oxm.schema.XMLSchemaClassPathReference)

Example 3 with XMLSchemaReference

use of org.eclipse.persistence.oxm.schema.XMLSchemaReference in project eclipselink by eclipse-ee4j.

the class JAXBHelperContext method getType.

/**
 * Return the SDO type corresponding to the wrapped class.
 * <pre>
 * Type customerType = jaxbHelperContext.getType(Customer.class);
 * DataObject customerDO = jaxbHelperContext.getDataFactory().create(customerType);
 * </pre>
 */
public Type getType(Class<?> entityClass) {
    if (null == entityClass) {
        return null;
    }
    XMLDescriptor entityDescriptor = null;
    try {
        entityDescriptor = (XMLDescriptor) jaxbContext.getXMLContext().getSession(entityClass).getDescriptor(entityClass);
    } catch (Exception e) {
        return null;
    }
    XMLSchemaReference schemaReference = entityDescriptor.getSchemaReference();
    if (null == schemaReference) {
        throw SDOException.sdoJaxbNoSchemaReference(entityClass);
    }
    QName qName = schemaReference.getSchemaContextAsQName(entityDescriptor.getNamespaceResolver());
    if (null == qName) {
        throw SDOException.sdoJaxbNoSchemaContext(entityClass);
    }
    Type wrapperType;
    if (entityDescriptor.getSchemaReference().getType() == XMLSchemaReference.COMPLEX_TYPE) {
        wrapperType = getTypeHelper().getType(qName.getNamespaceURI(), qName.getLocalPart());
    } else {
        Property property = getXSDHelper().getGlobalProperty(qName.getNamespaceURI(), qName.getLocalPart(), true);
        wrapperType = property.getType();
    }
    if (null == wrapperType) {
        throw SDOException.sdoJaxbNoTypeForClassBySchemaContext(entityClass, qName);
    }
    return wrapperType;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Type(commonj.sdo.Type) SDOType(org.eclipse.persistence.sdo.SDOType) XMLSchemaReference(org.eclipse.persistence.oxm.schema.XMLSchemaReference) QName(javax.xml.namespace.QName) Property(commonj.sdo.Property) SDOException(org.eclipse.persistence.exceptions.SDOException)

Example 4 with XMLSchemaReference

use of org.eclipse.persistence.oxm.schema.XMLSchemaReference 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 5 with XMLSchemaReference

use of org.eclipse.persistence.oxm.schema.XMLSchemaReference in project eclipselink by eclipse-ee4j.

the class SchemaModelGenerator method processDescriptor.

/**
 * Process a given descriptor.  Global complex types will be generated for based on
 * schema context, and global elements based on default root element.
 */
protected void processDescriptor(Descriptor desc, HashMap<String, Schema> schemaForNamespace, Schema workingSchema, SchemaModelGeneratorProperties properties, List<Descriptor> descriptors) {
    // determine if a simple type (or complex type with simple content) or complex type is required
    boolean simple = isSimple(desc);
    XMLSchemaReference schemaRef = desc.getSchemaReference();
    if (schemaRef != null) {
        if (schemaRef.getType() == org.eclipse.persistence.platform.xml.XMLSchemaReference.COMPLEX_TYPE) {
            workingSchema.addTopLevelComplexTypes(buildComplexType(false, desc, schemaForNamespace, workingSchema, properties, descriptors));
        } else if (schemaRef.getType() == org.eclipse.persistence.platform.xml.XMLSchemaReference.SIMPLE_TYPE) {
            workingSchema.addTopLevelSimpleTypes(buildSimpleType(desc, workingSchema, true));
        } else if (schemaRef.getType() == org.eclipse.persistence.platform.xml.XMLSchemaReference.ELEMENT) {
            workingSchema.addTopLevelElement(buildElement(desc, schemaForNamespace, workingSchema, properties, descriptors, simple));
        }
        for (DatabaseTable table : (Vector<DatabaseTable>) desc.getTables()) {
            String localName = getDefaultRootElementAsQName(desc, table.getName()).getLocalPart();
            // don't overwrite existing top level elements
            if (workingSchema.getTopLevelElements().get(localName) != null) {
                continue;
            }
            Element topLevelElement = new Element();
            topLevelElement.setName(localName);
            QName qname = schemaRef.getSchemaContextAsQName(workingSchema.getNamespaceResolver());
            String elementType = qname.getLocalPart();
            String elementTypeUri = qname.getNamespaceURI();
            String elementTypePrefix = workingSchema.getNamespaceResolver().resolveNamespaceURI(elementTypeUri);
            if (elementTypePrefix != null) {
                elementType = elementTypePrefix + Constants.COLON + elementType;
            }
            topLevelElement.setType(elementType);
            workingSchema.addTopLevelElement(topLevelElement);
        }
    } else {
        // there is a default root element set we need to generate a global element
        for (DatabaseTable table : (Vector<DatabaseTable>) desc.getTables()) {
            String localName = getDefaultRootElementAsQName(desc, table.getName()).getLocalPart();
            // a global element may have been created while generating an element ref
            if (workingSchema.getTopLevelElements().get(localName) == null) {
                Element topLevelElement = new Element();
                topLevelElement.setName(localName);
                if (simple) {
                    if (isComplexTypeWithSimpleContentRequired(desc)) {
                        topLevelElement.setComplexType(buildComplexTypeWithSimpleContent(desc, schemaForNamespace, workingSchema, properties, descriptors));
                    } else {
                        topLevelElement.setSimpleType(buildSimpleType(desc, workingSchema, false));
                    }
                } else {
                    topLevelElement.setComplexType(buildComplexType(true, desc, schemaForNamespace, workingSchema, properties, descriptors));
                }
                workingSchema.addTopLevelElement(topLevelElement);
            }
        }
    }
}
Also used : XMLSchemaReference(org.eclipse.persistence.oxm.schema.XMLSchemaReference) QName(javax.xml.namespace.QName) Element(org.eclipse.persistence.internal.oxm.schema.model.Element) DatabaseTable(org.eclipse.persistence.internal.helper.DatabaseTable) Vector(java.util.Vector)

Aggregations

XMLSchemaReference (org.eclipse.persistence.oxm.schema.XMLSchemaReference)18 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)13 QName (javax.xml.namespace.QName)5 XMLSchemaClassPathReference (org.eclipse.persistence.oxm.schema.XMLSchemaClassPathReference)5 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)4 XMLField (org.eclipse.persistence.oxm.XMLField)4 Vector (java.util.Vector)3 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)3 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)3 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)3 HashMap (java.util.HashMap)2 DatabaseTable (org.eclipse.persistence.internal.helper.DatabaseTable)2 XPathQName (org.eclipse.persistence.internal.oxm.XPathQName)2 Field (org.eclipse.persistence.internal.oxm.mappings.Field)2 Schema (org.eclipse.persistence.internal.oxm.schema.model.Schema)2 XMLContext (org.eclipse.persistence.oxm.XMLContext)2 XMLAnyCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping)2 XMLSchemaURLReference (org.eclipse.persistence.oxm.schema.XMLSchemaURLReference)2 Property (commonj.sdo.Property)1 Type (commonj.sdo.Type)1