Search in sources :

Example 26 with SDOMarshalListener

use of org.eclipse.persistence.sdo.helper.SDOMarshalListener in project eclipselink by eclipse-ee4j.

the class SDOXMLHelperDelegate method save.

private void save(XMLDocument xmlDocument, Writer outputWriter, XMLMarshaller anXMLMarshaller) throws IOException {
    if (xmlDocument == null) {
        throw new IllegalArgumentException(SDOException.cannotPerformOperationWithNullInputParameter("save", "xmlDocument"));
    }
    // 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());
    WriterRecord writerRecord;
    if (anXMLMarshaller.isFormattedOutput()) {
        writerRecord = new FormattedWriterRecord();
    } else {
        writerRecord = new WriterRecord();
    }
    writerRecord.setWriter(outputWriter);
    writerRecord.setMarshaller(anXMLMarshaller);
    SDOMarshalListener listener = ((SDOMarshalListener) anXMLMarshaller.getMarshalListener());
    listener.setMarshalledObject(xmlDocument.getRootObject());
    listener.setMarshalledObjectRootQName(new QName(xmlDocument.getRootElementURI(), xmlDocument.getRootElementName()));
    listener.setRootMarshalRecord(writerRecord);
    try {
        anXMLMarshaller.marshal(xmlDocument, writerRecord);
        writerRecord.flush();
    } catch (XMLMarshalException xme) {
        if (xme.getErrorCode() == XMLMarshalException.DESCRIPTOR_NOT_FOUND_IN_PROJECT) {
            if (aHelperContext != ((SDOType) xmlDocument.getRootObject().getType()).getHelperContext()) {
                throw SDOException.dataObjectNotFromHelperContext();
            }
        }
    } finally {
        listener.setMarshalledObject(null);
        listener.setMarshalledObjectRootQName(null);
        listener.setRootMarshalRecord(null);
    }
    outputWriter.flush();
}
Also used : WriterRecord(org.eclipse.persistence.oxm.record.WriterRecord) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord) QName(javax.xml.namespace.QName) SDOType(org.eclipse.persistence.sdo.SDOType) SDOMarshalListener(org.eclipse.persistence.sdo.helper.SDOMarshalListener) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord)

Example 27 with SDOMarshalListener

use of org.eclipse.persistence.sdo.helper.SDOMarshalListener in project eclipselink by eclipse-ee4j.

the class SDOXMLHelperDelegate method save.

/**
 * INTERNAL:
 * Saves the DataObject as an XML document with the specified root element.
 * Same as
 *   save(createDocument(dataObject, rootElementURI, rootElementName),
 *     writer, null);
 *
 * @param rootObject specifies DataObject to be saved
 * @param rootElementURI the Target Namespace URI of the root XML element
 * @param rootElementName the Name of the root XML element
 * @param writer specifies the Writer to write to.
 * @throws IllegalArgumentException if the dataObject tree
 *    is not closed or has no container.
 */
private void save(DataObject rootObject, String rootElementURI, String rootElementName, Writer writer, XMLMarshaller anXMLMarshaller) throws XMLMarshalException {
    SDOXMLDocument xmlDocument = (SDOXMLDocument) createDocument(rootObject, rootElementURI, rootElementName);
    // Ask the SDOXMLDocument if we should include the XML declaration in the resulting XML
    anXMLMarshaller.setFragment(!xmlDocument.isXMLDeclaration());
    WriterRecord writerRecord;
    if (anXMLMarshaller.isFormattedOutput()) {
        writerRecord = new FormattedWriterRecord();
    } else {
        writerRecord = new WriterRecord();
    }
    writerRecord.setWriter(writer);
    writerRecord.setMarshaller(anXMLMarshaller);
    SDOMarshalListener listener = ((SDOMarshalListener) anXMLMarshaller.getMarshalListener());
    listener.setMarshalledObject(rootObject);
    listener.setMarshalledObjectRootQName(new QName(rootElementURI, rootElementName));
    listener.setRootMarshalRecord(writerRecord);
    try {
        anXMLMarshaller.marshal(xmlDocument, writerRecord);
        writerRecord.flush();
    } catch (XMLMarshalException xme) {
        if (xme.getErrorCode() == XMLMarshalException.DESCRIPTOR_NOT_FOUND_IN_PROJECT) {
            if (aHelperContext != ((SDOType) rootObject.getType()).getHelperContext()) {
                throw SDOException.dataObjectNotFromHelperContext();
            }
        }
    } finally {
        listener.setMarshalledObject(null);
        listener.setMarshalledObjectRootQName(null);
        listener.setRootMarshalRecord(null);
    }
    try {
        writer.flush();
    } catch (IOException ex) {
        throw XMLMarshalException.marshalException(ex);
    }
}
Also used : SDOXMLDocument(org.eclipse.persistence.sdo.SDOXMLDocument) WriterRecord(org.eclipse.persistence.oxm.record.WriterRecord) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord) QName(javax.xml.namespace.QName) SDOType(org.eclipse.persistence.sdo.SDOType) SDOMarshalListener(org.eclipse.persistence.sdo.helper.SDOMarshalListener) XMLMarshalException(org.eclipse.persistence.exceptions.XMLMarshalException) IOException(java.io.IOException) FormattedWriterRecord(org.eclipse.persistence.oxm.record.FormattedWriterRecord)

Aggregations

SDOMarshalListener (org.eclipse.persistence.sdo.helper.SDOMarshalListener)27 SDOXMLHelper (org.eclipse.persistence.sdo.helper.SDOXMLHelper)22 SDODataObject (org.eclipse.persistence.sdo.SDODataObject)20 SDOChangeSummary (org.eclipse.persistence.sdo.SDOChangeSummary)16 QName (javax.xml.namespace.QName)3 XMLMarshaller (org.eclipse.persistence.oxm.XMLMarshaller)3 XMLMarshalException (org.eclipse.persistence.exceptions.XMLMarshalException)2 FormattedWriterRecord (org.eclipse.persistence.oxm.record.FormattedWriterRecord)2 WriterRecord (org.eclipse.persistence.oxm.record.WriterRecord)2 SDOType (org.eclipse.persistence.sdo.SDOType)2 SDOTypeHelper (org.eclipse.persistence.sdo.helper.SDOTypeHelper)2 DataObject (commonj.sdo.DataObject)1 IOException (java.io.IOException)1 OutputStreamWriter (java.io.OutputStreamWriter)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 Writer (java.io.Writer)1 DOMResult (javax.xml.transform.dom.DOMResult)1 SAXResult (javax.xml.transform.sax.SAXResult)1 StreamResult (javax.xml.transform.stream.StreamResult)1