use of org.eclipse.persistence.internal.oxm.mappings.Field in project eclipselink by eclipse-ee4j.
the class XMLBinaryDataCollectionMappingNodeValue method startElement.
@Override
public boolean startElement(XPathFragment xPathFragment, UnmarshalRecord unmarshalRecord, Attributes atts) {
try {
Field xmlField = (Field) xmlBinaryDataCollectionMapping.getField();
XPathFragment lastFragment = xmlField.getLastXPathFragment();
if (!lastFragment.isAttribute()) {
// set a new content handler to deal with the Include element's event.
BinaryMappingContentHandler handler = new BinaryMappingContentHandler(unmarshalRecord, this, this.xmlBinaryDataCollectionMapping);
String qnameString = xPathFragment.getLocalName();
if (xPathFragment.getPrefix() != null) {
qnameString = xPathFragment.getPrefix() + Constants.COLON + qnameString;
}
handler.startElement(xPathFragment.getNamespaceURI(), xPathFragment.getLocalName(), qnameString, atts);
XMLReader xmlReader = unmarshalRecord.getXMLReader();
xmlReader.setContentHandler(handler);
xmlReader.setLexicalHandler(handler);
} else if (lastFragment.isAttribute()) {
// handle swaRef and inline attribute cases here:
String value = atts.getValue(lastFragment.getNamespaceURI(), lastFragment.getLocalName());
Object fieldValue = null;
if (xmlBinaryDataCollectionMapping.isSwaRef()) {
if (unmarshalRecord.getUnmarshaller().getAttachmentUnmarshaller() != null) {
if (xmlBinaryDataCollectionMapping.getAttributeClassification() == XMLBinaryDataHelper.getXMLBinaryDataHelper().DATA_HANDLER) {
fieldValue = unmarshalRecord.getUnmarshaller().getAttachmentUnmarshaller().getAttachmentAsDataHandler(value);
} else {
fieldValue = unmarshalRecord.getUnmarshaller().getAttachmentUnmarshaller().getAttachmentAsByteArray(value);
}
xmlBinaryDataCollectionMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), XMLBinaryDataHelper.getXMLBinaryDataHelper().convertObject(fieldValue, xmlBinaryDataCollectionMapping.getAttributeClassification(), unmarshalRecord.getSession(), xmlBinaryDataCollectionMapping.getContainerPolicy()));
}
} else {
// value should be base64 binary string
fieldValue = ((ConversionManager) unmarshalRecord.getSession().getDatasourcePlatform().getConversionManager()).convertSchemaBase64ToByteArray(value);
xmlBinaryDataCollectionMapping.setAttributeValueInObject(unmarshalRecord.getCurrentObject(), XMLBinaryDataHelper.getXMLBinaryDataHelper().convertObject(fieldValue, xmlBinaryDataCollectionMapping.getAttributeClassification(), unmarshalRecord.getSession(), xmlBinaryDataCollectionMapping.getContainerPolicy()));
}
}
return true;
} catch (SAXException ex) {
throw XMLMarshalException.unmarshalException(ex);
}
}
use of org.eclipse.persistence.internal.oxm.mappings.Field in project eclipselink by eclipse-ee4j.
the class XMLChoiceCollectionMappingMarshalNodeValue method getNodeValueForValue.
private NodeValue getNodeValueForValue(Object value) {
if (value == null) {
Iterator<NodeValue> nodeValues = fieldToNodeValues.values().iterator();
while (nodeValues.hasNext()) {
XMLChoiceCollectionMappingUnmarshalNodeValue unmarshalNodeValue = (XMLChoiceCollectionMappingUnmarshalNodeValue) nodeValues.next();
NodeValue nextNodeValue = unmarshalNodeValue.getChoiceElementMarshalNodeValue();
if (nextNodeValue instanceof MappingNodeValue) {
Mapping nextMapping = ((MappingNodeValue) nextNodeValue).getMapping();
if (nextMapping.isAbstractCompositeCollectionMapping()) {
if (((CompositeCollectionMapping) nextMapping).getNullPolicy().isNullRepresentedByXsiNil()) {
return unmarshalNodeValue;
}
} else if (nextMapping.isAbstractCompositeDirectCollectionMapping()) {
if (((DirectCollectionMapping) nextMapping).getNullPolicy().isNullRepresentedByXsiNil()) {
return unmarshalNodeValue;
}
} else if (nextMapping instanceof BinaryDataCollectionMapping) {
if (((BinaryDataCollectionMapping) nextMapping).getNullPolicy().isNullRepresentedByXsiNil()) {
return unmarshalNodeValue;
}
}
}
}
return null;
}
Field associatedField = null;
NodeValue nodeValue = null;
if (value instanceof Root) {
Root rootValue = (Root) value;
String localName = rootValue.getLocalName();
String namespaceUri = rootValue.getNamespaceURI();
Object fieldValue = rootValue.getObject();
associatedField = getFieldForName(localName, namespaceUri);
if (associatedField == null) {
if (xmlChoiceCollectionMapping.isAny()) {
return this.anyNodeValue;
}
Class<?> theClass = fieldValue.getClass();
while (associatedField == null) {
associatedField = (Field) xmlChoiceCollectionMapping.getClassToFieldMappings().get(theClass);
if (theClass.getSuperclass() != null) {
theClass = theClass.getSuperclass();
} else {
break;
}
}
}
if (associatedField != null) {
nodeValue = this.fieldToNodeValues.get(associatedField);
}
} else {
Class<?> theClass = value.getClass();
while (associatedField == null) {
associatedField = (Field) xmlChoiceCollectionMapping.getClassToFieldMappings().get(theClass);
List<FieldNodeValue> fieldNodes = classToNodeValues.get(theClass);
nodeValue = null;
if (null != fieldNodes) {
// match also field
if (null != associatedField && fieldNodes.size() > 1) {
for (FieldNodeValue fieldNode : fieldNodes) {
if (fieldNode.getField().equals(associatedField)) {
nodeValue = fieldNode.getNodeValue();
}
}
}
if (null == nodeValue && fieldNodes.size() > 0) {
nodeValue = fieldNodes.get(0).getNodeValue();
}
}
if (theClass.getSuperclass() != null) {
theClass = theClass.getSuperclass();
} else {
break;
}
}
}
if (associatedField == null) {
// check the field associations
List<Field> sourceFields = null;
Class<?> theClass = value.getClass();
while (theClass != null) {
sourceFields = (List<Field>) xmlChoiceCollectionMapping.getClassToSourceFieldsMappings().get(theClass);
if (sourceFields != null) {
break;
}
theClass = theClass.getSuperclass();
}
if (sourceFields != null) {
associatedField = sourceFields.get(0);
nodeValue = fieldToNodeValues.get(associatedField);
}
}
if (nodeValue != null) {
return nodeValue;
}
if (associatedField != null) {
return fieldToNodeValues.get(associatedField);
}
if (xmlChoiceCollectionMapping.isMixedContent() && value instanceof String) {
// use this as a placeholder for the nodevalue for mixedcontent
return this;
}
if (xmlChoiceCollectionMapping.isAny()) {
return anyNodeValue;
}
return null;
}
use of org.eclipse.persistence.internal.oxm.mappings.Field in project eclipselink by eclipse-ee4j.
the class ReferenceResolver method getValue.
/**
* Add java doc if you understand this code.
*/
private Object getValue(final CoreAbstractSession session, final Reference reference, final CacheId primaryKey, final ErrorHandler handler) {
final Class<?> referenceTargetClass = reference.getTargetClass();
if (null == referenceTargetClass || referenceTargetClass == CoreClassConstants.OBJECT) {
for (Object entry : session.getDescriptors().values()) {
Object value = null;
final Descriptor targetDescriptor = (Descriptor) entry;
final List pkFields = targetDescriptor.getPrimaryKeyFields();
if (null != pkFields && 1 == pkFields.size()) {
Field pkField = (Field) pkFields.get(0);
pkField = (Field) targetDescriptor.getTypedField(pkField);
final Class<?> targetType = pkField.getType();
if (targetType == CoreClassConstants.STRING || targetType == CoreClassConstants.OBJECT) {
value = getValue(targetDescriptor.getJavaClass(), primaryKey);
} else {
try {
final Object[] pkValues = primaryKey.getPrimaryKey();
final Object[] convertedPkValues = new Object[pkValues.length];
for (int x = 0; x < pkValues.length; x++) {
convertedPkValues[x] = session.getDatasourcePlatform().getConversionManager().convertObject(pkValues[x], targetType);
}
value = getValue(targetDescriptor.getJavaClass(), new CacheId(convertedPkValues));
} catch (ConversionException ignored) {
}
}
if (null != value) {
return value;
}
}
}
if (primaryKey.getPrimaryKey()[0] != null) {
final XMLMarshalException e = XMLMarshalException.missingIDForIDRef(Object.class.getName(), primaryKey.getPrimaryKey());
if (handler != null) {
final SAXParseException saxParseException = new SAXParseException(e.getLocalizedMessage(), null, e);
try {
handler.warning(saxParseException);
} catch (SAXException saxException) {
throw e;
}
}
}
return null;
} else {
Object value = getValue(referenceTargetClass, primaryKey);
if (null == value) {
final CoreMapping mapping = (CoreMapping) reference.getMapping();
final CoreDescriptor targetDescriptor = mapping.getReferenceDescriptor();
if (targetDescriptor.hasInheritance()) {
final CoreInheritancePolicy inheritancePolicy = targetDescriptor.getInheritancePolicy();
final List<CoreDescriptor> childDescriptors = inheritancePolicy.getAllChildDescriptors();
for (CoreDescriptor childDescriptor : childDescriptors) {
value = getValue(childDescriptor.getJavaClass(), primaryKey);
if (null != value) {
return value;
}
}
}
}
if (value == null && (primaryKey.getPrimaryKey()[0] != null)) {
final XMLMarshalException e = XMLMarshalException.missingIDForIDRef(referenceTargetClass.getName(), primaryKey.getPrimaryKey());
if (handler != null) {
SAXParseException saxParseException = new SAXParseException(e.getLocalizedMessage(), null, e);
try {
handler.warning(saxParseException);
} catch (SAXException saxException) {
throw e;
}
}
}
return value;
}
}
use of org.eclipse.persistence.internal.oxm.mappings.Field in project eclipselink by eclipse-ee4j.
the class XMLDirectMappingNodeValue method marshalSingleValue.
@Override
public boolean marshalSingleValue(XPathFragment xPathFragment, MarshalRecord marshalRecord, Object object, Object objectValue, CoreAbstractSession session, NamespaceResolver namespaceResolver, MarshalContext marshalContext) {
Object fieldValue = xmlDirectMapping.getFieldValue(objectValue, session, marshalRecord);
// Check for a null value
if (null == fieldValue) {
// Perform marshal operations based on the null policy
return xmlDirectMapping.getNullPolicy().directMarshal(xPathFragment, marshalRecord, object, session, namespaceResolver);
} else {
QName schemaType = ((Field) xmlDirectMapping.getField()).getSchemaTypeForValue(fieldValue, session);
XPathFragment groupingFragment = null;
boolean isQName = false;
if (Constants.QNAME_QNAME.equals(schemaType)) {
// if marshalling a QName, handle grouping elements here in case namespace adjustments need
// to happen
groupingFragment = openGroupingElementsForQName((QName) fieldValue, marshalRecord);
isQName = true;
}
if (groupingFragment == null) {
groupingFragment = marshalRecord.openStartGroupingElements(namespaceResolver);
}
if (xPathFragment.hasAttribute) {
marshalRecord.attribute(xPathFragment, namespaceResolver, fieldValue, schemaType);
marshalRecord.closeStartGroupingElements(groupingFragment);
} else {
if (((Field) xmlDirectMapping.getField()).getXPathFragment().nameIsText) {
XPathNode parentNode = xPathNode.getParent();
if (parentNode.getAttributeChildren() != null) {
marshalRecord.forceValueWrapper();
}
}
Field xmlField = (Field) xmlDirectMapping.getField();
if (xmlField.isTypedTextField()) {
updateNamespaces(schemaType, marshalRecord, xmlField);
}
marshalRecord.closeStartGroupingElements(groupingFragment);
marshalRecord.characters(schemaType, fieldValue, null, xmlDirectMapping.isCDATA());
}
if (isQName) {
// check to see if the last grouping fragment was swapped
XPathFragment fragment = getLastGroupingFragment();
if (fragment != groupingFragment) {
marshalRecord.endElement(groupingFragment, namespaceResolver);
return false;
}
}
return true;
}
}
use of org.eclipse.persistence.internal.oxm.mappings.Field in project eclipselink by eclipse-ee4j.
the class XMLChoiceCollectionMapping method valueFromRow.
@Override
public Object valueFromRow(AbstractRecord row, JoinedAttributeManager joinManager, ObjectBuildingQuery sourceQuery, CacheKey cacheKey, AbstractSession executionSession, boolean isTargetProtected, Boolean[] wasCacheUsed) throws DatabaseException {
List<XMLEntry> values = ((DOMRecord) row).getValuesIndicatingNoEntry(this.getFields());
Object container = getContainerPolicy().containerInstance(values.size());
for (XMLEntry next : values) {
Field valueField = next.getXMLField();
DatabaseMapping nextMapping = (DatabaseMapping) this.choiceElementMappings.get(valueField);
if (nextMapping.isAbstractCompositeCollectionMapping()) {
XMLCompositeCollectionMapping xmlMapping = (XMLCompositeCollectionMapping) nextMapping;
Object value = xmlMapping.buildObjectFromNestedRow((AbstractRecord) next.getValue(), joinManager, sourceQuery, executionSession, isTargetProtected);
value = convertDataValueToObjectValue(value, executionSession, ((XMLRecord) row).getUnmarshaller());
getContainerPolicy().addInto(value, container, executionSession);
} else if (nextMapping instanceof XMLCompositeDirectCollectionMapping) {
XMLCompositeDirectCollectionMapping xmlMapping = (XMLCompositeDirectCollectionMapping) nextMapping;
Object value = next.getValue();
value = convertDataValueToObjectValue(value, executionSession, ((XMLRecord) row).getUnmarshaller());
getContainerPolicy().addInto(value, container, executionSession);
}
}
ArrayList<XMLMapping> processedMappings = new ArrayList<>();
for (XMLMapping mapping : choiceElementMappings.values()) {
if (((DatabaseMapping) mapping).isObjectReferenceMapping() && ((DatabaseMapping) mapping).isCollectionMapping() && !(processedMappings.contains(mapping))) {
((XMLCollectionReferenceMapping) mapping).readFromRowIntoObject(row, joinManager, ((XMLRecord) row).getCurrentObject(), cacheKey, sourceQuery, executionSession, isTargetProtected, container);
processedMappings.add(mapping);
}
}
return container;
}
Aggregations