package textbender.g.xml.sax; // Copyright 2005, 2007, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Textbender Software"), to deal in the Textbender Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Textbender Software, and to permit persons to whom the Textbender Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Textbender Software. THE TEXTBENDER SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE TEXTBENDER SOFTWARE OR THE USE OR OTHER DEALINGS IN THE TEXTBENDER SOFTWARE. import java.io.PrintWriter; import org.xml.sax.*; /** A SAX error handler that outputs messages to a print writer. */ public final class ErrorHandlerPW implements ErrorHandler // cf. textbender.g.xml.dom.DOMErrorHandlerPW { /** Constructs an ErrorHandlerPW. * If using it repeatedly, call {@linkplain #reset() reset}() between parses. */ public ErrorHandlerPW( PrintWriter pw ) { this.pw = pw; } // ------------------------------------------------------------------------------------ /** Returns the sum of error, fatal error and warning counts. */ public int count() { return errorCount + fatalErrorCount + warningCount; }; /** Returns the error count. */ public int getErrorCount() { return errorCount; }; private int errorCount; /** Returns the fatal error count. */ public int getFatalErrorCount() { return fatalErrorCount; }; private int fatalErrorCount; /** Returns the warning count. */ public int getWarningCount() { return warningCount; }; private int warningCount; /** Resets all counts to zero. */ public void reset() { errorCount = 0; fatalErrorCount = 0; warningCount = 0; } // - E r r o r - H a n d l e r -------------------------------------------------------- /** Outputs an error through the print writer. * Increments the {@linkplain #getErrorCount() error count}. */ public void error( SAXParseException x ) { ++errorCount; output( "error", x ); } /** Outputs a fatal error through the print writer. * Increments the {@linkplain #getFatalErrorCount() fatal error count}. */ public void fatalError( SAXParseException x ) { ++fatalErrorCount; output( "fatal error", x ); } /** Outputs a warning through the print writer. * Increments the {@linkplain #getWarningCount() warning count}. */ public void warning( SAXParseException x ) { ++warningCount; output( "warning", x ); } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final PrintWriter pw; private void output( String severityName, SAXParseException x ) { pw.print( '\n' ); pw.print( severityName ); if( x.getLineNumber() != -1 ) { pw.print( ", line " ); pw.print( x.getLineNumber() ); if( x.getColumnNumber() != -1 ) { pw.print( ':' ); pw.print( x.getColumnNumber() ); } } if( x.getPublicId() != null ) { pw.print( ", " ); pw.print( x.getPublicId() ); } if( x.getSystemId() != null ) { pw.print( ", " ); pw.print( x.getSystemId() ); } pw.print( '\n' ); pw.print( x.getMessage() ); for( Throwable t = x.getCause(); t != null; t = t.getCause() ) { pw.print( '\n' ); pw.print( "caused by: " + t.toString() ); } } }