/* Copyright (C) 2009  CSE,IIT Bombay  http://www.cse.iitb.ac.in

This file is part of the ConStore open source storage facility for concept-nets.

ConStore is free software and distributed under the 
Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License;
you can copy, distribute and transmit the work
with the work attribution in the manner specified by the author or licensor.
You may not use this work for commercial purposes and may not alter, 
transform, or build upon this work.

Please refer the legal code of the license, available at
http://creativecommons.org/licenses/by-nc-nd/3.0/legalcode

ConStore 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.  */

package iitb.con.util;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Properties Configuration util
 * 
 * @author Prathab K
 *
 */
public class PropertiesConfig {

    static Logger log = Logger.getLogger(PropertiesConfig.class.getName());

    /**
     * Loads the properties from the config file
     * @param fileName config file name
     * @return {@link Properties}
     */
    public static Properties loadProperties(String fileName) {
        Properties properties = new Properties();
        try {
            FileInputStream innputFile = new FileInputStream (fileName);
            properties.load(innputFile);
            innputFile.close();
        } catch (IOException ioException) {
            log.log(Level.SEVERE,"Error in reading the file '" + fileName + "'",ioException);
        }
        return properties;
    }
    
    /**
     * Loads the properties from the config file, if file is inside a package
     * @param clazz package class
     * @param fileName config file name
     * @return {@link Properties}
     */
    public static Properties loadProperties(Class clazz, String fileName) {
        Properties properties = null;
        
        InputStream is = null;

        if (clazz != null) {
            is = clazz.getResourceAsStream(fileName);
        } else {
            try {
                is = new FileInputStream(fileName);
            } catch (FileNotFoundException fe) {
                log.log(Level.SEVERE,"Error in reading the file '" + fileName + "'",fe);
                return null;
            }
        }
        try {
            properties = new Properties();
            properties.load(is);
            is.close();
            return properties;
        }catch (IOException ioException) {
            log.log(Level.SEVERE,"Error in reading the file '" + fileName + "'",ioException);
            return null;
        }
    }

    /**
     * Stores the properties into the config file
     * @param fileName config file name 
     * @param properties {@link Properties}
     */
    public static void storeProperties(String fileName, Properties properties) {
        try {
            FileOutputStream outputFile = new FileOutputStream (fileName) ;
            properties.store(outputFile, null);
            outputFile.close();
        } catch (IOException ioException) {
            log.log(Level.SEVERE,"Error in writing the file ",ioException);
        }
    }
    
    /**
     * Stores the properties into the config file, if file is inside a package
     * @param clazz package class
     * @param fileName config file name 
     * @param properties {@link Properties}
     */
    public static void storeProperties(Class clazz, String fileName, Properties properties) {
        try {
            FileOutputStream outputFile = 
                new FileOutputStream (resolveName(PropertiesConfig.class, fileName)) ;
            properties.store(outputFile, null);
            outputFile.close();
        } catch (IOException ioException) {
            log.log(Level.SEVERE,"Error in writing the file ",ioException);
        }
    }
    
    /**
     * Add a package name prefix if the name is not absolute. <br> 
     * Remove leading "/" if name is absolute
     * @param clazz package class
     * @param fileName config file name
     * @return the resolved class string
     */
    private static String resolveName(Class clazz, String name) {
        if (name == null) {
            return name;
        }
        if (!name.startsWith("/")) {
            Class c = clazz;
            while (c.isArray()) {
                c = c.getComponentType();
            }
            String baseName = c.getName();
            int index = baseName.lastIndexOf('.');
            if (index != -1) {
                name = baseName.substring(0, index).replace('.', '/')
                    +"/"+name;
            }
        } else {
            name = name.substring(1);
        }
        return name;
    }
}