Reusing xalan transformer causing its extension functions break
- by Leslie Norman
I am using xalan 2.7.1 to validate my xml docs with xslt style sheet. It works fine for the first document and returns error message in case of error along with correct line and column number of xml source by making use of NodeInfo.lineNumber and NodeInfo.columnNumber extensions.
The problem is when I try to reuse transformer to validate other xml docs, it successfully transforms the document but always returns lineNumber=columnNumber=-1 for all errors.
Any idea?
Here is my code:
TransformerFactory tFactory = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl", null);
tFactory.setAttribute(TransformerFactoryImpl.FEATURE_SOURCE_LOCATION, Boolean.TRUE);
StreamSource xsltStreamSource = new StreamSource(new File("E:\\Temp\\Test\\myXslt.xsl"));
Transformer transformer=null;
try {
transformer = tFactory.newTransformer(xsltStreamSource);
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
File srcFolder = new File("E:\\Temp\\Test");
for (File file :srcFolder.listFiles()) {
if (file.getName().endsWith("xml")) {
transformer.transform(new StreamSource(file), new StreamResult(outStream));
transformer.reset();
}
}
System.out.println(outStream.toString());
} catch (TransformerException e) {
e.printStackTrace();
}
Edit: New code after implementing @rsp suggestions:
package mycompany;
import java.io.File;
import javax.xml.transform.ErrorListener;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.apache.xalan.processor.TransformerFactoryImpl;
public class XsltTransformer {
public static void main(String[] args) {
TransformerFactory tFactory = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl", null);
tFactory.setAttribute(TransformerFactoryImpl.FEATURE_SOURCE_LOCATION, Boolean.TRUE);
StreamSource xsltStreamSource = new StreamSource(new File("E:\\Temp\\Test\\myXslt.xsl"));
try {
Transformer transformer = tFactory.newTransformer(xsltStreamSource);
File srcFolder = new File("E:\\Temp\\Test");
for (File file : srcFolder.listFiles()) {
if (file.getName().endsWith("xml")) {
Source source = new StreamSource(file);
StreamResult result = new StreamResult(System.out);
XsltTransformer xsltTransformer = new XsltTransformer();
ErrorListenerImpl errorHandler = xsltTransformer.new ErrorListenerImpl();
transformer.setErrorListener(errorHandler);
transformer.transform(source, result);
if (errorHandler.e != null) {
System.out.println("Transformation Exception: " + errorHandler.e.getMessage());
}
transformer.reset();
}
}
} catch (TransformerException e) {
e.printStackTrace();
}
}
private class ErrorListenerImpl implements ErrorListener {
public TransformerException e = null;
public void error(TransformerException exception) {
this.e = exception;
}
public void fatalError(TransformerException exception) {
this.e = exception;
}
public void warning(TransformerException exception) {
this.e = exception;
}
}
}