/*
 * 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 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;

import javax.xml.namespace.NamespaceContext;

import net.sf.saxon.Configuration;
import net.sf.saxon.Controller;
import net.sf.saxon.IdentityTransformerHandler;

import org.apache.log4j.Logger;
import org.xml.sax.helpers.AttributesImpl;

/**
 *
 * @author chm
 */
public class SaxXmlWriter extends IdentityTransformerHandler {
    private static Logger logger = Logger.getLogger(SaxXmlWriter.class);
    private String encoding = "UTF-8";
    private NamespaceContext nsCtx;
    
    /** Creates a new instance of SaxXmlWriter */
    public SaxXmlWriter() {
        super(new Controller(new Configuration()));
    }
    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());
        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());
            char[] ch = ano.getRegle().toCharArray();
            characters(ch,0,ch.length);
            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());
            char[] ch = ano.getMessage().toCharArray();
            characters(ch,0,ch.length);
            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 writtind 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);
        }
    }
}
