final ByteArrayInputStream xslStream = new ByteArrayInputStream(xsl.getBytes());
final ByteArrayOutputStream retValueStream = new ByteArrayOutputStream();
/* http://xml.apache.org/xalan-j/usagepatterns.html#basic */
Templates template = null;
synchronized (templates)
{
if (templates.containsKey(xslSystemId))
{
template = templates.get(xslSystemId);
}
else
{
System.out.println("Initialising Templates for " + xslSystemId);
/*
* Instantiate a TransformerFactory. make sure to get a
* org.apache.xalan.processor.TransformerFactoryImpl instead
* of the default
* org.apache.xalan.xsltc.trax.TransformerFactoryImpl. The
* latter doesn't work for docbook xsl.
*/
final TransformerFactory transformerFactory = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl", null);
/*
* Set the URIResolver that will handle request to external
* resources
*/
transformerFactory.setURIResolver(new XSLTResolver(resources));
/*
* see http://nlp.stanford.edu/nlp/javadoc/xalan-docs/
* extensionslib .html#nodeinfo
*/
transformerFactory.setAttribute(TransformerFactoryImpl.FEATURE_SOURCE_LOCATION, Boolean.TRUE);
// transformerFactory.setAttribute(TransformerFactoryImpl.FEATURE_INCREMENTAL,
// Boolean.TRUE);
final StreamSource xslStreamSource = new StreamSource(xslStream);
xslStreamSource.setSystemId(xslSystemId);
/* save the template */
templates.put(xslSystemId, transformerFactory.newTemplates(xslStreamSource));
System.out.println("Done Initialising Templates for " + xslSystemId);
}
template = templates.get(xslSystemId);
}
/*
* Use the TransformerFactory to process the stylesheet Source and
* generate a Transformer.
*/
final Transformer transformer = template.newTransformer();
/* set the global variables */
if (globalParameters != null)
for (final String varibleName : globalParameters.keySet())
transformer.setParameter(varibleName, globalParameters.get(varibleName));