/*
 * Cr�� le 24 sept. 07
 *
 */

package fr.gouv.finances.cp.xemelios.controls.tech;

import java.util.Hashtable;
import java.util.Vector;

import org.apache.log4j.Logger;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import fr.gouv.finances.cp.xemelios.common.config.DocumentModel;
import fr.gouv.finances.cp.xemelios.controls.Anomalie;
import fr.gouv.finances.cp.xemelios.controls.IdGenerator;
import fr.gouv.finances.cp.xemelios.controls.Node;

public class TECH03 extends DefaultHandler {
	/**
	 * @author jp.tessier
	 *  AXYUS
	 */
	private final static Logger logger = Logger.getLogger(TECH03.class);
	public static final transient String CTRL_ID = "TECH03";
	private Vector<Anomalie> anos = new Vector<Anomalie>();
	private DocumentModel docModel;
	private String domainesControles;
	public int MAX_ANOS = 300;
	
	public void startDocument() throws SAXException {
		logger.info("In "+CTRL_ID);
	}
		
	public void error(SAXParseException e) throws SAXException {
		createAnomalie(e);
	}
	
	public void fatalError(SAXParseException e) throws SAXException {
		createAnomalie(e);
	}
	
	public void warning(SAXParseException e) throws SAXException {
		createAnomalie(e);
	}
	private void createAnomalie(SAXParseException e){			
		Anomalie ano = new Anomalie(IdGenerator.nextId(),
				CTRL_ID,
				"Contr�le de la conformance au sch�ma de : "+docModel.getTitre()+".",
				CTRL_ID,
				domainesControles,
				"",
				"",
				"",
				"",
				"&lt;b&gt;Line n�"+e.getLineNumber()+" : &lt;/b&gt;"+e.getMessage().substring(e.getMessage().indexOf(":")+2,e.getMessage().length()).replaceAll("<","").replaceAll(">","").replaceAll("/",""),
				"Le fichier doit �tre conforme au sch�ma : "+docModel.getTitre()+".",
				"BLOQUANT",
				"xpath namespacise",
				new Hashtable<String, Object>());
				ano.addNode(new Node(null));
				if(!(anos.size() > MAX_ANOS)){
					anos.add(ano);
				}
	}
	
	public Vector<Anomalie> getAnomalies(){
		return anos;
	}
	
	public void setDocumentModel(DocumentModel dm){
		docModel=dm;
	}
	
	public void setDomainesControles(String dc){
		domainesControles=dc;
	}
}
