use of org.eclipse.persistence.internal.oxm.XMLObjectBuilder in project eclipselink by eclipse-ee4j.
the class EISCompositeObjectMapping method writeFromObjectIntoRow.
/**
* INTERNAL:
* Build the value for the database field and put it in the
* specified database row.
*/
@Override
public void writeFromObjectIntoRow(Object object, AbstractRecord record, AbstractSession session, WriteType writeType) throws DescriptorException {
if (this.isReadOnly()) {
return;
}
Object attributeValue = this.getAttributeValueFromObject(object);
// handle "." xpath - condition: xml data format AND xml field is "self"
if ((((EISDescriptor) getDescriptor()).isXMLFormat()) && ((XMLField) getField()).isSelfField()) {
XMLObjectBuilder objectBuilder = (XMLObjectBuilder) getReferenceDescriptor(attributeValue, session).getObjectBuilder();
objectBuilder.buildIntoNestedRow(record, attributeValue, session);
} else {
Object fieldValue = null;
if (attributeValue != null) {
fieldValue = buildCompositeRow(attributeValue, session, record, writeType);
}
record.put(this.getField(), fieldValue);
}
}
use of org.eclipse.persistence.internal.oxm.XMLObjectBuilder in project eclipselink by eclipse-ee4j.
the class XMLInteraction method createXMLRecord.
/**
* INTERNAL:
* Use the createRecord method on ObjectBuilder in case the root element is namespace qualified
*/
protected XMLRecord createXMLRecord(String rootName) {
XMLRecord xmlRec;
if (getQuery().getDescriptor() != null && getQuery().getDescriptor() instanceof EISDescriptor) {
xmlRec = (XMLRecord) ((XMLObjectBuilder) this.getQuery().getDescriptor().getObjectBuilder()).createRecord(getInputRootElementName(), getQuery().getSession());
} else {
xmlRec = new org.eclipse.persistence.oxm.record.DOMRecord(getInputRootElementName());
xmlRec.setSession(getQuery().getSession());
}
return xmlRec;
}
use of org.eclipse.persistence.internal.oxm.XMLObjectBuilder 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.XMLObjectBuilder in project eclipselink by eclipse-ee4j.
the class XMLMarshaller method objectToXML.
/**
* INTERNAL:
* Convert the given object to an XML Document
* @param object the object to marshal
* @return the document which the specified object has been marshalled to
* @param descriptor the XMLDescriptor for the object being marshalled
* @throws XMLMarshalException if an error occurred during marshalling
*/
@Override
protected Document objectToXML(Object object, XMLDescriptor descriptor, boolean isXMLRoot) throws XMLMarshalException {
AbstractSession session = context.getSession(descriptor);
DocumentPreservationPolicy docPresPolicy = context.getDocumentPreservationPolicy(session);
if (docPresPolicy != null && docPresPolicy.shouldPreserveDocument()) {
XMLRecord xmlRow = null;
if (!isXMLRoot) {
xmlRow = (XMLRecord) ((XMLObjectBuilder) descriptor.getObjectBuilder()).createRecordFor(object, context.getDocumentPreservationPolicy(session));
xmlRow.setMarshaller(this);
if (this.attachmentMarshaller != null) {
xmlRow.setXOPPackage(this.attachmentMarshaller.isXOPPackage());
}
addDescriptorNamespacesToXMLRecord(descriptor, xmlRow);
}
return objectToXML(object, descriptor, xmlRow, isXMLRoot, docPresPolicy);
}
return super.objectToXML(object, descriptor, isXMLRoot);
}
use of org.eclipse.persistence.internal.oxm.XMLObjectBuilder in project eclipselink by eclipse-ee4j.
the class XMLBinder method updateXML.
public void updateXML(Object obj, Node associatedNode) {
if (obj instanceof Root) {
obj = ((Root) obj).getObject();
}
Node objNode = this.getXMLNode(obj);
AbstractSession session = context.getSession(obj);
if (objNode == associatedNode) {
DOMRecord root = new DOMRecord((Element) associatedNode);
root.setMarshaller(marshaller);
root.setDocPresPolicy(this.documentPreservationPolicy);
XMLDescriptor rootDescriptor = (XMLDescriptor) session.getDescriptor(obj);
((XMLObjectBuilder) rootDescriptor.getObjectBuilder()).buildIntoNestedRow(root, obj, session);
}
}
Aggregations