From 1a2b07f01ba4616fd9e894dddf474b56d020158c Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期一, 07 四月 2025 15:18:51 +0800 Subject: [PATCH] 整理版本 --- pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/rtuData/TreeParse.java | 191 +++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 191 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/rtuData/TreeParse.java b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/rtuData/TreeParse.java new file mode 100644 index 0000000..1e0afa7 --- /dev/null +++ b/pipIrr-platform/pipIrr-mw/pipIrr-mw-rtu/src/main/java/com/dy/rtuMw/server/rtuData/TreeParse.java @@ -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>() ; + } + + /** + * 瑙f瀽澶勭悊鍣ㄩ厤缃� + * @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; + } + } + */ + + /** + * 瑙f瀽澶勭悊鍣ㄩ厤缃� + * @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("鐢熸垚涓婅鏁版嵁澶勭悊浠诲姟閰嶇疆鏂囦欢鐨凞OM瀵硅薄澶辫触!", 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("涓婅鏁版嵁澶勭悊浠诲姟閰嶇疆鏂囦欢涓璱d蹇呴』閰嶇疆锛�"); + } + id = id.trim() ; + if(ids.contains(id)){ + throw new Exception("涓婅鏁版嵁澶勭悊浠诲姟閰嶇疆鏂囦欢涓璱d=" + id + "閲嶅閰嶇疆锛�"); + } + taskConf.id = id ; + ids.add(id) ; + + String name = taskEle.getAttributeValue("name") ; + if(name == null || name.trim().equals("")){ + throw new Exception("涓婅鏁版嵁澶勭悊浠诲姟閰嶇疆鏂囦欢涓璶ame蹇呴』閰嶇疆锛�"); + } + 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("涓婅鏁版嵁澶勭悊浠诲姟閰嶇疆鏂囦欢涓璭nable蹇呴』閰嶇疆锛屽苟涓斿�煎彧鑳戒负true鎴杅alse锛�"); + } + 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("涓婅鏁版嵁澶勭悊浠诲姟閰嶇疆鏂囦欢涓璫lass蹇呴』閰嶇疆锛�"); + } + clazz = clazz.trim() ; + if(classes.contains(clazz)){ + throw new Exception("涓婅鏁版嵁澶勭悊浠诲姟閰嶇疆鏂囦欢涓璫lass=" + 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("涓婅鏁版嵁澶勭悊浠诲姟閰嶇疆鏂囦欢涓璫lass=" + clazz + "蹇呴』瀹炵幇Task鎺ュ彛锛�"); + } + } + } +} + + -- Gitblit v1.8.0