package com.censoft.db;

import java.io.*;
import java.util.Properties;

/**
 * @author ：wyl
 * @date ：Created in 9/19/2020 5:05 PM
 */
public class PropertiesUtils {

    private static Properties pro = new Properties();
    private static final String ACTION_STR = "properties.action";
    private static final String PRO_CHANGE_LEFT = "${";
    private static final String PRO_CHANGE_RIGHT = "}";
    static {
        InputStream in;
        //String u = PropertiesUtils.class.getResource("/").getPath().replace("%20", " ");
        //String path = u + "app.properties";
        try {

            in =  DBUtil.class.getResourceAsStream("db.properties");
            pro.load(new InputStreamReader(in, "utf-8"));
           /* if(pro.getProperty(ACTION_STR) != null && !"".equals(pro.getProperty(ACTION_STR))){
                String replace = pro.getProperty("properties.action").replace("，", ",");
                String[] split = replace.split(",");
                for (String s : split) {
                    if(s!=null && !"".equals(s)){
                        path = u + s + ".properties";
                        try {
                            in =  new BufferedInputStream(new FileInputStream(path));
                            pro.load(new InputStreamReader(in, "utf-8"));
                        } catch (FileNotFoundException e) {
                            System.err.println("##@%配置文件加载失败%@##");
                        }

                    }
                }
            }*/
            //change();
            System.out.println("##@%配置文件加载成功%@##" + pro);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
    private PropertiesUtils() {
        super();
    }

    public static String getProperty(String key) {
        return pro.getProperty(key);
    }
    private static void change(){
        for (String key : pro.stringPropertyNames()) {
            pro.setProperty(key,change(pro.getProperty(key)));
        }
    }

    private static String change(String src){
        if(src.contains(PRO_CHANGE_LEFT) && src.contains(PRO_CHANGE_RIGHT)){
            String before = src.substring(0,src.indexOf(PRO_CHANGE_LEFT));
            String mid = src.substring(src.indexOf(PRO_CHANGE_LEFT)+2,src.indexOf(PRO_CHANGE_RIGHT));
            String after = src.substring(src.indexOf(PRO_CHANGE_RIGHT)+1);
            src = before + pro.getProperty(mid) + after;
            src = change(src);
        }
        return src;
    }
}
