package com.ejie.m62fb.util;

import java.util.List;
import java.util.Locale;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/**
 * 
XXXX
 * 
 */
public class ParserErrorHandler implements ErrorHandler {
	private static final Logger logger = LoggerFactory
			.getLogger(ParserErrorHandler.class);

	private ReloadableResourceBundleMessageSource messageSource;
	Locale locale = LocaleContextHolder.getLocale();
	private List warnings;

	private List errors;

	/**
     *  
     */
	public ParserErrorHandler(List warnings, List errors,
			ReloadableResourceBundleMessageSource messageSource) {
		super();
		this.warnings = warnings;
		this.errors = errors;
		this.messageSource = messageSource;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.ErrorHandler#warning(org.xml.sax.SAXParseException)
	 */
	public void warning(SAXParseException exception) throws SAXException {
		warnings.add(messageSource.getMessage("Warning.7",
				new Object[] { Integer.toString(exception.getLineNumber()),
						exception.getMessage() }, locale));
		logger.warn(messageSource.getMessage("Warning.7",
				new Object[] { Integer.toString(exception.getLineNumber()),
						exception.getMessage() }, locale));
		throw new SAXException(exception);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.ErrorHandler#error(org.xml.sax.SAXParseException)
	 */
	public void error(SAXParseException exception) throws SAXException {
		errors.add(messageSource.getMessage("RuntimeError.4",
				new Object[] { Integer.toString(exception.getLineNumber()),
						exception.getMessage() }, locale));
		logger.error(messageSource.getMessage("RuntimeError.4",
				new Object[] { Integer.toString(exception.getLineNumber()),
						exception.getMessage() }, locale));
		throw new SAXException(exception);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.xml.sax.ErrorHandler#fatalError(org.xml.sax.SAXParseException)
	 */
	public void fatalError(SAXParseException exception) throws SAXException {
		errors.add(messageSource.getMessage("RuntimeError.4",
				new Object[] { Integer.toString(exception.getLineNumber()),
						exception.getMessage() }, locale));
		logger.error(messageSource.getMessage("RuntimeError.4",
				new Object[] { Integer.toString(exception.getLineNumber()),
						exception.getMessage() }, locale));
		throw new SAXException(exception);
	}
}
