import org.;
import org.;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
/**
* 配置处理工具类
*
* @author yml
*/
public class YamlUtil
{
private static final Logger log = ();
/**
* 读取yml文件配置
* @param fileName 文件的路径
* @return yaml文件转Map(hashmap)
* @throws FileNotFoundException 1.文件不存在 2. 文件没有读写权限
*/
public static Map<?, ?> loadYaml(String fileName) throws FileNotFoundException
{
InputStream in = ().getResourceAsStream(fileName);
return (fileName) ? (LinkedHashMap<?, ?>) new Yaml().load(in) : null;
}
public static void dumpYaml(String fileName, Map<?, ?> map) throws IOException
{
if ((fileName))
{
FileWriter fileWriter = new FileWriter(((fileName)).getFile());
DumperOptions options = new DumperOptions();
();
Yaml yaml = new Yaml(options);
(map, fileWriter);
}
}
/**
* 获取指定key的值
* @param key 要获取的值对应的key的名称
* @param fileName 文件存放路径
* @return 对应key的值(String)
*/
public static String getConfig(String key , String fileName)
{
HashMap<String, String> map = null;
try {
map = (HashMap<String, String>) loadYaml(fileName);
} catch (FileNotFoundException e) {
return null;
}
String value = (key);
if (value == null)
{
Map<?, ?> yamlMap = null;
try
{
yamlMap = (fileName);
value = ((yamlMap, key));
(key, value != null ? value : );
}
catch (FileNotFoundException ignored)
{
("获取全局配置异常 {}", key);
}
}
return value;
}
/**
* 获取指定key的值
* @param map yml配置文件,可以调34行的loadYaml()方法获取
* @param qualifiedKey key
* @return 对应key的值(Object)
*/
public static Object getProperty(Map<?, ?> map, Object qualifiedKey)
{
if (map != null && !() && qualifiedKey != null)
{
String input = (qualifiedKey);
if (!(""))
{
if (("."))
{
int index = (".");
String left = (0, index);
String right = (index + 1, ());
return getProperty((Map<?, ?>) (left), right);
}
else if ((input))
{
return (input);
}
else
{
return null;
}
}
}
return null;
}
public static void setProperty(Map<?, ?> map, Object qualifiedKey, Object value)
{
if (map != null && !() && qualifiedKey != null)
{
String input = (qualifiedKey);
if (!(""))
{
if (("."))
{
int index = (".");
String left = (0, index);
String right = (index + 1, ());
setProperty((Map<?, ?>) (left), right, value);
}
else
{
((Map<Object, Object>) map).put(qualifiedKey, value);
}
}
}
}
}