zhubaomin
2025-04-07 1a2b07f01ba4616fd9e894dddf474b56d020158c
pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/rtuData/TreeParse.java
New file
@@ -0,0 +1,191 @@
package com.dy.rtuMw.server.rtuData;
import java.net.URL;
import java.util.*;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
public class TreeParse {
   public List<String> ids ;
   public List<String> classes ;
//   public static void main(String args[]) {
//      TreeParse o = new TreeParse();
//      TreeConfig conf = o.parseConfig();
//   }
   public TreeParse(){
      ids = new ArrayList<String>() ;
      classes = new ArrayList<String>() ;
   }
   /**
    * 解析处理器配置
    * @return
   protected TreeConfig parseConfig() {
      try {
         ClassLoader classLoader = ClassLoader.getSystemClassLoader();
         URL configFileURL = classLoader.getResource("RtuDataDealTree.xml");
         return this.parse(this.createDom(configFileURL)) ;
      } catch (Exception e) {
         System.out.println("系统启动时,初始上行数据处理任务配置出错 !");
         System.out.println(e.getMessage());
         e.printStackTrace();
         return null;
      }
   }
    */
   /**
    * 解析处理器配置
    * @return
    */
   protected TreeConfig parseConfig(ResourceLoader resourceLoader) {
      try {
         Resource resource = resourceLoader.getResource("classpath:RtuDataDealTree.xml");
         URL configFileURL = resource.getURL() ;
         return this.parse(this.createDom(configFileURL)) ;
      } catch (Exception e) {
         System.out.println("系统启动时,初始上行数据处理任务配置出错 !");
         System.out.println(e.getMessage());
         e.printStackTrace();
         return null;
      }
   }
   /**
    *
    * @return
    */
   private Document createDom(URL configFileURL) throws Exception {
      if (configFileURL == null) {
         throw new Exception("未得到上行数据处理任务配置文件!", null);
      }
      Document doc = null;
      try {
         SAXBuilder sb = new SAXBuilder();
         doc = sb.build(configFileURL);
         if (doc == null) {
            throw new Exception("未生成上行数据处理任务配置文件的DOM对象!", null);
         }
      } catch (Exception e) {
         throw new Exception("生成上行数据处理任务配置文件的DOM对象失败!", e);
      }
      return doc;
   }
   /**
    * 分析
    * @return ArrayList<String>
    * @throws Exception
    */
   private TreeConfig parse(Document doc) throws Exception {
      Element root = doc.getRootElement();
      if (root == null) {
         throw new Exception("未得到上行数据处理任务配置文件根元素project!");
      }
      List<Element> rootTasks = root.getChildren("task") ;
      if(rootTasks == null || rootTasks.size() == 0){
         throw new Exception("未得到上行数据处理任务配置文件根任务task!");
      }
      if(rootTasks.size() > 1){
         throw new Exception("未得到上行数据处理任务配置文件根任务task必须只有一个!");
      }
      TreeConfig tree = new TreeConfig() ;
      tree.taskConf = new TaskConfig() ;
      this.parseNode(rootTasks.get(0), tree, tree.taskConf) ;
      return tree ;
   }
   private void parseNode(Element taskEle, TreeConfig me, TaskConfig taskConf)throws Exception {
//      <task id="task2.1" name="构造数据" enable="true" class="" />
      if(taskEle == null){
         throw new Exception("分析上行数据处理任务配置文件出错!");
      }
      String id = taskEle.getAttributeValue("id") ;
      if(id == null || id.trim().equals("")){
         throw new Exception("上行数据处理任务配置文件中id必须配置!");
      }
      id = id.trim() ;
      if(ids.contains(id)){
         throw new Exception("上行数据处理任务配置文件中id=" + id + "重复配置!");
      }
      taskConf.id = id ;
      ids.add(id) ;
      String name = taskEle.getAttributeValue("name") ;
      if(name == null || name.trim().equals("")){
         throw new Exception("上行数据处理任务配置文件中name必须配置!");
      }
      name = name.trim() ;
      taskConf.name = name ;
      String enable = taskEle.getAttributeValue("enable") ;
      if(enable == null || !(enable.trim().equals("true") || enable.trim().equals("false"))){
         throw new Exception("上行数据处理任务配置文件中enable必须配置,并且值只能为true或false!");
      }
      if(enable.trim().equals("true")){
         taskConf.enable = true ;
      }
      if(enable.trim().equals("false")){
         taskConf.enable = false ;
      }
      String clazz = taskEle.getAttributeValue("class") ;
      if(clazz == null || clazz.trim().equals("")){
         throw new Exception("上行数据处理任务配置文件中class必须配置!");
      }
      clazz = clazz.trim() ;
      if(classes.contains(clazz)){
         throw new Exception("上行数据处理任务配置文件中class=" + clazz + "重复配置!");
      }
      taskConf.clazz = clazz ;
      classes.add(clazz) ;
      this.checkClass(taskConf.clazz) ;
      this.parseSubNode(taskEle, me) ;
   }
   private void parseSubNode(Element ele, TreeConfig parent)throws Exception {
      List<?> list = ele.getChildren("task") ;
      if(list != null && list.size() > 0){
         parent.subTreeConfs = new TreeConfig[list.size()] ;
         Iterator<?> it = list.iterator();
         Element e = null;
         int count = 0 ;
         TreeConfig me ;
         while(it.hasNext()){
            e = (Element) it.next();
            me = new TreeConfig() ;
            me.taskConf = new TaskConfig() ;
            parent.subTreeConfs[count++] = me ;
            parseNode(e, me, me.taskConf) ;
         }
      }
   }
   private void checkClass(String clazz)throws Exception {
      Class<?> c = Class.forName(clazz);
      if (c == null) {
         throw new Exception("上行数据处理任务配置文件中不能实例化class=" + clazz + "!");
      }else{
         Object o = c.getDeclaredConstructor().newInstance();
         //Object o = c.newInstance();
         if(!(o instanceof Task)){
            throw new Exception("上行数据处理任务配置文件中class=" + clazz + "必须实现Task接口!");
         }
      }
   }
}