001    package com.thaiopensource.xml.sax;
002    
003    import org.xml.sax.ErrorHandler;
004    import org.xml.sax.SAXParseException;
005    import org.xml.sax.SAXException;
006    
007    public class CountingErrorHandler implements ErrorHandler {
008      private ErrorHandler errorHandler;
009      private int fatalErrorCount = 0;
010      private int errorCount = 0;
011      private int warningCount = 0;
012      private boolean hadErrorOrFatalError = false;
013    
014      public CountingErrorHandler() {
015        this(null);
016      }
017    
018      public CountingErrorHandler(ErrorHandler errorHandler) {
019        this.errorHandler = errorHandler;
020      }
021    
022      public void reset() {
023        fatalErrorCount = 0;
024        errorCount = 0;
025        warningCount = 0;
026        hadErrorOrFatalError = false;
027      }
028    
029      public boolean getHadErrorOrFatalError() {
030        return hadErrorOrFatalError;
031      }
032    
033      public int getFatalErrorCount() {
034        return fatalErrorCount;
035      }
036    
037      public int getErrorCount() {
038        return errorCount;
039      }
040    
041      public int getWarningCount() {
042        return warningCount;
043      }
044    
045      public ErrorHandler getErrorHandler() {
046        return errorHandler;
047      }
048    
049      public void setErrorHandler(ErrorHandler errorHandler) {
050        this.errorHandler = errorHandler;
051      }
052    
053      public void warning(SAXParseException exception)
054              throws SAXException {
055        warningCount++;
056        if (errorHandler != null)
057          errorHandler.warning(exception);
058      }
059    
060      public void error(SAXParseException exception)
061              throws SAXException {
062        errorCount++;
063        hadErrorOrFatalError = true;
064        if (errorHandler != null)
065          errorHandler.error(exception);
066      }
067    
068      public void fatalError(SAXParseException exception)
069              throws SAXException {
070        fatalErrorCount++;
071        hadErrorOrFatalError = true;
072        if (errorHandler != null)
073          errorHandler.fatalError(exception);
074      }
075    }