Title: RE: can transform warnings be reported as errors

Here is the solution I am using.  It seems to work OK.  If someone has a better solution please let me know.

import javax.xml.transform.*;

public class XSLTErrorListener implements ErrorListener {

    public void fatalError(TransformerException e) {}
    public void error(TransformerException e) {}

    // treat warnings as errors
    // <xsl:message>Error Message</xsl:message>
    public void warning(TransformerException e) {
        throw new RuntimeException(e.getMessage());
    }

}

-----Original Message-----
From: Don Coleman [mailto:[EMAIL PROTECTED]]
Sent: Friday, September 21, 2001 6:45 PM
To: [EMAIL PROTECTED]
Subject: can transform warnings be reported as errors


I'd like to have the Transformer treat warnings as Errors and throw an TransformerException with the contents of my xsl:message tag.  Can the Transformer be configured to do this?

If an error occurs in my style sheet I have the following line
        <xsl:message terminate="yes">My Error Message</xsl:message>
When I transform this an error message prints to Standard.err:
file:///foo/bar/style.xsl; Line 36; Column -1; My Error Message
My Java class catches a TransformerException
javax.xml.transform.TransformerException: Stylesheet directed termination.  It does not seem to contain a root cause or exception with a message of "My Error Message" like I would expect.

I can write an ErrorListener that catches the warning.  But I'm not sure how I stop processing and report this error to my code.  Is there a standard way to do this?

void transform(Document document, File styleSheet, File outFile) throws TransformerException {
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = null;
    StreamSource styleSource = new StreamSource(styleSheet);
    transformer = tFactory.newTransformer(styleSource);
         
    JDOMSource in = new JDOMSource(document);
    StreamResult result = new StreamResult(outFile);
    transformer.transform(in, result);
}

Reply via email to