Search in sources :

Example 6 with MarshalException

use of javax.xml.bind.MarshalException in project OpenAM by OpenRock.

the class MarshallerImpl method write.

private void write(XMLSerializable obj, ContentHandler writer) throws JAXBException {
    try {
        if (getSchemaLocation() != null || getNoNSSchemaLocation() != null) {
            // if we need to add xsi:schemaLocation or its brother,
            // throw in the component to do that.
            writer = new SchemaLocationFilter(getSchemaLocation(), getNoNSSchemaLocation(), writer);
        }
        SAXMarshaller serializer = new SAXMarshaller(writer, prefixMapper, this);
        // set a DocumentLocator that doesn't provide any information
        writer.setDocumentLocator(new LocatorImpl());
        writer.startDocument();
        serializer.childAsBody(obj, null);
        writer.endDocument();
        // extra check
        serializer.reconcileID();
    } catch (SAXException e) {
        throw new MarshalException(e);
    }
}
Also used : SchemaLocationFilter(com.sun.xml.bind.marshaller.SchemaLocationFilter) MarshalException(javax.xml.bind.MarshalException) LocatorImpl(org.xml.sax.helpers.LocatorImpl) SAXException(org.xml.sax.SAXException)

Example 7 with MarshalException

use of javax.xml.bind.MarshalException in project OpenAM by OpenRock.

the class MarshallerImpl method marshal.

public void marshal(Object obj, Result result) throws JAXBException {
    //XMLSerializable so = Util.toXMLSerializable(obj);
    XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);
    if (so == null)
        throw new MarshalException(Messages.format(Messages.NOT_MARSHALLABLE));
    if (result instanceof SAXResult) {
        write(so, ((SAXResult) result).getHandler());
        return;
    }
    if (result instanceof DOMResult) {
        Node node = ((DOMResult) result).getNode();
        if (node == null) {
            try {
                DocumentBuilder db = XMLUtils.getSafeDocumentBuilder(false);
                Document doc = db.newDocument();
                ((DOMResult) result).setNode(doc);
                write(so, new SAX2DOMEx(doc));
            } catch (ParserConfigurationException pce) {
                throw new JAXBAssertionError(pce);
            }
        } else {
            write(so, new SAX2DOMEx(node));
        }
        return;
    }
    if (result instanceof StreamResult) {
        StreamResult sr = (StreamResult) result;
        XMLWriter w = null;
        if (sr.getWriter() != null)
            w = createWriter(sr.getWriter());
        else if (sr.getOutputStream() != null)
            w = createWriter(sr.getOutputStream());
        else if (sr.getSystemId() != null) {
            String fileURL = sr.getSystemId();
            if (fileURL.startsWith("file:///")) {
                if (fileURL.substring(8).indexOf(":") > 0)
                    fileURL = fileURL.substring(8);
                else
                    fileURL = fileURL.substring(7);
            }
            try {
                w = createWriter(new FileOutputStream(fileURL));
            } catch (IOException e) {
                throw new MarshalException(e);
            }
        }
        if (w == null)
            throw new IllegalArgumentException();
        write(so, w);
        return;
    }
    // unsupported parameter type
    throw new MarshalException(Messages.format(Messages.UNSUPPORTED_RESULT));
}
Also used : MarshalException(javax.xml.bind.MarshalException) DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLWriter(com.sun.xml.bind.marshaller.XMLWriter) SAXResult(javax.xml.transform.sax.SAXResult) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) SAX2DOMEx(com.sun.xml.bind.marshaller.SAX2DOMEx) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 8 with MarshalException

use of javax.xml.bind.MarshalException in project OpenAM by OpenRock.

the class MarshallerImpl method write.

private void write(XMLSerializable obj, ContentHandler writer) throws JAXBException {
    try {
        if (getSchemaLocation() != null || getNoNSSchemaLocation() != null) {
            // if we need to add xsi:schemaLocation or its brother,
            // throw in the component to do that.
            writer = new SchemaLocationFilter(getSchemaLocation(), getNoNSSchemaLocation(), writer);
        }
        SAXMarshaller serializer = new SAXMarshaller(writer, prefixMapper, this);
        // set a DocumentLocator that doesn't provide any information
        writer.setDocumentLocator(new LocatorImpl());
        writer.startDocument();
        serializer.childAsBody(obj, null);
        writer.endDocument();
        // extra check
        serializer.reconcileID();
    } catch (SAXException e) {
        throw new MarshalException(e);
    }
}
Also used : SchemaLocationFilter(com.sun.xml.bind.marshaller.SchemaLocationFilter) MarshalException(javax.xml.bind.MarshalException) LocatorImpl(org.xml.sax.helpers.LocatorImpl) SAXException(org.xml.sax.SAXException)

Example 9 with MarshalException

use of javax.xml.bind.MarshalException in project OpenAM by OpenRock.

the class MarshallerImpl method marshal.

public void marshal(Object obj, Result result) throws JAXBException {
    //XMLSerializable so = Util.toXMLSerializable(obj);
    XMLSerializable so = context.getGrammarInfo().castToXMLSerializable(obj);
    if (so == null)
        throw new MarshalException(Messages.format(Messages.NOT_MARSHALLABLE));
    if (result instanceof SAXResult) {
        write(so, ((SAXResult) result).getHandler());
        return;
    }
    if (result instanceof DOMResult) {
        Node node = ((DOMResult) result).getNode();
        if (node == null) {
            try {
                DocumentBuilder db = XMLUtils.getSafeDocumentBuilder(false);
                Document doc = db.newDocument();
                ((DOMResult) result).setNode(doc);
                write(so, new SAX2DOMEx(doc));
            } catch (ParserConfigurationException pce) {
                throw new JAXBAssertionError(pce);
            }
        } else {
            write(so, new SAX2DOMEx(node));
        }
        return;
    }
    if (result instanceof StreamResult) {
        StreamResult sr = (StreamResult) result;
        XMLWriter w = null;
        if (sr.getWriter() != null)
            w = createWriter(sr.getWriter());
        else if (sr.getOutputStream() != null)
            w = createWriter(sr.getOutputStream());
        else if (sr.getSystemId() != null) {
            String fileURL = sr.getSystemId();
            if (fileURL.startsWith("file:///")) {
                if (fileURL.substring(8).indexOf(":") > 0)
                    fileURL = fileURL.substring(8);
                else
                    fileURL = fileURL.substring(7);
            }
            try {
                w = createWriter(new FileOutputStream(fileURL));
            } catch (IOException e) {
                throw new MarshalException(e);
            }
        }
        if (w == null)
            throw new IllegalArgumentException();
        write(so, w);
        return;
    }
    // unsupported parameter type
    throw new MarshalException(Messages.format(Messages.UNSUPPORTED_RESULT));
}
Also used : MarshalException(javax.xml.bind.MarshalException) DOMResult(javax.xml.transform.dom.DOMResult) StreamResult(javax.xml.transform.stream.StreamResult) Node(org.w3c.dom.Node) IOException(java.io.IOException) Document(org.w3c.dom.Document) XMLWriter(com.sun.xml.bind.marshaller.XMLWriter) SAXResult(javax.xml.transform.sax.SAXResult) JAXBAssertionError(com.sun.xml.bind.JAXBAssertionError) DocumentBuilder(javax.xml.parsers.DocumentBuilder) FileOutputStream(java.io.FileOutputStream) SAX2DOMEx(com.sun.xml.bind.marshaller.SAX2DOMEx) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 10 with MarshalException

use of javax.xml.bind.MarshalException 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

MarshalException (javax.xml.bind.MarshalException)11 IOException (java.io.IOException)6 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)5 StreamResult (javax.xml.transform.stream.StreamResult)5 SAXException (org.xml.sax.SAXException)5 JAXBAssertionError (com.sun.xml.bind.JAXBAssertionError)4 SAX2DOMEx (com.sun.xml.bind.marshaller.SAX2DOMEx)4 SchemaLocationFilter (com.sun.xml.bind.marshaller.SchemaLocationFilter)4 XMLWriter (com.sun.xml.bind.marshaller.XMLWriter)4 FileOutputStream (java.io.FileOutputStream)4 DocumentBuilder (javax.xml.parsers.DocumentBuilder)4 DOMResult (javax.xml.transform.dom.DOMResult)4 SAXResult (javax.xml.transform.sax.SAXResult)4 Document (org.w3c.dom.Document)4 Node (org.w3c.dom.Node)4 LocatorImpl (org.xml.sax.helpers.LocatorImpl)4 JAXBException (javax.xml.bind.JAXBException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 OutputStream (java.io.OutputStream)1