/** *Class SAXErrorHandler *By Martin Carlsson *Internetprogrammering 4 Course */ import org.xml.sax.*; /** *Takes care of Exceptions that are thrown during the parsing of non-well-formed or non-validating XML documents. *@see SAXChecker */ public class SAXErrorHandler implements ErrorHandler { private String filename; public SAXErrorHandler(String filename) { this.filename = filename; } public void warning(SAXParseException sE) throws SAXException { showParserExceptions("The file \""+ filename + "\" seem to validate ok but have warnings!", sE, "Warning"); throw (sE); } public void error(SAXParseException sE) throws SAXException { showParserExceptions("The file \""+ filename + "\" does NOT VALIDATE!", sE, "Error"); throw (sE); } public void fatalError(SAXParseException sE) throws SAXException { showParserExceptions("The file \""+ filename + "\" is NOT WELLFORMED!", sE, "Fatal Error"); throw (sE); } /** Shows error messages *@param description shows a description of the error message *@param sE exception to collect information from *@param errorType the type of error message */ private void showParserExceptions(String description, SAXParseException sE, String errorType) { System.out.println("\n**********************SAXParser*************************"); System.out.println("\nParsed file: " + filename + "."); System.out.println(description); System.out.println(errorType + " location: " + sE.getLineNumber() + ", column " + sE.getColumnNumber() +"."); System.out.println("Error Message:\n" + sE.getMessage() + "\n"); } }