Search in sources :

Example 26 with SAXResult

use of javax.xml.transform.sax.SAXResult 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 27 with SAXResult

use of javax.xml.transform.sax.SAXResult 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 28 with SAXResult

use of javax.xml.transform.sax.SAXResult in project voltdb by VoltDB.

the class JDBCSQLXML method createSAXResult.

/**
     *  Retrieves a new SAXResult for setting the XML value designated by this
     *  SQLXML instance.
     *
     *  @param resultClass The class of the result, or null.
     *  @throws java.sql.SQLException if there is an error processing the XML
     *          value
     *  @return for setting the XML value designated by this SQLXML instance.
     */
@SuppressWarnings("unchecked")
protected <T extends Result> T createSAXResult(Class<T> resultClass) throws SQLException {
    SAXResult result = null;
    try {
        result = (resultClass == null) ? new SAXResult() : (SAXResult) resultClass.newInstance();
    } catch (SecurityException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (InstantiationException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (IllegalAccessException ex) {
        throw Exceptions.resultInstantiation(ex);
    } catch (ClassCastException ex) {
        throw Exceptions.resultInstantiation(ex);
    }
    StAXResult staxResult = createStAXResult(null);
    XMLStreamWriter xmlWriter = staxResult.getXMLStreamWriter();
    SAX2XMLStreamWriter handler = new SAX2XMLStreamWriter(xmlWriter);
    result.setHandler(handler);
    return (T) result;
}
Also used : StAXResult(javax.xml.transform.stax.StAXResult) SAXResult(javax.xml.transform.sax.SAXResult) XMLStreamWriter(javax.xml.stream.XMLStreamWriter)

Example 29 with SAXResult

use of javax.xml.transform.sax.SAXResult in project jackrabbit by apache.

the class ClientSession method exportSystemView.

/**
     * Exports the XML system view of the specified repository location
     * to the given XML content handler. This method first requests the
     * raw XML data from the remote session, and then uses an identity
     * transformation to feed the data to the given XML content handler.
     * Possible IO and transformer exceptions are thrown as SAXExceptions.
     *
     * {@inheritDoc}
     */
public void exportSystemView(String path, ContentHandler handler, boolean binaryAsLink, boolean noRecurse) throws SAXException, RepositoryException {
    try {
        byte[] xml = remote.exportSystemView(path, binaryAsLink, noRecurse);
        Source source = new StreamSource(new ByteArrayInputStream(xml));
        Result result = new SAXResult(handler);
        TransformerFactory factory = TransformerFactory.newInstance();
        Transformer transformer = factory.newTransformer();
        transformer.transform(source, result);
    } catch (RemoteException ex) {
        throw new RemoteRepositoryException(ex);
    } catch (IOException ex) {
        throw new SAXException(ex);
    } catch (TransformerConfigurationException ex) {
        throw new SAXException(ex);
    } catch (TransformerException ex) {
        throw new SAXException(ex);
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) StreamSource(javax.xml.transform.stream.StreamSource) IOException(java.io.IOException) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Result(javax.xml.transform.Result) SAXResult(javax.xml.transform.sax.SAXResult) SAXException(org.xml.sax.SAXException) SAXResult(javax.xml.transform.sax.SAXResult) ByteArrayInputStream(java.io.ByteArrayInputStream) RemoteException(java.rmi.RemoteException) TransformerException(javax.xml.transform.TransformerException)

Example 30 with SAXResult

use of javax.xml.transform.sax.SAXResult in project jackrabbit by apache.

the class DefaultItemResource method spoolMultiValued.

private void spoolMultiValued(OutputStream out) {
    try {
        Document doc = DomUtil.createDocument();
        doc.appendChild(getProperty(JCR_VALUES).toXml(doc));
        ContentHandler handler = SerializingContentHandler.getSerializer(out);
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.transform(new DOMSource(doc), new SAXResult(handler));
    } catch (SAXException e) {
        log.error("Failed to set up XML serializer for " + item, e);
    } catch (TransformerConfigurationException e) {
        log.error("Failed to set up XML transformer for " + item, e);
    } catch (ParserConfigurationException e) {
        log.error("Failed to set up XML document for " + item, e);
    } catch (TransformerException e) {
        log.error("Failed to serialize the values of " + item, e);
    }
}
Also used : DOMSource(javax.xml.transform.dom.DOMSource) Transformer(javax.xml.transform.Transformer) SAXResult(javax.xml.transform.sax.SAXResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) Document(org.w3c.dom.Document) ContentHandler(org.xml.sax.ContentHandler) SerializingContentHandler(org.apache.jackrabbit.commons.xml.SerializingContentHandler) TransformerException(javax.xml.transform.TransformerException) SAXException(org.xml.sax.SAXException)

Aggregations

SAXResult (javax.xml.transform.sax.SAXResult)34 Transformer (javax.xml.transform.Transformer)15 TransformerException (javax.xml.transform.TransformerException)14 IOException (java.io.IOException)13 TransformerFactory (javax.xml.transform.TransformerFactory)10 StreamResult (javax.xml.transform.stream.StreamResult)10 StreamSource (javax.xml.transform.stream.StreamSource)10 Source (javax.xml.transform.Source)9 DOMResult (javax.xml.transform.dom.DOMResult)9 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 Result (javax.xml.transform.Result)8 ContentHandler (org.xml.sax.ContentHandler)8 Fop (org.apache.fop.apps.Fop)7 Document (org.w3c.dom.Document)7 DocumentBuilder (javax.xml.parsers.DocumentBuilder)6 Node (org.w3c.dom.Node)6 SAXException (org.xml.sax.SAXException)6 FileOutputStream (java.io.FileOutputStream)5 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)5 LexicalHandler (org.xml.sax.ext.LexicalHandler)5