liurunyu
2024-06-28 4fe7ea4fbdc0c45f14d1d8de77e3424f826ba909
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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;
 
 
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
     */
    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接口!");
            }
        }
    }
}