Search in sources :

Example 31 with SAXResult

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

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

the class SlingTransformer method setXMLConsumer.

@Override
protected void setXMLConsumer(XMLConsumer consumer) {
    TransformerHandler transformerHandler;
    try {
        transformerHandler = this.createTransformerHandler();
    } catch (Exception ex) {
        throw new RuntimeException("Could not initialize transformer handler.", ex);
    }
    final Map<String, Object> map = this.getLogicSheetParameters();
    if (map != null) {
        final Transformer transformer = transformerHandler.getTransformer();
        for (Entry<String, Object> entry : map.entrySet()) {
            transformer.setParameter(entry.getKey(), entry.getValue());
        }
    }
    final SAXResult result = new SAXResult();
    result.setHandler(consumer);
    // According to TrAX specs, all TransformerHandlers are LexicalHandlers
    result.setLexicalHandler(consumer);
    transformerHandler.setResult(result);
    super.setXMLConsumer(new XMLConsumerAdapter(transformerHandler, transformerHandler));
}
Also used : XMLConsumerAdapter(org.apache.cocoon.pipeline.component.sax.XMLConsumerAdapter) TransformerHandler(javax.xml.transform.sax.TransformerHandler) Transformer(javax.xml.transform.Transformer) AbstractTransformer(org.apache.cocoon.pipeline.component.sax.AbstractTransformer) SAXResult(javax.xml.transform.sax.SAXResult)

Example 33 with SAXResult

use of javax.xml.transform.sax.SAXResult in project webservices-axiom by apache.

the class TestGetSAXResult method runTest.

@Override
protected void runTest() throws Throwable {
    TransformerFactory transformerFactory = xsltImplementation.newTransformerFactory();
    StreamSource source = new StreamSource(file.getUrl().toString());
    OMDocument document = metaFactory.getOMFactory().createOMDocument();
    SAXResult result = document.getSAXResult();
    transformerFactory.newTransformer().transform(source, result);
    assertAbout(xml()).that(xml(OMDocument.class, document)).ignoringWhitespaceInPrologAndEpilog().ignoringRedundantNamespaceDeclarations().expandingEntityReferences().hasSameContentAs(file.getUrl());
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) SAXResult(javax.xml.transform.sax.SAXResult) StreamSource(javax.xml.transform.stream.StreamSource) OMDocument(org.apache.axiom.om.OMDocument)

Example 34 with SAXResult

use of javax.xml.transform.sax.SAXResult in project webservices-axiom by apache.

the class XSLTImplementation method supportsLexicalHandlerWithStreamSource.

/**
     * Determine if an identity transformation from a {@link StreamSource} to a {@link SAXResult}
     * will generate events defined by {@link LexicalHandler}.
     * 
     * @return <code>true</code> if the XSLT implementation will invoke the methods on the
     *         {@link LexicalHandler} set on the {@link SAXResult}, <code>false</code> otherwise
     */
public final synchronized boolean supportsLexicalHandlerWithStreamSource() {
    if (supportsLexicalHandlerWithStreamSource == null) {
        StreamSource source = new StreamSource(new StringReader("<!DOCTYPE root><root><![CDATA[test]]></root>"));
        TestContentHandler handler = new TestContentHandler();
        SAXResult result = new SAXResult(handler);
        result.setLexicalHandler(handler);
        try {
            newTransformerFactory().newTransformer().transform(source, result);
        } catch (TransformerException ex) {
            throw new RuntimeException(ex);
        }
        supportsLexicalHandlerWithStreamSource = handler.getLexicalEventsReceived() == 4;
    }
    return supportsLexicalHandlerWithStreamSource;
}
Also used : SAXResult(javax.xml.transform.sax.SAXResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) TransformerException(javax.xml.transform.TransformerException)

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