package org.mcraig.cs445.refentry;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
* Error handler for SAX parse errors, useful during validation.
* <p>The methods here do throw exceptions when triggered, so
* if you do not catch them in the parser, they may stop your
* progress, even for non-fatal errors and warnings.
* They are intended to provide useful messages to human beings
* checking their RefEntry documents for validity.
* @author <a href=mailto:mark@mcraig.org>Mark Craig</a>
*/
public class RefEntryErrorHandler implements ErrorHandler {
/**
* Log a diagnostic message for a non-fatal error from the SAX parser.
* @param exception The exception from the parser.
* @throws SAXException every time this method is called.
*/
public void error(SAXParseException exception)
throws SAXException {
log("==>Parse error<==", exception);
throw new SAXException("Parse error encountered");
}
/**
* Log a diagnostic message for a fatal error from the SAX parser.
* @param exception The exception from the parser.
* @throws SAXException every time this method is called.
*/
public void fatalError(SAXParseException exception)
throws SAXException {
log("==>Fatal parse error<==", exception);
throw new SAXException("Fatal parse error encountered");
}
/**
* Log a diagnostic message for a warning from the SAX parser.
* @param exception The exception from the parser.
* @throws SAXException every time this method is called.
*/
public void warning(SAXParseException exception)
throws SAXException {
log("==>Parse warning<==", exception);
throw new SAXException("Parse warning encountered");
}
/**
* Utility to format a diagnostic message on System.out.
* @param message Header to identify the message.
* @param spe Exception containing the diagnostic information.
*/
private void log(String message, SAXParseException spe) {
System.out.println(
message + "\n" +
" Line: " + spe.getLineNumber() + "\n" +
" URI: " + spe.getSystemId() + "\n" +
" Message: " + spe.getMessage()
);
}
} |