From 3ca86f0511be09bd2a1c2d30e7b34273be845dab Mon Sep 17 00:00:00 2001 From: "paul.xiong" Date: Wed, 18 Oct 2017 19:27:19 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9Component=E7=B1=BB=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E7=9A=84=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../liteflow/parser/FlowParser.java | 141 ++++++++---------- 1 file changed, 64 insertions(+), 77 deletions(-) diff --git a/src/main/java/com/thebeastshop/liteflow/parser/FlowParser.java b/src/main/java/com/thebeastshop/liteflow/parser/FlowParser.java index 32dedef9e..e185a19f0 100644 --- a/src/main/java/com/thebeastshop/liteflow/parser/FlowParser.java +++ b/src/main/java/com/thebeastshop/liteflow/parser/FlowParser.java @@ -10,22 +10,17 @@ package com.thebeastshop.liteflow.parser; import java.util.ArrayList; -import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; -import org.apache.commons.lang3.ClassUtils; import org.apache.commons.lang3.StringUtils; import org.dom4j.Document; import org.dom4j.Element; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import com.poolik.classfinder.ClassFinder; -import com.poolik.classfinder.filter.Subclass; -import com.poolik.classfinder.info.ClassInfo; import com.thebeastshop.liteflow.core.Component; import com.thebeastshop.liteflow.entity.config.Chain; import com.thebeastshop.liteflow.entity.config.Condition; @@ -38,85 +33,77 @@ import com.thebeastshop.liteflow.util.IOUtil; @SuppressWarnings("unchecked") public class FlowParser { - + private static final Logger LOG = LoggerFactory.getLogger(FlowParser.class); - + private static final String ENCODING_FORMAT = "UTF-8"; - - public static void parseLocal(String rulePath) throws Exception{ + + public static void parseLocal(String rulePath) throws Exception { String ruleContent = IOUtil.read(rulePath, ENCODING_FORMAT); parse(ruleContent); } - - public static void parse(String content) throws Exception{ + + public static void parse(String content) throws Exception { Document document = Dom4JReader.getFormatDocument(content); parse(document); } - - public static void parse(Document document) throws Exception{ - Element rootElement = document.getRootElement(); - - //find所有的组件实现类并实例化 - Map compInstanceMap = new HashMap(); - ClassFinder finder = new ClassFinder().addClasspath(); - Collection classList = finder.findClasses(Subclass.of(Component.class)); - Component component = null; - for(ClassInfo classInfo : classList){ - LOG.info("component [{}] has been registered to flow parse manager",classInfo.getClassName()); - component = (Component)ClassUtils.getClass(classInfo.getClassName()).newInstance(); - compInstanceMap.put(classInfo.getClassName(), component); - } - - //解析node节点 - List nodeList = rootElement.element("nodes").elements("node"); - String id = null; - String clazz = null; - Node node = null; - Map nodeMap = new HashMap(); - for(Element e : nodeList){ - node = new Node(); - id = e.attributeValue("id"); - clazz = e.attributeValue("class"); - node.setId(id); - node.setClazz(clazz); - if(!compInstanceMap.containsKey(clazz)){ - LOG.error("couldn't find [{}] in registered component map",clazz); - } - component = compInstanceMap.get(clazz); - component.setNodeId(id); - node.setInstance(component); - nodeMap.put(id, node); - } - - //解析chain节点 - String chainName = null; - String condArrayStr = null; - String[] condArray = null; - List chainNodeList = null; - List conditionList = null; - - List chainList = rootElement.elements("chain"); - for(Element e : chainList){ - chainName = e.attributeValue("name"); - conditionList = new ArrayList(); - for (Iterator it = e.elementIterator(); it.hasNext();) { - Element condE = it.next(); - condArrayStr = condE.attributeValue("value"); - if(StringUtils.isBlank(condArrayStr)){ - continue; - } - chainNodeList = new ArrayList(); - condArray = condArrayStr.split(","); - for (int i = 0; i < condArray.length; i++) { - chainNodeList.add(nodeMap.get(condArray[i])); - } - if(condE.getName().equals("then")){ - conditionList.add(new ThenCondition(chainNodeList)); - }else if(condE.getName().equals("when")){ - conditionList.add(new WhenCondition(chainNodeList)); - } + + public static void parse(Document document) throws Exception { + try { + Element rootElement = document.getRootElement(); + + // 解析node节点 + List nodeList = rootElement.element("nodes").elements("node"); + String id = null; + String clazz = null; + Node node = null; + Component component = null; + Map nodeMap = new HashMap(); + for (Element e : nodeList) { + node = new Node(); + id = e.attributeValue("id"); + clazz = e.attributeValue("class"); + node.setId(id); + node.setClazz(clazz); + component = (Component) Class.forName(clazz).newInstance(); + component.setNodeId(id); + node.setInstance(component); + nodeMap.put(id, node); } - FlowBus.addChain(chainName, new Chain(conditionList)); - } - } + + // 解析chain节点 + String chainName = null; + String condArrayStr = null; + String[] condArray = null; + List chainNodeList = null; + List conditionList = null; + + List chainList = rootElement.elements("chain"); + for (Element e : chainList) { + chainName = e.attributeValue("name"); + conditionList = new ArrayList(); + for (Iterator it = e.elementIterator(); it.hasNext();) { + Element condE = it.next(); + condArrayStr = condE.attributeValue("value"); + if (StringUtils.isBlank(condArrayStr)) { + continue; + } + chainNodeList = new ArrayList(); + condArray = condArrayStr.split(","); + for (int i = 0; i < condArray.length; i++) { + chainNodeList.add(nodeMap.get(condArray[i])); + } + if (condE.getName().equals("then")) { + conditionList.add(new ThenCondition(chainNodeList)); + } else if (condE.getName().equals("when")) { + conditionList.add(new WhenCondition(chainNodeList)); + } + } + FlowBus.addChain(chainName, new Chain(conditionList)); + } + } catch (Exception e) { + LOG.error("FlowParser解析异常: {}", e); + } + + } }