/*
 * CheckConfigFile.java
 *
 * Created on 6 novembre 2007, 11:33
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


package fr.gouv.finances.cp.buildtools.chkconfig;

import fr.gouv.finances.dgfip.xemelios.common.config.DocumentsModel;
import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
import java.io.ByteArrayInputStream;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
import org.w3c.dom.Document;

/**
 * Effectue tous les controles necessaires sur le fichier de configuration passé en paramètre.
 * Si il y a une erreur, le code de sortie est different de 0, ce qui peut être intercepté par le script de build.
 * @author chm
 */
public class CheckConfigFile {
    private String fileName;
    
    /** Creates a new instance of CheckConfigFile */
    public CheckConfigFile(String fileName) {
        super();
        this.fileName=fileName;
    }
    
    public static void main(String[] args) {
        if(args.length==0) {
            showSyntax();
            System.exit(1);
        }
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            factory.setNamespaceAware(true);
            DocumentBuilder builder = factory.newDocumentBuilder();
//            Document doc = builder.newDocument();
//            Element root = doc.createElementNS("http://jakarta.apache.org/log4j/", "log4j:configuration");
//            Element appender = doc.createElement("appender");
//            appender.setAttribute("name", "console.appender");
//            appender.setAttribute("class","org.apache.log4j.ConsoleAppender");
//            Element layout = doc.createElement("layout");
//            layout.setAttribute("class","org.apache.log4j.PatternLayout");
//            Element param = doc.createElement("param");
//            param.setAttribute("name","ConversionPattern");
//            param.setAttribute("value","%-5p [%c] : %m%n");
//            layout.appendChild(param);
//            appender.appendChild(layout);
//            root.appendChild(appender);
//            Element logger = doc.createElement("root");
//            Element level = doc.createElement("level");
//            level.setAttribute("value", "debug");
//            logger.appendChild(level);
//            Element ref = doc.createElement("appender-ref");
//            ref.setAttribute("ref", "console.appender");
//            logger.appendChild(ref);
//            root.appendChild(ref);
            StringBuffer buff = new StringBuffer();
            buff.append("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n");
            buff.append("<!DOCTYPE log4j:configuration PUBLIC \"-//Apache Software Foundation//DTD log4j Configuration 1.0//EN\" \"conf/log4j.dtd\">\n");
            buff.append("<log4j:configuration threshold=\"all\" debug=\"false\">\n");
            buff.append("<appender name=\"console.appender\" class=\"org.apache.log4j.ConsoleAppender\">\n");
            buff.append("<layout class=\"org.apache.log4j.PatternLayout\">\n");
            buff.append("<param name=\"ConversionPattern\" value=\"%-5p [%c{1}] : %m%n\"/>\n");
            buff.append("</layout>\n");
            buff.append("</appender>\n");
            buff.append("<root>\n");
            buff.append("<level value=\"info\"/>\n");
            buff.append("<appender-ref ref=\"console.appender\"/>\n");
            buff.append("</root>\n");
            buff.append("</log4j:configuration>");
            Document root = builder.parse(new ByteArrayInputStream(buff.toString().getBytes()));
            DOMConfigurator.configure(root.getDocumentElement());
        } catch(Exception ex) {
            ex.printStackTrace();
        }
        CheckConfigFile ccf = new CheckConfigFile(args[0]);
        ccf.runChecks();
    }
    
    protected void runChecks() {
        try {
            DocumentsModel docs = Loader.getDocumentsInfos(fileName);
            docs.validate();
        } catch(Exception ex) {
            //System.err.println(ex.getMessage());
            Logger.getLogger(CheckConfigFile.class).error(ex.getMessage());
            System.exit(2);
        }
    }
    
    public static void showSyntax() {
        StringBuilder sb = new StringBuilder();
        sb.append("java ").append(CheckConfigFile.class.getName()).append(" <configFile>\n");
        sb.append("\twhere <configFile> is an absolute path name to a config file to test.\n");
    }
}
