Search in sources :

Example 11 with XMLChoiceCollectionMapping

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

the class MappingsGenerator method generateChoiceCollectionMapping.

public ChoiceCollectionMapping generateChoiceCollectionMapping(Property property, Descriptor descriptor, NamespaceInfo namespace) {
    ChoiceCollectionMapping mapping = new XMLChoiceCollectionMapping();
    initializeXMLContainerMapping(mapping, property.getType().isArray());
    initializeXMLMapping((XMLChoiceCollectionMapping) mapping, property);
    JavaClass collectionType = property.getType();
    if (collectionType.isArray()) {
        JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), helper.getClassLoader());
        JavaClass componentType = collectionType.getComponentType();
        if (componentType.isArray()) {
            JavaClass baseComponentType = getBaseComponentType(componentType);
            if (baseComponentType.isPrimitive()) {
                Class<?> primitiveClass = XMLConversionManager.getDefaultManager().convertClassNameToClass(baseComponentType.getRawName());
                accessor.setComponentClass(primitiveClass);
            } else {
                accessor.setComponentClassName(baseComponentType.getQualifiedName());
            }
        } else {
            accessor.setComponentClassName(componentType.getQualifiedName());
        }
        mapping.setAttributeAccessor(accessor);
    }
    collectionType = containerClassImpl(collectionType);
    mapping.useCollectionClassName(collectionType.getRawName());
    if (property.isSetXmlElementWrapper()) {
        mapping.setWrapperNullPolicy(getWrapperNullPolicyFromProperty(property));
    }
    boolean isIdRef = property.isXmlIdRef();
    Iterator<Property> choiceProperties = property.getChoiceProperties().iterator();
    while (choiceProperties.hasNext()) {
        Property next = choiceProperties.next();
        JavaClass type = next.getType();
        JavaClass originalType = next.getType();
        Converter converter = null;
        Field xmlField = null;
        TypeInfo info = typeInfo.get(type.getName());
        if (info != null) {
            XmlJavaTypeAdapter adapter = info.getXmlJavaTypeAdapter();
            if (adapter != null) {
                String adapterValue = adapter.getValue();
                JavaClass adapterClass = helper.getJavaClass(adapterValue);
                JavaClass theClass = CompilerHelper.getTypeFromAdapterClass(adapterClass, helper);
                type = theClass;
                converter = new XMLJavaTypeConverter(adapterClass.getQualifiedName());
            }
        }
        if (next.getXmlJoinNodes() != null) {
            // handle XmlJoinNodes
            List<Field> srcFlds = new ArrayList<>();
            List<Field> tgtFlds = new ArrayList<>();
            for (XmlJoinNode xmlJoinNode : next.getXmlJoinNodes().getXmlJoinNode()) {
                srcFlds.add(new XMLField(xmlJoinNode.getXmlPath()));
                tgtFlds.add(new XMLField(xmlJoinNode.getReferencedXmlPath()));
            }
            mapping.addChoiceElement(srcFlds, type.getQualifiedName(), tgtFlds);
        } else if (isIdRef) {
            // handle IDREF
            String tgtXPath = null;
            TypeInfo referenceType = typeInfo.get(type.getQualifiedName());
            if (null != referenceType && referenceType.isIDSet()) {
                Property prop = referenceType.getIDProperty();
                tgtXPath = getXPathForField(prop, namespace, !prop.isAttribute(), false).getXPath();
            }
            // if the XPath is set (via xml-path) use it, otherwise figure it out
            Field srcXPath;
            if (next.getXmlPath() != null) {
                srcXPath = new XMLField(next.getXmlPath());
            } else {
                srcXPath = getXPathForField(next, namespace, true, false);
            }
            mapping.addChoiceElement(srcXPath.getXPath(), type.getQualifiedName(), tgtXPath);
        } else {
            Field xpath;
            if (next.getXmlPath() != null) {
                xpath = new XMLField(next.getXmlPath());
            } else {
                xpath = getXPathForField(next, namespace, (!(this.typeInfo.containsKey(type.getQualifiedName()))) || type.isEnum(), false);
            }
            xmlField = xpath;
            mapping.addChoiceElement(xpath.getName(), type.getQualifiedName());
            if (!originalType.getQualifiedName().equals(type.getQualifiedName())) {
                mapping.getClassNameToFieldMappings().putIfAbsent(originalType.getQualifiedName(), xpath);
                mapping.addConverter(xpath, converter);
            }
        }
        if (xmlField != null) {
            Mapping nestedMapping = (Mapping) mapping.getChoiceElementMappings().get(xmlField);
            if (nestedMapping.isAbstractCompositeCollectionMapping()) {
                // handle null policy set via xml metadata
                if (property.isSetNullPolicy()) {
                    ((CompositeCollectionMapping) nestedMapping).setNullPolicy(getNullPolicyFromProperty(property, getNamespaceResolverForDescriptor(namespace)));
                } else if (next.isNillable() && property.isNillable()) {
                    ((CompositeCollectionMapping) nestedMapping).getNullPolicy().setNullRepresentedByXsiNil(true);
                    ((CompositeCollectionMapping) nestedMapping).getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
                }
            } else if (nestedMapping.isAbstractCompositeDirectCollectionMapping()) {
                if (next.isSetNullPolicy()) {
                    ((DirectCollectionMapping) nestedMapping).setNullPolicy(getNullPolicyFromProperty(next, getNamespaceResolverForDescriptor(namespace)));
                } else if (next.isNillable() && property.isNillable()) {
                    ((DirectCollectionMapping) nestedMapping).getNullPolicy().setNullRepresentedByXsiNil(true);
                    ((DirectCollectionMapping) nestedMapping).getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
                }
                ((DirectCollectionMapping) nestedMapping).getNullPolicy().setNullRepresentedByEmptyNode(false);
                if (type.isEnum()) {
                    ((DirectCollectionMapping) nestedMapping).setValueConverter(buildJAXBEnumTypeConverter(nestedMapping, (EnumTypeInfo) info));
                }
            } else if (nestedMapping instanceof BinaryDataCollectionMapping) {
                if (next.isSetNullPolicy()) {
                    ((BinaryDataCollectionMapping) nestedMapping).setNullPolicy(getNullPolicyFromProperty(next, getNamespaceResolverForDescriptor(namespace)));
                } else if (next.isNillable() && property.isNillable()) {
                    ((BinaryDataCollectionMapping) nestedMapping).getNullPolicy().setNullRepresentedByXsiNil(true);
                    ((BinaryDataCollectionMapping) nestedMapping).getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
                }
            }
        }
    }
    return mapping;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) JAXBArrayAttributeAccessor(org.eclipse.persistence.internal.jaxb.many.JAXBArrayAttributeAccessor) XmlJavaTypeAdapter(org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter) ArrayList(java.util.ArrayList) XMLVariableXPathObjectMapping(org.eclipse.persistence.oxm.mappings.XMLVariableXPathObjectMapping) AnyObjectMapping(org.eclipse.persistence.internal.oxm.mappings.AnyObjectMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) XMLCompositeObjectMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping) VariableXPathCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathCollectionMapping) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) ChoiceObjectMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceObjectMapping) XMLVariableXPathCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLVariableXPathCollectionMapping) XMLAnyObjectMapping(org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) XMLChoiceObjectMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping) XMLMapping(org.eclipse.persistence.oxm.mappings.XMLMapping) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLBinaryDataMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping) VariableXPathObjectMapping(org.eclipse.persistence.internal.oxm.mappings.VariableXPathObjectMapping) DirectMapping(org.eclipse.persistence.internal.oxm.mappings.DirectMapping) CollectionReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping) CompositeObjectMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping) TransformationMapping(org.eclipse.persistence.internal.oxm.mappings.TransformationMapping) ObjectReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping) XMLAnyAttributeMapping(org.eclipse.persistence.oxm.mappings.XMLAnyAttributeMapping) XMLInverseReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLInverseReferenceMapping) InverseReferenceMapping(org.eclipse.persistence.internal.oxm.mappings.InverseReferenceMapping) XMLTransformationMapping(org.eclipse.persistence.oxm.mappings.XMLTransformationMapping) CoreMapping(org.eclipse.persistence.core.mappings.CoreMapping) AnyCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.AnyCollectionMapping) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) XMLAnyCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) Mapping(org.eclipse.persistence.internal.oxm.mappings.Mapping) XMLObjectReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping) AnyAttributeMapping(org.eclipse.persistence.internal.oxm.mappings.AnyAttributeMapping) BinaryDataMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping) XMLContainerMapping(org.eclipse.persistence.internal.oxm.mappings.XMLContainerMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) XMLCollectionReferenceMapping(org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) XMLBinaryDataCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping) BinaryDataCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) ChoiceCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.ChoiceCollectionMapping) XmlJoinNode(org.eclipse.persistence.jaxb.xmlmodel.XmlJoinNodes.XmlJoinNode) DirectCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping) XMLCompositeDirectCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping) 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) JavaClass(org.eclipse.persistence.jaxb.javamodel.JavaClass) CompositeCollectionMapping(org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) 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) XMLJavaTypeConverter(org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter)

Example 12 with XMLChoiceCollectionMapping

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

the class XmlElementsTestCases method testContainerType.

/**
 * Test setting the container class via container-type attribute.
 *
 * Positive test.
 */
public void testContainerType() {
    XMLDescriptor xDesc = ((JAXBContext) jaxbContext).getXMLContext().getDescriptor(new QName("foo"));
    assertNotNull("No descriptor was generated for Foo.", xDesc);
    DatabaseMapping mapping = xDesc.getMappingForAttributeName("items");
    assertNotNull("No mapping exists on Foo for attribute [items].", mapping);
    assertTrue("Expected an XMLChoiceCollectionMapping for attribute [items], but was [" + mapping.toString() + "].", mapping instanceof XMLChoiceCollectionMapping);
    assertTrue("Expected container class [java.util.LinkedList] but was [" + mapping.getContainerPolicy().getContainerClassName() + "]", mapping.getContainerPolicy().getContainerClassName().equals("java.util.LinkedList"));
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) QName(javax.xml.namespace.QName) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping)

Example 13 with XMLChoiceCollectionMapping

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

the class XmlElementRefWithWrapperTestCases method testContainerType.

public void testContainerType() {
    // make sure container-type was processed correctly
    XMLDescriptor xDesc = xmlContext.getDescriptor(new QName("foos"));
    assertNotNull("No descriptor was generated for Foos.", xDesc);
    DatabaseMapping mapping = xDesc.getMappingForAttributeName("items");
    assertNotNull("No mapping exists on Foos for attribute [items].", mapping);
    assertTrue("Expected an XMLChoiceCollectionMapping for attribute [items], but was [" + mapping.toString() + "].", mapping instanceof XMLChoiceCollectionMapping);
    assertTrue("Expected container class [java.util.LinkedList] but was [" + mapping.getContainerPolicy().getContainerClassName() + "]", mapping.getContainerPolicy().getContainerClassName().equals("java.util.LinkedList"));
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) QName(javax.xml.namespace.QName) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping)

Example 14 with XMLChoiceCollectionMapping

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

the class SDOProperty method buildXMLChoiceCollectionMapping.

private DatabaseMapping buildXMLChoiceCollectionMapping(String mappingUri) {
    XMLChoiceCollectionMapping mapping = new XMLChoiceCollectionMapping();
    mapping.setAttributeName(getName());
    mapping.useCollectionClass(ListWrapper.class);
    // First add XPath for this property
    String xPath = getQualifiedXPath(mappingUri, getType().isDataType());
    mapping.addChoiceElement(xPath, getType().getImplClass());
    // For each substitutable property, create the xpath and add it.
    Iterator<SDOProperty> properties = this.getSubstitutableElements().iterator();
    while (properties.hasNext()) {
        SDOProperty nextProp = properties.next();
        xPath = nextProp.getQualifiedXPath(mappingUri, nextProp.getType().isDataType(), getContainingType());
        mapping.addChoiceElement(xPath, nextProp.getType().getImplClass());
    }
    return mapping;
}
Also used : XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping)

Example 15 with XMLChoiceCollectionMapping

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

the class DBWSBuilderModelProject method buildDBWSBuilderModelDescriptor.

protected ClassDescriptor buildDBWSBuilderModelDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setJavaClass(DBWSBuilderModel.class);
    descriptor.setDefaultRootElement("dbws-builder");
    XMLCompositeCollectionMapping propertiesMapping = new XMLCompositeCollectionMapping();
    propertiesMapping.setReferenceClass(Association.class);
    propertiesMapping.setAttributeAccessor(new AttributeAccessor() {

        @Override
        public String getAttributeName() {
            return "properties";
        }

        @Override
        public Object getAttributeValueFromObject(Object object) throws DescriptorException {
            DBWSBuilderModel model = (DBWSBuilderModel) object;
            Vector<Association> associations = new Vector<Association>();
            for (Map.Entry<String, String> me : model.properties.entrySet()) {
                associations.add(new Association(me.getKey(), me.getValue()));
            }
            return associations;
        }

        @Override
        public void setAttributeValueInObject(Object object, Object value) throws DescriptorException {
            DBWSBuilderModel model = (DBWSBuilderModel) object;
            Vector<Association> associations = (Vector<Association>) value;
            for (Association a : associations) {
                model.properties.put((String) a.getKey(), (String) a.getValue());
            }
        }
    });
    propertiesMapping.setXPath("properties/property");
    descriptor.addMapping(propertiesMapping);
    XMLChoiceCollectionMapping operationsMapping = new XMLChoiceCollectionMapping();
    operationsMapping.setAttributeName("operations");
    operationsMapping.setContainerPolicy(new ListContainerPolicy(ArrayList.class));
    operationsMapping.addChoiceElement("table", TableOperationModel.class);
    operationsMapping.addChoiceElement("procedure", ProcedureOperationModel.class);
    operationsMapping.addChoiceElement("plsql-procedure", PLSQLProcedureOperationModel.class);
    operationsMapping.addChoiceElement("sql", SQLOperationModel.class);
    operationsMapping.addChoiceElement("batch-sql", BatchSQLOperationModel.class);
    descriptor.addMapping(operationsMapping);
    return descriptor;
}
Also used : DescriptorException(org.eclipse.persistence.exceptions.DescriptorException) ListContainerPolicy(org.eclipse.persistence.internal.queries.ListContainerPolicy) ArrayList(java.util.ArrayList) XMLChoiceCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Association(org.eclipse.persistence.mappings.Association) XMLCompositeCollectionMapping(org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping) AttributeAccessor(org.eclipse.persistence.mappings.AttributeAccessor) Vector(java.util.Vector)

Aggregations

XMLChoiceCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping)18 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)13 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)11 ArrayList (java.util.ArrayList)6 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)6 XMLField (org.eclipse.persistence.oxm.XMLField)6 XMLChoiceObjectMapping (org.eclipse.persistence.oxm.mappings.XMLChoiceObjectMapping)6 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)6 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)5 XMLBinaryDataCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLBinaryDataCollectionMapping)4 XMLBinaryDataMapping (org.eclipse.persistence.oxm.mappings.XMLBinaryDataMapping)4 XMLCompositeDirectCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping)4 XMLObjectReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLObjectReferenceMapping)4 QName (javax.xml.namespace.QName)3 XMLJavaTypeConverter (org.eclipse.persistence.internal.jaxb.XMLJavaTypeConverter)3 XMLAnyCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLAnyCollectionMapping)3 XMLAnyObjectMapping (org.eclipse.persistence.oxm.mappings.XMLAnyObjectMapping)3 XMLCollectionReferenceMapping (org.eclipse.persistence.oxm.mappings.XMLCollectionReferenceMapping)3 XMLMapping (org.eclipse.persistence.oxm.mappings.XMLMapping)3 XMLTransformationMapping (org.eclipse.persistence.oxm.mappings.XMLTransformationMapping)3