package com.progress.stylus;

import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
import org.xml.sax.SAXException;

public class SAXErrorHandler implements ErrorHandler
{
    int nErrors;
	public SAXErrorHandler()
	{
        nErrors=0;
	}

	// For each of these three methods, the following rules apply:
	//
	// 1. System.out writes to the Stylus output window.
	//
	// 2. In order for backmapping to work (double-click on an error, and
	//    the cursor moves to the line in the source), the messages must
	//    be formated:
	//    <filepath>:<line>,<column>: <reason>

	public void warning(SAXParseException exception) throws SAXException
	{
		String file = exception.getSystemId();
		int line = exception.getLineNumber();
		int col = exception.getColumnNumber();
		String msg = exception.getLocalizedMessage();

		System.out.println(file + ":" + line + "," + col + ": (Warning) " + msg);
	}

	public void error(SAXParseException exception) throws SAXException
	{
		String file = exception.getSystemId();
		int line = exception.getLineNumber();
		int col = exception.getColumnNumber();
		String msg = exception.getLocalizedMessage();

		System.out.println(file + ":" + line + "," + col + ": (Error) " + msg);

        nErrors++;
	}

	public void fatalError(SAXParseException exception) throws SAXException
	{
		String file = exception.getSystemId();
		int line = exception.getLineNumber();
		int col = exception.getColumnNumber();
		String msg = exception.getLocalizedMessage();

		System.out.println(file + ":" + line + "," + col + ": (Fatal Error) " + msg);

        nErrors++;
	}
}
