Search in sources :

Example 1 with Descriptor

use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project aai-aai-common by onap.

the class OxmSchemaLoader method loadXmlLookupMap.

private static void loadXmlLookupMap(String version, DynamicJAXBContext jaxbContext) {
    @SuppressWarnings("rawtypes") List<Descriptor> descriptorsList = jaxbContext.getXMLContext().getDescriptors();
    HashMap<String, DynamicType> types = new HashMap<String, DynamicType>();
    for (@SuppressWarnings("rawtypes") Descriptor desc : descriptorsList) {
        DynamicType entity = jaxbContext.getDynamicType(desc.getAlias());
        String entityName = desc.getDefaultRootElement();
        types.put(entityName, entity);
    }
    xmlElementLookup.put(version, types);
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) DynamicType(org.eclipse.persistence.dynamic.DynamicType) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor)

Example 2 with Descriptor

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

the class JAXBUnmarshaller method unmarshal.

/**
 * Unmarshal the object based on the binding metadata associated with the
 * TypeMappingInfo.
 */
public JAXBElement unmarshal(XMLStreamReader streamReader, TypeMappingInfo type) throws JAXBException {
    try {
        Descriptor xmlDescriptor = type.getXmlDescriptor();
        if (type.getType() instanceof Class) {
            Class<?> javaClass = (Class) type.getType();
            Class<?> componentClass = javaClass.getComponentType();
            if (javaClass.isArray() && javaClass != CoreClassConstants.APBYTE && javaClass != CoreClassConstants.ABYTE && XMLConversionManager.getDefaultJavaTypes().get(componentClass) != null) {
                // Top-level array.  Descriptor will be for an EL-generated class, containing one DirectCollection mapping.
                DirectCollectionMapping mapping = (DirectCollectionMapping) xmlDescriptor.getMappings().get(0);
                XMLStreamReaderReader staxReader = new XMLStreamReaderReader(xmlUnmarshaller);
                staxReader.setErrorHandler(xmlUnmarshaller.getErrorHandler());
                PrimitiveArrayContentHandler primitiveArrayContentHandler = new PrimitiveArrayContentHandler(javaClass, componentClass, mapping.usesSingleNode());
                staxReader.setContentHandler(primitiveArrayContentHandler);
                XMLStreamReaderInputSource inputSource = new XMLStreamReaderInputSource(streamReader);
                staxReader.parse(inputSource);
                return primitiveArrayContentHandler.getJaxbElement();
            }
        }
        if (null != xmlDescriptor && null == getSchema()) {
            RootLevelXmlAdapter adapter = null;
            if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
                adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
            }
            UnmarshalRecord wrapper = (UnmarshalRecord) xmlDescriptor.getObjectBuilder().createRecordFromXMLContext(xmlUnmarshaller.getXMLContext());
            org.eclipse.persistence.internal.oxm.record.UnmarshalRecord unmarshalRecord = wrapper.getUnmarshalRecord();
            XMLStreamReaderReader staxReader = new XMLStreamReaderReader(xmlUnmarshaller);
            unmarshalRecord.setUnmarshaller(xmlUnmarshaller);
            unmarshalRecord.setXMLReader(staxReader);
            staxReader.setContentHandler(unmarshalRecord);
            staxReader.parse(streamReader);
            Object value = null;
            if (unmarshalRecord.isNil()) {
                value = null;
            } else {
                value = unmarshalRecord.getCurrentObject();
            }
            if (value instanceof WrappedValue) {
                value = ((WrappedValue) value).getValue();
            }
            if (value instanceof ManyValue) {
                value = ((ManyValue) value).getItem();
            }
            if (adapter != null) {
                try {
                    value = adapter.getXmlAdapter().unmarshal(value);
                } catch (Exception ex) {
                    throw new JAXBException(XMLMarshalException.marshalException(ex));
                }
            }
            Class<?> declaredClass = null;
            if (type.getType() instanceof Class) {
                declaredClass = (Class) type.getType();
            } else {
                declaredClass = Object.class;
            }
            return new JAXBElement(new QName(unmarshalRecord.getRootElementNamespaceUri(), unmarshalRecord.getLocalName()), declaredClass, value);
        }
        if (jaxbContext.getTypeMappingInfoToGeneratedType() == null) {
            return unmarshal(streamReader, type.getType());
        }
        RootLevelXmlAdapter adapter = null;
        if (jaxbContext.getTypeMappingInfoToJavaTypeAdapters().size() > 0) {
            adapter = jaxbContext.getTypeMappingInfoToJavaTypeAdapters().get(type);
        }
        Class<?> unmarshalClass = null;
        if (jaxbContext.getTypeMappingInfoToGeneratedType().size() > 0) {
            unmarshalClass = jaxbContext.getTypeMappingInfoToGeneratedType().get(type);
        }
        if (unmarshalClass != null) {
            JAXBElement unmarshalled = unmarshal(streamReader, unmarshalClass);
            Class<?> declaredClass = null;
            if (type.getType() instanceof Class) {
                declaredClass = (Class) type.getType();
            } else {
                declaredClass = Object.class;
            }
            Object value = unmarshalled.getValue();
            if (adapter != null) {
                try {
                    value = adapter.getXmlAdapter().unmarshal(value);
                } catch (Exception ex) {
                    throw new JAXBException(XMLMarshalException.marshalException(ex));
                }
            }
            JAXBElement returnVal = new JAXBElement(unmarshalled.getName(), declaredClass, unmarshalled.getScope(), value);
            return returnVal;
        } else if (type.getType() instanceof Class) {
            if (adapter != null) {
                JAXBElement element = unmarshal(streamReader, adapter.getBoundType());
                try {
                    Object value = adapter.getXmlAdapter().unmarshal(element.getValue());
                    element.setValue(value);
                    return element;
                } catch (Exception ex) {
                    throw new JAXBException(XMLMarshalException.marshalException(ex));
                }
            }
            return unmarshal(streamReader, (Class) type.getType());
        } else if (type.getType() instanceof ParameterizedType) {
            return unmarshal(streamReader, ((ParameterizedType) type.getType()).getRawType());
        }
        return null;
    } catch (XMLMarshalException xmlMarshalException) {
        throw handleXMLMarshalException(xmlMarshalException);
    } catch (SAXException e) {
        throw new JAXBException(e);
    }
}
Also used : QName(javax.xml.namespace.QName) JAXBException(jakarta.xml.bind.JAXBException) ManyValue(org.eclipse.persistence.internal.jaxb.many.ManyValue) JAXBElement(jakarta.xml.bind.JAXBElement) WrappedValue(org.eclipse.persistence.internal.jaxb.WrappedValue) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) BeanValidationException(org.eclipse.persistence.exceptions.BeanValidationException) SAXException(org.xml.sax.SAXException) UnmarshalException(jakarta.xml.bind.UnmarshalException) PropertyException(jakarta.xml.bind.PropertyException) JAXBException(jakarta.xml.bind.JAXBException) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) RootLevelXmlAdapter(org.eclipse.persistence.jaxb.JAXBContext.RootLevelXmlAdapter) SAXException(org.xml.sax.SAXException) ParameterizedType(java.lang.reflect.ParameterizedType) XMLStreamReaderReader(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderReader) UnmarshalRecord(org.eclipse.persistence.oxm.record.UnmarshalRecord) XMLStreamReaderInputSource(org.eclipse.persistence.internal.oxm.record.XMLStreamReaderInputSource) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException)

Example 3 with Descriptor

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

the class JAXBBinder method updateJAXB.

@Override
public Object updateJAXB(Object obj) throws JAXBException {
    if (null == obj) {
        throw new IllegalArgumentException();
    }
    try {
        xmlBinder.updateObject((Node) obj);
        Object updatedObj = xmlBinder.getObject((Node) obj);
        boolean shouldWrapInJAXBElement = true;
        Descriptor desc = (Descriptor) xmlBinder.getMarshaller().getXMLContext().getSession().getClassDescriptor(updatedObj);
        if (desc == null) {
            return updatedObj;
        }
        if (desc.getDefaultRootElementField() != null) {
            String objRootElem = desc.getDefaultRootElement();
            String rootElemNS = objRootElem.substring(0, objRootElem.lastIndexOf(':'));
            String rootElemName = objRootElem.substring(objRootElem.lastIndexOf(':') + 1);
            String resolvedNS = desc.getNamespaceResolver().resolveNamespacePrefix(rootElemNS);
            String nodeName = ((Node) obj).getLocalName();
            String nodeNS = ((Node) obj).getNamespaceURI();
            if (rootElemName.equals(nodeName) && resolvedNS.equals(nodeNS)) {
                shouldWrapInJAXBElement = false;
            }
        }
        if (!shouldWrapInJAXBElement) {
            return updatedObj;
        } else {
            QName qname = new QName(((Node) obj).getNamespaceURI(), ((Node) obj).getLocalName());
            return new JAXBElement(qname, updatedObj.getClass(), updatedObj);
        }
    } catch (Exception e) {
        throw new JAXBException(e);
    }
}
Also used : QName(javax.xml.namespace.QName) Node(org.w3c.dom.Node) JAXBException(jakarta.xml.bind.JAXBException) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) JAXBElement(jakarta.xml.bind.JAXBElement) UnmarshalException(jakarta.xml.bind.UnmarshalException) PropertyException(jakarta.xml.bind.PropertyException) MarshalException(jakarta.xml.bind.MarshalException) JAXBException(jakarta.xml.bind.JAXBException)

Example 4 with Descriptor

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

the class MappingsGenerator method generateWrapperClassAndDescriptor.

private Class<?> generateWrapperClassAndDescriptor(TypeInfo type, QName next, ElementDeclaration nextElement, String nextClassName, String attributeTypeName) {
    String namespaceUri = null;
    if (next != null) {
        // generate a class/descriptor for this element
        namespaceUri = next.getNamespaceURI();
        if (namespaceUri == null || namespaceUri.equals(XMLProcessor.DEFAULT)) {
            namespaceUri = "";
        }
    }
    TypeMappingInfo tmi = nextElement.getTypeMappingInfo();
    Class<?> generatedClass = null;
    JaxbClassLoader loader = getJaxbClassLoader();
    if (tmi != null) {
        generatedClass = CompilerHelper.getExisitingGeneratedClass(tmi, typeMappingInfoToGeneratedClasses, typeMappingInfoToAdapterClasses, helper.getClassLoader());
        if (generatedClass == null) {
            generatedClass = this.generateWrapperClass(loader.nextAvailableGeneratedClassName(), attributeTypeName, nextElement.isList(), next);
        }
        typeMappingInfoToGeneratedClasses.put(tmi, generatedClass);
    } else {
        generatedClass = this.generateWrapperClass(loader.nextAvailableGeneratedClassName(), attributeTypeName, nextElement.isList(), next);
    }
    this.qNamesToGeneratedClasses.put(next, generatedClass);
    try {
        Class<Object> declaredClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
        this.qNamesToDeclaredClasses.put(next, declaredClass);
    } catch (Exception e) {
    }
    Descriptor desc = (Descriptor) project.getDescriptor(generatedClass);
    if (desc == null) {
        desc = new XMLDescriptor();
        desc.setJavaClass(generatedClass);
        if (nextElement.isList()) {
            DirectCollectionMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLCompositeDirectCollectionMapping();
            mapping.setAttributeName("value");
            mapping.setXPath("text()");
            mapping.setUsesSingleNode(true);
            mapping.setReuseContainer(true);
            if (type != null && type.isEnumerationType()) {
                mapping.setValueConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo) type));
            } else {
                try {
                    Class<Object> fieldElementClass = PrivilegedAccessHelper.getClassForName(nextClassName, false, helper.getClassLoader());
                    mapping.setFieldElementClass(fieldElementClass);
                } catch (ClassNotFoundException e) {
                }
            }
            if (nextClassName.equals("[B") || nextClassName.equals("[Ljava.lang.Byte;")) {
                ((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
            } else if (nextClassName.equals("javax.xml.namespace.QName")) {
                ((Field) mapping.getField()).setSchemaType(Constants.QNAME_QNAME);
            }
            desc.addMapping((CoreMapping) mapping);
        } else {
            if (nextElement.getJavaTypeName().equals(OBJECT_CLASS_NAME)) {
                CompositeObjectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, XMLRecord> mapping = new XMLCompositeObjectMapping();
                mapping.setAttributeName("value");
                mapping.setSetMethodName("setValue");
                mapping.setGetMethodName("getValue");
                mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
                mapping.setXPath(".");
                setTypedTextField((Field) mapping.getField());
                desc.addMapping((CoreMapping) mapping);
            } else if (isBinaryData(nextElement.getJavaType())) {
                BinaryDataMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, MimeTypePolicy, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLBinaryDataMapping();
                mapping.setAttributeName("value");
                mapping.setXPath(".");
                ((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
                mapping.setSetMethodName("setValue");
                mapping.setGetMethodName("getValue");
                mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
                mapping.getNullPolicy().setNullRepresentedByEmptyNode(false);
                Class<?> attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(attributeTypeName, helper.getClassLoader());
                mapping.setAttributeClassification(attributeClassification);
                mapping.setShouldInlineBinaryData(false);
                // if(nextElement.getTypeMappingInfo() != null) {
                mapping.setSwaRef(nextElement.isXmlAttachmentRef());
                mapping.setMimeType(nextElement.getXmlMimeType());
                // }
                desc.addMapping((CoreMapping) mapping);
            } else {
                DirectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, XMLUnmarshaller, XMLRecord> mapping = new XMLDirectMapping();
                mapping.setNullValueMarshalled(true);
                mapping.setAttributeName("value");
                mapping.setXPath("text()");
                mapping.setSetMethodName("setValue");
                mapping.setGetMethodName("getValue");
                if (nextElement.getDefaultValue() != null) {
                    mapping.setNullValue(nextElement.getDefaultValue());
                    mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
                }
                if (helper.isBuiltInJavaType(nextElement.getJavaType())) {
                    Class<?> attributeClassification = null;
                    if (nextElement.getJavaType().isPrimitive()) {
                        attributeClassification = XMLConversionManager.getDefaultManager().convertClassNameToClass(attributeTypeName);
                    } else {
                        attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(attributeTypeName, helper.getClassLoader());
                    }
                    mapping.setAttributeClassification(attributeClassification);
                }
                IsSetNullPolicy nullPolicy = new IsSetNullPolicy("isSetValue", false, true, XMLNullRepresentationType.ABSENT_NODE);
                // nullPolicy.setNullRepresentedByEmptyNode(true);
                mapping.setNullPolicy(nullPolicy);
                if (type != null && type.isEnumerationType()) {
                    mapping.setConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo) type));
                }
                if (nextClassName.equals("[B") || nextClassName.equals("[Ljava.lang.Byte;")) {
                    ((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
                } else if (nextClassName.equals("javax.xml.namespace.QName")) {
                    ((Field) mapping.getField()).setSchemaType(Constants.QNAME_QNAME);
                }
                if (nextElement.getJavaTypeAdapterClass() != null) {
                    mapping.setConverter(new XMLJavaTypeConverter(nextElement.getJavaTypeAdapterClass()));
                }
                desc.addMapping((CoreMapping) mapping);
            }
        }
        if (next != null) {
            NamespaceInfo info = getNamespaceInfoForURI(namespaceUri);
            if (info != null) {
                NamespaceResolver resolver = getNamespaceResolverForDescriptor(info);
                String prefix = null;
                if (namespaceUri != Constants.EMPTY_STRING) {
                    prefix = resolver.resolveNamespaceURI(namespaceUri);
                    if (prefix == null) {
                        prefix = getPrefixForNamespace(namespaceUri, resolver);
                    }
                }
                desc.setNamespaceResolver(resolver);
                if (nextElement.isXmlRootElement()) {
                    desc.setDefaultRootElement(getQualifiedString(prefix, next.getLocalPart()));
                } else {
                    desc.setDefaultRootElement("");
                    desc.addRootElement(getQualifiedString(prefix, next.getLocalPart()));
                    desc.setResultAlwaysXMLRoot(true);
                }
            } else {
                if (namespaceUri.equals("")) {
                    desc.setDefaultRootElement(next.getLocalPart());
                } else {
                    NamespaceResolver resolver = new org.eclipse.persistence.oxm.NamespaceResolver();
                    String prefix = getPrefixForNamespace(namespaceUri, resolver);
                    desc.setNamespaceResolver(resolver);
                    if (nextElement.isXmlRootElement()) {
                        desc.setDefaultRootElement(getQualifiedString(prefix, next.getLocalPart()));
                    } else {
                        desc.setDefaultRootElement("");
                        desc.addRootElement(getQualifiedString(prefix, next.getLocalPart()));
                        desc.setResultAlwaysXMLRoot(true);
                    }
                }
            }
        }
        project.addDescriptor((CoreDescriptor) desc);
    }
    return generatedClass;
}
Also used : ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) IsSetNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.IsSetNullPolicy) XmlIsSetNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlIsSetNullPolicy) 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) CoreConverter(org.eclipse.persistence.core.mappings.converters.CoreConverter) JAXBEnumTypeConverter(org.eclipse.persistence.jaxb.JAXBEnumTypeConverter) JAXBElementConverter(org.eclipse.persistence.internal.jaxb.JAXBElementConverter) XMLListConverter(org.eclipse.persistence.oxm.mappings.converters.XMLListConverter) XMLConverter(org.eclipse.persistence.oxm.mappings.converters.XMLConverter) Converter(org.eclipse.persistence.mappings.converters.Converter) DefaultElementConverter(org.eclipse.persistence.internal.jaxb.DefaultElementConverter) DomHandlerConverter(org.eclipse.persistence.internal.jaxb.DomHandlerConverter) JAXBElementRootConverter(org.eclipse.persistence.internal.jaxb.JAXBElementRootConverter) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) XMLUnmarshaller(org.eclipse.persistence.oxm.XMLUnmarshaller) UnmarshalKeepAsElementPolicy(org.eclipse.persistence.oxm.mappings.UnmarshalKeepAsElementPolicy) TypeMappingInfo(org.eclipse.persistence.jaxb.TypeMappingInfo) JaxbClassLoader(org.eclipse.persistence.internal.jaxb.JaxbClassLoader) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) XMLRecord(org.eclipse.persistence.oxm.record.XMLRecord) JAXBException(org.eclipse.persistence.exceptions.JAXBException) DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) ContainerPolicy(org.eclipse.persistence.internal.queries.ContainerPolicy) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DatabaseField(org.eclipse.persistence.internal.helper.DatabaseField) 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) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) VirtualAttributeAccessor(org.eclipse.persistence.internal.descriptors.VirtualAttributeAccessor) CustomAccessorAttributeAccessor(org.eclipse.persistence.internal.jaxb.CustomAccessorAttributeAccessor) JAXBSetMethodAttributeAccessor(org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) MapValueAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.MapValueAttributeAccessor) MethodAttributeAccessor(org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor) InstanceVariableAttributeAccessor(org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) Session(org.eclipse.persistence.sessions.Session) AbstractSession(org.eclipse.persistence.internal.sessions.AbstractSession)

Example 5 with Descriptor

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

the class MappingsGenerator method generateDescriptorForJAXBElementSubclass.

public void generateDescriptorForJAXBElementSubclass(JavaClass javaClass, CoreProject project, NamespaceResolver nsr) {
    String jClassName = javaClass.getQualifiedName();
    TypeInfo info = typeInfo.get(jClassName);
    Descriptor xmlDescriptor = new XMLDescriptor();
    xmlDescriptor.setJavaClassName(jClassName);
    String[] factoryMethodParamTypes = info.getFactoryMethodParamTypes();
    MultiArgInstantiationPolicy policy = new MultiArgInstantiationPolicy();
    policy.useFactoryInstantiationPolicy(info.getObjectFactoryClassName(), info.getFactoryMethodName());
    policy.setParameterTypeNames(factoryMethodParamTypes);
    policy.setDefaultValues(new String[] { null });
    xmlDescriptor.setInstantiationPolicy(policy);
    JavaClass paramClass = helper.getJavaClass(factoryMethodParamTypes[0]);
    boolean isObject = paramClass.getName().equals("java.lang.Object");
    if (helper.isBuiltInJavaType(paramClass) && !isObject) {
        if (isBinaryData(paramClass)) {
            BinaryDataMapping mapping = new XMLBinaryDataMapping();
            mapping.setAttributeName("value");
            mapping.setXPath(".");
            ((Field) mapping.getField()).setSchemaType(Constants.BASE_64_BINARY_QNAME);
            mapping.setSetMethodName("setValue");
            mapping.setGetMethodName("getValue");
            Class<?> attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(factoryMethodParamTypes[0], helper.getClassLoader());
            mapping.setAttributeClassification(attributeClassification);
            mapping.getNullPolicy().setNullRepresentedByEmptyNode(false);
            mapping.setShouldInlineBinaryData(false);
            if (mapping.getMimeType() == null) {
                if (areEquals(paramClass, javax.xml.transform.Source.class)) {
                    mapping.setMimeTypePolicy(new FixedMimeTypePolicy("application/xml"));
                } else {
                    mapping.setMimeTypePolicy(new FixedMimeTypePolicy("application/octet-stream"));
                }
            }
            xmlDescriptor.addMapping((CoreMapping) mapping);
        } else {
            DirectMapping mapping = new XMLDirectMapping();
            mapping.setNullValueMarshalled(true);
            mapping.setAttributeName("value");
            mapping.setGetMethodName("getValue");
            mapping.setSetMethodName("setValue");
            mapping.setXPath("text()");
            Class<?> attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(factoryMethodParamTypes[0], helper.getClassLoader());
            mapping.setAttributeClassification(attributeClassification);
            xmlDescriptor.addMapping((CoreMapping) mapping);
        }
    } else if (paramClass.isEnum()) {
        EnumTypeInfo enumInfo = (EnumTypeInfo) typeInfo.get(paramClass.getQualifiedName());
        DirectMapping mapping = new XMLDirectMapping();
        mapping.setConverter(buildJAXBEnumTypeConverter(mapping, enumInfo));
        mapping.setNullValueMarshalled(true);
        mapping.setAttributeName("value");
        mapping.setGetMethodName("getValue");
        mapping.setSetMethodName("setValue");
        mapping.setXPath("text()");
        Class<?> attributeClassification = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(factoryMethodParamTypes[0], helper.getClassLoader());
        mapping.setAttributeClassification(attributeClassification);
        xmlDescriptor.addMapping((CoreMapping) mapping);
    } else {
        CompositeObjectMapping mapping = new XMLCompositeObjectMapping();
        mapping.setAttributeName("value");
        mapping.setGetMethodName("getValue");
        mapping.setSetMethodName("setValue");
        mapping.setXPath(".");
        if (isObject) {
            mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
        } else {
            mapping.setReferenceClassName(factoryMethodParamTypes[0]);
        }
        xmlDescriptor.addMapping((CoreMapping) mapping);
    }
    xmlDescriptor.setNamespaceResolver(nsr);
    setSchemaContext(xmlDescriptor, info);
    project.addDescriptor((CoreDescriptor) xmlDescriptor);
    info.setDescriptor(xmlDescriptor);
}
Also used : FixedMimeTypePolicy(org.eclipse.persistence.oxm.mappings.FixedMimeTypePolicy) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) MultiArgInstantiationPolicy(org.eclipse.persistence.internal.jaxb.MultiArgInstantiationPolicy) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) 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) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) 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) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)

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