Search in sources :

Example 1 with JAXBEnumTypeConverter

use of org.eclipse.persistence.jaxb.JAXBEnumTypeConverter 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 JAXBEnumTypeConverter

use of org.eclipse.persistence.jaxb.JAXBEnumTypeConverter 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 JAXBEnumTypeConverter

use of org.eclipse.persistence.jaxb.JAXBEnumTypeConverter in project eclipselink by eclipse-ee4j.

the class MappingsGenerator method buildJAXBEnumTypeConverter.

private JAXBEnumTypeConverter buildJAXBEnumTypeConverter(Mapping mapping, EnumTypeInfo enumInfo) {
    JAXBEnumTypeConverter converter = new JAXBEnumTypeConverter(mapping, enumInfo.getClassName(), false);
    List<String> fieldNames = enumInfo.getFieldNames();
    List<Object> xmlEnumValues = enumInfo.getXmlEnumValues();
    for (int i = 0; i < fieldNames.size(); i++) {
        converter.addConversionValue(xmlEnumValues.get(i), fieldNames.get(i));
    }
    return converter;
}
Also used : JAXBEnumTypeConverter(org.eclipse.persistence.jaxb.JAXBEnumTypeConverter)

Example 4 with JAXBEnumTypeConverter

use of org.eclipse.persistence.jaxb.JAXBEnumTypeConverter in project eclipselink by eclipse-ee4j.

the class JAXBEnumTypeConverterProject method getEmployeeDescriptor.

public ClassDescriptor getEmployeeDescriptor() {
    XMLDescriptor descriptor = new XMLDescriptor();
    descriptor.setDefaultRootElement("employee");
    descriptor.setJavaClass(Employee.class);
    XMLDirectMapping firstNameMapping = new XMLDirectMapping();
    firstNameMapping.setAttributeName("firstName");
    firstNameMapping.setXPath("first-name/text()");
    JAXBEnumTypeConverter converter = new JAXBEnumTypeConverter(firstNameMapping, "theClassName", false);
    firstNameMapping.setConverter(converter);
    descriptor.addMapping(firstNameMapping);
    return descriptor;
}
Also used : XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping) JAXBEnumTypeConverter(org.eclipse.persistence.jaxb.JAXBEnumTypeConverter)

Aggregations

JAXBEnumTypeConverter (org.eclipse.persistence.jaxb.JAXBEnumTypeConverter)4 ArrayList (java.util.ArrayList)2 DirectMapping (org.eclipse.persistence.internal.oxm.mappings.DirectMapping)2 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)2 List (java.util.List)1 Set (java.util.Set)1 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)1 Property (org.eclipse.persistence.internal.jaxb.json.schema.model.Property)1 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)1 BinaryDataCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataCollectionMapping)1 BinaryDataMapping (org.eclipse.persistence.internal.oxm.mappings.BinaryDataMapping)1 CollectionReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.CollectionReferenceMapping)1 CompositeCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeCollectionMapping)1 CompositeObjectMapping (org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping)1 DirectCollectionMapping (org.eclipse.persistence.internal.oxm.mappings.DirectCollectionMapping)1 ObjectReferenceMapping (org.eclipse.persistence.internal.oxm.mappings.ObjectReferenceMapping)1 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)1 XMLField (org.eclipse.persistence.oxm.XMLField)1 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)1