/*
 * Récupère le code collectivité ainsi que le code bugdétaire du document contrôllé en question (c)JPT
 */

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

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

import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;

import fr.gouv.finances.cp.utils.Pair;
import fr.gouv.finances.cp.xemelios.controls.AbstractUnitControl;
import fr.gouv.finances.cp.xemelios.controls.Anomalie;
import fr.gouv.finances.cp.xemelios.controls.ControlContentHandler;

public class COLLBUDG extends AbstractUnitControl {

    public static final transient String CTRL_ID = "COLLBUDG";
    private static Logger logger = Logger.getLogger(COLLBUDG.class);
//	private DocumentControlModel docModel;
    private String coll_path = null;
    private String budg_path = null;
    private String coll_lib = null;
    private String budg_lib = null;
    private ControlContentHandler cch = null;

    public void startDocument() throws SAXException {
    }

    public void startElement(String uri, String localName, String qName, Attributes atts, String xpath) {
        for (int i = 0; i < atts.getLength(); i++) {
            String path = "/".concat(xpath).concat("@").concat(atts.getLocalName(i));
            String value = atts.getValue(i);
            checkPath(path, value);
        }
    }

    public void checkPath(String path, String value) {
        if (coll_path.equals(path)) {
            Pair p = cch.getCollectivite();
            if (p == null) {
                p = new Pair();
            }
            p.key = value;
            cch.setCollectivite(p);
        }
        if (coll_lib.equals(path)) {
            Pair p = cch.getCollectivite();
            if (p == null) {
                p = new Pair();
            }
            p.libelle = value;
            cch.setCollectivite(p);
        }

        if (budg_path.equals(path)) {
            Pair p = cch.getBudget();
            if (p == null) {
                p = new Pair();
            }
            p.key = value;
            cch.setBudget(p);
        }
        if (budg_lib.equals(path)) {
            Pair p = cch.getBudget();
            if (p == null) {
                p = new Pair();
            }
            p.libelle = value;
            cch.setBudget(p);
        }

    }

    public Vector<Anomalie> endElement(String uri, String localName, String qName, String xpath) {
        return null;
    }

    public void endDocument() throws SAXException {
    }

    public void characters(char[] ch, int start, int length, String xpath) {
        checkPath(xpath.concat("/text()"), new String(ch, start, length));
    }

    public void ignorableWhitespace(char[] ch, int start, int length, String xpath) {
    }

    public void startPrefixMapping(String prefix, String uri, String xpath) {
    }

    public void endPrefixMapping(String prefix, String xpath) {
    }

    public void processingInstruction(String target, String data, String xpath) {
    }

    public void skippedEntity(String name, String xpath) {
    }

    public void setParameters(Hashtable<String, Object> params) {
    }

    public void setDocumentLocator(Locator locator) {
    }

    public String getColl_path() {
        return coll_path;
    }

    public void setColl_path(String coll_path) {
        this.coll_path = coll_path;
    }

    public ControlContentHandler getCch() {
        return cch;
    }

    public void setCch(
            ControlContentHandler cch) {
        this.cch = cch;
    }

    public String getBudg_path() {
        return budg_path;
    }

    public void setBudg_path(String budg_path) {
        this.budg_path = budg_path;
    }

    public String getBudg_lib() {
        return budg_lib;
    }

    public void setBudg_lib(
            String budg_lib) {
        this.budg_lib = budg_lib;
    }

    public String getColl_lib() {
        return coll_lib;
    }

    public void setColl_lib(
            String coll_lib) {
        this.coll_lib = coll_lib;
    }
}
