FileDocCategorySizeDatePackage
MyErrorHandler.javaAPI DocExample2095Fri Feb 01 12:49:52 GMT 2002None

MyErrorHandler.java

import org.xml.sax.*;

/**
 * Customizable Diagnostic Error Handler
 */
public class MyErrorHandler implements ErrorHandler
{
    private int         flags;

    // bitmask values for flags
    public static final int ERR_PRINT = 1;
    public static final int ERR_IGNORE = 2;
    public static final int WARN_PRINT = 4;
    public static final int FATAL_PRINT = 8;
    public static final int FATAL_IGNORE = 16;

    public MyErrorHandler () { flags = ~0; }
    public MyErrorHandler (int flags) { this.flags = flags; }

    public void error (SAXParseException e)
    throws SAXParseException
    {
	if ((flags & ERR_PRINT) != 0)
	    System.err.print (printParseException ("Error", e));
	if ((flags & ERR_IGNORE) == 0)
	    throw e;
    }

    public void fatalError (SAXParseException e)
    throws SAXParseException
    {
	if ((flags & FATAL_PRINT) != 0)
	    System.err.print (printParseException ("FATAL", e));
	if ((flags & FATAL_IGNORE) == 0)
	    throw e;
    }

    public void warning (SAXParseException e)
    throws SAXParseException
    {
	if ((flags & WARN_PRINT) != 0)
	    System.err.print (printParseException ("Warning", e));
	// always ignored
    }

    // Getting Diagnostics from a SAXParseException  
    static private String printParseException (
	String              label,
	SAXParseException   e
    ) {
	StringBuffer        buf = new StringBuffer ();
	int                 temp;

	buf.append ("** ");
	buf.append (label);
	buf.append (": ");
	buf.append (e.getMessage ());
	buf.append ('\n');
	// most such exceptions include the (absolute) URI for the text
	if (e.getSystemId () != null) {
	    buf.append ("   URI:  ");
	    buf.append (e.getSystemId ());
	    buf.append ('\n');
	}
	// many include line and column numbers
	if ((temp = e.getLineNumber ()) != -1) {
	    buf.append ("   line: ");
	    buf.append (temp);
	    buf.append ('\n');
	}
	if ((temp = e.getColumnNumber ()) != -1) {
	    buf.append ("   char: ");
	    buf.append (temp);
	    buf.append ('\n');
	}
	// public ID might be available, but is seldom useful

	return buf.toString ();
    }
}