Search in sources :

Example 26 with TransformerHandler

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

the class SerializingContentHandler method needsXmlnsAttributes.

/**
     * Probes the available XML serializer for xmlns support. Used to set
     * the value of the {@link #NEEDS_XMLNS_ATTRIBUTES} flag.
     *
     * @return whether the XML serializer needs explicit xmlns attributes
     */
private static boolean needsXmlnsAttributes() {
    try {
        StringWriter writer = new StringWriter();
        TransformerHandler probe = FACTORY.newTransformerHandler();
        probe.setResult(new StreamResult(writer));
        probe.startDocument();
        probe.startPrefixMapping("p", "uri");
        probe.startElement("uri", "e", "p:e", new AttributesImpl());
        probe.endElement("uri", "e", "p:e");
        probe.endPrefixMapping("p");
        probe.endDocument();
        return writer.toString().indexOf("xmlns") == -1;
    } catch (Exception e) {
        throw new UnsupportedOperationException("XML serialization fails");
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXException(org.xml.sax.SAXException)

Example 27 with TransformerHandler

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

the class SerializingContentHandler method getSerializer.

/**
     * Creates a serializing content handler that writes to the given result.
     *
     * @param result serialization target
     * @return serializing content handler
     * @throws SAXException if the content handler could not be initialized
     */
public static DefaultHandler getSerializer(Result result) throws SAXException {
    try {
        TransformerHandler handler = FACTORY.newTransformerHandler();
        handler.setResult(result);
        // Specify the output properties to avoid surprises especially in
        // character encoding or the output method (might be html for some
        // documents!)
        Transformer transformer = handler.getTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, ENCODING);
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        if (NEEDS_XMLNS_ATTRIBUTES) {
            // so we need to do it explicitly with this wrapper
            return new SerializingContentHandler(handler);
        } else {
            return new DefaultContentHandler(handler);
        }
    } catch (TransformerConfigurationException e) {
        throw new SAXException("Failed to initialize XML serializer", e);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXException(org.xml.sax.SAXException)

Example 28 with TransformerHandler

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

the class ResultHelper method needsXmlnsAttributes.

/**
     * Probes the available XML serializer for xmlns support. Used to set
     * the value of the {@link #NEEDS_XMLNS_ATTRIBUTES} flag.
     *
     * @return whether the XML serializer needs explicit xmlns attributes
     */
private static boolean needsXmlnsAttributes() {
    try {
        StringWriter writer = new StringWriter();
        TransformerHandler probe = FACTORY.newTransformerHandler();
        probe.setResult(new StreamResult(writer));
        probe.startDocument();
        probe.startPrefixMapping("p", "uri");
        probe.startElement("uri", "e", "p:e", new AttributesImpl());
        probe.endElement("uri", "e", "p:e");
        probe.endPrefixMapping("p");
        probe.endDocument();
        return writer.toString().indexOf("xmlns") == -1;
    } catch (Exception e) {
        throw new UnsupportedOperationException("XML serialization fails");
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXException(org.xml.sax.SAXException)

Example 29 with TransformerHandler

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

the class ResultHelper method getResult.

/**
     * In case the underlying XML library doesn't properly handle xmlns attributes
     * this method creates new content handler dealing with the misbehavior and
     * returns an new instance of SAXResult. Otherwise the passed result
     * is returned back.
     *
     * @param result
     * @return A instance of <code>Result</code> that properly handles xmlns attributes.
     * @throws SAXException
     */
public static Result getResult(Result result) throws SAXException {
    try {
        TransformerHandler handler = FACTORY.newTransformerHandler();
        handler.setResult(result);
        // Specify the output properties to avoid surprises especially in
        // character encoding or the output method (might be html for some
        // documents!)
        Transformer transformer = handler.getTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        if (NEEDS_XMLNS_ATTRIBUTES) {
            // so we need to do it explicitly with this wrapper
            return new SAXResult(new SerializingContentHandler(handler));
        } else {
            return result;
        }
    } catch (TransformerConfigurationException e) {
        throw new SAXException("Failed to initialize XML serializer", e);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) SAXResult(javax.xml.transform.sax.SAXResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXException(org.xml.sax.SAXException)

Example 30 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project jackrabbit-oak by apache.

the class HtmlRepresentation method startResponse.

private XHTMLContentHandler startResponse(HttpServletResponse response, String title) throws IOException {
    try {
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
        TransformerHandler handler = factory.newTransformerHandler();
        Transformer transformer = handler.getTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "html");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        handler.setResult(new StreamResult(response.getOutputStream()));
        Metadata metadata = new Metadata();
        metadata.set(Metadata.TITLE, title);
        return new XHTMLContentHandler(handler, metadata);
    } catch (TransformerConfigurationException e) {
        throw new IOException(e);
    }
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) Metadata(org.apache.tika.metadata.Metadata) IOException(java.io.IOException) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler)

Aggregations

TransformerHandler (javax.xml.transform.sax.TransformerHandler)36 StreamResult (javax.xml.transform.stream.StreamResult)25 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)22 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)13 StringWriter (java.io.StringWriter)10 Transformer (javax.xml.transform.Transformer)9 SAXException (org.xml.sax.SAXException)9 AttributesImpl (org.xml.sax.helpers.AttributesImpl)9 InputStream (java.io.InputStream)7 Metadata (org.apache.tika.metadata.Metadata)7 ParseContext (org.apache.tika.parser.ParseContext)5 File (java.io.File)4 IOException (java.io.IOException)4 TikaTest (org.apache.tika.TikaTest)4 AutoDetectParser (org.apache.tika.parser.AutoDetectParser)4 Test (org.junit.Test)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 OutputStream (java.io.OutputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3 Parser (org.apache.tika.parser.Parser)3