Search in sources :

Example 6 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project lucene-solr by apache.

the class TikaEntityProcessor method getXmlContentHandler.

private static ContentHandler getXmlContentHandler(Writer writer) throws TransformerConfigurationException {
    SAXTransformerFactory factory = (SAXTransformerFactory) TransformerFactory.newInstance();
    TransformerHandler handler = factory.newTransformerHandler();
    handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "xml");
    handler.setResult(new StreamResult(writer));
    return handler;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory)

Example 7 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 8 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 9 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project ddf by codice.

the class EXIEncoderTest method testEncode.

/**
     * Tests that the encode method converts xml into exi-compressed xml.
     *
     * @throws Exception
     */
@Test
public void testEncode() throws Exception {
    ByteArrayOutputStream exiStream = new ByteArrayOutputStream();
    InputStream xmlStream = getClass().getResourceAsStream(TEST_FILE);
    EXIEncoder.encode(xmlStream, exiStream);
    StringWriter stringWriter = new StringWriter();
    GrammarCache grammarCache;
    SAXTransformerFactory saxTransformerFactory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
    saxParserFactory.setNamespaceAware(true);
    TransformerHandler transformerHandler = saxTransformerFactory.newTransformerHandler();
    EXIReader reader = new EXIReader();
    grammarCache = new GrammarCache(null, GrammarOptions.DEFAULT_OPTIONS);
    reader.setGrammarCache(grammarCache);
    transformerHandler.setResult(new StreamResult(stringWriter));
    reader.setContentHandler(transformerHandler);
    reader.parse(new InputSource(new ByteArrayInputStream(exiStream.toByteArray())));
    XMLUnit.setNormalize(true);
    XMLUnit.setNormalizeWhitespace(true);
    InputStream stream = getClass().getResourceAsStream(TEST_FILE);
    Diff diff = XMLUnit.compareXML(IOUtils.toString(stream), stringWriter.getBuffer().toString());
    IOUtils.closeQuietly(stream);
    assertTrue("The XML input file (" + TEST_FILE + ") did not match the EXI-decoded output", diff.similar());
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) InputSource(org.xml.sax.InputSource) EXIReader(org.openexi.sax.EXIReader) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) Diff(org.custommonkey.xmlunit.Diff) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) ByteArrayOutputStream(java.io.ByteArrayOutputStream) GrammarCache(org.openexi.proc.grammars.GrammarCache) SAXParserFactory(javax.xml.parsers.SAXParserFactory) Test(org.junit.Test)

Example 10 with TransformerHandler

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

the class TikaResource method produceOutput.

private StreamingOutput produceOutput(final InputStream is, final MultivaluedMap<String, String> httpHeaders, final UriInfo info, final String format) {
    final Parser parser = createParser();
    final Metadata metadata = new Metadata();
    final ParseContext context = new ParseContext();
    fillMetadata(parser, metadata, context, httpHeaders);
    fillParseContext(context, httpHeaders, parser);
    logRequest(LOG, info, metadata);
    return new StreamingOutput() {

        public void write(OutputStream outputStream) throws IOException, WebApplicationException {
            Writer writer = new OutputStreamWriter(outputStream, UTF_8);
            ContentHandler content;
            try {
                SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
                TransformerHandler handler = factory.newTransformerHandler();
                handler.getTransformer().setOutputProperty(OutputKeys.METHOD, format);
                handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
                handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, UTF_8.name());
                handler.setResult(new StreamResult(writer));
                content = new ExpandedTitleContentHandler(handler);
            } catch (TransformerConfigurationException e) {
                throw new WebApplicationException(e);
            }
            parse(parser, LOG, info.getPath(), is, content, metadata, context);
        }
    };
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) StreamResult(javax.xml.transform.stream.StreamResult) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) WebApplicationException(javax.ws.rs.WebApplicationException) OutputStream(java.io.OutputStream) Metadata(org.apache.tika.metadata.Metadata) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) StreamingOutput(javax.ws.rs.core.StreamingOutput) BoilerpipeContentHandler(org.apache.tika.parser.html.BoilerpipeContentHandler) ExpandedTitleContentHandler(org.apache.tika.sax.ExpandedTitleContentHandler) BodyContentHandler(org.apache.tika.sax.BodyContentHandler) ContentHandler(org.xml.sax.ContentHandler) RichTextContentHandler(org.apache.tika.sax.RichTextContentHandler) ExpandedTitleContentHandler(org.apache.tika.sax.ExpandedTitleContentHandler) Parser(org.apache.tika.parser.Parser) HtmlParser(org.apache.tika.parser.html.HtmlParser) AutoDetectParser(org.apache.tika.parser.AutoDetectParser) DigestingParser(org.apache.tika.parser.DigestingParser) ParseContext(org.apache.tika.parser.ParseContext) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer) OutputStreamWriter(java.io.OutputStreamWriter)

Aggregations

TransformerHandler (javax.xml.transform.sax.TransformerHandler)46 StreamResult (javax.xml.transform.stream.StreamResult)33 SAXTransformerFactory (javax.xml.transform.sax.SAXTransformerFactory)29 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)18 Transformer (javax.xml.transform.Transformer)16 SAXException (org.xml.sax.SAXException)13 StringWriter (java.io.StringWriter)11 AttributesImpl (org.xml.sax.helpers.AttributesImpl)11 IOException (java.io.IOException)8 InputStream (java.io.InputStream)7 Metadata (org.apache.tika.metadata.Metadata)7 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6 File (java.io.File)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 OutputStream (java.io.OutputStream)5 FileOutputStream (java.io.FileOutputStream)4 TransformerException (javax.xml.transform.TransformerException)4 ParseContext (org.apache.tika.parser.ParseContext)4 Test (org.junit.Test)4 OutputStreamWriter (java.io.OutputStreamWriter)3