Search in sources :

Example 66 with Descriptor

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

the class MappingsGenerator method generateMapEntryClassAndDescriptor.

private Class<?> generateMapEntryClassAndDescriptor(Property property, NamespaceResolver nr) {
    JavaClass keyType = property.getKeyType();
    JavaClass valueType = property.getValueType();
    if (keyType == null) {
        keyType = helper.getJavaClass("java.lang.Object");
    }
    if (valueType == null) {
        valueType = helper.getJavaClass("java.lang.Object");
    }
    String mapEntryClassName = getJaxbClassLoader().nextAvailableGeneratedClassName();
    MapEntryGeneratedKey mapKey = new MapEntryGeneratedKey(keyType.getQualifiedName(), valueType.getQualifiedName());
    Class<?> generatedClass = getGeneratedMapEntryClasses().get(mapKey);
    if (generatedClass == null) {
        generatedClass = generateMapEntryClass(mapEntryClassName, keyType.getQualifiedName(), valueType.getQualifiedName());
        getGeneratedMapEntryClasses().put(mapKey, generatedClass);
        Descriptor desc = new XMLDescriptor();
        desc.setJavaClass(generatedClass);
        desc.addMapping((CoreMapping) generateMappingForType(keyType, Property.DEFAULT_KEY_NAME));
        desc.addMapping((CoreMapping) generateMappingForType(valueType, Property.DEFAULT_VALUE_NAME));
        desc.setNamespaceResolver(nr);
        project.addDescriptor((CoreDescriptor) desc);
    }
    return generatedClass;
}
Also used : 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)

Example 67 with Descriptor

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

the class MappingsGenerator method processGlobalElements.

public void processGlobalElements(CoreProject project) {
    // root elements.
    if (this.globalElements == null && this.localElements == null) {
        return;
    }
    List<ElementDeclaration> elements = new ArrayList<>();
    elements.addAll(this.localElements);
    elements.addAll(this.globalElements.values());
    for (ElementDeclaration nextElement : elements) {
        QName next = nextElement.getElementName();
        String nextClassName = nextElement.getJavaTypeName();
        TypeInfo type = this.typeInfo.get(nextClassName);
        if (helper.isBuiltInJavaType(nextElement.getJavaType()) || (type != null && type.isEnumerationType())) {
            // generate a class/descriptor for this element
            String attributeTypeName = nextClassName;
            if (nextElement.getJavaType().isPrimitive()) {
                attributeTypeName = helper.getClassForJavaClass(nextElement.getJavaType()).getName();
            }
            if (nextElement.getAdaptedJavaTypeName() != null) {
                attributeTypeName = nextElement.getAdaptedJavaTypeName();
            }
            if (next == null) {
                if (isBinaryData(nextElement.getJavaType())) {
                    Class<?> generatedClass = addByteArrayWrapperAndDescriptor(type, nextElement.getJavaType().getRawName(), nextElement, nextClassName, attributeTypeName);
                    this.qNamesToGeneratedClasses.put(next, generatedClass);
                    if (nextElement.getTypeMappingInfo() != null) {
                        typeMappingInfoToGeneratedClasses.put(nextElement.getTypeMappingInfo(), generatedClass);
                    }
                    try {
                        Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
                        this.qNamesToDeclaredClasses.put(next, declaredClass);
                    } catch (Exception e) {
                    }
                }
                if (nextElement.getJavaType().isEnum()) {
                    if (!(helper.getClassLoader() instanceof DynamicClassLoader)) {
                        // Only generate enum wrappers in non-dynamic case.
                        Class<?> generatedClass = addEnumerationWrapperAndDescriptor(type, nextElement.getJavaType().getRawName(), nextElement, nextClassName, attributeTypeName);
                        this.qNamesToGeneratedClasses.put(next, generatedClass);
                        if (nextElement.getTypeMappingInfo() != null) {
                            typeMappingInfoToGeneratedClasses.put(nextElement.getTypeMappingInfo(), generatedClass);
                        }
                        try {
                            Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
                            this.qNamesToDeclaredClasses.put(next, declaredClass);
                        } catch (Exception ex) {
                        }
                    }
                }
                continue;
            }
            Class<?> generatedClass = generateWrapperClassAndDescriptor(type, next, nextElement, nextClassName, attributeTypeName);
            this.qNamesToGeneratedClasses.put(next, generatedClass);
            if (type != null && type.isEnumerationType() && nextElement.isXmlRootElement()) {
                this.classToGeneratedClasses.put(type.getJavaClassName(), generatedClass);
            }
            try {
                Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
                this.qNamesToDeclaredClasses.put(next, declaredClass);
            } catch (Exception e) {
            }
        } else if (type != null && !type.isTransient()) {
            if (next.getNamespaceURI() == null || next.getNamespaceURI().equals("")) {
                type.getDescriptor().addRootElement(next.getLocalPart());
            } else {
                Descriptor descriptor = type.getDescriptor();
                String uri = next.getNamespaceURI();
                String prefix = getPrefixForNamespace(uri, descriptor.getNamespaceResolver());
                descriptor.addRootElement(getQualifiedString(prefix, next.getLocalPart()));
            }
        }
    }
}
Also used : DynamicClassLoader(org.eclipse.persistence.dynamic.DynamicClassLoader) QName(javax.xml.namespace.QName) ArrayList(java.util.ArrayList) 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) JAXBException(org.eclipse.persistence.exceptions.JAXBException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException)

Example 68 with Descriptor

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

the class JAXBUnmarshaller method buildJAXBElementFromObject.

/**
 * Create a JAXBElement instance.  If the object is an instance
 * of XMLRoot, we will use its field values to create the
 * JAXBElement.  If the object is not an XMLRoot instance, we
 * will have to determine the 'name' value.  This will be done
 * using the object's descriptor default root element - any
 * prefix will be resolved, and a QName created.
 */
private JAXBElement buildJAXBElementFromObject(Object obj, Class<?> declaredClass) {
    // JAXBElement from the returned XMLRoot object
    if (obj instanceof Root) {
        JAXBElement jaxbElement = jaxbContext.createJAXBElementFromXMLRoot(((Root) obj), declaredClass);
        if (((Root) obj).isNil()) {
            jaxbElement.setNil(((Root) obj).isNil());
            jaxbElement.setValue(null);
        }
        return jaxbElement;
    }
    if (obj instanceof JAXBElement) {
        return (JAXBElement) obj;
    }
    // at this point, the default root element of the object being marshalled
    // to == the root element - here we need to create a JAXBElement
    // instance using information from the returned object
    org.eclipse.persistence.sessions.Session sess = xmlUnmarshaller.getXMLContext().getSession(obj);
    Descriptor desc = (Descriptor) sess.getClassDescriptor(obj);
    // here we are assuming that if we've gotten this far, there
    // must be a default root element set on the descriptor.  if
    // this is incorrect, we need to check for null and throw an
    // exception
    String rootName = desc.getDefaultRootElement();
    if (rootName == null) {
        return jaxbContext.createJAXBElement(new QName(""), obj.getClass(), obj);
    }
    String rootNamespaceUri = null;
    int idx = rootName.indexOf(':');
    if (idx != -1) {
        rootNamespaceUri = desc.getNamespaceResolver().resolveNamespacePrefix(rootName.substring(0, idx));
        rootName = rootName.substring(idx + 1);
    }
    QName qname;
    if (rootNamespaceUri == null) {
        qname = new QName(rootName);
    } else {
        qname = new QName(rootNamespaceUri, rootName);
    }
    if (declaredClass != null) {
        return jaxbContext.createJAXBElement(qname, declaredClass, obj);
    } else {
        return jaxbContext.createJAXBElement(qname, obj.getClass(), obj);
    }
}
Also used : Root(org.eclipse.persistence.internal.oxm.Root) QName(javax.xml.namespace.QName) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) JAXBElement(jakarta.xml.bind.JAXBElement)

Example 69 with Descriptor

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

the class JAXBIntrospector method getElementName.

@Override
public QName getElementName(Object obj) {
    if (!isElement(obj)) {
        return null;
    }
    if (obj instanceof JAXBElement) {
        return ((JAXBElement) obj).getName();
    }
    try {
        Descriptor descriptor = (Descriptor) context.getSession(obj).getDescriptor(obj);
        XPathFragment rootFragment = descriptor.getDefaultRootElementField().getXPathFragment();
        return new QName(rootFragment.getNamespaceURI(), rootFragment.getLocalName());
    } catch (XMLMarshalException e) {
        return null;
    }
}
Also used : QName(javax.xml.namespace.QName) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) JAXBElement(jakarta.xml.bind.JAXBElement) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Example 70 with Descriptor

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

the class BaseDBWSBuilderHelper method buildSchema.

/**
 * Build the schema
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void buildSchema(NamingConventionTransformer nct) {
    Project oxProject = dbwsBuilder.getOxProject();
    Schema schema = null;
    List<Descriptor> descriptorsToProcess = new ArrayList<Descriptor>();
    for (Descriptor desc : (List<Descriptor>) (List) oxProject.getOrderedDescriptors()) {
        String alias = desc.getAlias();
        if (!DEFAULT_SIMPLE_XML_FORMAT_TAG.equals(alias)) {
            descriptorsToProcess.add(desc);
        }
    }
    if (descriptorsToProcess.size() > 0) {
        // need a deep-copy clone of oxProject; simplest way is to marshall/unmarshall to a stream
        StringWriter sw = new StringWriter();
        XMLProjectWriter.write(oxProject, sw);
        XRDynamicClassLoader specialLoader = new XRDynamicClassLoader(this.getClass().getClassLoader());
        Project oxProjectClone = XMLProjectReader.read(new StringReader(sw.toString()), specialLoader);
        ProjectHelper.fixOROXAccessors(oxProjectClone, null);
        XMLLogin xmlLogin = new XMLLogin();
        DOMPlatform domPlatform = new DOMPlatform();
        domPlatform.getConversionManager().setLoader(specialLoader);
        xmlLogin.setPlatform(domPlatform);
        oxProjectClone.setLogin(xmlLogin);
        // initialize reference descriptors
        oxProjectClone.createDatabaseSession();
        SchemaModelGenerator schemaGenerator = new SchemaModelGenerator(XMLConversionManager.getDefaultXMLManager(), true);
        SchemaModelGeneratorProperties sgProperties = new SchemaModelGeneratorProperties();
        // set element form default to qualified for target namespace
        sgProperties.addProperty(dbwsBuilder.getTargetNamespace(), ELEMENT_FORM_QUALIFIED_KEY, true);
        Map<String, Schema> schemaMap = schemaGenerator.generateSchemas(descriptorsToProcess, sgProperties);
        Schema s = schemaMap.get(dbwsBuilder.getTargetNamespace());
        // check existing schema for any top-level ComplexTypes
        if (dbwsBuilder.getSchema() != null && s != null) {
            Map<String, ComplexType> topLevelComplexTypes = dbwsBuilder.getSchema().getTopLevelComplexTypes();
            for (Map.Entry<String, ComplexType> me : topLevelComplexTypes.entrySet()) {
                s.addTopLevelComplexTypes(me.getValue());
            }
            // add any additional namespaces
            NamespaceResolver snr = s.getNamespaceResolver();
            NamespaceResolver nr = dbwsBuilder.getSchema().getNamespaceResolver();
            // we only want to add prefix/uri pairs for prefixes that don't already exist
            for (String prefix : nr.getPrefixesToNamespaces().keySet()) {
                if (snr.resolveNamespacePrefix(prefix) == null) {
                    snr.put(prefix, nr.resolveNamespacePrefix(prefix));
                }
            }
            // switch
            schema = s;
            schema.setNamespaceResolver(snr);
        }
    } else {
        schema = new Schema();
        addSimpleXMLFormat(schema);
        schema.setTargetNamespace(dbwsBuilder.getTargetNamespace());
    }
    dbwsBuilder.setSchema(schema);
}
Also used : DOMPlatform(org.eclipse.persistence.oxm.platform.DOMPlatform) Schema(org.eclipse.persistence.internal.oxm.schema.model.Schema) XRDynamicClassLoader(org.eclipse.persistence.internal.xr.XRDynamicClassLoader) ArrayList(java.util.ArrayList) XMLLogin(org.eclipse.persistence.oxm.XMLLogin) Project(org.eclipse.persistence.sessions.Project) SchemaModelProject(org.eclipse.persistence.internal.oxm.schema.SchemaModelProject) DBWSModelProject(org.eclipse.persistence.dbws.DBWSModelProject) ObjectPersistenceWorkbenchXMLProject(org.eclipse.persistence.internal.sessions.factories.ObjectPersistenceWorkbenchXMLProject) SimpleXMLFormatProject(org.eclipse.persistence.internal.xr.sxf.SimpleXMLFormatProject) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) RelationalDescriptor(org.eclipse.persistence.descriptors.RelationalDescriptor) Util.buildOXDescriptor(org.eclipse.persistence.tools.dbws.Util.buildOXDescriptor) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) Util.buildORDescriptor(org.eclipse.persistence.tools.dbws.Util.buildORDescriptor) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) SchemaModelGeneratorProperties(org.eclipse.persistence.internal.oxm.schema.SchemaModelGeneratorProperties) NamespaceResolver(org.eclipse.persistence.oxm.NamespaceResolver) ArrayList(java.util.ArrayList) List(java.util.List) SchemaModelGenerator(org.eclipse.persistence.internal.oxm.schema.SchemaModelGenerator) ComplexType(org.eclipse.persistence.internal.oxm.schema.model.ComplexType) Map(java.util.Map) HashMap(java.util.HashMap)

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