Search in sources :

Example 1 with ContentHandlerRecord

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);
    }
}
Also used : DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) XMLMarshaller(org.eclipse.persistence.oxm.XMLMarshaller) QName(javax.xml.namespace.QName) StreamSource(javax.xml.transform.stream.StreamSource) NodeRecord(org.eclipse.persistence.oxm.record.NodeRecord) SAXResult(javax.xml.transform.sax.SAXResult) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) SDOMarshalListener(org.eclipse.persistence.sdo.helper.SDOMarshalListener) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter) StringWriter(java.io.StringWriter) ContentHandlerRecord(org.eclipse.persistence.oxm.record.ContentHandlerRecord)

Example 2 with ContentHandlerRecord

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);
}
Also used : JSONWithPadding(org.eclipse.persistence.oxm.JSONWithPadding) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) ContentHandlerRecord(org.eclipse.persistence.oxm.record.ContentHandlerRecord)

Aggregations

ContentHandlerRecord (org.eclipse.persistence.oxm.record.ContentHandlerRecord)2 OutputStreamWriter (java.io.OutputStreamWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 QName (javax.xml.namespace.QName)1 DOMResult (javax.xml.transform.dom.DOMResult)1 SAXResult (javax.xml.transform.sax.SAXResult)1 StreamResult (javax.xml.transform.stream.StreamResult)1 StreamSource (javax.xml.transform.stream.StreamSource)1 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)1 JSONWithPadding (org.eclipse.persistence.oxm.JSONWithPadding)1 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)1 NodeRecord (org.eclipse.persistence.oxm.record.NodeRecord)1 SDOMarshalListener (org.eclipse.persistence.sdo.helper.SDOMarshalListener)1