| package com.zy.common.utils; | 
|   | 
| import com.alibaba.fastjson.JSON; | 
| import com.baomidou.mybatisplus.mapper.EntityWrapper; | 
| import com.zy.asrs.entity.DataResource; | 
| import com.zy.asrs.service.DataResourceService; | 
| import org.apache.commons.io.FileUtils; | 
| import org.springframework.beans.factory.annotation.Autowired; | 
| import org.springframework.stereotype.Component; | 
| import org.yaml.snakeyaml.Yaml; | 
|   | 
| import java.io.File; | 
| import java.io.FileInputStream; | 
| import java.io.FileWriter; | 
| import java.io.IOException; | 
| import java.util.*; | 
|   | 
| @Component | 
| public class YamlUtils { | 
|   | 
|     private static final String src = "src/main/resources/application-prod.yml"; | 
|     private final static String C = "#"; | 
|     private final static String CHARSET = "UTF-8"; | 
|     //读取后的每行数据 | 
|     private static List<String> LINES = null; | 
|     @Autowired | 
|     private DataResourceService dataResourceService; | 
|   | 
|     public static void main(String[] args) { | 
|         Map<String, Integer> keyCountMap = new HashMap<>(); | 
|         try { | 
|             List<String> lines = FileUtils.readLines(new File(src), CHARSET); | 
|             for (int i = 0; i < lines.size(); i++) { | 
|                 String line = lines.get(i); | 
|                 if (line.contains(C)) { | 
|                     String[] split = line.split(C); | 
|                     String tmp = split[0]; | 
|                     String memo = split[1];//获取注释 | 
|   | 
|                     String[] split1 = tmp.split(":"); | 
|                     String name = split1[0]; | 
|                     String data = split1[1]; | 
|                     System.out.println(memo); | 
|                     System.out.println(name); | 
|                     System.out.println(data); | 
|                 } | 
|             } | 
|   | 
|         } catch (IOException e) { | 
|             e.printStackTrace(); | 
|         } | 
|     } | 
|   | 
|     public Map<String, Object> read() { | 
|         Yaml yaml = new Yaml(); | 
|         try { | 
|             LINES = FileUtils.readLines(new File(src), CHARSET);//读取后的每行数据 | 
|             Map<String, Object> resultMap = (Map<String, Object>) yaml.load(new FileInputStream(new File(src))); | 
|             Map<String, Object> wcs = (Map<String, Object>) resultMap.get("wcs-slave"); | 
|             recursion(wcs, null); | 
|             return wcs; | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|         } | 
|         return null; | 
|     } | 
|   | 
|     public void recursion(Map<String, Object> resultMap, DataResource resource) { | 
|         Iterator<String> iterator = resultMap.keySet().iterator(); | 
|         while (iterator.hasNext()) { | 
|             String key = iterator.next(); | 
|             Object value = resultMap.get(key); | 
|   | 
|             DataResource dataResource = new DataResource(); | 
|             if (value == null) { | 
|                 dataResource.setData(null); | 
|             } else if (value instanceof Map) { | 
|                 dataResource.setData(JSON.toJSONString(value)); | 
|             } else { | 
|                 dataResource.setData(value.toString()); | 
|             } | 
|             dataResource.setCreateTime(new Date()); | 
|             dataResource.setName(key); | 
|             if (resource != null) { | 
|                 dataResource.setResourceId(resource.getId()); | 
|             } | 
|   | 
|             //获取备注 | 
|             String memo = getMemoFromLines(key); | 
|             dataResource.setMemo(memo); | 
|   | 
|             dataResourceService.insert(dataResource); | 
|             if (value instanceof Map) { | 
|                 Map<String, Object> map = (Map<String, Object>) resultMap.get(key); | 
|                 recursion(map, dataResource); | 
|             } | 
|   | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 从读取的数据中获取备注信息 | 
|      */ | 
|     public String getMemoFromLines(String name) { | 
|         for (int i = 0; i < LINES.size(); i++) { | 
|             String line = LINES.get(i); | 
|             String trim = line.trim(); | 
|             String[] split1 = trim.split(":"); | 
|             String lineName = split1[0]; | 
|             if (!lineName.equals(name)) { | 
|                 continue; | 
|             } | 
|   | 
|             if (line.contains(C)) { | 
|                 String[] split = trim.split(C); | 
|                 String memo = split[1];//获取注释 | 
|                 return memo; | 
|             } | 
|         } | 
|   | 
|         return null; | 
|     } | 
|   | 
|     /** | 
|      * 设置备注到字符串list | 
|      */ | 
|     public String setMemoToLines(String[] lines) { | 
|         List<DataResource> dataResources = dataResourceService.selectList(new EntityWrapper<DataResource>().ne("memo", "")); | 
|         for (DataResource dataResource : dataResources) { | 
|             String name = dataResource.getName(); | 
|             int idx = 0; | 
|             for (String line : lines) { | 
|                 String trim = line.trim(); | 
|                 String[] split = trim.split(":"); | 
|                 String lineName = split[0]; | 
|                 if (lineName.equals(name)) { | 
|                     String[] split1 = line.split("#"); | 
|                     lines[idx] = split1[0] + " #" + dataResource.getMemo(); | 
|                 } | 
|                 idx++; | 
|             } | 
|         } | 
|   | 
|         StringBuffer sb = new StringBuffer(); | 
|         for (String line : lines) { | 
|             sb.append(line).append("\n"); | 
|         } | 
|         return sb.toString(); | 
|     } | 
|   | 
|     public void update() { | 
|         Map<String, Object> ymlData = getYmlData(); | 
|         HashMap<String, Object> hashMap = new HashMap<>(); | 
|         hashMap.put("wcs-slave", ymlData); | 
|         save(hashMap); | 
|     } | 
|   | 
|     /** | 
|      * 将配置信息保存至文件中 | 
|      */ | 
|     public void save(Map<String, Object> resultMap) { | 
|         try { | 
|             Yaml yaml = new Yaml(); | 
|             FileWriter fileWriter = new FileWriter(new File(src)); | 
|             String dumpAsMap = yaml.dumpAsMap(resultMap); | 
|             dumpAsMap = dumpAsMap.replaceAll("'", ""); | 
|             String[] lines = dumpAsMap.split("\n"); | 
|             dumpAsMap = setMemoToLines(lines); | 
|             fileWriter.write(dumpAsMap); | 
|             fileWriter.flush(); | 
|             fileWriter.close(); | 
|         } catch (Exception e) { | 
|             e.printStackTrace(); | 
|         } | 
|     } | 
|   | 
|     /** | 
|      * 遍历数据库数据,转换成yml数据 | 
|      */ | 
|     public Map<String, Object> getYmlData() { | 
|         HashMap<String, Object> hashMap = new HashMap<>(); | 
|         List<DataResource> dataResources = dataResourceService.selectRootData(); | 
|         for (DataResource dataResource : dataResources) { | 
|             getYmlDataRecursion(dataResource, hashMap); | 
|         } | 
|         return hashMap; | 
|     } | 
|   | 
|     public void getYmlDataRecursion(DataResource dataResource, Map<String, Object> map) { | 
|         List<DataResource> resources = dataResourceService.selectByResourceId(dataResource.getId()); | 
|         if (resources.size() > 0) { | 
|             HashMap<String, Object> hashMap = new HashMap<>(); | 
|             map.put(dataResource.getName(), hashMap); | 
|             for (DataResource resource : resources) { | 
|                 getYmlDataRecursion(resource, hashMap); | 
|             } | 
|         } else { | 
|             String data = dataResource.getData(); | 
|             if (data == null) { | 
|                 data = ""; | 
|             } | 
|             map.put(dataResource.getName(), data); | 
|         } | 
|     } | 
|   | 
| } |