|
|
|
@@ -1,15 +1,22 @@ |
|
|
|
package com.ruoyi.platform.utils; |
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON; |
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import com.fasterxml.jackson.databind.PropertyNamingStrategies; |
|
|
|
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory; |
|
|
|
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.yaml.snakeyaml.DumperOptions; |
|
|
|
//import org.yaml.snakeyaml.Yaml; |
|
|
|
import org.yaml.snakeyaml.Yaml; |
|
|
|
|
|
|
|
import java.io.*; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
|
|
|
|
|
|
|
|
import org.ho.yaml.Yaml; |
|
|
|
//import org.ho.yaml.Yaml; |
|
|
|
|
|
|
|
@Component |
|
|
|
public class YamlUtils { |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -19,32 +26,15 @@ public class YamlUtils { |
|
|
|
* @param path 文件路径 |
|
|
|
* @param fileName 文件名 |
|
|
|
*/ |
|
|
|
// public static void generateYamlFile(Map<String, Object> data, String path, String fileName) { |
|
|
|
// DumperOptions options = new DumperOptions(); |
|
|
|
// options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
|
|
|
// options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); |
|
|
|
// |
|
|
|
// |
|
|
|
// // 创建Yaml实例 |
|
|
|
// Yaml yaml = new Yaml(options); |
|
|
|
// |
|
|
|
// File directory = new File(path); |
|
|
|
// if (!directory.exists()) { |
|
|
|
// boolean isCreated = directory.mkdirs(); |
|
|
|
// if (!isCreated) { |
|
|
|
// throw new RuntimeException("创建路径失败: " + path); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// String fullPath = path + "/" + fileName + ".yaml"; |
|
|
|
// |
|
|
|
// try (FileWriter writer = new FileWriter(fullPath)) { |
|
|
|
// yaml.dump(data, writer); |
|
|
|
// } catch (IOException e) { |
|
|
|
// e.printStackTrace(); |
|
|
|
// } |
|
|
|
// } |
|
|
|
public static void generateYamlFile(Map<String, Object> data, String path, String fileName) { |
|
|
|
DumperOptions options = new DumperOptions(); |
|
|
|
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
|
|
|
options.setDefaultScalarStyle(DumperOptions.ScalarStyle.PLAIN); |
|
|
|
|
|
|
|
|
|
|
|
// 创建Yaml实例 |
|
|
|
Yaml yaml = new Yaml(options); |
|
|
|
|
|
|
|
File directory = new File(path); |
|
|
|
if (!directory.exists()) { |
|
|
|
boolean isCreated = directory.mkdirs(); |
|
|
|
@@ -54,15 +44,60 @@ public class YamlUtils { |
|
|
|
} |
|
|
|
|
|
|
|
String fullPath = path + "/" + fileName + ".yaml"; |
|
|
|
File dumpFile = new File(fullPath); |
|
|
|
try { |
|
|
|
Yaml.dump(data, dumpFile); |
|
|
|
} catch (FileNotFoundException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
|
|
|
|
try (FileWriter writer = new FileWriter(fullPath)) { |
|
|
|
String dump = yaml.dump(data); |
|
|
|
|
|
|
|
yaml.dump(data, writer); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// public static void generateYamlFile1(Object data, String path, String fileName) { |
|
|
|
// try { |
|
|
|
// YAMLFactory yamlFactory = new YAMLFactory(); |
|
|
|
// yamlFactory.enable(YAMLGenerator.Feature.MINIMIZE_QUOTES); |
|
|
|
// yamlFactory.disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER); |
|
|
|
//// |
|
|
|
// ObjectMapper objectMapper = new ObjectMapper(yamlFactory); |
|
|
|
//// ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
// objectMapper.setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); |
|
|
|
// String s = objectMapper.writeValueAsString(data); |
|
|
|
// |
|
|
|
// File directory = new File(path); |
|
|
|
// if (!directory.exists()) { |
|
|
|
// boolean isCreated = directory.mkdirs(); |
|
|
|
// if (!isCreated) { |
|
|
|
// throw new RuntimeException("创建路径失败: " + path); |
|
|
|
// } |
|
|
|
// } |
|
|
|
// |
|
|
|
// try { |
|
|
|
// DumperOptions options = new DumperOptions(); |
|
|
|
// options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK); |
|
|
|
// // 创建Yaml实例 |
|
|
|
// Yaml yaml = new Yaml(options); |
|
|
|
// |
|
|
|
// String fullPath = path + "/" + fileName + ".yaml"; |
|
|
|
// FileWriter writer = new FileWriter(fullPath); |
|
|
|
// |
|
|
|
// yaml.dump(s, writer); |
|
|
|
// } catch (FileNotFoundException e) { |
|
|
|
// e.printStackTrace(); |
|
|
|
// |
|
|
|
// } catch (IOException e) { |
|
|
|
// throw new RuntimeException(e); |
|
|
|
// } |
|
|
|
// |
|
|
|
// |
|
|
|
// } catch (JsonProcessingException e) { |
|
|
|
// throw new RuntimeException(e); |
|
|
|
// } |
|
|
|
// } |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* 读取YAML文件并将其内容转换为Map<String, Object> |
|
|
|
* |
|
|
|
@@ -72,11 +107,14 @@ public class YamlUtils { |
|
|
|
public static Map<String, Object> loadYamlFile(String filePath) { |
|
|
|
Yaml yaml = new Yaml(); |
|
|
|
try (InputStream inputStream = new FileInputStream(new File(filePath))) { |
|
|
|
HashMap<String, Object> load = (HashMap<String, Object>) yaml.load(inputStream); |
|
|
|
return load; |
|
|
|
return yaml.load(inputStream); |
|
|
|
} catch (IOException e) { |
|
|
|
e.printStackTrace(); |
|
|
|
return null; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public static boolean isNumeric(String str) { |
|
|
|
return str.matches("-?\\\\d+(\\\\.\\\\d+)?"); // 匹配整数或小数,包括负数 |
|
|
|
} |
|
|
|
} |