Search in sources :

Example 1 with CoreDescriptor

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

use of org.eclipse.persistence.core.descriptors.CoreDescriptor 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)

Example 3 with CoreDescriptor

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

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

the class UnmarshalRecordImpl method endDocument.

@Override
public void endDocument() throws SAXException {
    if (unmarshaller.getIDResolver() != null && parentRecord == null) {
        unmarshaller.getIDResolver().endDocument();
    }
    if (null != selfRecords) {
        for (int x = 0, selfRecordsSize = selfRecords.size(); x < selfRecordsSize; x++) {
            UnmarshalRecord selfRecord = selfRecords.get(x);
            if (selfRecord != null) {
                selfRecord.endDocument();
            }
        }
    }
    if (null != xPathNode.getSelfChildren()) {
        int selfChildrenSize = xPathNode.getSelfChildren().size();
        for (int x = 0; x < selfChildrenSize; x++) {
            XPathNode selfNode = xPathNode.getSelfChildren().get(x);
            if (null != selfNode.getNodeValue()) {
                selfNode.getNodeValue().endSelfNodeValue(this, selfRecords.get(x), attributes);
            }
        }
    }
    CoreDescriptor xmlDescriptor = treeObjectBuilder.getDescriptor();
    try {
        // All populated containerValues need to be set on the object
        if (null != populatedContainerValues) {
            for (int populatedCVSize = populatedContainerValues.size(), i = populatedCVSize - 1; i >= 0; i--) {
                ContainerValue cv = (populatedContainerValues.get(i));
                cv.setContainerInstance(currentObject, getContainerInstance(cv, cv.isDefaultEmptyContainer()));
            }
        }
        // Additionally if any containerValues are defaultEmptyContainerValues they need to be set to a new empty container
        if (null != defaultEmptyContainerValues) {
            for (int defaultEmptyCVSize = defaultEmptyContainerValues.size(), i = defaultEmptyCVSize - 1; i >= 0; i--) {
                ContainerValue cv = (defaultEmptyContainerValues.get(i));
                cv.setContainerInstance(currentObject, getContainerInstance(cv, cv.isDefaultEmptyContainer()));
            }
        }
        // trigger the mapping.
        if (null != nullCapableValues) {
            for (int x = 0, nullValuesSize = nullCapableValues.size(); x < nullValuesSize; x++) {
                nullCapableValues.get(x).setNullValue(currentObject, session);
            }
        }
        // PROCESS TRANSFORMATION MAPPINGS
        List<TransformationMapping> transformationMappings = treeObjectBuilder.getTransformationMappings();
        if (null != transformationMappings) {
            for (int x = 0, transformationMappingsSize = transformationMappings.size(); x < transformationMappingsSize; x++) {
                TransformationMapping transformationMapping = transformationMappings.get(x);
                transformationMapping.readFromRowIntoObject((XMLRecord) transformationRecord, currentObject, session, true);
            }
        }
        Unmarshaller.Listener listener = unmarshaller.getUnmarshalListener();
        if (listener != null) {
            if (this.parentRecord != null) {
                listener.afterUnmarshal(currentObject, parentRecord.getCurrentObject());
            } else {
                listener.afterUnmarshal(currentObject, null);
            }
        }
        // HANDLE POST BUILD EVENTS
        if (xmlDescriptor.hasEventManager()) {
            CoreDescriptorEventManager eventManager = xmlDescriptor.getEventManager();
            if (null != eventManager && eventManager.hasAnyEventListeners()) {
                DescriptorEvent event = new DescriptorEvent(currentObject);
                event.setSession((AbstractSession) session);
                // this);
                event.setRecord(null);
                event.setEventCode(DescriptorEventManager.PostBuildEvent);
                eventManager.executeEvent(event);
            }
        }
    } catch (EclipseLinkException e) {
        if (null == xmlReader.getErrorHandler()) {
            throw e;
        } else {
            SAXParseException saxParseException = new SAXParseException(null, getDocumentLocator(), e);
            xmlReader.getErrorHandler().error(saxParseException);
        }
    }
    // if the object has any primary key fields set, add it to the cache
    if (null != referenceResolver) {
        if (null != xmlDescriptor) {
            List primaryKeyFields = xmlDescriptor.getPrimaryKeyFields();
            if (null != primaryKeyFields) {
                int primaryKeyFieldsSize = primaryKeyFields.size();
                if (primaryKeyFieldsSize > 0) {
                    CacheId pk = (CacheId) treeObjectBuilder.extractPrimaryKeyFromObject(currentObject, session);
                    for (int x = 0; x < primaryKeyFieldsSize; x++) {
                        Object value = pk.getPrimaryKey()[x];
                        if (null == value) {
                            Field pkField = (Field) xmlDescriptor.getPrimaryKeyFields().get(x);
                            pk.set(x, unmarshaller.getContext().getValueByXPath(currentObject, pkField.getXPath(), pkField.getNamespaceResolver(), Object.class));
                        }
                    }
                    referenceResolver.putValue(xmlDescriptor.getJavaClass(), pk, currentObject);
                    if (unmarshaller.getIDResolver() != null) {
                        try {
                            if (primaryKeyFieldsSize > 1) {
                                Map<String, Object> idWrapper = new HashMap<>();
                                for (int x = 0; x < primaryKeyFieldsSize; x++) {
                                    String idName = (String) xmlDescriptor.getPrimaryKeyFieldNames().get(x);
                                    Object idValue = pk.getPrimaryKey()[x];
                                    idWrapper.put(idName, idValue);
                                }
                                unmarshaller.getIDResolver().bind(idWrapper, currentObject);
                            } else {
                                unmarshaller.getIDResolver().bind(pk.getPrimaryKey()[0], currentObject);
                            }
                        } catch (SAXException e) {
                            throw XMLMarshalException.unmarshalException(e);
                        }
                    }
                }
            }
        }
    }
    if (null != parentRecord) {
        reset();
    }
    // Set XML Location if applicable
    if (xmlLocation != null && ((Descriptor) xmlDescriptor).getLocationAccessor() != null) {
        ((Descriptor) xmlDescriptor).getLocationAccessor().setAttributeValueInObject(getCurrentObject(), xmlLocation);
    }
}
Also used : CoreDescriptorEventManager(org.eclipse.persistence.core.descriptors.CoreDescriptorEventManager) TransformationMapping(org.eclipse.persistence.internal.oxm.mappings.TransformationMapping) HashMap(java.util.HashMap) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) DescriptorEvent(org.eclipse.persistence.descriptors.DescriptorEvent) XPathNode(org.eclipse.persistence.internal.oxm.XPathNode) ContainerValue(org.eclipse.persistence.internal.oxm.ContainerValue) SAXException(org.xml.sax.SAXException) Field(org.eclipse.persistence.internal.oxm.mappings.Field) CoreField(org.eclipse.persistence.internal.core.helper.CoreField) EclipseLinkException(org.eclipse.persistence.exceptions.EclipseLinkException) SAXParseException(org.xml.sax.SAXParseException) CacheId(org.eclipse.persistence.internal.identitymaps.CacheId) Descriptor(org.eclipse.persistence.internal.oxm.mappings.Descriptor) CoreDescriptor(org.eclipse.persistence.core.descriptors.CoreDescriptor) List(java.util.List) ArrayList(java.util.ArrayList) Unmarshaller(org.eclipse.persistence.internal.oxm.Unmarshaller)

Example 5 with CoreDescriptor

use of org.eclipse.persistence.core.descriptors.CoreDescriptor 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)

Aggregations

CoreDescriptor (org.eclipse.persistence.core.descriptors.CoreDescriptor)7 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)6 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)4 Field (org.eclipse.persistence.internal.oxm.mappings.Field)4 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)4 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 CoreMapping (org.eclipse.persistence.core.mappings.CoreMapping)3 CacheId (org.eclipse.persistence.internal.identitymaps.CacheId)3 DescriptorException (org.eclipse.persistence.exceptions.DescriptorException)2 JAXBException (org.eclipse.persistence.exceptions.JAXBException)2 InstanceVariableAttributeAccessor (org.eclipse.persistence.internal.descriptors.InstanceVariableAttributeAccessor)2 MethodAttributeAccessor (org.eclipse.persistence.internal.descriptors.MethodAttributeAccessor)2 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)2 JAXBSetMethodAttributeAccessor (org.eclipse.persistence.internal.jaxb.JAXBSetMethodAttributeAccessor)2 NamespaceResolver (org.eclipse.persistence.internal.oxm.NamespaceResolver)2 BinaryDataMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping)2 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)2 JavaField (org.eclipse.persistence.jaxb.javamodel.JavaField)2