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());
}
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()));
}
}
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();
}
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;
}
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();
}
Aggregations