Search in sources :

Example 16 with TransformerHandler

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

the class AbstractTraxSerializerFactory method createSerializer.

/**
     * @see org.apache.sling.rewriter.SerializerFactory#createSerializer()
     */
public Serializer createSerializer() {
    TransformerHandler tHandler = null;
    try {
        tHandler = this.tfactory.newTransformerHandler();
    } catch (TransformerConfigurationException e) {
        logger.error("Unable to create new transformer handler.", e);
    }
    final ContentHandler ch;
    if (this.needsNamespacesAsAttributes) {
        final NamespaceAsAttributes nsPipeline = new NamespaceAsAttributes(tHandler, this.logger);
        ch = nsPipeline;
    } else {
        ch = tHandler;
    }
    return new TraxSerializer(tHandler, ch, getOutputFormat(), getDoctypePublic(), getDoctypeSystem());
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) ContentHandler(org.xml.sax.ContentHandler)

Example 17 with TransformerHandler

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

the class AbstractTraxSerializerFactory method needsNamespacesAsAttributes.

/**
     * Checks if the used Trax implementation correctly handles namespaces set using
     * <code>startPrefixMapping()</code>, but wants them also as 'xmlns:' attributes.
     * <p>
     * The check consists in sending SAX events representing a minimal namespaced document
     * with namespaces defined only with calls to <code>startPrefixMapping</code> (no
     * xmlns:xxx attributes) and check if they are present in the resulting text.
     */
protected boolean needsNamespacesAsAttributes() throws Exception {
    // Serialize a minimal document to check how namespaces are handled.
    final StringWriter writer = new StringWriter();
    final String uri = "namespaceuri";
    final String prefix = "nsp";
    final String check = "xmlns:" + prefix + "='" + uri + "'";
    final TransformerHandler handler = this.tfactory.newTransformerHandler();
    handler.setResult(new StreamResult(writer));
    // Output a single element
    handler.startDocument();
    handler.startPrefixMapping(prefix, uri);
    handler.startElement(uri, "element", "element", new AttributesImpl());
    handler.endElement(uri, "element", "element");
    handler.endPrefixMapping(prefix);
    handler.endDocument();
    final String text = writer.toString();
    // Check if the namespace is there (replace " by ' to be sure of what we search in)
    return (text.replace('"', '\'').indexOf(check) == -1);
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) AttributesImpl(org.xml.sax.helpers.AttributesImpl) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult)

Example 18 with TransformerHandler

use of javax.xml.transform.sax.TransformerHandler in project bnd by bndtools.

the class SAXUtil method buildPipeline.

public static XMLReader buildPipeline(Result output, ContentFilter... filters) throws Exception {
    SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler handler = factory.newTransformerHandler();
    handler.setResult(output);
    ContentHandler last = handler;
    if (filters != null)
        for (ContentFilter filter : filters) {
            filter.setParent(last);
            last = filter;
        }
    XMLReader reader = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
    reader.setContentHandler(last);
    return reader;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) ContentHandler(org.xml.sax.ContentHandler) XMLReader(org.xml.sax.XMLReader)

Example 19 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 20 with TransformerHandler

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

the class TikaProducer method getTransformerHandler.

private TransformerHandler getTransformerHandler(OutputStream output, String method, boolean prettyPrint) throws TransformerConfigurationException, UnsupportedEncodingException {
    SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
    TransformerHandler handler = factory.newTransformerHandler();
    handler.getTransformer().setOutputProperty(OutputKeys.METHOD, method);
    handler.getTransformer().setOutputProperty(OutputKeys.INDENT, prettyPrint ? "yes" : "no");
    if (this.encoding != null) {
        handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, this.encoding);
    }
    handler.setResult(new StreamResult(new OutputStreamWriter(output, this.encoding)));
    return handler;
}
Also used : TransformerHandler(javax.xml.transform.sax.TransformerHandler) StreamResult(javax.xml.transform.stream.StreamResult) SAXTransformerFactory(javax.xml.transform.sax.SAXTransformerFactory) OutputStreamWriter(java.io.OutputStreamWriter)

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