use of org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping in project eclipselink by eclipse-ee4j.
the class MappingsGenerator method generateChoiceCollectionMapping.
public ChoiceCollectionMapping generateChoiceCollectionMapping(Property property, Descriptor descriptor, NamespaceInfo namespace) {
ChoiceCollectionMapping mapping = new XMLChoiceCollectionMapping();
initializeXMLContainerMapping(mapping, property.getType().isArray());
initializeXMLMapping((XMLChoiceCollectionMapping) mapping, property);
JavaClass collectionType = property.getType();
if (collectionType.isArray()) {
JAXBArrayAttributeAccessor accessor = new JAXBArrayAttributeAccessor(mapping.getAttributeAccessor(), mapping.getContainerPolicy(), helper.getClassLoader());
JavaClass componentType = collectionType.getComponentType();
if (componentType.isArray()) {
JavaClass baseComponentType = getBaseComponentType(componentType);
if (baseComponentType.isPrimitive()) {
Class<?> primitiveClass = XMLConversionManager.getDefaultManager().convertClassNameToClass(baseComponentType.getRawName());
accessor.setComponentClass(primitiveClass);
} else {
accessor.setComponentClassName(baseComponentType.getQualifiedName());
}
} else {
accessor.setComponentClassName(componentType.getQualifiedName());
}
mapping.setAttributeAccessor(accessor);
}
collectionType = containerClassImpl(collectionType);
mapping.useCollectionClassName(collectionType.getRawName());
if (property.isSetXmlElementWrapper()) {
mapping.setWrapperNullPolicy(getWrapperNullPolicyFromProperty(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;
Field xmlField = 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<Field> srcFlds = new ArrayList<>();
List<Field> 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 {
Field xpath;
if (next.getXmlPath() != null) {
xpath = new XMLField(next.getXmlPath());
} else {
xpath = getXPathForField(next, namespace, (!(this.typeInfo.containsKey(type.getQualifiedName()))) || type.isEnum(), false);
}
xmlField = xpath;
mapping.addChoiceElement(xpath.getName(), type.getQualifiedName());
if (!originalType.getQualifiedName().equals(type.getQualifiedName())) {
mapping.getClassNameToFieldMappings().putIfAbsent(originalType.getQualifiedName(), xpath);
mapping.addConverter(xpath, converter);
}
}
if (xmlField != null) {
Mapping nestedMapping = (Mapping) mapping.getChoiceElementMappings().get(xmlField);
if (nestedMapping.isAbstractCompositeCollectionMapping()) {
// handle null policy set via xml metadata
if (property.isSetNullPolicy()) {
((CompositeCollectionMapping) nestedMapping).setNullPolicy(getNullPolicyFromProperty(property, getNamespaceResolverForDescriptor(namespace)));
} else if (next.isNillable() && property.isNillable()) {
((CompositeCollectionMapping) nestedMapping).getNullPolicy().setNullRepresentedByXsiNil(true);
((CompositeCollectionMapping) nestedMapping).getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
}
} else if (nestedMapping.isAbstractCompositeDirectCollectionMapping()) {
if (next.isSetNullPolicy()) {
((DirectCollectionMapping) nestedMapping).setNullPolicy(getNullPolicyFromProperty(next, getNamespaceResolverForDescriptor(namespace)));
} else if (next.isNillable() && property.isNillable()) {
((DirectCollectionMapping) nestedMapping).getNullPolicy().setNullRepresentedByXsiNil(true);
((DirectCollectionMapping) nestedMapping).getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
}
((DirectCollectionMapping) nestedMapping).getNullPolicy().setNullRepresentedByEmptyNode(false);
if (type.isEnum()) {
((DirectCollectionMapping) nestedMapping).setValueConverter(buildJAXBEnumTypeConverter(nestedMapping, (EnumTypeInfo) info));
}
} else if (nestedMapping instanceof BinaryDataCollectionMapping) {
if (next.isSetNullPolicy()) {
((BinaryDataCollectionMapping) nestedMapping).setNullPolicy(getNullPolicyFromProperty(next, getNamespaceResolverForDescriptor(namespace)));
} else if (next.isNillable() && property.isNillable()) {
((BinaryDataCollectionMapping) nestedMapping).getNullPolicy().setNullRepresentedByXsiNil(true);
((BinaryDataCollectionMapping) nestedMapping).getNullPolicy().setMarshalNullRepresentation(XMLNullRepresentationType.XSI_NIL);
}
}
}
}
return mapping;
}
use of org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping in project eclipselink by eclipse-ee4j.
the class XmlElementsTestCases method testContainerType.
/**
* Test setting the container class via container-type attribute.
*
* Positive test.
*/
public void testContainerType() {
XMLDescriptor xDesc = ((JAXBContext) jaxbContext).getXMLContext().getDescriptor(new QName("foo"));
assertNotNull("No descriptor was generated for Foo.", xDesc);
DatabaseMapping mapping = xDesc.getMappingForAttributeName("items");
assertNotNull("No mapping exists on Foo for attribute [items].", mapping);
assertTrue("Expected an XMLChoiceCollectionMapping for attribute [items], but was [" + mapping.toString() + "].", mapping instanceof XMLChoiceCollectionMapping);
assertTrue("Expected container class [java.util.LinkedList] but was [" + mapping.getContainerPolicy().getContainerClassName() + "]", mapping.getContainerPolicy().getContainerClassName().equals("java.util.LinkedList"));
}
use of org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping in project eclipselink by eclipse-ee4j.
the class XmlElementRefWithWrapperTestCases method testContainerType.
public void testContainerType() {
// make sure container-type was processed correctly
XMLDescriptor xDesc = xmlContext.getDescriptor(new QName("foos"));
assertNotNull("No descriptor was generated for Foos.", xDesc);
DatabaseMapping mapping = xDesc.getMappingForAttributeName("items");
assertNotNull("No mapping exists on Foos for attribute [items].", mapping);
assertTrue("Expected an XMLChoiceCollectionMapping for attribute [items], but was [" + mapping.toString() + "].", mapping instanceof XMLChoiceCollectionMapping);
assertTrue("Expected container class [java.util.LinkedList] but was [" + mapping.getContainerPolicy().getContainerClassName() + "]", mapping.getContainerPolicy().getContainerClassName().equals("java.util.LinkedList"));
}
use of org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping in project eclipselink by eclipse-ee4j.
the class SDOProperty method buildXMLChoiceCollectionMapping.
private DatabaseMapping buildXMLChoiceCollectionMapping(String mappingUri) {
XMLChoiceCollectionMapping mapping = new XMLChoiceCollectionMapping();
mapping.setAttributeName(getName());
mapping.useCollectionClass(ListWrapper.class);
// First add XPath for this property
String xPath = getQualifiedXPath(mappingUri, getType().isDataType());
mapping.addChoiceElement(xPath, getType().getImplClass());
// For each substitutable property, create the xpath and add it.
Iterator<SDOProperty> properties = this.getSubstitutableElements().iterator();
while (properties.hasNext()) {
SDOProperty nextProp = properties.next();
xPath = nextProp.getQualifiedXPath(mappingUri, nextProp.getType().isDataType(), getContainingType());
mapping.addChoiceElement(xPath, nextProp.getType().getImplClass());
}
return mapping;
}
use of org.eclipse.persistence.oxm.mappings.XMLChoiceCollectionMapping in project eclipselink by eclipse-ee4j.
the class DBWSBuilderModelProject method buildDBWSBuilderModelDescriptor.
protected ClassDescriptor buildDBWSBuilderModelDescriptor() {
XMLDescriptor descriptor = new XMLDescriptor();
descriptor.setJavaClass(DBWSBuilderModel.class);
descriptor.setDefaultRootElement("dbws-builder");
XMLCompositeCollectionMapping propertiesMapping = new XMLCompositeCollectionMapping();
propertiesMapping.setReferenceClass(Association.class);
propertiesMapping.setAttributeAccessor(new AttributeAccessor() {
@Override
public String getAttributeName() {
return "properties";
}
@Override
public Object getAttributeValueFromObject(Object object) throws DescriptorException {
DBWSBuilderModel model = (DBWSBuilderModel) object;
Vector<Association> associations = new Vector<Association>();
for (Map.Entry<String, String> me : model.properties.entrySet()) {
associations.add(new Association(me.getKey(), me.getValue()));
}
return associations;
}
@Override
public void setAttributeValueInObject(Object object, Object value) throws DescriptorException {
DBWSBuilderModel model = (DBWSBuilderModel) object;
Vector<Association> associations = (Vector<Association>) value;
for (Association a : associations) {
model.properties.put((String) a.getKey(), (String) a.getValue());
}
}
});
propertiesMapping.setXPath("properties/property");
descriptor.addMapping(propertiesMapping);
XMLChoiceCollectionMapping operationsMapping = new XMLChoiceCollectionMapping();
operationsMapping.setAttributeName("operations");
operationsMapping.setContainerPolicy(new ListContainerPolicy(ArrayList.class));
operationsMapping.addChoiceElement("table", TableOperationModel.class);
operationsMapping.addChoiceElement("procedure", ProcedureOperationModel.class);
operationsMapping.addChoiceElement("plsql-procedure", PLSQLProcedureOperationModel.class);
operationsMapping.addChoiceElement("sql", SQLOperationModel.class);
operationsMapping.addChoiceElement("batch-sql", BatchSQLOperationModel.class);
descriptor.addMapping(operationsMapping);
return descriptor;
}
Aggregations