Map<String, Object> transformParameters,
javax.xml.transform.Result result) throws Docx4JException {
if (source == null ) {
Throwable t = new Throwable();
throw new Docx4JException( "Null Source doc", t);
}
// Use the template to create a transformer
// A Transformer may not be used in multiple threads running concurrently.
// Different Transformers may be used concurrently by different threads.
// A Transformer may be used multiple times. Parameters and output properties
// are preserved across transformations.
javax.xml.transform.Transformer xformer;
try {
xformer = template.newTransformer();
} catch (TransformerConfigurationException e) {
throw new Docx4JException("The Transformer is ill-configured", e);
}
if (!xformer.getClass().getName().equals(
"org.apache.xalan.transformer.TransformerImpl")) {
log
.error("Detected "
+ xformer.getClass().getName()
+ ", but require org.apache.xalan.transformer.TransformerImpl. "
+ "Ensure Xalan 2.7.0 is on your classpath!");
}
LoggingErrorListener errorListener = new LoggingErrorListener(false);
xformer.setErrorListener(errorListener);
if (transformParameters != null) {
Iterator parameterIterator = transformParameters.entrySet()
.iterator();
while (parameterIterator.hasNext()) {
Map.Entry pairs = (Map.Entry) parameterIterator.next();
if (pairs.getKey() == null) {
log.info("Skipped null key");
// pairs = (Map.Entry)parameterIterator.next();
continue;
}
if (pairs.getKey().equals("customXsltTemplates")) continue;
if (pairs.getValue() == null) {
log.warn("parameter '" + pairs.getKey() + "' was null.");
} else {
xformer.setParameter((String) pairs.getKey(), pairs
.getValue());
}
}
}
/* SUPER DEBUGGING
// http://xml.apache.org/xalan-j/usagepatterns.html#debugging
// debugging
// Set up a PrintTraceListener object to print to a file.
java.io.FileWriter fw = new java.io.FileWriter("/tmp/xslt-events" + xsltCount++ + ".log");
java.io.PrintWriter pw = new java.io.PrintWriter(fw);
PrintTraceListener ptl = new PrintTraceListener(pw);
// Print information as each node is 'executed' in the stylesheet.
ptl.m_traceElements = true;
// Print information after each result-tree generation event.
ptl.m_traceGeneration = true;
// Print information after each selection event.
ptl.m_traceSelection = true;
// Print information whenever a template is invoked.
ptl.m_traceTemplates = true;
// Print information whenever an extension is called.
ptl.m_traceExtension = true;
TransformerImpl transformerImpl = (TransformerImpl)xformer;
// Register the TraceListener with the TraceManager associated
// with the TransformerImpl.
TraceManager trMgr = transformerImpl.getTraceManager();
trMgr.addTraceListener(ptl);
*/
// DEBUGGING
// use the identity transform if you want to send wordDocument;
// otherwise you'll get the XHTML
// javax.xml.transform.Transformer xformer = tfactory.newTransformer();
try {
xformer.transform(source, result);
} catch (TransformerException e) {
throw new Docx4JException("Cannot perform the transformation", e);
} finally {
//pw.flush();
}
}