Search in sources :

Example 1 with Templates

use of javax.xml.transform.Templates 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 2 with Templates

use of javax.xml.transform.Templates 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 3 with Templates

use of javax.xml.transform.Templates 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 4 with Templates

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

the class TransformerProvider method getTemplates.

/** Return a Templates object for the given filename */
private Templates getTemplates(ResourceLoader loader, String filename, int cacheLifetimeSeconds) throws IOException {
    Templates result = null;
    lastFilename = null;
    try {
        if (log.isDebugEnabled()) {
            log.debug("compiling XSLT templates:" + filename);
        }
        final String fn = "xslt/" + filename;
        final TransformerFactory tFactory = TransformerFactory.newInstance();
        tFactory.setURIResolver(new SystemIdResolver(loader).asURIResolver());
        tFactory.setErrorListener(xmllog);
        final StreamSource src = new StreamSource(loader.openResource(fn), SystemIdResolver.createSystemIdFromResourceName(fn));
        try {
            result = tFactory.newTemplates(src);
        } finally {
            // some XML parsers are broken and don't close the byte stream (but they should according to spec)
            IOUtils.closeQuietly(src.getInputStream());
        }
    } catch (Exception e) {
        log.error(getClass().getName(), "newTemplates", e);
        throw new IOException("Unable to initialize Templates '" + filename + "'", e);
    }
    lastFilename = filename;
    lastTemplates = result;
    cacheExpiresTimeout = new TimeOut(cacheLifetimeSeconds, TimeUnit.SECONDS);
    return result;
}
Also used : TransformerFactory(javax.xml.transform.TransformerFactory) SystemIdResolver(org.apache.solr.util.SystemIdResolver) TimeOut(org.apache.solr.util.TimeOut) StreamSource(javax.xml.transform.stream.StreamSource) Templates(javax.xml.transform.Templates) IOException(java.io.IOException) TransformerConfigurationException(javax.xml.transform.TransformerConfigurationException) IOException(java.io.IOException)

Example 5 with Templates

use of javax.xml.transform.Templates in project gocd by gocd.

the class XsltStylesheet method retryXsltTransformation.

private String retryXsltTransformation(ThreadContext context, IRubyObject[] args, DOMSource domSource, NokogiriXsltErrorListener elistener) throws TransformerException, IOException {
    Templates templates = getTemplatesFromStreamSource();
    Transformer transf = templates.newTransformer();
    transf.setErrorListener(elistener);
    if (args.length > 1) {
        addParametersToTransformer(context, transf, args[1]);
    }
    PipedWriter pwriter = new PipedWriter();
    PipedReader preader = new PipedReader();
    pwriter.connect(preader);
    StreamResult result = new StreamResult(pwriter);
    transf.transform(domSource, result);
    char[] cbuf = new char[1024];
    int len = preader.read(cbuf, 0, 1024);
    StringBuilder builder = new StringBuilder();
    builder.append(CharBuffer.wrap(cbuf, 0, len));
    // judge from the first chunk
    htmlish = isHtml(builder.toString());
    while (len == 1024) {
        len = preader.read(cbuf, 0, 1024);
        if (len > 0) {
            builder.append(CharBuffer.wrap(cbuf, 0, len));
        }
    }
    preader.close();
    pwriter.close();
    return builder.toString();
}
Also used : PipedWriter(java.io.PipedWriter) Transformer(javax.xml.transform.Transformer) StreamResult(javax.xml.transform.stream.StreamResult) Templates(javax.xml.transform.Templates) PipedReader(java.io.PipedReader)

Aggregations

Templates (javax.xml.transform.Templates)61 TransformerFactory (javax.xml.transform.TransformerFactory)26 StreamSource (javax.xml.transform.stream.StreamSource)25 Transformer (javax.xml.transform.Transformer)21 Source (javax.xml.transform.Source)20 TransformerConfigurationException (javax.xml.transform.TransformerConfigurationException)18 StreamResult (javax.xml.transform.stream.StreamResult)16 TransformerException (javax.xml.transform.TransformerException)10 InputStream (java.io.InputStream)8 IOException (java.io.IOException)7 OutputStream (java.io.OutputStream)7 DOMResult (javax.xml.transform.dom.DOMResult)7 DOMSource (javax.xml.transform.dom.DOMSource)7 File (java.io.File)5 Result (javax.xml.transform.Result)5 SAXSource (javax.xml.transform.sax.SAXSource)5 FileOutputStream (java.io.FileOutputStream)4 StringReader (java.io.StringReader)4 BufferedOutputStream (java.io.BufferedOutputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3