Search in sources :

Example 46 with TransformerFactory

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

the class XSLT method main.

public static void main(String[] args) throws Exception {
    ByteArrayOutputStream resStream = new ByteArrayOutputStream();
    TransformerFactory trf = TransformerFactory.newInstance();
    Transformer tr = trf.newTransformer(new StreamSource(System.getProperty("test.src", ".") + "/" + args[1]));
    String res, expectedRes;
    tr.transform(new StreamSource(System.getProperty("test.src", ".") + "/" + args[0]), new StreamResult(resStream));
    res = resStream.toString();
    System.out.println("Transformation completed. Result:" + res);
    if (!res.replaceAll("\\s", "").equals(args[2]))
        throw new RuntimeException("Incorrect transformation result. Expected:" + args[2] + " Observed:" + res);
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) StreamSource(javax.xml.transform.stream.StreamSource) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 47 with TransformerFactory

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

the class NamespacePrefixTest method testConcurrentTransformations.

@Test
public void testConcurrentTransformations() throws Exception {
    final TransformerFactory tf = TransformerFactory.newInstance();
    final Source xslsrc = new StreamSource(new StringReader(XSL));
    final Templates tmpl = tf.newTemplates(xslsrc);
    concurrentTestPassed.set(true);
    // Execute multiple TestWorker tasks
    for (int id = 0; id < THREADS_COUNT; id++) {
        EXECUTOR.execute(new TransformerThread(tmpl.newTransformer(), id));
    }
    // Initiate shutdown of previously submitted task
    EXECUTOR.shutdown();
    // Wait for termination of submitted tasks
    if (!EXECUTOR.awaitTermination(THREADS_COUNT, TimeUnit.SECONDS)) {
        // If not all tasks terminates during the time out force them to shutdown
        EXECUTOR.shutdownNow();
    }
    // Check if all transformation threads generated the correct namespace prefix
    assertTrue(concurrentTestPassed.get());
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) StreamSource(javax.xml.transform.stream.StreamSource) StringReader(java.io.StringReader) Templates(javax.xml.transform.Templates) StreamSource(javax.xml.transform.stream.StreamSource) Source(javax.xml.transform.Source) Test(org.testng.annotations.Test)

Example 48 with TransformerFactory

use of javax.xml.transform.TransformerFactory 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 49 with TransformerFactory

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

the class NamespacePrefixTest method testReuseTemplates.

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

Example 50 with TransformerFactory

use of javax.xml.transform.TransformerFactory 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)

Aggregations

TransformerFactory (javax.xml.transform.TransformerFactory)257 Transformer (javax.xml.transform.Transformer)221 StreamResult (javax.xml.transform.stream.StreamResult)198 DOMSource (javax.xml.transform.dom.DOMSource)157 TransformerException (javax.xml.transform.TransformerException)86 StringWriter (java.io.StringWriter)77 StreamSource (javax.xml.transform.stream.StreamSource)77 Document (org.w3c.dom.Document)67 Source (javax.xml.transform.Source)56 IOException (java.io.IOException)55 File (java.io.File)47 DocumentBuilder (javax.xml.parsers.DocumentBuilder)43 ByteArrayOutputStream (java.io.ByteArrayOutputStream)37 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)37 Element (org.w3c.dom.Element)36 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)35 Result (javax.xml.transform.Result)32 ByteArrayInputStream (java.io.ByteArrayInputStream)29 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)29 StringReader (java.io.StringReader)28