use of org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping 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.internal.oxm.mappings.CompositeObjectMapping 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);
}
use of org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateMapping.
/**
* Generate a mapping for a given Property.
*
* @return newly created mapping
*/
public Mapping generateMapping(Property property, Descriptor descriptor, JavaClass descriptorJavaClass, NamespaceInfo namespaceInfo) {
if (property.isSetXmlJavaTypeAdapter()) {
// if we are dealing with a reference, generate mapping and return
if (property.isReference()) {
return generateMappingForReferenceProperty(property, descriptor, namespaceInfo);
}
XmlJavaTypeAdapter xja = property.getXmlJavaTypeAdapter();
JavaClass adapterClass = helper.getJavaClass(xja.getValue());
JavaClass valueType = null;
String sValType = xja.getValueType();
if (sValType.equals("jakarta.xml.bind.annotation.adapters.XmlJavaTypeAdapter.DEFAULT")) {
valueType = property.getActualType();
} else {
valueType = helper.getJavaClass(xja.getValueType());
}
Mapping mapping;
boolean isArray = property.getType().isArray() && !property.getType().getRawName().equals("byte[]");
// a composite mapping
if (property.isChoice()) {
if (helper.isCollectionType(property.getType()) || property.getType().isArray()) {
mapping = generateChoiceCollectionMapping(property, descriptor, namespaceInfo);
((ChoiceCollectionMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
} else {
mapping = generateChoiceMapping(property, descriptor, namespaceInfo);
((ChoiceObjectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
}
} else if (typeInfo.containsKey(valueType.getQualifiedName())) {
TypeInfo reference = typeInfo.get(valueType.getQualifiedName());
if (helper.isCollectionType(property.getType())) {
if (reference.isEnumerationType()) {
mapping = generateEnumCollectionMapping(property, descriptor, namespaceInfo, (EnumTypeInfo) reference);
XMLJavaTypeConverter converter = new XMLJavaTypeConverter(adapterClass.getQualifiedName());
converter.setNestedConverter(((DirectCollectionMapping) mapping).getValueConverter());
((DirectCollectionMapping) mapping).setValueConverter(converter);
} else {
if (property.getVariableAttributeName() != null) {
mapping = generateVariableXPathCollectionMapping(property, descriptor, namespaceInfo, valueType);
((VariableXPathCollectionMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
} else {
mapping = generateCompositeCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo, valueType.getQualifiedName());
((CompositeCollectionMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
}
}
} else {
if (reference.isEnumerationType()) {
mapping = generateDirectEnumerationMapping(property, descriptor, namespaceInfo, (EnumTypeInfo) reference);
XMLJavaTypeConverter converter = new XMLJavaTypeConverter(adapterClass.getQualifiedName());
converter.setNestedConverter(((DirectMapping) mapping).getConverter());
((DirectMapping) mapping).setConverter(converter);
} else if (property.isInverseReference()) {
mapping = generateInverseReferenceMapping(property, descriptor, namespaceInfo);
} else {
if (property.getVariableAttributeName() != null) {
mapping = generateVariableXPathObjectMapping(property, descriptor, namespaceInfo, valueType);
((VariableXPathObjectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
} else {
mapping = generateCompositeObjectMapping(property, descriptor, namespaceInfo, valueType.getQualifiedName());
((CompositeObjectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
}
}
}
} else {
// no descriptor for value type
if (property.isAny()) {
if (helper.isCollectionType(property.getType())) {
mapping = generateAnyCollectionMapping(property, descriptor, namespaceInfo, property.isMixedContent());
((AnyCollectionMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
} else {
mapping = generateAnyObjectMapping(property, descriptor, namespaceInfo);
((AnyObjectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
}
} else if (helper.isCollectionType(property.getType()) || isArray) {
if (property.isSwaAttachmentRef() || property.isMtomAttachment()) {
mapping = generateBinaryDataCollectionMapping(property, descriptor, namespaceInfo);
((BinaryDataCollectionMapping) mapping).setValueConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
} else {
mapping = generateDirectCollectionMapping(property, descriptor, namespaceInfo);
if (adapterClass.getQualifiedName().equals(CollapsedStringAdapter.class.getName())) {
((DirectCollectionMapping) mapping).setCollapsingStringValues(true);
} else if (adapterClass.getQualifiedName().equals(NormalizedStringAdapter.class.getName())) {
((DirectCollectionMapping) mapping).setNormalizingStringValues(true);
} else {
((DirectCollectionMapping) mapping).setValueConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
}
}
} else if (property.isSwaAttachmentRef() || property.isMtomAttachment()) {
mapping = generateBinaryMapping(property, descriptor, namespaceInfo);
((BinaryDataMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
} else {
if (!property.isAttribute() && areEquals(valueType, Object.class) || property.isTyped()) {
mapping = generateCompositeObjectMapping(property, descriptor, namespaceInfo, null);
((CompositeObjectMapping) mapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
((CompositeObjectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
return mapping;
}
mapping = generateDirectMapping(property, descriptor, namespaceInfo);
if (adapterClass.getQualifiedName().equals(CollapsedStringAdapter.class.getName())) {
((DirectMapping) mapping).setCollapsingStringValues(true);
} else if (adapterClass.getQualifiedName().equals(NormalizedStringAdapter.class.getName())) {
((DirectMapping) mapping).setNormalizingStringValues(true);
} else {
((DirectMapping) mapping).setConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
}
}
}
return mapping;
}
if (property.getVariableAttributeName() != null) {
if (helper.isCollectionType(property.getType()) || property.getType().isArray() || property.isMap()) {
return generateVariableXPathCollectionMapping(property, descriptor, namespaceInfo, property.getActualType());
} else {
return generateVariableXPathObjectMapping(property, descriptor, namespaceInfo, property.getActualType());
}
}
if (property.isSetXmlJoinNodes()) {
if (helper.isCollectionType(property.getType())) {
return generateXMLCollectionReferenceMapping(property, descriptor, namespaceInfo, property.getActualType());
}
return generateXMLObjectReferenceMapping(property, descriptor, namespaceInfo, property.getType());
}
if (property.isXmlTransformation()) {
return generateTransformationMapping(property, descriptor, namespaceInfo);
}
if (property.isChoice()) {
if (helper.isCollectionType(property.getType()) || property.getType().isArray()) {
return generateChoiceCollectionMapping(property, descriptor, namespaceInfo);
}
return generateChoiceMapping(property, descriptor, namespaceInfo);
}
if (property.isInverseReference()) {
return generateInverseReferenceMapping(property, descriptor, namespaceInfo);
}
if (property.isReference()) {
return generateMappingForReferenceProperty(property, descriptor, namespaceInfo);
}
if (property.isAny()) {
if (helper.isCollectionType(property.getType()) || property.getType().isArray()) {
return generateAnyCollectionMapping(property, descriptor, namespaceInfo, property.isMixedContent());
}
return generateAnyObjectMapping(property, descriptor, namespaceInfo);
}
if (property.isMap()) {
if (property.isAnyAttribute()) {
return generateAnyAttributeMapping(property, descriptor, namespaceInfo);
}
return generateCompositeCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo, null);
}
if (helper.isCollectionType(property.getType())) {
return generateCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo);
}
JavaClass referenceClass = property.getType();
String referenceClassName = referenceClass.getRawName();
if (referenceClass.isArray() && !referenceClassName.equals("byte[]")) {
JavaClass componentType = referenceClass.getComponentType();
TypeInfo reference = typeInfo.get(componentType.getName());
if (reference != null && reference.isEnumerationType()) {
return generateEnumCollectionMapping(property, descriptor, namespaceInfo, (EnumTypeInfo) reference);
}
if (areEquals(componentType, Object.class)) {
CompositeCollectionMapping mapping = generateCompositeCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo, null);
mapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
return mapping;
}
if (reference != null || componentType.isArray()) {
if (property.isXmlIdRef() || property.isSetXmlJoinNodes()) {
return generateXMLCollectionReferenceMapping(property, descriptor, namespaceInfo, componentType);
}
return generateCompositeCollectionMapping(property, descriptor, descriptorJavaClass, namespaceInfo, componentType.getQualifiedName());
}
return generateDirectCollectionMapping(property, descriptor, namespaceInfo);
}
if (property.isXmlIdRef()) {
return generateXMLObjectReferenceMapping(property, descriptor, namespaceInfo, referenceClass);
}
TypeInfo reference = typeInfo.get(referenceClass.getQualifiedName());
if (reference != null) {
if (reference.isEnumerationType()) {
return generateDirectEnumerationMapping(property, descriptor, namespaceInfo, (EnumTypeInfo) reference);
}
if (property.isXmlLocation()) {
CompositeObjectMapping locationMapping = generateCompositeObjectMapping(property, descriptor, namespaceInfo, referenceClass.getQualifiedName());
reference.getDescriptor().setInstantiationPolicy(new NullInstantiationPolicy());
descriptor.setLocationAccessor(locationMapping.getAttributeAccessor());
return locationMapping;
} else {
return generateCompositeObjectMapping(property, descriptor, namespaceInfo, referenceClass.getQualifiedName());
}
}
if (property.isSwaAttachmentRef() || property.isMtomAttachment()) {
return generateBinaryMapping(property, descriptor, namespaceInfo);
}
if (referenceClass.getQualifiedName().equals(OBJECT_CLASS_NAME) && !property.isAttribute() || property.isTyped()) {
CompositeObjectMapping coMapping = generateCompositeObjectMapping(property, descriptor, namespaceInfo, null);
coMapping.setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
return coMapping;
}
if (property.isXmlLocation()) {
return null;
}
return generateDirectMapping(property, descriptor, namespaceInfo);
}
use of org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateMappingForType.
private Mapping generateMappingForType(JavaClass theType, String attributeName) {
Mapping mapping;
boolean typeIsObject = theType.getRawName().equals(OBJECT_CLASS_NAME);
TypeInfo info = typeInfo.get(theType.getQualifiedName());
if ((info != null && !(info.isEnumerationType())) || typeIsObject) {
mapping = new XMLCompositeObjectMapping();
mapping.setAttributeName(attributeName);
((CompositeObjectMapping) mapping).setXPath(attributeName);
if (typeIsObject) {
((CompositeObjectMapping) mapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
setTypedTextField((Field) mapping.getField());
} else {
((CompositeObjectMapping) mapping).setReferenceClassName(theType.getQualifiedName());
}
} else if (theType.isArray() || helper.isCollectionType(theType)) {
DirectCollectionMapping directCollectionMapping;
mapping = directCollectionMapping = new XMLCompositeDirectCollectionMapping();
initializeXMLContainerMapping(directCollectionMapping, theType.isArray());
directCollectionMapping.setAttributeName(attributeName);
if (theType.isArray()) {
JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(directCollectionMapping.getAttributeAccessor(), directCollectionMapping.getContainerPolicy(), helper.getClassLoader());
String componentClassName = theType.getComponentType().getQualifiedName();
if (theType.getComponentType().isPrimitive()) {
Class<Object> primitiveClass = XMLConversionManager.getDefaultManager().convertClassNameToClass(componentClassName);
accessor.setComponentClass(primitiveClass);
directCollectionMapping.setAttributeAccessor(accessor);
Class<Object> declaredClass = XMLConversionManager.getObjectClass(primitiveClass);
directCollectionMapping.setAttributeElementClass(declaredClass);
} else {
accessor.setComponentClassName(componentClassName);
directCollectionMapping.setAttributeAccessor(accessor);
JavaClass componentType = theType.getComponentType();
Class<?> declaredClass = PrivilegedAccessHelper.callDoPrivilegedWithException(() -> PrivilegedAccessHelper.getClassForName(componentType.getRawName(), false, helper.getClassLoader()), (ex) -> JAXBException.classNotFoundException(componentType.getRawName()));
directCollectionMapping.setAttributeElementClass(declaredClass);
}
} else if (helper.isCollectionType(theType)) {
Collection args = theType.getActualTypeArguments();
if (args.size() > 0) {
JavaClass itemType = (JavaClass) args.iterator().next();
Class<?> declaredClass = PrivilegedAccessHelper.callDoPrivilegedWithException(() -> PrivilegedAccessHelper.getClassForName(itemType.getRawName(), false, helper.getClassLoader()), (ex) -> JAXBException.classNotFoundException(itemType.getRawName()));
if (declaredClass != String.class) {
directCollectionMapping.setAttributeElementClass(declaredClass);
}
}
}
theType = containerClassImpl(theType);
directCollectionMapping.useCollectionClassName(theType.getRawName());
directCollectionMapping.setXPath(attributeName + TXT);
} else {
mapping = new XMLDirectMapping();
mapping.setAttributeName(attributeName);
((DirectMapping) mapping).setNullValueMarshalled(true);
((DirectMapping) mapping).setXPath(attributeName + TXT);
QName schemaType = userDefinedSchemaTypes.get(theType.getQualifiedName());
if (schemaType == null) {
schemaType = helper.getXMLToJavaTypeMap().get(theType.getName());
}
((Field) mapping.getField()).setSchemaType(schemaType);
if (info != null && info.isEnumerationType()) {
((DirectMapping) mapping).setConverter(buildJAXBEnumTypeConverter(mapping, (EnumTypeInfo) info));
}
}
return mapping;
}
use of org.eclipse.persistence.internal.oxm.mappings.CompositeObjectMapping in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateCompositeObjectMapping.
public CompositeObjectMapping generateCompositeObjectMapping(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo, String referenceClassName) {
CompositeObjectMapping mapping = new XMLCompositeObjectMapping();
initializeXMLMapping((XMLMapping) mapping, property);
// if the XPath is set (via xml-path) use it; otherwise figure it out
mapping.setField(getXPathForField(property, namespaceInfo, false, false));
// handle null policy set via xml metadata
if (property.isSetNullPolicy()) {
mapping.setNullPolicy(getNullPolicyFromProperty(property, getNamespaceResolverForDescriptor(namespaceInfo)));
} else {
NullPolicy nullPolicy = (NullPolicy) mapping.getNullPolicy();
nullPolicy.setSetPerformedForAbsentNode(false);
if (property.isNillable()) {
nullPolicy.setNullRepresentedByXsiNil(true);
nullPolicy.setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
}
}
if (referenceClassName == null) {
setTypedTextField((Field) mapping.getField());
String defaultValue = property.getDefaultValue();
if (null != defaultValue) {
mapping.setConverter(new DefaultElementConverter(defaultValue));
}
} else {
mapping.setReferenceClassName(referenceClassName);
}
if (property.isTransientType()) {
mapping.setReferenceClassName(Constants.UNKNOWN_OR_TRANSIENT_CLASS);
}
if (property.isRequired()) {
((Field) mapping.getField()).setRequired(true);
}
return mapping;
}
Aggregations