package textbender.g.xml.dom; // 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.w3c.dom.*; /** A DOM error handler that outputs messages to a print writer. */ public class DOMErrorHandlerPW implements DOMErrorHandler // cf. textbender.g.xml.sax.ErrorHandlerPW { /** Constructs a DOMErrorHandlerPW. * If using it repeatedly, call {@linkplain #reset() reset}() between parses. */ public DOMErrorHandlerPW( PrintWriter w ) { this.w = w; } // ------------------------------------------------------------------------------------ /** 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; } // - D O M - E r r o r - H a n d l e r ------------------------------------------------ /** Outputs an error through the print writer. * Increments the {@linkplain #count() count}. */ public boolean handleError( DOMError e ) { // System.err.println( "DOMEHPW error: " + e ); final int s = e.getSeverity(); final String severityName; if( s == e.SEVERITY_ERROR ) { severityName = "error"; ++errorCount; } else if( s == e.SEVERITY_FATAL_ERROR ) { severityName = "fatal error"; ++fatalErrorCount; } else { assert e.getSeverity() == e.SEVERITY_WARNING; severityName = "warning"; ++warningCount; } output( severityName, e ); return true; } //// P r i v a t e /////////////////////////////////////////////////////////////////////// private final PrintWriter w; protected void output( String severityName, DOMError e ) { w.print( '\n' ); w.print( severityName ); final DOMLocator loc = e.getLocation(); // System.err.println( "DOMEHPW line: " + loc.getLineNumber() ); if( loc.getLineNumber() != -1 ) { w.print( ", line " ); w.print( loc.getLineNumber() ); if( loc.getColumnNumber() != -1 ) { w.print( ':' ); w.print( loc.getColumnNumber() ); } } if( loc.getUri() != null ) { w.print( ", " ); w.print( loc.getUri() ); } w.print( '\n' ); // System.out.println( "DOMEHPW node: " + loc.getRelatedNode() ); // System.out.println( "DOMEHPW type: " + e.getType() ); // System.out.println( "DOMEHPW data: " + e.getRelatedData() ); w.print( e.getMessage() ); for( Throwable t = (Throwable)e.getRelatedException(); t != null; t = t.getCause() ) { w.print( "\ncaused by: " + t.toString() ); // usually, but not always redundant } } }