Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass an error message generated by xslt to java application caller

Tags:

java

xml

xslt

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?


2 Answers

It looks as if you have placed an error handler on the source document XML parser, but are trying to catch errors from the transformer. So place the error handler on the Transformer rather than the XMLReader

tHandler2.getTransformer().setErrorListener(myHandler);

IIRC, I've always kept it simple and used Transformer directly rather than go for ContentHandler/TranformerHandler.

As alternatives you could go for an implementation specific extension or cause a special URI to be resolved and handle that (although as XSLT is a functional language, I guess it could technically resolve a URI that wont actually impact the result).

like image 181
Tom Hawtin - tackline Avatar answered Nov 18 '25 10:11

Tom Hawtin - tackline


In what way is the XSLT generating an error and terminating? This might be crucial since the <xsl:message> tag does not always write to the standard output like you might expect. Especially with Saxon.

like image 31
Jon W Avatar answered Nov 18 '25 10:11

Jon W



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!