Search in sources :

Example 36 with Transformer

use of javax.xml.transform.Transformer in project jdk8u_jdk by JetBrains.

the class NamespacePrefixTest method testReuseTransformer.

@Test
public void testReuseTransformer() throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new StringReader(XSL));
    final Transformer t = tf.newTransformer(xslsrc);
    for (int i = 0; i < TRANSF_COUNT; i++) {
        checkResult(doTransformation(t));
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.testng.annotations.Test)

Example 37 with Transformer

use of javax.xml.transform.Transformer in project jdk8u_jdk by JetBrains.

the class TransformerTest method testBug8169112.

/**
     * @bug 8169112
     * @summary Test compilation of large xsl file with outlining.
     *
     * This test merely compiles a large xsl file and tests if its bytecode
     * passes verification by invoking the transform() method for
     * dummy content. The test succeeds if no Exception is thrown
     */
@Test
public final void testBug8169112() throws FileNotFoundException, TransformerException {
    TransformerFactory tf = TransformerFactory.newInstance();
    String xslFile = getClass().getResource("Bug8169112.xsl").toString();
    Transformer t = tf.newTransformer(new StreamSource(xslFile));
    String xmlIn = "<?xml version=\"1.0\"?><DOCROOT/>";
    ByteArrayInputStream bis = new ByteArrayInputStream(xmlIn.getBytes());
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    t.transform(new StreamSource(bis), new StreamResult(bos));
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayInputStream(java.io.ByteArrayInputStream) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test)

Example 38 with Transformer

use of javax.xml.transform.Transformer in project jdk8u_jdk by JetBrains.

the class JAXPSAXParserTest method testTransform.

public final void testTransform() {
    String data = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<r>\n" + "    <e/>\n" + "</r>\n";
    // #5064280 workaround
    String IDENTITY_XSLT_WITH_INDENT = "<xsl:stylesheet version='1.0' " + "xmlns:xsl='http://www.w3.org/1999/XSL/Transform' " + "xmlns:xalan='http://xml.apache.org/xslt' " + "exclude-result-prefixes='xalan'>" + "<xsl:output method='xml' indent='yes' xalan:indent-amount='4'/>" + "<xsl:template match='@*|node()'>" + "<xsl:copy>" + "<xsl:apply-templates select='@*|node()'/>" + "</xsl:copy>" + "</xsl:template>" + "</xsl:stylesheet>";
    try {
        //Skip the default XMLReader
        System.setProperty("org.xml.sax.driver", "com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser");
        StringWriter sw = new StringWriter();
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer t = tf.newTransformer(new StreamSource(new StringReader(IDENTITY_XSLT_WITH_INDENT)));
        Result result = new StreamResult(sw);
        t.transform(new StreamSource(new StringReader(data)), result);
        success("JAXPSAXParserTest passed");
    } catch (Exception e) {
        /**
             * JAXPSAXParser throws NullPointerException since the jaxp 1.5 security
             * manager is not initialized when JAXPSAXParser is instantiated using
             * the default constructor.
            */
        fail(e.toString());
    } finally {
        System.clearProperty("org.xml.sax.driver");
    }
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 39 with Transformer

use of javax.xml.transform.Transformer in project jdk8u_jdk by JetBrains.

the class XPathNegativeZero method xform.

private static String xform(final String xml, final String xsl) throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new File(xsl));
    final Templates tmpl = tf.newTemplates(xslsrc);
    final Transformer t = tmpl.newTransformer();
    StringWriter writer = new StringWriter();
    final Source src = new StreamSource(new File(xml));
    final Result res = new StreamResult(writer);
    t.transform(src, res);
    return writer.toString();
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StringWriter(java.io.StringWriter) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) File(java.io.File) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) StreamResult(javax.xml.transform.stream.StreamResult) Result(javax.xml.transform.Result)

Example 40 with Transformer

use of javax.xml.transform.Transformer in project jdk8u_jdk by JetBrains.

the class Test method toString.

private static String toString(Source response) throws TransformerException, IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.transform(response, new StreamResult(bos));
    bos.close();
    return new String(bos.toByteArray());
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

Transformer (javax.xml.transform.Transformer)1034 StreamResult (javax.xml.transform.stream.StreamResult)799 DOMSource (javax.xml.transform.dom.DOMSource)644 TransformerFactory (javax.xml.transform.TransformerFactory)511 TransformerException (javax.xml.transform.TransformerException)398 StringWriter (java.io.StringWriter)322 IOException (java.io.IOException)264 Document (org.w3c.dom.Document)260 StreamSource (javax.xml.transform.stream.StreamSource)204 Source (javax.xml.transform.Source)188 File (java.io.File)162 DocumentBuilder (javax.xml.parsers.DocumentBuilder)149 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)145 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)142 Element (org.w3c.dom.Element)136 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)124 SAXException (org.xml.sax.SAXException)112 StringReader (java.io.StringReader)108 Result (javax.xml.transform.Result)106 ByteArrayOutputStream (java.io.ByteArrayOutputStream)104