Search in sources :

Example 1 with XMLField

use of org.eclipse.persistence.oxm.XMLField in project aai-aai-common by onap.

the class MoxyStrategy method init.

private void init() {
    isInitialized = true;
    Set<String> props = new LinkedHashSet<>();
    for (String s : internalType.getPropertiesNames()) {
        String value = caseFormatStore.fromLowerCamelToLowerHyphen(s).orElseGet(() -> {
            LOGGER.debug("Unable to find {} in the store from lower camel to lower hyphen", s);
            return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_HYPHEN, s);
        });
        props.add(value);
    }
    props = Collections.unmodifiableSet(props);
    this.properties = props;
    Set<String> requiredProps = new LinkedHashSet<>();
    for (DatabaseMapping dm : cd.getMappings()) {
        if (dm.getField() instanceof XMLField) {
            XMLField x = (XMLField) dm.getField();
            if (x != null && x.isRequired()) {
                requiredProps.add(this.removeXPathDescriptor(x.getName()));
            }
        }
    }
    requiredProps = Collections.unmodifiableSet(requiredProps);
    this.requiredProperties = requiredProps;
    Set<String> keys = new LinkedHashSet<>();
    for (String name : internalType.getDescriptor().getPrimaryKeyFieldNames()) {
        keys.add(this.removeXPathDescriptor(name));
    }
    keys = Collections.unmodifiableSet(keys);
    this.keys = keys;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) DatabaseMapping(org.eclipse.persistence.mappings.DatabaseMapping)

Example 2 with XMLField

use of org.eclipse.persistence.oxm.XMLField 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 3 with XMLField

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

the class JsonSchemaGenerator method generateSchema.

public JsonSchema generateSchema(Class<?> rootClass) {
    this.rootClass = rootClass;
    schema = new JsonSchema();
    schema.setTitle(rootClass.getName());
    if (rootClass.isEnum()) {
        Class<?> generatedWrapper = jaxbContext.getClassToGeneratedClasses().get(rootClass.getName());
        if (generatedWrapper != null) {
            rootClass = generatedWrapper;
        } else {
            schema.setType(JsonType.STRING);
            return schema;
        }
    }
    // Check for simple type
    JsonType rootType = getJsonTypeForJavaType(rootClass);
    if (rootType != JsonType.OBJECT) {
        if (rootType == JsonType.BINARYTYPE) {
            schema.setAnyOf(getXopIncludeProperties());
            return schema;
        } else {
            schema.setType(rootType);
            return schema;
        }
    }
    Map<String, Property> properties = null;
    // Check for root level list or array
    if (rootClass.isArray() || isCollection(rootClass)) {
        schema.setType(JsonType.ARRAY);
        schema.setItems(new Property());
        Class<?> itemType = Object.class;
        if (rootClass.isArray()) {
            itemType = rootClass.getComponentType();
        } else {
            Type pType = rootClass.getGenericSuperclass();
            if (pType instanceof ParameterizedType) {
                itemType = (Class<?>) ((ParameterizedType) pType).getActualTypeArguments()[0];
            }
        }
        rootType = getJsonTypeForJavaType(itemType);
        schema.getItems().setType(rootType);
        if (rootType != JsonType.OBJECT) {
            return schema;
        }
        rootClass = itemType;
        properties = schema.getItems().getProperties();
    } else {
        schema.setType(JsonType.OBJECT);
        properties = schema.getProperties();
    }
    this.project = this.xmlContext.getSession(rootClass).getProject();
    XMLDescriptor descriptor = (XMLDescriptor) project.getDescriptor(rootClass);
    Boolean includeRoot = Boolean.TRUE;
    if (contextProperties != null) {
        includeRoot = (Boolean) this.contextProperties.get(JAXBContextProperties.JSON_INCLUDE_ROOT);
        if (includeRoot == null) {
            includeRoot = Boolean.TRUE;
        }
    }
    if (Boolean.TRUE.equals(includeRoot)) {
        XMLField field = descriptor.getDefaultRootElementField();
        if (field != null) {
            rootProperty = new Property();
            rootProperty.setType(JsonType.OBJECT);
            rootProperty.setName(getNameForFragment(field.getXPathFragment()));
            properties.put(rootProperty.getName(), rootProperty);
            properties = rootProperty.getProperties();
        }
    }
    boolean allowsAdditionalProperties = hasAnyMappings(descriptor);
    if (descriptor.hasInheritance()) {
        // handle inheritence
        // schema.setAnyOf(new Property[descriptor.getInheritancePolicy().getAllChildDescriptors().size()]);
        List<ClassDescriptor> descriptors = this.getAllDescriptorsForInheritance(descriptor);
        Property[] props = new Property[descriptors.size()];
        for (int i = 0; i < props.length; i++) {
            XMLDescriptor nextDescriptor = (XMLDescriptor) descriptors.get(i);
            Property ref = new Property();
            ref.setRef(getReferenceForDescriptor(nextDescriptor, true));
            props[i] = ref;
        }
        if (rootProperty != null) {
            rootProperty.setAnyOf(props);
            rootProperty.setProperties(null);
            rootProperty.setType(null);
            rootProperty.setAdditionalProperties(null);
            rootProperty.setAdditionalProperties(null);
        } else {
            this.schema.setAnyOf(props);
            this.schema.setProperties(null);
            this.schema.setType(null);
            this.schema.setAdditionalProperties(null);
        }
    } else {
        JsonType type = populateProperties(properties, descriptor);
        if (type != null) {
            if (type == JsonType.BINARYTYPE) {
                if (rootProperty != null) {
                    rootProperty.setAnyOf(getXopIncludeProperties());
                    rootProperty.setProperties(null);
                    rootProperty.setAdditionalProperties(null);
                    rootProperty.setType(null);
                } else {
                    this.schema.setAnyOf(getXopIncludeProperties());
                    this.schema.setProperties(null);
                    this.schema.setType(null);
                    this.schema.setAdditionalProperties(null);
                }
            } else if (type == JsonType.ENUMTYPE) {
                if (rootProperty != null) {
                    rootProperty.setType(JsonType.STRING);
                    rootProperty.setProperties(null);
                    rootProperty.setEnumeration(getEnumeration(descriptor));
                } else {
                    this.schema.setType(JsonType.STRING);
                    this.schema.setProperties(null);
                    this.schema.setEnumeration(getEnumeration(descriptor));
                }
            } else {
                if (rootProperty != null) {
                    rootProperty.setType(type);
                } else {
                    schema.setType(type);
                }
            }
        } else {
            if (rootProperty != null) {
                rootProperty.setAdditionalProperties(allowsAdditionalProperties);
            } else {
                this.schema.setAdditionalProperties(allowsAdditionalProperties);
            }
        }
    }
    return schema;
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) JsonType(org.eclipse.persistence.internal.jaxb.json.schema.model.JsonType) ClassDescriptor(org.eclipse.persistence.descriptors.ClassDescriptor) JsonSchema(org.eclipse.persistence.internal.jaxb.json.schema.model.JsonSchema) ParameterizedType(java.lang.reflect.ParameterizedType) JsonType(org.eclipse.persistence.internal.jaxb.json.schema.model.JsonType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) XMLDescriptor(org.eclipse.persistence.oxm.XMLDescriptor) Property(org.eclipse.persistence.internal.jaxb.json.schema.model.Property)

Example 4 with XMLField

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

the class PersonCustomizer method customize.

@Override
public void customize(ClassDescriptor descriptor) throws Exception {
    XMLDirectMapping nameMapping = (XMLDirectMapping) descriptor.getMappingForAttributeName("name");
    XMLField nameField = (XMLField) nameMapping.getField();
    nameField.setXPath("contact-info/personal-info/name/text()");
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping)

Example 5 with XMLField

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

the class MyEmployeeCustomizer method customize.

@Override
public void customize(ClassDescriptor descriptor) throws Exception {
    XMLDirectMapping firstNameMapping = (XMLDirectMapping) descriptor.getMappingForAttributeName("firstName");
    XMLField fnxField = (XMLField) firstNameMapping.getField();
    fnxField.setXPath("first-name/text()");
    XMLDirectMapping lastNameMapping = (XMLDirectMapping) descriptor.getMappingForAttributeName("lastName");
    XMLField lnxField = (XMLField) lastNameMapping.getField();
    lnxField.setXPath("last-name/text()");
}
Also used : XMLField(org.eclipse.persistence.oxm.XMLField) XMLDirectMapping(org.eclipse.persistence.oxm.mappings.XMLDirectMapping)

Aggregations

XMLField (org.eclipse.persistence.oxm.XMLField)302 XMLDescriptor (org.eclipse.persistence.oxm.XMLDescriptor)141 XMLDirectMapping (org.eclipse.persistence.oxm.mappings.XMLDirectMapping)99 XMLCompositeObjectMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeObjectMapping)48 QName (javax.xml.namespace.QName)43 Element (org.w3c.dom.Element)41 NodeList (org.w3c.dom.NodeList)39 NamespaceResolver (org.eclipse.persistence.oxm.NamespaceResolver)36 XMLCompositeCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeCollectionMapping)30 ArrayList (java.util.ArrayList)27 Node (org.w3c.dom.Node)25 ClassDescriptor (org.eclipse.persistence.descriptors.ClassDescriptor)22 DatabaseField (org.eclipse.persistence.internal.helper.DatabaseField)21 DatabaseMapping (org.eclipse.persistence.mappings.DatabaseMapping)17 XMLRecord (org.eclipse.persistence.oxm.record.XMLRecord)17 XPathFragment (org.eclipse.persistence.internal.oxm.XPathFragment)15 JavaClass (org.eclipse.persistence.jaxb.javamodel.JavaClass)15 XMLCompositeDirectCollectionMapping (org.eclipse.persistence.oxm.mappings.XMLCompositeDirectCollectionMapping)15 AttributeAccessor (org.eclipse.persistence.mappings.AttributeAccessor)14 Field (org.eclipse.persistence.internal.oxm.mappings.Field)13