use of org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy in project eclipselink by eclipse-ee4j.
the class XMLObjectBuilder method createRecordFor.
public AbstractRecord createRecordFor(Object attributeValue, Field xmlField, XMLRecord parentRecord, Mapping mapping) {
DocumentPreservationPolicy policy = parentRecord.getDocPresPolicy();
Element newNode = null;
if (policy != null) {
newNode = (Element) policy.getNodeForObject(attributeValue);
}
if (newNode == null) {
newNode = XPathEngine.getInstance().createUnownedElement(parentRecord.getDOM(), xmlField);
if (policy != null) {
if (xmlField.isSelfField()) {
policy.addObjectToCache(attributeValue, newNode, mapping);
} else {
policy.addObjectToCache(attributeValue, newNode);
}
}
}
DOMRecord nestedRecord = new DOMRecord(newNode);
nestedRecord.setMarshaller(parentRecord.getMarshaller());
nestedRecord.setLeafElementType(parentRecord.getLeafElementType());
parentRecord.setLeafElementType((XPathQName) null);
nestedRecord.setDocPresPolicy(policy);
nestedRecord.setXOPPackage(parentRecord.isXOPPackage());
nestedRecord.setReferenceResolver(((DOMRecord) parentRecord).getReferenceResolver());
return nestedRecord;
}
use of org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy 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.oxm.documentpreservation.DocumentPreservationPolicy in project eclipselink by eclipse-ee4j.
the class XMLMarshaller method objectToXMLNode.
@Override
protected Node objectToXMLNode(Object object, Node rootNode, AbstractSession session, XMLDescriptor descriptor, boolean isXMLRoot) throws XMLMarshalException {
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());
}
if (xmlRow.getDOM().getNodeType() == Node.ELEMENT_NODE) {
addDescriptorNamespacesToXMLRecord(descriptor, xmlRow);
}
}
Document doc = objectToXML(object, rootNode, descriptor, xmlRow, isXMLRoot, docPresPolicy);
if ((xmlRow != null) && (xmlRow.getDOM().getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)) {
return xmlRow.getDOM();
} else {
return doc;
}
}
return super.objectToXMLNode(object, rootNode, session, descriptor, isXMLRoot);
}
use of org.eclipse.persistence.oxm.documentpreservation.DocumentPreservationPolicy in project eclipselink by eclipse-ee4j.
the class XMLObjectBuilder method buildObject.
/**
* INTERNAL: Override the parent's buildObject to allow for the caching of
* aggregate objects in OX. By caching aggregates along with XML Nodes that
* they were created from, we are able to preserve the structure and
* unmapped content of the document that was used to create these objects.
*/
@Override
public Object buildObject(ObjectBuildingQuery query, AbstractRecord databaseRow, JoinedAttributeManager joinManager) throws DatabaseException, QueryException {
XMLRecord row = (XMLRecord) databaseRow;
row.setSession(query.getSession());
XMLUnmarshaller unmarshaller = row.getUnmarshaller();
Object parent = row.getOwningObject();
if (!(isXmlDescriptor() || getDescriptor().isDescriptorTypeAggregate())) {
return super.buildObject(query, databaseRow, joinManager);
}
query.getSession().startOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
ClassDescriptor concreteDescriptor = getDescriptor();
Object domainObject = null;
// in the mapping
if (concreteDescriptor.hasInheritance() && (parent == null)) {
// look for an xsi:type attribute in the xml document
InheritancePolicy inheritancePolicy = concreteDescriptor.getInheritancePolicy();
Class<?> classValue = inheritancePolicy.classFromRow(databaseRow, query.getSession());
if ((classValue == null) && isXmlDescriptor()) {
// no xsi:type attribute - look for type indicator on the
// default root element
QName leafElementType = ((Descriptor) concreteDescriptor).getDefaultRootElementType();
// inheritance policy
if (leafElementType != null) {
XPathQName xpathQName = new XPathQName(leafElementType, row.isNamespaceAware());
Object indicator = inheritancePolicy.getClassIndicatorMapping().get(xpathQName);
if (indicator != null) {
classValue = (Class) indicator;
}
}
}
// class, if non-abstract
if (classValue != null) {
concreteDescriptor = query.getSession().getDescriptor(classValue);
if ((concreteDescriptor == null) && query.hasPartialAttributeExpressions()) {
concreteDescriptor = getDescriptor();
}
if (concreteDescriptor == null) {
throw QueryException.noDescriptorForClassFromInheritancePolicy(query, classValue);
}
} else {
// make sure the class is non-abstract
if (Modifier.isAbstract(concreteDescriptor.getJavaClass().getModifiers())) {
// throw an exception
throw DescriptorException.missingClassIndicatorField(databaseRow, inheritancePolicy.getDescriptor());
}
}
}
domainObject = concreteDescriptor.getObjectBuilder().buildNewInstance();
row.setCurrentObject(domainObject);
if ((unmarshaller != null) && (unmarshaller.getUnmarshalListener() != null)) {
unmarshaller.getUnmarshalListener().beforeUnmarshal(domainObject, parent);
}
concreteDescriptor.getObjectBuilder().buildAttributesIntoObject(domainObject, null, databaseRow, query, joinManager, null, false, query.getSession());
if (isXmlDescriptor() && ((Descriptor) concreteDescriptor).getPrimaryKeyFieldNames().size() > 0) {
Object pk = extractPrimaryKeyFromRow(databaseRow, query.getSession());
if ((pk != null) && (((CacheId) pk).getPrimaryKey().length > 0)) {
DOMRecord domRecord = (DOMRecord) databaseRow;
domRecord.getReferenceResolver().putValue(concreteDescriptor.getJavaClass(), pk, domainObject);
}
}
DocumentPreservationPolicy docPresPolicy = row.getDocPresPolicy();
if (docPresPolicy != null) {
// EIS XML Cases won't have a doc pres policy set
row.getDocPresPolicy().addObjectToCache(domainObject, row.getDOM());
}
query.getSession().endOperationProfile(SessionProfiler.ObjectBuilding, query, SessionProfiler.ALL);
if ((unmarshaller != null) && (unmarshaller.getUnmarshalListener() != null)) {
unmarshaller.getUnmarshalListener().afterUnmarshal(domainObject, parent);
}
return domainObject;
}
Aggregations