Search in sources :

Example 1 with DirectMapping

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

the class JsonSchemaGenerator method generateProperty.

private Property generateProperty(Mapping next, XMLDescriptor descriptor, Map<String, Property> properties) {
    Property prop = null;
    if (next.isCollectionMapping()) {
        if (next instanceof CollectionReferenceMapping) {
            CollectionReferenceMapping mapping = (CollectionReferenceMapping) next;
            Set<XMLField> sourceFields = mapping.getSourceToTargetKeyFieldAssociations().keySet();
            XMLDescriptor reference = (XMLDescriptor) mapping.getReferenceDescriptor();
            for (XMLField nextField : sourceFields) {
                XPathFragment frag = nextField.getXPathFragment();
                String propertyName = getNameForFragment(frag);
                XMLField targetField = (XMLField) mapping.getSourceToTargetKeyFieldAssociations().get(nextField);
                Class<?> type = CoreClassConstants.STRING;
                if (reference != null) {
                    type = getTypeForTargetField(targetField, reference);
                }
                prop = properties.get(propertyName);
                if (prop == null) {
                    prop = new Property();
                    prop.setName(propertyName);
                }
                Property nestedProperty = getNestedPropertyForFragment(frag, prop);
                nestedProperty.setType(JsonType.ARRAY);
                nestedProperty.setItem(new Property());
                nestedProperty.getItem().setType(getJsonTypeForJavaType(type));
                if (!properties.containsKey(prop.getName())) {
                    properties.put(prop.getName(), prop);
                }
            }
            return prop;
        } else if (next.isAbstractCompositeCollectionMapping()) {
            CompositeCollectionMapping mapping = (CompositeCollectionMapping) next;
            XMLField field = (XMLField) mapping.getField();
            XPathFragment frag = field.getXPathFragment();
            String propName = getNameForFragment(frag);
            // for paths, there may already be an existing property
            prop = properties.get(propName);
            if (prop == null) {
                prop = new Property();
                prop.setName(propName);
            }
            Property nestedProperty = getNestedPropertyForFragment(frag, prop);
            nestedProperty.setType(JsonType.ARRAY);
            nestedProperty.setItem(new Property());
            XMLDescriptor referenceDescriptor = (XMLDescriptor) mapping.getReferenceDescriptor();
            if (referenceDescriptor != null && referenceDescriptor.hasInheritance()) {
                // handle inheritence
                // schema.setAnyOf(new Property[descriptor.getInheritancePolicy().getAllChildDescriptors().size()]);
                List<ClassDescriptor> descriptors = getAllDescriptorsForInheritance(referenceDescriptor);
                Property[] props = new Property[descriptors.size()];
                for (int i = 0; i < props.length; i++) {
                    XMLDescriptor nextDescriptor = null;
                    nextDescriptor = (XMLDescriptor) descriptors.get(i);
                    Property ref = new Property();
                    ref.setRef(getReferenceForDescriptor(nextDescriptor, true));
                    props[i] = ref;
                }
                nestedProperty.getItem().setAnyOf(props);
            } else {
                nestedProperty.getItem().setRef(getReferenceForDescriptor(referenceDescriptor, false));
            // populateProperties(nestedProperty.getItem().getProperties(), (XMLDescriptor)mapping.getReferenceDescriptor());
            }
        } else if (next.isAbstractCompositeDirectCollectionMapping()) {
            DirectCollectionMapping mapping = (DirectCollectionMapping) next;
            XMLField field = (XMLField) mapping.getField();
            XPathFragment frag = field.getXPathFragment();
            List<String> enumeration = null;
            if (mapping.getValueConverter() instanceof JAXBEnumTypeConverter) {
                enumeration = getEnumeration((DatabaseMapping) next);
            }
            String propertyName = getNameForFragment(frag);
            if (frag.nameIsText()) {
                propertyName = (String) this.contextProperties.get(MarshallerProperties.JSON_VALUE_WRAPPER);
            }
            if (frag.isAttribute() && this.attributePrefix != null) {
                propertyName = attributePrefix + propertyName;
            }
            prop = properties.get(propertyName);
            if (prop == null) {
                prop = new Property();
                prop.setName(propertyName);
            }
            Property nestedProperty = getNestedPropertyForFragment(frag, prop);
            nestedProperty.setType(JsonType.ARRAY);
            nestedProperty.setItem(new Property());
            if (enumeration != null) {
                nestedProperty.getItem().setEnumeration(enumeration);
            }
            Class<?> type = mapping.getAttributeElementClass();
            if (type == null) {
                type = CoreClassConstants.STRING;
            }
            nestedProperty.getItem().setType(getJsonTypeForJavaType(type));
            return prop;
        } else if (next instanceof BinaryDataCollectionMapping) {
            BinaryDataCollectionMapping mapping = (BinaryDataCollectionMapping) next;
            XMLField field = (XMLField) mapping.getField();
            XPathFragment frag = field.getXPathFragment();
            String propertyName = getNameForFragment(frag);
            if (frag.isSelfFragment()) {
                propertyName = Constants.VALUE_WRAPPER;
                if (this.contextProperties != null) {
                    String valueWrapper = (String) this.contextProperties.get(JAXBContextProperties.JSON_VALUE_WRAPPER);
                    if (valueWrapper != null) {
                        propertyName = valueWrapper;
                    }
                }
            }
            if (frag.isAttribute() && this.attributePrefix != null) {
                propertyName = attributePrefix + propertyName;
            }
            prop = properties.get(propertyName);
            if (prop == null) {
                prop = new Property();
                prop.setName(propertyName);
            }
            Property nestedProperty = getNestedPropertyForFragment(frag, prop);
            nestedProperty.setType(JsonType.ARRAY);
            nestedProperty.setItem(new Property());
            if (mapping.shouldInlineBinaryData()) {
                nestedProperty.getItem().setType(JsonType.STRING);
            } else {
                nestedProperty.getItem().setAnyOf(getXopIncludeProperties());
            }
            return prop;
        }
    } else {
        if (next.isAbstractDirectMapping()) {
            // handle direct mapping
            DirectMapping directMapping = (DirectMapping) next;
            XMLField field = (XMLField) directMapping.getField();
            XPathFragment frag = field.getXPathFragment();
            List<String> enumeration = null;
            if (directMapping.getConverter() instanceof JAXBEnumTypeConverter) {
                enumeration = getEnumeration((DatabaseMapping) directMapping);
            }
            String propertyName = getNameForFragment(frag);
            if (frag.nameIsText()) {
                propertyName = Constants.VALUE_WRAPPER;
                if (this.contextProperties != null) {
                    String valueWrapper = (String) this.contextProperties.get(JAXBContextProperties.JSON_VALUE_WRAPPER);
                    if (valueWrapper != null) {
                        propertyName = valueWrapper;
                    }
                }
            }
            if (frag.isAttribute() && this.attributePrefix != null) {
                propertyName = attributePrefix + propertyName;
            }
            prop = properties.get(propertyName);
            if (prop == null) {
                prop = new Property();
                prop.setName(propertyName);
            }
            Property nestedProperty = getNestedPropertyForFragment(frag, prop);
            if (enumeration != null) {
                nestedProperty.setEnumeration(enumeration);
            }
            if (directMapping instanceof BinaryDataMapping) {
                BinaryDataMapping binaryMapping = (BinaryDataMapping) directMapping;
                if (binaryMapping.shouldInlineBinaryData() || binaryMapping.isSwaRef()) {
                    nestedProperty.setType(JsonType.STRING);
                } else {
                    if (this.xopIncludeProp == null) {
                        initXopIncludeProp();
                    }
                    nestedProperty.setAnyOf(this.xopIncludeProp);
                }
            } else {
                nestedProperty.setType(getJsonTypeForJavaType(directMapping.getAttributeClassification()));
            }
            return prop;
        } else if (next instanceof ObjectReferenceMapping) {
            ObjectReferenceMapping mapping = (ObjectReferenceMapping) next;
            Set<XMLField> sourceFields = mapping.getSourceToTargetKeyFieldAssociations().keySet();
            XMLDescriptor reference = (XMLDescriptor) mapping.getReferenceDescriptor();
            for (XMLField nextField : sourceFields) {
                XPathFragment frag = nextField.getXPathFragment();
                String propName = getNameForFragment(frag);
                XMLField targetField = (XMLField) mapping.getSourceToTargetKeyFieldAssociations().get(nextField);
                Class<?> type = CoreClassConstants.STRING;
                if (reference != null) {
                    type = getTypeForTargetField(targetField, reference);
                }
                prop = properties.get(propName);
                if (prop == null) {
                    prop = new Property();
                    prop.setName(propName);
                }
                Property nestedProperty = getNestedPropertyForFragment(frag, prop);
                // nestedProperty.setType(JsonType.ARRAY);
                // nestedProperty.setItem(new Property());
                nestedProperty.setType(getJsonTypeForJavaType(type));
                if (!properties.containsKey(prop.getName())) {
                    properties.put(prop.getName(), prop);
                }
            }
            return prop;
        } else if (next.isAbstractCompositeObjectMapping()) {
            CompositeObjectMapping mapping = (CompositeObjectMapping) next;
            XMLDescriptor nextDescriptor = (XMLDescriptor) mapping.getReferenceDescriptor();
            XMLField field = (XMLField) mapping.getField();
            XPathFragment firstFragment = field.getXPathFragment();
            if (firstFragment.isSelfFragment() || firstFragment.nameIsText()) {
                if (nextDescriptor != null) {
                    populateProperties(properties, nextDescriptor);
                }
            } else {
                String propName = getNameForFragment(firstFragment);
                prop = properties.get(propName);
                if (prop == null) {
                    prop = new Property();
                    prop.setName(propName);
                }
                // prop.setType(JsonType.OBJECT);
                prop.setName(propName);
                Property nestedProperty = getNestedPropertyForFragment(firstFragment, prop);
                XMLDescriptor referenceDescriptor = (XMLDescriptor) mapping.getReferenceDescriptor();
                if (referenceDescriptor != null && referenceDescriptor.hasInheritance()) {
                    // handle inheritence
                    // schema.setAnyOf(new Property[descriptor.getInheritancePolicy().getAllChildDescriptors().size()]);
                    List<ClassDescriptor> descriptors = getAllDescriptorsForInheritance(referenceDescriptor);
                    Property[] props = new Property[descriptors.size()];
                    for (int i = 0; i < props.length; i++) {
                        XMLDescriptor nextDesc = (XMLDescriptor) descriptors.get(i);
                        Property ref = new Property();
                        ref.setRef(getReferenceForDescriptor(nextDesc, true));
                        props[i] = ref;
                    }
                    nestedProperty.setAnyOf(props);
                } else {
                    nestedProperty.setRef(getReferenceForDescriptor(referenceDescriptor, false));
                // populateProperties(nestedProperty.getItem().getProperties(), (XMLDescriptor)mapping.getReferenceDescriptor());
                }
            // populateProperties(nestedProperty.getProperties(), nextDescriptor);
            }
        } else if (next instanceof BinaryDataMapping) {
            BinaryDataMapping binaryMapping = (BinaryDataMapping) next;
            XMLField field = (XMLField) binaryMapping.getField();
            XPathFragment frag = field.getXPathFragment();
            String propertyName = getNameForFragment(frag);
            if (frag.isSelfFragment()) {
                propertyName = Constants.VALUE_WRAPPER;
                if (this.contextProperties != null) {
                    String valueWrapper = (String) this.contextProperties.get(MarshallerProperties.JSON_VALUE_WRAPPER);
                    if (valueWrapper != null) {
                        propertyName = valueWrapper;
                    }
                }
            }
            if (frag.isAttribute() && this.attributePrefix != null) {
                propertyName = attributePrefix + propertyName;
            }
            prop = properties.get(propertyName);
            if (prop == null) {
                prop = new Property();
                prop.setName(propertyName);
            }
            Property nestedProperty = getNestedPropertyForFragment(frag, prop);
            if (binaryMapping.shouldInlineBinaryData() || binaryMapping.isSwaRef()) {
                nestedProperty.setType(JsonType.STRING);
            } else {
                if (this.xopIncludeProp == null) {
                    initXopIncludeProp();
                }
                nestedProperty.setAnyOf(this.xopIncludeProp);
            }
            return prop;
        }
    }
    return prop;
}
Also used : Set(java.util.Set) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) XPathFragment(org.eclipse.persistence.internal.oxm.XPathFragment) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) ArrayList(java.util.ArrayList) List(java.util.List) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) JAXBEnumTypeConverter(org.eclipse.persistence.jaxb.JAXBEnumTypeConverter) Property(org.eclipse.persistence.internal.jaxb.json.schema.model.Property) XMLField(org.eclipse.persistence.oxm.XMLField) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping)

Example 2 with DirectMapping

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

the class JsonSchemaGenerator method getEnumeration.

private List<String> getEnumeration(DatabaseMapping textMapping) {
    JAXBEnumTypeConverter converter = null;
    if (textMapping.isAbstractDirectMapping()) {
        converter = (JAXBEnumTypeConverter) ((DirectMapping) textMapping).getConverter();
    } else if (textMapping.isAbstractCompositeDirectCollectionMapping()) {
        converter = (JAXBEnumTypeConverter) ((DirectCollectionMapping) textMapping).getValueConverter();
    }
    if (converter == null) {
        return null;
    }
    List<String> enumeration = new ArrayList<String>();
    for (Object nextValue : converter.getAttributeToFieldValues().values()) {
        enumeration.add(nextValue.toString());
    }
    return enumeration;
}
Also used : ArrayList(java.util.ArrayList) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) JAXBEnumTypeConverter(org.eclipse.persistence.jaxb.JAXBEnumTypeConverter)

Example 3 with DirectMapping

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

the class MappingsGenerator method generateDirectEnumerationMapping.

public DirectMapping generateDirectEnumerationMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo, EnumTypeInfo enumInfo) {
    DirectMapping mapping = new XMLDirectMapping();
    initializeXMLMapping((XMLMapping) mapping, property);
    mapping.setNullValueMarshalled(true);
    mapping.setConverter(buildJAXBEnumTypeConverter(mapping, enumInfo));
    // handle null policy set via xml metadata
    if (property.isSetNullPolicy()) {
        mapping.setNullPolicy(getNullPolicyFromProperty(property, getNamespaceResolverForDescriptor(namespaceInfo)));
    } else if (property.isNillable()) {
        mapping.getNullPolicy().setNullRepresentedByXsiNil(true);
        mapping.getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
    }
    mapping.setField(getXPathForField(property, namespaceInfo, true, false));
    if (!mapping.getXPath().equals("text()")) {
        ((NullPolicy) mapping.getNullPolicy()).setSetPerformedForAbsentNode(false);
    }
    return mapping;
}
Also used : IsSetNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.IsSetNullPolicy) XmlIsSetNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlIsSetNullPolicy) NullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.NullPolicy) XmlNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlNullPolicy) XmlAbstractNullPolicy(org.eclipse.persistence.jaxb.xmlmodel.XmlAbstractNullPolicy) AbstractNullPolicy(org.eclipse.persistence.oxm.mappings.nullpolicy.AbstractNullPolicy) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping)

Example 4 with DirectMapping

use of org.eclipse.persistence.internal.oxm.mappings.DirectMapping 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 DirectMapping

use of org.eclipse.persistence.internal.oxm.mappings.DirectMapping 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

DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)17 CompositeObjectMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping)11 Field (org.eclipse.persistence.internal.oxm.mappings.Field)11 Mapping (org.eclipse.persistence.internal.oxm.mappings.Mapping)9 BinaryDataMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping)8 Descriptor (org.eclipse.persistence.internal.oxm.mappings.Descriptor)8 CoreMapping (org.eclipse.persistence.core.mappings.CoreMapping)7 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)7 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)6 BinaryDataCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping)6 CollectionReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping)6 CompositeCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping)6 DirectCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping)6 InverseReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.InverseReferenceMapping)6 ObjectReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping)6 VariableXPathObjectMapping (org.eclipse.persistence.internal.oxm.mappings.VariableXPathObjectMapping)6 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)6 XMLField (org.eclipse.persistence.oxm.XMLField)6 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)6 List (java.util.List)5