use of org.eclipse.persistence.oxm.mappings.XMLMapping 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.oxm.mappings.XMLMapping in project eclipselink by eclipse-ee4j.
the class XMLDescriptor method initialize.
/**
* INTERNAL:
* Initialize the mappings as a separate step.
* This is done as a separate step to ensure that inheritance has been first resolved.
*/
@Override
public void initialize(AbstractSession session) throws DescriptorException {
if (this.hasInheritance()) {
((org.eclipse.persistence.internal.oxm.QNameInheritancePolicy) this.getInheritancePolicy()).setNamespaceResolver(this.getNamespaceResolver());
}
if (null != this.defaultRootElementField) {
defaultRootElementField.setNamespaceResolver(this.namespaceResolver);
defaultRootElementField.initialize();
}
if (schemaReference != null && schemaReference.getSchemaContext() != null && (schemaReference.getType() == XMLSchemaReference.COMPLEX_TYPE || schemaReference.getType() == XMLSchemaReference.SIMPLE_TYPE) && getDefaultRootElementType() == null) {
if (hasInheritance() && isChildDescriptor()) {
XMLField parentField = ((XMLDescriptor) getInheritancePolicy().getParentDescriptor()).getDefaultRootElementField();
// if this descriptor has a root element field different than it's parent set the leaf element type of the default root field
if (parentField == null || (parentField != null && defaultRootElementField != null && !defaultRootElementField.getXPathFragment().equals(parentField.getXPathFragment()))) {
setDefaultRootElementType(schemaReference.getSchemaContextAsQName(getNamespaceResolver()));
}
} else {
setDefaultRootElementType(schemaReference.getSchemaContextAsQName(getNamespaceResolver()));
}
}
if (null != primaryKeyFields) {
for (int x = 0, primaryKeyFieldsSize = this.primaryKeyFields.size(); x < primaryKeyFieldsSize; x++) {
XMLField pkField = (XMLField) this.primaryKeyFields.get(x);
pkField.setNamespaceResolver(this.namespaceResolver);
pkField.initialize();
}
}
// If defined as read-only, add to it's project's default read-only classes collection.
if (shouldBeReadOnly() && (!session.getDefaultReadOnlyClasses().contains(getJavaClass()))) {
session.getDefaultReadOnlyClasses().add(getJavaClass());
}
// Avoid repetitive initialization (this does not solve loops)
if (isInitialized(INITIALIZED) || isInvalid()) {
return;
}
setInitializationStage(INITIALIZED);
// make sure that parent mappings are initialized?
if (isChildDescriptor()) {
ClassDescriptor parentDescriptor = getInheritancePolicy().getParentDescriptor();
parentDescriptor.initialize(session);
if (parentDescriptor.hasEventManager()) {
getEventManager();
}
}
for (Enumeration<DatabaseMapping> mappingsEnum = getMappings().elements(); mappingsEnum.hasMoreElements(); ) {
DatabaseMapping mapping = mappingsEnum.nextElement();
validateMappingType(mapping);
mapping.initialize(session);
if (mapping.isObjectReferenceMapping()) {
this.hasReferenceMappings = true;
}
if (mapping instanceof XMLChoiceObjectMapping) {
XMLChoiceObjectMapping choiceMapping = ((XMLChoiceObjectMapping) mapping);
for (XMLMapping next : choiceMapping.getChoiceElementMappings().values()) {
if (((DatabaseMapping) next).isObjectReferenceMapping()) {
this.hasReferenceMappings = true;
}
}
}
if (mapping instanceof XMLChoiceCollectionMapping) {
XMLChoiceCollectionMapping choiceMapping = ((XMLChoiceCollectionMapping) mapping);
for (XMLMapping next : choiceMapping.getChoiceElementMappings().values()) {
if (((DatabaseMapping) next).isObjectReferenceMapping()) {
this.hasReferenceMappings = true;
}
}
}
// Add all the fields in the mapping to myself.
Helper.addAllUniqueToVector(getFields(), mapping.getFields());
}
// If this has inheritance then it needs to be initialized before all fields is set.
if (hasInheritance()) {
getInheritancePolicy().initialize(session);
}
// Initialize the allFields to its fields, this can be done now because the fields have been computed.
setAllFields((Vector) getFields().clone());
getObjectBuilder().initialize(session);
if (hasInterfacePolicy()) {
interfaceInitialization(session);
}
if (hasReturningPolicy()) {
getReturningPolicy().initialize(session);
}
if (eventManager != null) {
eventManager.initialize(session);
}
if (copyPolicy != null) {
copyPolicy.initialize(session);
}
getInstantiationPolicy().initialize(session);
if (getSchemaReference() != null) {
getSchemaReference().initialize(session);
}
// If a Location Accessor is set on a superclass, inherit it
if (getInheritancePolicyOrNull() != null && getInheritancePolicy().getParentDescriptor() != null) {
XMLDescriptor d = (XMLDescriptor) getInheritancePolicy().getParentDescriptor();
locationAccessor = d.getLocationAccessor();
}
if (locationAccessor != null) {
locationAccessor.initializeAttributes(getJavaClass());
}
}
use of org.eclipse.persistence.oxm.mappings.XMLMapping in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateMappingForReferenceProperty.
public Mapping generateMappingForReferenceProperty(Property property, Descriptor descriptor, NamespaceInfo namespaceInfo) {
boolean isCollection = helper.isCollectionType(property.getType()) || property.getType().isArray();
Mapping mapping;
if (isCollection) {
mapping = new XMLChoiceCollectionMapping();
initializeXMLContainerMapping((ChoiceCollectionMapping) mapping, property.getType().isArray());
JavaClass collectionType = property.getType();
collectionType = containerClassImpl(collectionType);
((ChoiceCollectionMapping) mapping).useCollectionClassName(collectionType.getRawName());
JAXBElementRootConverter jaxbERConverter = new JAXBElementRootConverter(Object.class);
if (property.isSetXmlJavaTypeAdapter()) {
JavaClass adapterClass = helper.getJavaClass(property.getXmlJavaTypeAdapter().getValue());
jaxbERConverter.setNestedConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
}
((ChoiceCollectionMapping) mapping).setConverter(jaxbERConverter);
if (property.isSetWriteOnly()) {
((ChoiceCollectionMapping) mapping).setIsWriteOnly(property.isWriteOnly());
}
if (property.isSetXmlElementWrapper()) {
((ChoiceCollectionMapping) mapping).setWrapperNullPolicy(getWrapperNullPolicyFromProperty(property));
}
} else {
mapping = new XMLChoiceObjectMapping();
JAXBElementRootConverter jaxbERConverter = new JAXBElementRootConverter(Object.class);
if (property.isSetXmlJavaTypeAdapter()) {
JavaClass adapterClass = helper.getJavaClass(property.getXmlJavaTypeAdapter().getValue());
jaxbERConverter.setNestedConverter(new XMLJavaTypeConverter(adapterClass.getQualifiedName()));
}
((ChoiceObjectMapping) mapping).setConverter(jaxbERConverter);
if (property.isSetWriteOnly()) {
((ChoiceObjectMapping) mapping).setIsWriteOnly(property.isWriteOnly());
}
}
initializeXMLMapping((XMLMapping) mapping, property);
List<ElementDeclaration> referencedElements = property.getReferencedElements();
JavaClass propertyType = property.getType();
if (propertyType.isArray()) {
JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), helper.getClassLoader());
accessor.setComponentClassName(property.getType().getComponentType().getQualifiedName());
JavaClass componentType = propertyType.getComponentType();
if (componentType.isArray()) {
Class<?> adaptedClass = classToGeneratedClasses.get(componentType.getQualifiedName());
accessor.setAdaptedClassName(adaptedClass.getName());
}
mapping.setAttributeAccessor(accessor);
}
String wrapperXPath = "";
// handle XmlElementWrapper
if (property.isSetXmlElementWrapper()) {
XmlElementWrapper wrapper = property.getXmlElementWrapper();
String namespace = wrapper.getNamespace();
if (namespace.equals(XMLProcessor.DEFAULT)) {
if (namespaceInfo.isElementFormQualified()) {
namespace = namespaceInfo.getNamespace();
} else {
namespace = "";
}
}
if (namespace.equals("")) {
wrapperXPath += (wrapper.getName() + "/");
} else {
String prefix = getPrefixForNamespace(namespace, getNamespaceResolverForDescriptor(namespaceInfo));
wrapperXPath += getQualifiedString(prefix, wrapper.getName() + "/");
}
}
if (property.isMixedContent() && isCollection) {
if (wrapperXPath.length() == 0) {
((ChoiceCollectionMapping) mapping).setMixedContent(true);
} else {
((ChoiceCollectionMapping) mapping).setMixedContent(wrapperXPath.substring(0, wrapperXPath.length() - 1));
}
}
for (ElementDeclaration element : referencedElements) {
QName elementName = element.getElementName();
JavaClass pType = element.getJavaType();
String pTypeName = element.getJavaTypeName();
boolean isBinaryType = (areEquals(pType, AnnotationsProcessor.JAVAX_ACTIVATION_DATAHANDLER) || areEquals(pType, byte[].class) || areEquals(pType, Image.class) || areEquals(pType, Source.class) || areEquals(pType, AnnotationsProcessor.JAVAX_MAIL_INTERNET_MIMEMULTIPART));
boolean isText = pType.isEnum() || (!isBinaryType && !(this.typeInfo.containsKey(element.getJavaTypeName())) && !(element.getJavaTypeName().equals(OBJECT_CLASS_NAME)));
String xPath = wrapperXPath;
Field xmlField = this.getXPathForElement(xPath, elementName, namespaceInfo, isText);
// ensure byte[] goes to base64 instead of the default hex.
if (helper.getXMLToJavaTypeMap().get(pType.getRawName()) == Constants.BASE_64_BINARY_QNAME) {
xmlField.setSchemaType(Constants.BASE_64_BINARY_QNAME);
}
if (areEquals(pType, Object.class)) {
setTypedTextField(xmlField);
}
Mapping nestedMapping;
AbstractNullPolicy nullPolicy = null;
if (isCollection) {
ChoiceCollectionMapping xmlChoiceCollectionMapping = (ChoiceCollectionMapping) mapping;
xmlChoiceCollectionMapping.addChoiceElement(xmlField, pTypeName);
nestedMapping = (Mapping) xmlChoiceCollectionMapping.getChoiceElementMappings().get(xmlField);
if (nestedMapping.isAbstractCompositeCollectionMapping()) {
((CompositeCollectionMapping) nestedMapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
nullPolicy = ((CompositeCollectionMapping) nestedMapping).getNullPolicy();
}
if (nestedMapping.isAbstractCompositeDirectCollectionMapping()) {
DirectCollectionMapping nestedCompositeDirectCollectionMapping = (DirectCollectionMapping) nestedMapping;
nullPolicy = nestedCompositeDirectCollectionMapping.getNullPolicy();
if (pType.isEnum()) {
TypeInfo enumTypeInfo = typeInfo.get(pType.getQualifiedName());
nestedCompositeDirectCollectionMapping.setValueConverter(buildJAXBEnumTypeConverter(nestedCompositeDirectCollectionMapping, (EnumTypeInfo) enumTypeInfo));
}
if (element.isList()) {
XMLListConverter listConverter = new XMLListConverter();
listConverter.setObjectClassName(pType.getQualifiedName());
((DirectCollectionMapping) nestedMapping).setValueConverter(listConverter);
}
} else if (nestedMapping instanceof BinaryDataCollectionMapping) {
nullPolicy = ((BinaryDataCollectionMapping) nestedMapping).getNullPolicy();
if (element.isList()) {
((XMLField) nestedMapping.getField()).setUsesSingleNode(true);
}
}
} else {
ChoiceObjectMapping xmlChoiceObjectMapping = (ChoiceObjectMapping) mapping;
xmlChoiceObjectMapping.addChoiceElement(xmlField, pTypeName);
nestedMapping = (Mapping) xmlChoiceObjectMapping.getChoiceElementMappings().get(xmlField);
if (pType.isEnum()) {
TypeInfo enumTypeInfo = typeInfo.get(pType.getQualifiedName());
((DirectMapping) nestedMapping).setConverter(buildJAXBEnumTypeConverter(nestedMapping, (EnumTypeInfo) enumTypeInfo));
}
if (nestedMapping.isAbstractCompositeObjectMapping()) {
((CompositeObjectMapping) nestedMapping).setKeepAsElementPolicy(UnmarshalKeepAsElementPolicy.KEEP_UNKNOWN_AS_ELEMENT);
nullPolicy = ((CompositeObjectMapping) nestedMapping).getNullPolicy();
} else if (nestedMapping instanceof BinaryDataMapping) {
nullPolicy = ((BinaryDataMapping) nestedMapping).getNullPolicy();
} else if (nestedMapping instanceof DirectMapping) {
nullPolicy = ((DirectMapping) nestedMapping).getNullPolicy();
}
}
if (nullPolicy != null) {
nullPolicy.setNullRepresentedByEmptyNode(false);
nullPolicy.setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
nullPolicy.setNullRepresentedByXsiNil(true);
nullPolicy.setIgnoreAttributesForNil(false);
}
if (!element.isXmlRootElement()) {
Class<?> scopeClass = element.getScopeClass();
if (scopeClass == jakarta.xml.bind.annotation.XmlElementDecl.GLOBAL.class) {
scopeClass = JAXBElement.GlobalScope.class;
}
Class<?> declaredType = null;
if (element.getAdaptedJavaType() != null) {
declaredType = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(element.getAdaptedJavaType().getQualifiedName(), helper.getClassLoader());
} else {
declaredType = org.eclipse.persistence.internal.helper.Helper.getClassFromClasseName(pType.getQualifiedName(), helper.getClassLoader());
}
JAXBElementConverter converter = new JAXBElementConverter(xmlField, declaredType, scopeClass);
if (isCollection) {
ChoiceCollectionMapping xmlChoiceCollectionMapping = (ChoiceCollectionMapping) mapping;
if (element.getJavaTypeAdapterClass() != null) {
converter.setNestedConverter(new XMLJavaTypeConverter(element.getJavaTypeAdapterClass().getName()));
} else {
CoreConverter originalConverter = xmlChoiceCollectionMapping.getConverter(xmlField);
converter.setNestedConverter(originalConverter);
}
xmlChoiceCollectionMapping.addConverter(xmlField, converter);
} else {
ChoiceObjectMapping xmlChoiceObjectMapping = (ChoiceObjectMapping) mapping;
if (element.getJavaTypeAdapterClass() != null) {
converter.setNestedConverter(new XMLJavaTypeConverter(element.getJavaTypeAdapterClass().getName()));
} else {
CoreConverter originalConverter = xmlChoiceObjectMapping.getConverter(xmlField);
converter.setNestedConverter(originalConverter);
}
xmlChoiceObjectMapping.addConverter(xmlField, converter);
}
}
}
if (property.isAny()) {
if (isCollection) {
XMLChoiceCollectionMapping xmlChoiceCollectionMapping = (XMLChoiceCollectionMapping) mapping;
xmlChoiceCollectionMapping.setIsAny(true);
}
}
return mapping;
}
use of org.eclipse.persistence.oxm.mappings.XMLMapping in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method logMappingGeneration.
private void logMappingGeneration(Descriptor xmlDescriptor) {
String i18nmsg = JAXBLocalization.buildMessage("create_mappings", new Object[] { xmlDescriptor.getJavaClassName() });
AbstractSessionLog.getLog().log(SessionLog.FINEST, SessionLog.MOXY, i18nmsg, new Object[0], false);
Iterator mappingIterator = xmlDescriptor.getMappings().iterator();
Mapping xmlMapping;
while (mappingIterator.hasNext()) {
xmlMapping = (Mapping) mappingIterator.next();
AbstractSessionLog.getLog().log(SessionLog.FINEST, SessionLog.MOXY, xmlMapping.toString(), new Object[0], false);
}
}
use of org.eclipse.persistence.oxm.mappings.XMLMapping in project eclipselink by eclipse-ee4j.
the class SDOProperty method addMappingToOwner.
/**
* INTERNAL:
*/
public void addMappingToOwner(boolean sdoMethodAttributeAccessor, int indexToAdd) {
if (xmlMapping != null) {
if (sdoMethodAttributeAccessor) {
SDOMethodAttributeAccessor accessor = null;
if (this.getType().isDataType()) {
Class<?> theClass = getType().getInstanceClass();
accessor = new SDOMethodAttributeAccessor(this, theClass);
} else {
accessor = new SDOMethodAttributeAccessor(this);
}
xmlMapping.setAttributeAccessor(accessor);
}
if ((getContainingType() != null) && !getContainingType().isDataType()) {
ClassDescriptor containingDescriptor = getContainingType().getXmlDescriptor();
xmlMapping.setDescriptor(containingDescriptor);
XMLMapping mapping = (XMLMapping) getContainingType().getXmlDescriptor().getMappingForAttributeName(getName());
if (mapping != null) {
getContainingType().getXmlDescriptor().getMappings().remove(mapping);
}
if (indexToAdd == -1) {
getContainingType().getXmlDescriptor().getMappings().add(xmlMapping);
} else {
// iterate over the mappings and find the correct place to insert this mapping relative to the
// indecies of the others.
SDOType containingType = getContainingType();
Vector<DatabaseMapping> mappings = containingType.getXmlDescriptor().getMappings();
boolean added = false;
for (int i = 0; i < mappings.size(); i++) {
DatabaseMapping next = mappings.get(i);
SDOProperty associatedProperty = containingType.getProperty(next.getAttributeName());
if (associatedProperty != null && indexToAdd < associatedProperty.getIndexInType()) {
mappings.add(i, xmlMapping);
added = true;
break;
}
}
if (!added) {
getContainingType().getXmlDescriptor().getMappings().add(xmlMapping);
}
}
}
}
}
Aggregations