/*
 * Copyright 
 *   2007 axYus - www.axyus.com
 *   2007 JP.Tessier - jean-philippe.tessier@axyus.com
 * 
 * This file is part of XEMELIOS.
 * 
 * XEMELIOS is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * XEMELIOS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

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.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

import fr.gouv.finances.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.common.config.EtatModel;
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 TECH01 extends DefaultHandler {
	/**
	 * @author jp.tessier
	 *  AXYUS
	 */

	private final static Logger logger = Logger.getLogger(TECH01.class);
	public static final transient String CTRL_ID = "TECH01";
	private Vector<Anomalie> anos = new Vector<Anomalie>();

	public String baliseCourante="";
	public StringBuffer message=new StringBuffer(),domaines=new StringBuffer();
	private DocumentModel docModel;

	public void error(SAXParseException e) throws SAXException {
            if(isValidError(e)) {
		Anomalie ano = new Anomalie(IdGenerator.nextId(),
				CTRL_ID,
				"Contrôle de la validité du XML.",
				CTRL_ID,
				"",
				"",
				"",
				"",
				"",
				addMessage(e),
				"Le fichier doit être un fichier xml valide.",
				"BLOQUANT",
				"xpath namespacise",
				new Hashtable<String, Object>());
		ano.addNode(new Node(null));
		anos.add(ano);
            }
	}

	public void fatalError(SAXParseException e) throws SAXException {
            if(isValidError(e)) {
		Anomalie ano = new Anomalie(IdGenerator.nextId(),
				CTRL_ID,
				"Contrôle de la validité du XML.",
				CTRL_ID,
				"",
				"",
				"",
				"",
				"",
				addMessage(e),
				"Le fichier doit être un fichier xml valide.",
				"BLOQUANT",
				"xpath namespacise",
				new Hashtable<String, Object>());
		ano.addNode(new Node(null));
		anos.add(ano);
            }
	}

	public void warning(SAXParseException e) throws SAXException {
            if(isValidError(e)) {
		Anomalie ano = new Anomalie(IdGenerator.nextId(),
				CTRL_ID,
				"Contrôle de la validité du XML.",
				CTRL_ID,
				"",
				"",
				"",
				"",
				"",
				addMessage(e),
				"Le fichier doit être un fichier xml valide.",
				"BLOQUANT",
				"xpath namespacise",
				new Hashtable<String, Object>());
		ano.addNode(new Node(null));
		anos.add(ano);
            }
	}
	private String addMessage(SAXParseException e){
		return "&lt;b&gt;Line n°"+e.getLineNumber()+" : &lt;/b&gt;"+e.getMessage().replaceAll("<","").replaceAll(">","").replaceAll("/","");
	}

	public void startDocument() throws SAXException {
		logger.info("In "+CTRL_ID);
	}
	public void setDocModel(DocumentModel dm){
		docModel=dm;
	}
	public void startElement(String URI, String localName, String rawName, Attributes attributs) throws SAXException {
		baliseCourante=localName;
		//int sizeGetEtat = docModel.getEtats().size();
		//int courant = 0;
		for(EtatModel etat:docModel.getEtats()){
			if(etat.getBalise().equals(localName)){
//				courant++;
//				if(sizeGetEtat < courant){
					domaines.append((domaines.length()>0?",":"") + localName);
//				}else{
//					domaines.append(localName);
//				}
			}
		}
	}
	
	public Vector<Anomalie> getAnomalies(){
		return anos;
	}

	public String getDomaines() {
		return domaines.toString();
	}
        
        public boolean isValidError(SAXParseException ex) {
            return !ex.getMessage().contains("processing instruction");
            //return true;
        }
}
