/*
 * Copyright 
 *   2007 axYus - www.axyus.com
 *   2007 C.Marchand - christophe.marchand@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;

import fr.gouv.finances.cp.utils.xml.marshal.XmlOutputter;
import fr.gouv.finances.dgfip.utils.Pair;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;


import javax.xml.namespace.QName;
import org.apache.log4j.Logger;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;

/**
 *
 * @author chm
 */
@Deprecated
public class SaxXmlWriter extends DefaultHandler {
    private static Logger logger = Logger.getLogger(SaxXmlWriter.class);
    private static final String DEFAULT_ENCODING = "UTF-8";
    private String encoding = DEFAULT_ENCODING;
    private NamespaceContext nsCtx;
    private XmlOutputter output;
    private ArrayList<Pair> nsMappingToWrite;
    
    /** Creates a new instance of SaxXmlWriter */
    public SaxXmlWriter(OutputStreamWriter osw, String encoding) {
//        super(new Controller(new Configuration()));
        super();
        this.encoding = encoding;
        output = new XmlOutputter(osw,this.encoding);
        nsMappingToWrite = new ArrayList<Pair>();
    }
//    public void setEncoding(String encoding) {
//        this.encoding=encoding;
//    }
    public void setNamespaceContext(NamespaceContext nsCtx) {
        this.nsCtx = nsCtx;
    }
    public void writeAnomally(Anomalie ano) {
        AttributesImpl attrs = new AttributesImpl();
        attrs.addAttribute(Anomalie.ANOMALY_NS_URI,"anoId",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":anoId","string",ano.getIdAnomalie());
        attrs.addAttribute(Anomalie.ANOMALY_NS_URI,"ctrlLibelle",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":ctrlLibelle","string",ano.getControleLibelle());
        attrs.addAttribute(Anomalie.ANOMALY_NS_URI,"ctrlId",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":ctrlId","string",ano.getControleID());
        attrs.addAttribute(Anomalie.ANOMALY_NS_URI,"severity",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":severity","string",ano.getSeverity());
        attrs.addAttribute(Anomalie.ANOMALY_NS_URI,"visibility",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":visibility","string",ano.isVisible()?"visible":"hidden");
        try {
            startElement(Anomalie.ANOMALY_NS_URI,"Anomalie",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":Anomalie",attrs);
        } catch(Throwable t) {
            logger.error("while calling startElement on Anomalie",t);
        }
        try {
            startElement(Anomalie.ANOMALY_NS_URI,"ctrlRegleFonct",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":ctrlRegleFonct",new AttributesImpl());
            addCData(ano.getRegle());
            endElement(Anomalie.ANOMALY_NS_URI,"ctrlRegleFonct",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":ctrlRegleFonct");
        } catch(Throwable t) {
            logger.error("while writting ctrlRegleFonct",t);
        }
        try {
            startElement(Anomalie.ANOMALY_NS_URI,"message",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":message",new AttributesImpl());
            addCData(ano.getMessage());
            endElement(Anomalie.ANOMALY_NS_URI,"message",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":message");
        } catch(Throwable t) {
            logger.error("while writting message",t);
        }
        for(Node node:ano.getNodes()) {
            AttributesImpl attrs2 = new AttributesImpl();
            attrs2.addAttribute(Anomalie.ANOMALY_NS_URI,"id",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":id","string",node.getId());
            try {
                startElement(Anomalie.ANOMALY_NS_URI,"node",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":node",attrs2);
                endElement(Anomalie.ANOMALY_NS_URI,"node",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":node");
            } catch(Throwable t) {
                logger.error("while writting node",t);
            }
        }
        try {
            endElement(Anomalie.ANOMALY_NS_URI,"Anomalie",nsCtx.getPrefix(Anomalie.ANOMALY_NS_URI)+":Anomalie");
        } catch(Throwable t) {
            logger.error("while calling endElement on Anomalie",t);
        }
    }

    @Override
    public void startDocument() throws SAXException {
        super.startDocument();
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
//        super.startElement(uri, localName, qName, attributes);
        output.startTag(qName);
        for(int attCount=0;attCount<attributes.getLength();attCount++) {
            output.addAttribute(attributes.getQName(attCount), attributes.getValue(attCount));
        }
        if(!nsMappingToWrite.isEmpty()) {
            for(Pair p:nsMappingToWrite) {
                QName att = new QName(XMLConstants.XMLNS_ATTRIBUTE_NS_URI,p.key,"xmlns");
                output.addAttribute(att, p.libelle);
            }
            nsMappingToWrite = new ArrayList<Pair>();
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
  //      super.endElement(uri, localName, qName);
        output.endTag(qName);
    }

    @Override
    public void startPrefixMapping(String prefix, String uri) throws SAXException {
//        super.startPrefixMapping(prefix, uri);
        nsMappingToWrite.add(new Pair(prefix,uri));
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {
//        super.characters(ch, start, length);
        output.addCharacterData(new String(ch,0,length));
    }

    public void addCData(char[] ch, int start, int length) throws SAXException {
        addCData(new String(ch,start,length));
    }

    public void addCData(String s) throws SAXException {
        output.addCData(s);
    }



}
