use of org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter 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.jaxb.xmlmodel.XmlJavaTypeAdapter in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateChoiceMapping.
public ChoiceObjectMapping generateChoiceMapping(Property property, Descriptor descriptor, NamespaceInfo namespace) {
XMLChoiceObjectMapping mapping = new XMLChoiceObjectMapping();
initializeXMLMapping(mapping, 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;
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<XMLField> srcFlds = new ArrayList<>();
List<XMLField> 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 {
XMLField xpath;
if (next.getXmlPath() != null) {
xpath = new XMLField(next.getXmlPath());
} else {
xpath = (XMLField) getXPathForField(next, namespace, (!(this.typeInfo.containsKey(type.getQualifiedName()))) || next.isMtomAttachment() || type.isEnum(), false);
}
mapping.addChoiceElement(xpath, type.getQualifiedName());
if (!originalType.getQualifiedName().equals(type.getQualifiedName())) {
mapping.getClassNameToFieldMappings().putIfAbsent(originalType.getQualifiedName(), xpath);
mapping.addConverter(xpath, converter);
}
XMLMapping nestedMapping = mapping.getChoiceElementMappings().get(xpath);
if (nestedMapping instanceof BinaryDataMapping) {
((BinaryDataMapping) nestedMapping).getNullPolicy().setNullRepresentedByEmptyNode(false);
}
if (type.isEnum()) {
if (nestedMapping.isAbstractDirectMapping()) {
((DirectMapping) nestedMapping).setConverter(buildJAXBEnumTypeConverter(nestedMapping, (EnumTypeInfo) info));
}
}
}
}
return mapping;
}
use of org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter in project eclipselink by eclipse-ee4j.
the class DynamicXMLMetadataSource method createJAXBType.
/**
* Create a javaType to be used by JAXB to map a particular class.
* For static classes, JAXB annotations, xml and defaults will be used to map the class.
* For Dynamic classes we create properties for each JPA mapping on the class
* thing we create is a
*/
private JavaType createJAXBType(ClassDescriptor classDescriptor, ObjectFactory objectFactory) {
JavaType javaType = new JavaType();
String alias = classDescriptor.getAlias();
if (alias == null || alias.isEmpty()) {
alias = classDescriptor.getJavaClass().getSimpleName();
}
javaType.setName(alias);
javaType.setJavaAttributes(new JavaAttributes());
boolean isDynamic = DynamicEntity.class.isAssignableFrom(classDescriptor.getJavaClass());
for (DatabaseMapping ormMapping : classDescriptor.getMappings()) {
JAXBElement<XmlElement> element = createJAXBProperty(ormMapping, objectFactory, javaType, isDynamic);
if (element != null) {
javaType.getJavaAttributes().getJavaAttribute().add(element);
}
}
// Embeddables don't need Rest adapters, return if the classDescriptor is an aggregate descriptor.
if (classDescriptor.isAggregateDescriptor()) {
return javaType;
}
// Set an adapter that is a subclass of ReferenceAdapter that can adapt the class to create a link for
// the persistence_href field that has been weaved in.
String name = RestAdapterClassWriter.constructClassNameForReferenceAdapter(classDescriptor.getJavaClassName());
XmlJavaTypeAdapter adapter = new XmlJavaTypeAdapter();
adapter.setValue(name);
adapter.setValueType(classDescriptor.getJavaClassName());
adapter.setType(classDescriptor.getJavaClassName());
javaType.setXmlJavaTypeAdapter(adapter);
return javaType;
}
use of org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter in project eclipselink by eclipse-ee4j.
the class DynamicXmlV2MetadataSource method createProperty.
/**
* Create a JAXB property for given reference mapping.
*/
private JAXBElement<XmlElement> createProperty(ForeignReferenceMapping mapping, ObjectFactory objectFactory) {
final String referenceClassName = mapping.getReferenceClassName();
final XmlElement xmlElement = new XmlElement();
xmlElement.setJavaAttribute(mapping.getAttributeName());
xmlElement.setType(referenceClassName);
final String adapterName = RestReferenceAdapterV2ClassWriter.getClassName(referenceClassName);
final XmlJavaTypeAdapter adapter = new XmlJavaTypeAdapter();
adapter.setValue(adapterName);
adapter.setType(referenceClassName);
xmlElement.setXmlJavaTypeAdapter(adapter);
return objectFactory.createXmlElement(xmlElement);
}
use of org.eclipse.persistence.jaxb.xmlmodel.XmlJavaTypeAdapter in project eclipselink by eclipse-ee4j.
the class DynamicXmlV2MetadataSource method createCollectionProperty.
/**
* Create a JAXB property for given collection mapping.
*/
private JAXBElement<XmlElement> createCollectionProperty(DatabaseMapping mapping, ObjectFactory objectFactory) {
final XmlElement xmlElement = new XmlElement();
xmlElement.setJavaAttribute(mapping.getAttributeName());
xmlElement.setType(((CollectionMapping) mapping).getReferenceClassName());
final XmlJavaTypeAdapter adapter = new XmlJavaTypeAdapter();
final String adapterName = RestCollectionAdapterClassWriter.getClassName(((CollectionMapping) mapping).getReferenceClassName());
adapter.setValue(adapterName);
adapter.setType(Collection.class.getName());
xmlElement.setXmlJavaTypeAdapter(adapter);
return objectFactory.createXmlElement(xmlElement);
}
Aggregations