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;
}
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;
}
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;
}
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;
}
Aggregations