/*
 * 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.io.File;
import java.io.IOException;

import org.apache.log4j.Logger;

import fr.gouv.finances.cp.xemelios.controls.models.ControlModelTech;
import fr.gouv.finances.cp.xemelios.controls.models.ParamModelTech;
import fr.gouv.finances.dgfip.xemelios.utils.FileUtils;
import java.nio.charset.Charset;
import java.util.ArrayList;

/**
 * Controls if input file has an acceptable encoding.
 * Valid encodings must be listed as parameters with name="encoding".
 * @author cmarchand
 */
public class TECH02 {
    public static final transient String ENCODING_PARAM_NAME = "encoding";
	private String inputFileEncoding="";
	private final static Logger logger = Logger.getLogger(TECH02.class);
        private ArrayList<String> acceptableEncodings;
        private String validEncodings;

        public TECH02() {
            super();
            acceptableEncodings = new ArrayList<String>();
        }
	
	public boolean encodingValid(ControlModelTech cmt,File fichierAController){
            // build the acceptable encodings list
            StringBuilder sb = new StringBuilder();
            for(ParamModelTech pmt:cmt.getParams()){
                if(ENCODING_PARAM_NAME.equals(pmt.getName())) {
                    try {
                        Charset cs = Charset.forName(pmt.getValue());
                        String encodingName = cs.name();
                        acceptableEncodings.add(encodingName);
                        sb.append(encodingName).append(',');
                    } catch(Exception ex) {
                        logger.error("while initializing charset: "+pmt.getValue(),ex);
                    }
                }
            }
            if(sb.length()>0) sb.deleteCharAt(sb.length()-1);
            validEncodings = sb.toString().replaceAll(",", ", ");

            boolean encodingValid=false;
            try {
                    inputFileEncoding = FileUtils.getFileEncoding(fichierAController);
            } catch (IOException e1) {
                    e1.printStackTrace();
            }
            try {
                Charset cs = Charset.forName(inputFileEncoding);
                inputFileEncoding = cs.name();
                encodingValid = acceptableEncodings.contains(inputFileEncoding);
            } catch(Exception ex) {
                // unknown or illega encoding => invalid
                encodingValid = false;
            }
            return encodingValid;
	}
	
	public String getInputEncoding(){
		return inputFileEncoding;		
	}

        public String getValidEncodings() {
            return validEncodings;
        }
}
