use of org.eclipse.persistence.oxm.record.NodeRecord in project eclipselink by eclipse-ee4j.
the class SDOXMLHelperDelegate method save.
@Override
public void save(XMLDocument xmlDocument, Result result, Object options) throws IOException {
if (xmlDocument == null) {
throw new IllegalArgumentException(SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument"));
}
if (result instanceof StreamResult) {
StreamResult streamResult = (StreamResult) result;
Writer writer = streamResult.getWriter();
if (null == writer) {
save(xmlDocument, streamResult.getOutputStream(), options);
} else {
save(xmlDocument, writer, options);
}
} else {
// get XMLMarshaller once - as we may create a new instance if this helper isDirty=true
XMLMarshaller anXMLMarshaller = getXmlMarshaller(options);
// Ask the SDOXMLDocument if we should include the XML declaration in the resulting XML
anXMLMarshaller.setFragment(!xmlDocument.isXMLDeclaration());
anXMLMarshaller.setEncoding(xmlDocument.getEncoding());
anXMLMarshaller.setSchemaLocation(xmlDocument.getSchemaLocation());
anXMLMarshaller.setNoNamespaceSchemaLocation(xmlDocument.getNoNamespaceSchemaLocation());
SDOMarshalListener listener = ((SDOMarshalListener) anXMLMarshaller.getMarshalListener());
listener.setMarshalledObject(xmlDocument.getRootObject());
listener.setMarshalledObjectRootQName(new QName(xmlDocument.getRootElementURI(), xmlDocument.getRootElementName()));
if (result instanceof SAXResult) {
ContentHandlerRecord marshalRecord = new ContentHandlerRecord();
marshalRecord.setContentHandler(((SAXResult) result).getHandler());
marshalRecord.setMarshaller(anXMLMarshaller);
listener.setRootMarshalRecord(marshalRecord);
anXMLMarshaller.marshal(xmlDocument, marshalRecord);
marshalRecord.flush();
} else if (result instanceof DOMResult) {
NodeRecord marshalRecord = new NodeRecord();
marshalRecord.setDOM(((DOMResult) result).getNode());
marshalRecord.setMarshaller(anXMLMarshaller);
listener.setRootMarshalRecord(marshalRecord);
anXMLMarshaller.marshal(xmlDocument, marshalRecord);
marshalRecord.flush();
} else {
StringWriter writer = new StringWriter();
this.save(xmlDocument, writer, options);
String xml = writer.toString();
StreamSource source = new StreamSource(new java.io.StringReader(xml));
anXMLMarshaller.getTransformer().transform(source, result);
}
listener.setMarshalledObject(null);
listener.setMarshalledObjectRootQName(null);
listener.setRootMarshalRecord(null);
}
}
use of org.eclipse.persistence.oxm.record.NodeRecord 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
*/
protected Document objectToXML(Object object, DESCRIPTOR descriptor, boolean isXMLRoot) throws XMLMarshalException {
ABSTRACT_SESSION session = context.getSession(descriptor);
MarshalRecord marshalRecord = new NodeRecord();
marshalRecord.setMarshaller(this);
marshal(object, marshalRecord, session, descriptor, isXMLRoot);
return marshalRecord.getDocument();
}
use of org.eclipse.persistence.oxm.record.NodeRecord in project eclipselink by eclipse-ee4j.
the class XMLMarshaller method marshal.
/**
* PUBLIC:
* @param object the object to marshal
* @param node the node which the specified object should be marshalled to
* @throws XMLMarshalException if an error occurred during marshalling
*/
public void marshal(Object object, Node node) throws XMLMarshalException {
if (object instanceof JSONWithPadding && !isApplicationJSON()) {
object = ((JSONWithPadding) object).getObject();
}
if ((object == null) || (node == null)) {
throw XMLMarshalException.nullArgumentException();
}
ABSTRACT_SESSION session = null;
DESCRIPTOR xmlDescriptor = null;
boolean isXMLRoot = (object instanceof Root);
if (isXMLRoot) {
try {
session = context.getSession(((Root) object).getObject());
if (session != null) {
xmlDescriptor = getDescriptor(((Root) object).getObject(), session);
}
} catch (XMLMarshalException marshalException) {
if (!isSimpleXMLRoot((Root) object)) {
throw marshalException;
}
}
} else {
Class<?> objectClass = object.getClass();
session = context.getSession(objectClass);
xmlDescriptor = getDescriptor(objectClass, session);
}
NodeRecord contentHandlerRecord = new NodeRecord(node);
contentHandlerRecord.setMarshaller(this);
if (!isXMLRoot) {
if ((null == xmlDescriptor.getDefaultRootElement()) && (node.getNodeType() == Node.ELEMENT_NODE) && (xmlDescriptor.getSchemaReference() != null) && (xmlDescriptor.getSchemaReference().getType() == XMLSchemaReference.COMPLEX_TYPE)) {
Attr typeAttr = ((Element) node).getAttributeNodeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, Constants.SCHEMA_TYPE_ATTRIBUTE);
if (typeAttr == null) {
NamespaceResolver namespaceResolver = xmlDescriptor.getNonNullNamespaceResolver();
String xsiPrefix = namespaceResolver.resolveNamespaceURI(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
if (null == xsiPrefix) {
xsiPrefix = namespaceResolver.generatePrefix(Constants.SCHEMA_INSTANCE_PREFIX);
}
String value = xmlDescriptor.getSchemaReference().getSchemaContext();
((Element) node).setAttributeNS(javax.xml.XMLConstants.XMLNS_ATTRIBUTE_NS_URI, javax.xml.XMLConstants.XMLNS_ATTRIBUTE + Constants.COLON + xsiPrefix, javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI);
((Element) node).setAttributeNS(javax.xml.XMLConstants.W3C_XML_SCHEMA_INSTANCE_NS_URI, xsiPrefix + Constants.COLON + Constants.SCHEMA_TYPE_ATTRIBUTE, value);
} else {
String value = xmlDescriptor.getSchemaReference().getSchemaContext();
typeAttr.setValue(value);
}
}
}
marshal(object, contentHandlerRecord, session, xmlDescriptor, isXMLRoot);
}
use of org.eclipse.persistence.oxm.record.NodeRecord in project eclipselink by eclipse-ee4j.
the class TreeObjectBuilder method createRecord.
/**
* Create a new row/record for the object builder with the given name.
* This allows subclasses to define different record types.
*/
@Override
public AbstractMarshalRecord createRecord(String rootName, Node parent, AbstractSession session) {
NodeRecord nRec = new NodeRecord(rootName, getNamespaceResolver(), parent);
nRec.setSession(session);
return nRec;
}
use of org.eclipse.persistence.oxm.record.NodeRecord in project eclipselink by eclipse-ee4j.
the class XMLMarshaller method objectToXML.
public Document objectToXML(Object object, Node parent, DocumentPreservationPolicy docPresPolicy) {
boolean isXMLRoot = (object instanceof Root);
AbstractSession session = null;
XMLDescriptor descriptor = null;
if (isXMLRoot) {
try {
session = context.getSession(((Root) object).getObject());
if (session != null) {
descriptor = getDescriptor(((Root) object).getObject(), session);
}
} catch (XMLMarshalException marshalException) {
if (!isSimpleXMLRoot((Root) object)) {
throw marshalException;
}
}
} else {
Class<?> objectClass = object.getClass();
session = context.getSession(objectClass);
descriptor = getDescriptor(objectClass, session);
}
String localRootName = descriptor.getDefaultRootElement();
if (null == localRootName) {
throw XMLMarshalException.defaultRootElementNotSpecified(descriptor);
}
if (docPresPolicy == null) {
docPresPolicy = context.getDocumentPreservationPolicy(session);
}
if (docPresPolicy != null && docPresPolicy.shouldPreserveDocument()) {
XMLRecord xmlRow = (XMLRecord) ((XMLObjectBuilder) descriptor.getObjectBuilder()).createRecord(localRootName, parent, session);
xmlRow.setMarshaller(this);
if (this.attachmentMarshaller != null) {
xmlRow.setXOPPackage(this.attachmentMarshaller.isXOPPackage());
}
return objectToXML(object, descriptor, xmlRow, isXMLRoot, docPresPolicy);
}
MarshalRecord marshalRecord = new NodeRecord(localRootName, parent);
marshalRecord.setMarshaller(this);
marshal(object, marshalRecord, session, descriptor, isXMLRoot);
return marshalRecord.getDocument();
}
Aggregations