I use a java application to call a xslt to do a xml transformation. The xslt file will generate a message and terminate the process if some condition happens. However, my java application couldn't catch the error message generated by xslt, it only catch an exception with general information - "Stylesheet directed termination".
Here is my java code:
SAXTransformerFactory saxTFactory = ((SAXTransformerFactory) tFactory);
// Create a TransformerHandler for stylesheet.
File f2 = new File(styleSheetPath);
TransformerHandler tHandler2 = saxTFactory.newTransformerHandler(new StreamSource(f2));
XMLReader reader = XMLReaderFactory.createXMLReader();
reader.setContentHandler(tHandler2);
reader.setProperty("http://xml.org/sax/properties/lexical-handler", tHandler2);
CharArrayWriter outputWriter = new CharArrayWriter();
Result result = new StreamResult(outputWriter);
tHandler2.setResult(result);
try
{
reader.parse(new InputSource(new StringReader(XMLinput)));
}
catch(Exception ee)
{
dsiplay(ee.getMessage())
throw ee;
}
How can I catch the error message from xslt?
I tried to write a class:
private class MyErrorHandler extends DefaultHandler{
public void error(SAXParseException e)
{
System.out.println("error method "+e.getMessage());
}
public void fatalError(SAXParseException e)
{
System.out.println("fatal error method "+e.getMessage());
}
public void warning(SAXParseException e)
{
System.out.println("warning method "+e.getMessage());
}
and
MyErrorHandler myHandler = new MyErrorHandler();
reader.setErrorHandler(myHandler);
It didn't work.
Do you have any suggestion?