/*
 * 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 Lesser General Public License as published by
 * the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.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.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;
	
    @Override
	public void startDocument() throws SAXException {
		logger.info("In "+CTRL_ID);
	}
		
    @Override
	public void error(SAXParseException e) throws SAXException {
		createAnomalie(e);
	}
	
    @Override
	public void fatalError(SAXParseException e) throws SAXException {
		createAnomalie(e);
	}
	
    @Override
	public void warning(SAXParseException e) throws SAXException {
		createAnomalie(e);
	}
	private void createAnomalie(SAXParseException e) {
            if(isValidError(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("/",""),
//				"<b>Line n°"+e.getLineNumber()+" : </b>"+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;
	}
        public boolean isValidError(SAXParseException ex) {
            return !ex.getMessage().contains("processing instruction");
        }
}
