package net.sourceforge.rtf;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import javax.xml.transform.ErrorListener;
import javax.xml.transform.TransformerException;

import org.apache.commons.logging.Log;

public class SimpleErrorListener implements ErrorListener {

	private Log log;
	private Collection<TransformerException> errors = new ArrayList<TransformerException>();
	private Collection<TransformerException> warnings = new ArrayList<TransformerException>();
	
	public SimpleErrorListener(Log log) {
		super();
		this.log = log;
	}

	public void error(TransformerException arg0) throws TransformerException {
		log.error(arg0);
		errors.add(arg0);
	}

	public void fatalError(TransformerException arg0)
			throws TransformerException {
		log.fatal(arg0);
		errors.add(arg0);
	}

	public void warning(TransformerException arg0) throws TransformerException {
		log.warn(arg0);
		warnings.add(arg0);
	}
	
	public boolean hasErrors() {
		return errors.size() > 0;
	}

	public StringBuffer getAllMessages() {
		StringBuffer retMsg = new StringBuffer();
		Iterator<TransformerException> i1 = errors.iterator();
		while (i1.hasNext()) {
			TransformerException transformerException = (TransformerException) i1.next();
			retMsg.append("error: " + transformerException.getLocalizedMessage()).append("\n\r");
		}
		i1 = warnings.iterator();
		while (i1.hasNext()) {
			TransformerException transformerException = (TransformerException) i1.next();
			retMsg.append("warning: " + transformerException.getLocalizedMessage()).append("\n\r");
		}
		return retMsg;
	}
}
