use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class DOMUnmarshaller method xmlToObject.
/**
* INTERNAL: Convert the Oracle XMLDocument to the reference-class.
*/
public Object xmlToObject(DOMRecord xmlRow, Class<?> referenceClass) throws XMLMarshalException {
try {
// Try to get the Encoding and Version from DOM3 APIs if available
String xmlEncoding = "UTF-8";
String xmlVersion = "1.0";
try {
xmlEncoding = xmlRow.getDocument().getXmlEncoding() != null ? xmlRow.getDocument().getXmlEncoding() : xmlEncoding;
xmlVersion = xmlRow.getDocument().getXmlVersion() != null ? xmlRow.getDocument().getXmlVersion() : xmlVersion;
} catch (Exception ex) {
// if the methods aren't available, then just use the default values
}
XMLContext xmlContext = xmlUnmarshaller.getXMLContext();
// populate and return an XMLRoot
if (referenceClass != null && (XMLConversionManager.getDefaultJavaTypes().get(referenceClass) != null || CoreClassConstants.XML_GREGORIAN_CALENDAR.isAssignableFrom(referenceClass) || CoreClassConstants.DURATION.isAssignableFrom(referenceClass))) {
// we're assuming that since we're unmarshalling to a primitive
// wrapper, the root element has a single text node
Object nodeVal;
try {
Text rootTxt = (Text) xmlRow.getDOM().getFirstChild();
nodeVal = rootTxt.getNodeValue();
} catch (Exception ex) {
// here, either the root element doesn't have a text node as a
// first child, or there is no first child at all - in any case,
// try converting null
nodeVal = null;
}
Object obj = xmlContext.getSession().getDatasourcePlatform().getConversionManager().convertObject(nodeVal, referenceClass);
Root xmlRoot = new XMLRoot();
xmlRoot.setObject(obj);
String lName = xmlRow.getDOM().getLocalName();
if (lName == null) {
lName = xmlRow.getDOM().getNodeName();
}
xmlRoot.setLocalName(lName);
xmlRoot.setNamespaceURI(xmlRow.getDOM().getNamespaceURI());
xmlRoot.setEncoding(xmlEncoding);
xmlRoot.setVersion(xmlVersion);
return xmlRoot;
}
Descriptor descriptor = null;
CoreAbstractSession readSession = null;
boolean shouldWrap = true;
if (referenceClass == null) {
QName rootQName = new QName(xmlRow.getNamespaceURI(), xmlRow.getLocalName());
descriptor = xmlContext.getDescriptor(rootQName);
if (null == descriptor) {
String type = ((Element) xmlRow.getDOM()).getAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, "type");
if (null != type) {
XPathFragment typeFragment = new XPathFragment(type);
String namespaceURI = xmlRow.resolveNamespacePrefix(typeFragment.getPrefix());
typeFragment.setNamespaceURI(namespaceURI);
descriptor = xmlContext.getDescriptorByGlobalType(typeFragment);
}
} else {
if (null != descriptor.getDefaultRootElementField() && !descriptor.isResultAlwaysXMLRoot() && !xmlUnmarshaller.isResultAlwaysXMLRoot()) {
String descLocalName = descriptor.getDefaultRootElementField().getXPathFragment().getLocalName();
String localName = xmlRow.getDOM().getLocalName();
if (localName == null) {
localName = xmlRow.getDOM().getNodeName();
}
String namespaceURI = xmlRow.getDOM().getNamespaceURI();
if (descLocalName != null && descLocalName.equals(localName)) {
String descUri = descriptor.getDefaultRootElementField().getXPathFragment().getNamespaceURI();
if ((namespaceURI == null && descUri == null) || (namespaceURI != null && namespaceURI.length() == 0 && descUri == null) || (namespaceURI != null && namespaceURI.equals(descUri))) {
// found a descriptor based on root element then know we won't need to wrap in an XMLRoot
shouldWrap = false;
}
}
}
}
if (null == descriptor) {
throw XMLMarshalException.noDescriptorWithMatchingRootElement(rootQName.toString());
} else {
readSession = xmlContext.getSession(descriptor.getJavaClass());
}
} else {
// for XMLObjectReferenceMappings we need a non-shared cache, so
// try and get a Unit Of Work from the XMLContext
readSession = xmlContext.getSession(referenceClass);
descriptor = (Descriptor) readSession.getDescriptor(referenceClass);
if (descriptor == null) {
throw XMLMarshalException.descriptorNotFoundInProject(referenceClass.getName());
}
}
Object object = null;
if (null == xmlRow.getDOM().getAttributes().getNamedItemNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_NIL_ATTRIBUTE)) {
xmlRow.setUnmarshaller(xmlUnmarshaller);
xmlRow.setDocPresPolicy(xmlContext.getDocumentPreservationPolicy((AbstractSession) readSession));
XMLObjectBuilder objectBuilder = (XMLObjectBuilder) descriptor.getObjectBuilder();
ReadObjectQuery query = new ReadObjectQuery();
query.setReferenceClass(referenceClass);
query.setSession((AbstractSession) readSession);
object = objectBuilder.buildObject(query, xmlRow, null);
// resolve mapping references
xmlRow.resolveReferences(readSession, xmlUnmarshaller.getIDResolver());
}
String elementNamespaceUri = xmlRow.getDOM().getNamespaceURI();
String elementLocalName = xmlRow.getDOM().getLocalName();
if (elementLocalName == null) {
elementLocalName = xmlRow.getDOM().getNodeName();
}
String elementPrefix = xmlRow.getDOM().getPrefix();
if (shouldWrap || descriptor.isResultAlwaysXMLRoot() || isResultAlwaysXMLRoot) {
return descriptor.wrapObjectInXMLRoot(object, elementNamespaceUri, elementLocalName, elementPrefix, xmlEncoding, xmlVersion, this.isResultAlwaysXMLRoot, true, xmlUnmarshaller);
} else {
return object;
}
} finally {
xmlUnmarshaller.getStringBuffer().reset();
}
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class XPathObjectBuilder method buildRow.
@Override
public XMLRecord buildRow(XMLRecord record, Object object, CoreAbstractSession session, Marshaller marshaller, XPathFragment rootFragment) {
lazyInitialize();
XPathNode textNode = rootXPathNode.getTextNode();
List<XPathNode> nonAttributeChildren = rootXPathNode.getNonAttributeChildren();
if (null == textNode && null == nonAttributeChildren) {
return record;
}
Descriptor xmlDescriptor = (Descriptor) descriptor;
XPathNode node = rootXPathNode;
MarshalRecord marshalRecord = (MarshalRecord) record;
QName schemaType = null;
if (marshalRecord.getCycleDetectionStack().contains(object, marshaller.isEqualUsingIdenity())) {
if (cycleRecoverableClass == null) {
initCycleRecoverableClasses();
}
if (cycleRecoverableClass != null && cycleRecoverableClass.isAssignableFrom(object.getClass())) {
try {
Object jaxbMarshaller = marshaller.getProperty(Constants.JAXB_MARSHALLER);
// Create a proxy instance of CycleRecoverable$Context, a parameter to
// the onCycleDetected method
Object contextProxy = CycleRecoverableContextProxy.getProxy(cycleRecoverableContextClass, jaxbMarshaller);
// Invoke onCycleDetected method, passing in proxy, and reset
// 'object' to the returned value
Method onCycleDetectedMethod = object.getClass().getMethod(ON_CYCLE_DETECTED, cycleRecoverableContextClass);
object = PrivilegedAccessHelper.invokeMethod(onCycleDetectedMethod, object, new Object[] { contextProxy });
} catch (Exception e) {
throw XMLMarshalException.marshalException(e);
}
// Returned object might have a different descriptor
xmlDescriptor = (Descriptor) session.getDescriptor(object.getClass());
if (xmlDescriptor != null) {
node = ((ObjectBuilder) xmlDescriptor.getObjectBuilder()).getRootXPathNode();
} else {
node = null;
}
// Push new object
marshalRecord.getCycleDetectionStack().push(object);
// Write xsi:type if onCycleDetected returned an object of a type different than the one mapped
if (xmlDescriptor != descriptor) {
if (xmlDescriptor == null) {
schemaType = record.getConversionManager().schemaType(object.getClass());
} else {
schemaType = xmlDescriptor.getSchemaReference().getSchemaContextAsQName();
}
marshalRecord.writeXsiTypeAttribute(xmlDescriptor, schemaType.getNamespaceURI(), schemaType.getLocalPart(), schemaType.getPrefix(), false);
}
} else {
// Push the duplicate object anyway, so that we can get the complete cycle string
marshalRecord.getCycleDetectionStack().push(object);
throw XMLMarshalException.objectCycleDetected(marshalRecord.getCycleDetectionStack().getCycleString());
}
} else {
marshalRecord.getCycleDetectionStack().push(object);
}
NamespaceResolver namespaceResolver = null;
if (xmlDescriptor != null) {
namespaceResolver = xmlDescriptor.getNamespaceResolver();
}
MarshalContext marshalContext = null;
if (xmlDescriptor != null && xmlDescriptor.isSequencedObject()) {
SequencedObject sequencedObject = (SequencedObject) object;
marshalContext = new SequencedMarshalContext(sequencedObject.getSettings());
} else {
marshalContext = ObjectMarshalContext.getInstance();
}
if (null == nonAttributeChildren) {
textNode.marshal((MarshalRecord) record, object, session, namespaceResolver, marshaller, marshalContext, rootFragment);
} else {
if (node == null) {
// No descriptor for this object, so manually create a MappingNodeValue and marshal it
XPathNode n = new XPathNode();
CompositeObjectMapping<AbstractSession, AttributeAccessor, ContainerPolicy, Converter, ClassDescriptor, DatabaseField, XMLMarshaller, Session, UnmarshalKeepAsElementPolicy, XMLUnmarshaller, org.eclipse.persistence.oxm.record.XMLRecord> m = new XMLCompositeObjectMapping();
m.setXPath(".");
XMLCompositeObjectMappingNodeValue nv = new XMLCompositeObjectMappingNodeValue(m);
n.setMarshalNodeValue(nv);
nv.marshalSingleValue(new XPathFragment("."), marshalRecord, null, object, session, namespaceResolver, marshalContext);
} else {
for (int x = 0, size = marshalContext.getNonAttributeChildrenSize(node); x < size; x++) {
XPathNode xPathNode = (XPathNode) marshalContext.getNonAttributeChild(x, node);
xPathNode.marshal((MarshalRecord) record, object, session, namespaceResolver, marshaller, marshalContext.getMarshalContext(x), rootFragment);
}
}
}
marshalRecord.getCycleDetectionStack().pop();
return record;
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class XPathObjectBuilder method marshalAttributes.
@Override
public boolean marshalAttributes(MarshalRecord marshalRecord, Object object, CoreAbstractSession session) {
lazyInitialize();
boolean hasValue = false;
NamespaceResolver namespaceResolver = ((Descriptor) descriptor).getNamespaceResolver();
List<XPathNode> attributeChildren = rootXPathNode.getAttributeChildren();
if (null != attributeChildren) {
ObjectMarshalContext objectMarshalContext = ObjectMarshalContext.getInstance();
for (XPathNode anAttributeChildren : attributeChildren) {
hasValue = anAttributeChildren.marshal(marshalRecord, object, session, namespaceResolver, null, objectMarshalContext, null) || hasValue;
}
}
if (rootXPathNode.getAnyAttributeNode() != null) {
hasValue = rootXPathNode.getAnyAttributeNode().marshal(marshalRecord, object, session, namespaceResolver, null, ObjectMarshalContext.getInstance(), null) || hasValue;
}
List<XPathNode> selfChildren = rootXPathNode.getSelfChildren();
if (null != selfChildren) {
for (XPathNode selfXPathNode : selfChildren) {
NodeValue marshalNodeValue = selfXPathNode.getMarshalNodeValue();
if (marshalNodeValue instanceof MappingNodeValue) {
Mapping selfMapping = ((MappingNodeValue) marshalNodeValue).getMapping();
Object value = selfMapping.getAttributeValueFromObject(object);
Descriptor referenceDescriptor = (Descriptor) selfMapping.getReferenceDescriptor();
Descriptor valueDescriptor;
if (value != null && (referenceDescriptor == null || referenceDescriptor.hasInheritance())) {
valueDescriptor = (Descriptor) session.getDescriptor(value.getClass());
} else {
valueDescriptor = referenceDescriptor;
}
if (null != valueDescriptor) {
marshalRecord.addXsiTypeAndClassIndicatorIfRequired(valueDescriptor, referenceDescriptor, (Field) selfMapping.getField(), false);
}
}
selfXPathNode.marshalSelfAttributes(marshalRecord, object, session, namespaceResolver, marshalRecord.getMarshaller());
}
}
return hasValue;
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class UnmarshalRecordImpl method endDocument.
@Override
public void endDocument() throws SAXException {
if (unmarshaller.getIDResolver() != null && parentRecord == null) {
unmarshaller.getIDResolver().endDocument();
}
if (null != selfRecords) {
for (int x = 0, selfRecordsSize = selfRecords.size(); x < selfRecordsSize; x++) {
UnmarshalRecord selfRecord = selfRecords.get(x);
if (selfRecord != null) {
selfRecord.endDocument();
}
}
}
if (null != xPathNode.getSelfChildren()) {
int selfChildrenSize = xPathNode.getSelfChildren().size();
for (int x = 0; x < selfChildrenSize; x++) {
XPathNode selfNode = xPathNode.getSelfChildren().get(x);
if (null != selfNode.getNodeValue()) {
selfNode.getNodeValue().endSelfNodeValue(this, selfRecords.get(x), attributes);
}
}
}
CoreDescriptor xmlDescriptor = treeObjectBuilder.getDescriptor();
try {
// All populated containerValues need to be set on the object
if (null != populatedContainerValues) {
for (int populatedCVSize = populatedContainerValues.size(), i = populatedCVSize - 1; i >= 0; i--) {
ContainerValue cv = (populatedContainerValues.get(i));
cv.setContainerInstance(currentObject, getContainerInstance(cv, cv.isDefaultEmptyContainer()));
}
}
// Additionally if any containerValues are defaultEmptyContainerValues they need to be set to a new empty container
if (null != defaultEmptyContainerValues) {
for (int defaultEmptyCVSize = defaultEmptyContainerValues.size(), i = defaultEmptyCVSize - 1; i >= 0; i--) {
ContainerValue cv = (defaultEmptyContainerValues.get(i));
cv.setContainerInstance(currentObject, getContainerInstance(cv, cv.isDefaultEmptyContainer()));
}
}
// trigger the mapping.
if (null != nullCapableValues) {
for (int x = 0, nullValuesSize = nullCapableValues.size(); x < nullValuesSize; x++) {
nullCapableValues.get(x).setNullValue(currentObject, session);
}
}
// PROCESS TRANSFORMATION MAPPINGS
List<TransformationMapping> transformationMappings = treeObjectBuilder.getTransformationMappings();
if (null != transformationMappings) {
for (int x = 0, transformationMappingsSize = transformationMappings.size(); x < transformationMappingsSize; x++) {
TransformationMapping transformationMapping = transformationMappings.get(x);
transformationMapping.readFromRowIntoObject((XMLRecord) transformationRecord, currentObject, session, true);
}
}
Unmarshaller.Listener listener = unmarshaller.getUnmarshalListener();
if (listener != null) {
if (this.parentRecord != null) {
listener.afterUnmarshal(currentObject, parentRecord.getCurrentObject());
} else {
listener.afterUnmarshal(currentObject, null);
}
}
// HANDLE POST BUILD EVENTS
if (xmlDescriptor.hasEventManager()) {
CoreDescriptorEventManager eventManager = xmlDescriptor.getEventManager();
if (null != eventManager && eventManager.hasAnyEventListeners()) {
DescriptorEvent event = new DescriptorEvent(currentObject);
event.setSession((AbstractSession) session);
// this);
event.setRecord(null);
event.setEventCode(DescriptorEventManager.PostBuildEvent);
eventManager.executeEvent(event);
}
}
} catch (EclipseLinkException e) {
if (null == xmlReader.getErrorHandler()) {
throw e;
} else {
SAXParseException saxParseException = new SAXParseException(null, getDocumentLocator(), e);
xmlReader.getErrorHandler().error(saxParseException);
}
}
// if the object has any primary key fields set, add it to the cache
if (null != referenceResolver) {
if (null != xmlDescriptor) {
List primaryKeyFields = xmlDescriptor.getPrimaryKeyFields();
if (null != primaryKeyFields) {
int primaryKeyFieldsSize = primaryKeyFields.size();
if (primaryKeyFieldsSize > 0) {
CacheId pk = (CacheId) treeObjectBuilder.extractPrimaryKeyFromObject(currentObject, session);
for (int x = 0; x < primaryKeyFieldsSize; x++) {
Object value = pk.getPrimaryKey()[x];
if (null == value) {
Field pkField = (Field) xmlDescriptor.getPrimaryKeyFields().get(x);
pk.set(x, unmarshaller.getContext().getValueByXPath(currentObject, pkField.getXPath(), pkField.getNamespaceResolver(), Object.class));
}
}
referenceResolver.putValue(xmlDescriptor.getJavaClass(), pk, currentObject);
if (unmarshaller.getIDResolver() != null) {
try {
if (primaryKeyFieldsSize > 1) {
Map<String, Object> idWrapper = new HashMap<>();
for (int x = 0; x < primaryKeyFieldsSize; x++) {
String idName = (String) xmlDescriptor.getPrimaryKeyFieldNames().get(x);
Object idValue = pk.getPrimaryKey()[x];
idWrapper.put(idName, idValue);
}
unmarshaller.getIDResolver().bind(idWrapper, currentObject);
} else {
unmarshaller.getIDResolver().bind(pk.getPrimaryKey()[0], currentObject);
}
} catch (SAXException e) {
throw XMLMarshalException.unmarshalException(e);
}
}
}
}
}
}
if (null != parentRecord) {
reset();
}
// Set XML Location if applicable
if (xmlLocation != null && ((Descriptor) xmlDescriptor).getLocationAccessor() != null) {
((Descriptor) xmlDescriptor).getLocationAccessor().setAttributeValueInObject(getCurrentObject(), xmlLocation);
}
}
use of org.eclipse.persistence.internal.oxm.mappings.Descriptor in project eclipselink by eclipse-ee4j.
the class UnmarshalRecordImpl method initializeRecord.
@Override
public void initializeRecord(Mapping selfRecordMapping) throws SAXException {
try {
Descriptor xmlDescriptor = (Descriptor) treeObjectBuilder.getDescriptor();
if (xmlDescriptor.isSequencedObject()) {
unmarshalContext = new SequencedUnmarshalContext();
} else {
unmarshalContext = ObjectUnmarshalContext.getInstance();
}
currentObject = this.xmlReader.getCurrentObject(session, selfRecordMapping);
if (currentObject == null) {
currentObject = treeObjectBuilder.buildNewInstance();
}
if (xmlDescriptor.getLocationAccessor() != null && xmlReader.getLocator() != null) {
// Check to see if this Descriptor isLocationAware
// Store the snapshot of the current documentLocator
xmlLocation = new Locator2Impl(xmlReader.getLocator());
}
Object parentRecordCurrentObject = null;
if (null != this.parentRecord) {
parentRecordCurrentObject = parentRecord.getCurrentObject();
}
Unmarshaller.Listener xmlUnmarshalListener = unmarshaller.getUnmarshalListener();
if (null != xmlUnmarshalListener) {
if (null == this.parentRecord) {
xmlUnmarshalListener.beforeUnmarshal(currentObject, null);
} else {
xmlUnmarshalListener.beforeUnmarshal(currentObject, parentRecordCurrentObject);
}
}
if (null == parentRecord) {
this.xmlReader.newObjectEvent(currentObject, null, selfRecordMapping);
} else {
this.xmlReader.newObjectEvent(currentObject, parentRecordCurrentObject, selfRecordMapping);
}
List containerValues = treeObjectBuilder.getContainerValues();
if (null != containerValues) {
int containerSize = containerValues.size();
containerInstances = new Object[containerSize];
populatedContainerValues = new ArrayList(containerSize);
}
if (null != xPathNode.getSelfChildren()) {
int selfChildrenSize = xPathNode.getSelfChildren().size();
selfRecords = new ArrayList<>(selfChildrenSize);
for (int x = 0; x < selfChildrenSize; x++) {
NodeValue nv = xPathNode.getSelfChildren().get(x).getNodeValue();
if (null != nv) {
selfRecords.add(nv.buildSelfRecord(this, attributes));
}
}
}
} catch (EclipseLinkException e) {
if (null == xmlReader.getErrorHandler()) {
throw e;
} else {
SAXParseException saxParseException = new SAXParseException(null, getDocumentLocator(), e);
xmlReader.getErrorHandler().error(saxParseException);
}
}
}
Aggregations