}
private static void transformStream(String xslt, Document document, StreamResult streamResult, Map parameters)
throws TransformException
{
if (xslt == null) { throw new TransformException("Invalid Transform, no stylesheet set!"); }
synchronized (mutex)
{
if (transformerFactory == null)
{
System.setProperty(JAX_TRANSFORM_PROPERTY, jaxTransformFactoryProp);
System.setProperty(JAX_SAX_PARSER_PROPERTY, jaxSaxFactoryProp);
System.setProperty(JAX_DOM_PARSER_PROPERTY, jaxDomFactoryProp);
System.setProperty(SAX_XML_READER_PROPERTY, saxXmlReaderProp);
TransformerFactory tFactory = TransformerFactory.newInstance();
domFactory = DocumentBuilderFactory.newInstance();
domFactory.setValidating(false);
saxFactory = SAXParserFactory.newInstance();
saxFactory.setValidating(false);
if (!tFactory.getFeature(SAXTransformerFactory.FEATURE)) { throw new TransformException(
"Invalid SAX Tranformer. Doesn't support SAX"); }
transformerFactory = ((SAXTransformerFactory) tFactory);
}
}
//
// create a new document builder to load the XML file for transformation
//
DocumentBuilder docBuilder = null;
try
{
docBuilder = domFactory.newDocumentBuilder();
docBuilder.setEntityResolver(new TransformDTDEntityResolver(dtds));
}
catch (ParserConfigurationException e)
{
throw new TransformException("Failed to load JAX Document Builder: " + e.toString());
}
try
{
// Create a ContentHandler to handle parsing of the stylesheet.
TemplatesHandler templatesHandler = transformerFactory.newTemplatesHandler();
// Create an XMLReader and set its ContentHandler.
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(templatesHandler);
// Set it to solve Entities via cache
reader.setEntityResolver(new TransformDTDEntityResolver(dtds));
//
// Get the stylesheet's content from the deployment
//
java.io.FileInputStream is = new java.io.FileInputStream(xslt);
InputStreamReader ssreader = new InputStreamReader(is);
// Parse the stylesheet.
final InputSource xstyle = new InputSource(ssreader);
xstyle.setSystemId(xslt);
reader.parse(xstyle);
//Get the Templates object from the ContentHandler.
Templates templates = templatesHandler.getTemplates();
// Create a ContentHandler to handle parsing of the XML source.
TransformerHandler handler = transformerFactory.newTransformerHandler(templates);
// Reset the XMLReader's ContentHandler.
reader.setContentHandler(handler);
//
// Parse the Document into a DOM tree
//
//
// org.w3c.dom.Document doc = docBuilder.parse(inputSource);
// reader.setProperty("http://xml.org/sax/properties/lexical-handler",
// handler);
final Transformer processor = handler.getTransformer();
//
// Get the transform variables (parameters)
//
Iterator keys = parameters.keySet().iterator();
while (keys.hasNext())
{
String name = (String) keys.next();
String value = (String) parameters.get(name);
processor.setParameter(name, value);
}
//
// do the transformation now
//
processor.transform(new DOMSource(document), streamResult);
}
catch (Exception e)
{
throw new TransformException("Error in Transformation: " + e.toString());
}
}