The best example available is the StandardErrorListener used in Saxon itself. The source code, of course, is available. It's in package net.sf.saxon.
The class TransformerException has a method getLocator() which returns a SourceLocator, which in turn gives you module and line number information if it's available. Some errors have no location information associated with them and you should write your ErrorListener to deal with whatever it finds.
If the TransformerException was thrown by Saxon, then it will be an instance of net.sf.saxon.trans.XPathException, and you can get extra information (for example the error code) by casting to this class.
Michael Kay

From: [] On Behalf Of Jeffrey Schrab
Sent: 08 November 2006 16:08
Subject: [saxon] Help :: Implementing an ErrorListener and getting errorline numbers?

I know this should be something that has probably been discussed to death here but I beg your collective patience.  Question: How do I find the line number (and more details) of a thrown TransformerConfigurationException so I can get to the bottom of a "Failed to compile stylesheet. 1 error detected. " error?  Given...

            try {
                TransformerFactory factory = TransformerFactory.newInstance();

                ErrorListener factoryListener = new LoggingErrorListener();
                factory.setAttribute(FeatureKeys.LINE_NUMBERING, Boolean.TRUE);

                sd = new File(path);
                x = factory.newTemplates(new StreamSource(sd));
                cache.put(path, x);
                cacheTS.put(path, Long.toString(sd.lastModified()));
            } catch( TransformerConfigurationException te ) {
                log.logActivity("ERROR","transform :: tryCache :: TransformerConfigurationException :: "+te.getMessageAndLocation());
            } catch( TransformerException te ) {
                log.logActivity("ERROR","transform :: tryCache :: TransformerException :: "+te.getMessageAndLocation());
            } // try..catch

I finally found out the bit where SAXON will not put line number in TransformerExceptions because there could be more than one - an ErrorListener can handle that.  So...

public class LoggingErrorListener implements ErrorListener {

    protected EventLog log = null;

    public void warning(TransformerException exception) {
        log.logActivity("WARN", "transform :: LoggingErrorListener :: "+exception.getMessage());
    } // method..warning


    public void error(TransformerException exception) throws TransformerException {
        log.logActivity("SEVERE", "transform :: LoggingErrorListener :: "+exception.getMessage());
        throw exception;
    } // method..error

    public void fatalError(TransformerException exception) throws TransformerException {
        log.logActivity("FATAL", "transform :: LoggingErrorListener :: "+exception.getMessage());
        throw exception;
    } // method..fatalError

    public void LoggingErrorListener() {
        PropertyContainer nmyProperties = new PropertyContainer();
        this.log = new EventLog(nmyProperties.configProperty("logPath"),nmyProperties.configProperty("logFilenamePrefix"));
    } // constructor..LoggingErrorListener


} // class..LoggingErrorListener

My LoggingErrorListener is made use of; a TransformerConfigurationException in my case does occur and the fatalError method is called.  However, NOW I have an exception occurring because of the exception.getMessage() call.

After numerous web searches and SourceForge searches, I am turning up not much for canned examples of how to do this.  I've also found that archived maillist messages at SourceForge appear to be slowly purged.  Messages with a subject line that hint at "this might be what I'm looking for!" aren't there anymore.

Is there anywhere on the web that gives a good example of using ErrorListeners for the benefit of getting details on compilation errors (line numbers and the like)?

I am using SAXON 8.5.1 under Java 1.5.

Jeffrey Schrab - Internet Application Developer
GS Design • 6665 N. Sidney Place • Milwaukee, WI 53209
P: 414.228.9666 • F: 414.228.9652 • E: