package com.bw.edgeagent.adapter.util;
import io.quarkus.logging.Log;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import java.io.*;
import java.util.HashMap;
import java.util.Map;
/**
* @Author mxf
* @Description 参考 https://blog.csdn.net/qq_44843880/article/details/107937185?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-107937185-blog-106142404.t5_refersearch_landing&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7ECTRLIST%7ERate-1-107937185-blog-106142404.t5_refersearch_landing&utm_relevant_index=1
* @Date 2022/9/22 17:48
*/
public class YmlUtil {
private YmlUtil() {
}
private static DumperOptions dumperOptions = new DumperOptions();
static {
//设置yaml读取方式为块读取
dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
dumperOptions.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN);
dumperOptions.setPrettyFlow(false);
}
public static boolean updateYaml(String url, String paramKey, Object paramValue) {
Yaml yaml = new Yaml(dumperOptions);
//yaml文件路径
String yamlUr = url;
Map map = null;
try {
//将yaml文件加载为map格式
map = yaml.loadAs(new FileInputStream(yamlUr), Map.class);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
//获取当前参数值并且修改
boolean flag = updateYaml(paramKey, paramValue, map, yamlUr, yaml);
return flag;
}
/**
* 修改yaml中属性的值
*
* @param key key是properties的方式: aaa.bbb.ccc (key不存在不修改)
* @param value 新的属性值 (新属性值和旧属性值一样,不修改)
* @param yamlToMap map结构
* @param path 路径
* @return true 修改成功,false 修改失败。
*/
public static boolean updateYaml(String key, Object value, Map<String, Object> yamlToMap, String path, Yaml yaml) {
Object oldVal = getValue(key, yamlToMap);
//未找到key 不修改
/* if (null == oldVal) {
return false;
}*/
System.out.println(key+" 原始旧值为:"+oldVal);
try {
Map<String, Object> resultMap = setValue(yamlToMap, key, value);
if (resultMap != null) {
yaml.dump(resultMap, new FileWriter(path));
return true;
} else {
return false;
}
} catch (Exception e) {
System.out.println("yaml file update failed !");
}
return false;
}
/**
* key格式:aaa.bbb.ccc
* 通过properties的方式获取yaml中的属性值
*
* @param key
* @param yamlMap
* @return
*/
public static Object getValue(String key, Map<String, Object> yamlMap) {
String[] keys = key.split("[.]");
Object o = yamlMap.get(keys[0]);
if (key.contains(".")) {
if (o instanceof Map) {
return getValue(key.substring(key.indexOf(".") + 1), (Map<String, Object>) o);
} else {
return null;
}
} else {
return o;
}
}
/**
* 修改yaml中的属性值
*/
public static Map<String, Object> setValue(Map<String, Object> map, String key, Object value) {
String[] keys = key.split("\\.");
int len = keys.length;
Map temp = map;
for (int i = 0; i < len - 1; i++) {
if (temp.containsKey(keys[i])) {
temp = (Map) temp.get(keys[i]);
} else {
return null;
}
if (i == len - 2) {
temp.put(keys[i + 1], value);
}
}
for (int j = 0; j < len - 1; j++) {
if (j == len - 1) {
map.put(keys[j], temp);
}
}
return map;
}
public boolean insertYaml(String key, Object value, Map<String, Object> yamlToMap, String path) {
Yaml yaml = new Yaml(dumperOptions);
String[] keys = key.split("\\.");
int len = keys.length;
Map temp = yamlToMap;
for (int i = 0; i < len; i++) {
if (temp.containsKey(keys[i])) {
temp = (Map) temp.get(keys[i]);
} else {
//该处对yaml的层级需要有判断,可能有问题
if (i + 2 == len) {
Map<String, Object> newMap = new HashMap<>();
newMap.put(keys[len - 1], value);
temp.put(keys[i], newMap);
break;
} else {
temp.put(keys[i], value);
break;
}
}
}
try {
yaml.dump(yamlToMap, new FileWriter(path));
} catch (Exception e) {
System.out.println("yaml file insert failed !");
return false;
}
return true;
}
/* public static void main(String[] args) {
boolean b = YmlUtil.updateYaml("./././config/application.yml", "north.gatewayId", "value1");
System.out.println("修改结果:" + b);
}*/
}
评论0
最新资源