use of org.eclipse.persistence.oxm.record.ContentHandlerRecord 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.ContentHandlerRecord in project eclipselink by eclipse-ee4j.
the class XMLMarshaller method marshal.
/**
* PUBLIC:
* Convert the given object to XML and update the given contentHandler with that XML Document
* @param object the object to marshal
* @param contentHandler the contentHandler which the specified object should be marshalled to
* @throws XMLMarshalException if an error occurred during marshalling
*/
public void marshal(Object object, ContentHandler contentHandler, LexicalHandler lexicalHandler) throws XMLMarshalException {
if (object instanceof JSONWithPadding && !isApplicationJSON()) {
object = ((JSONWithPadding) object).getObject();
}
if ((object == null) || (contentHandler == 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);
}
ContentHandlerRecord contentHandlerRecord = new ContentHandlerRecord();
contentHandlerRecord.setMarshaller(this);
contentHandlerRecord.setContentHandler(contentHandler);
contentHandlerRecord.setLexicalHandler(lexicalHandler);
marshal(object, contentHandlerRecord, session, xmlDescriptor, isXMLRoot);
}
Aggregations