Search in sources :

Example 1 with XoXMLStreamWriter

use of org.metatype.sxc.util.XoXMLStreamWriter in project tomee by apache.

the class Sxc method marshal.

public static void marshal(final JAXBObject objectType, final Object object, final Result result) throws JAXBException {
    if (result == null)
        throw new IllegalArgumentException("result is null");
    if (!(result instanceof StreamResult))
        throw new IllegalArgumentException("result is null");
    if (object == null)
        throw new IllegalArgumentException("object is null");
    if (objectType == null)
        throw new IllegalArgumentException("jaxbObject is null");
    final StreamResult streamResult = (StreamResult) result;
    XMLStreamWriter writer = null;
    try {
        final XMLOutputFactory xof = getXmOutputFactory();
        writer = xof.createXMLStreamWriter(streamResult.getOutputStream(), "UTF-8");
        writer = new PrettyPrintXMLStreamWriter(writer);
        final XoXMLStreamWriter w = new XoXMLStreamWriterImpl(writer);
        try {
            w.writeStartDocument("UTF-8", null);
            // write xsi:type if there is no default root element for this type
            final RuntimeContext context = new RuntimeContext((ExtendedMarshaller) null);
            try {
                final QName name = objectType.getXmlRootElement();
                // open element
                w.writeStartElementWithAutoPrefix(name.getNamespaceURI(), name.getLocalPart());
                objectType.write(w, object, context);
                w.writeEndElement();
            } catch (Exception e) {
                if (e instanceof JAXBException) {
                    // assume event handler has already been notified
                    throw (JAXBException) e;
                }
                if (e instanceof RuntimeXMLStreamException) {
                    // simply unwrap and handle below
                    e = ((RuntimeXMLStreamException) e).getCause();
                }
                if (e instanceof XMLStreamException) {
                    final Throwable cause = e.getCause();
                    if (cause instanceof JAXBException) {
                        throw (JAXBException) e;
                    }
                    throw new MarshalException(cause == null ? e : cause);
                }
                throw new MarshalException(e);
            }
            w.writeEndDocument();
        } catch (final Exception e) {
            throw new MarshalException(e);
        }
    } catch (final XMLStreamException e) {
        throw new JAXBException("Could not close XMLStreamWriter.", e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (final XMLStreamException ignored) {
            }
        }
    }
}
Also used : XMLOutputFactory(javax.xml.stream.XMLOutputFactory) MarshalException(javax.xml.bind.MarshalException) StreamResult(javax.xml.transform.stream.StreamResult) QName(javax.xml.namespace.QName) JAXBException(javax.xml.bind.JAXBException) RuntimeXMLStreamException(org.metatype.sxc.util.RuntimeXMLStreamException) MarshalException(javax.xml.bind.MarshalException) XMLStreamException(javax.xml.stream.XMLStreamException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) XoXMLStreamWriterImpl(org.metatype.sxc.util.XoXMLStreamWriterImpl) PrettyPrintXMLStreamWriter(org.metatype.sxc.util.PrettyPrintXMLStreamWriter) RuntimeXMLStreamException(org.metatype.sxc.util.RuntimeXMLStreamException) XoXMLStreamWriter(org.metatype.sxc.util.XoXMLStreamWriter) RuntimeXMLStreamException(org.metatype.sxc.util.RuntimeXMLStreamException) XMLStreamException(javax.xml.stream.XMLStreamException) XoXMLStreamWriter(org.metatype.sxc.util.XoXMLStreamWriter) PrettyPrintXMLStreamWriter(org.metatype.sxc.util.PrettyPrintXMLStreamWriter) XMLStreamWriter(javax.xml.stream.XMLStreamWriter) RuntimeContext(org.metatype.sxc.jaxb.RuntimeContext)

Aggregations

IOException (java.io.IOException)1 JAXBException (javax.xml.bind.JAXBException)1 MarshalException (javax.xml.bind.MarshalException)1 QName (javax.xml.namespace.QName)1 XMLOutputFactory (javax.xml.stream.XMLOutputFactory)1 XMLStreamException (javax.xml.stream.XMLStreamException)1 XMLStreamWriter (javax.xml.stream.XMLStreamWriter)1 StreamResult (javax.xml.transform.stream.StreamResult)1 RuntimeContext (org.metatype.sxc.jaxb.RuntimeContext)1 PrettyPrintXMLStreamWriter (org.metatype.sxc.util.PrettyPrintXMLStreamWriter)1 RuntimeXMLStreamException (org.metatype.sxc.util.RuntimeXMLStreamException)1 XoXMLStreamWriter (org.metatype.sxc.util.XoXMLStreamWriter)1 XoXMLStreamWriterImpl (org.metatype.sxc.util.XoXMLStreamWriterImpl)1