fix #I7SVZF 重构抽象Chain解析逻辑

This commit is contained in:
zy 2023-09-26 18:58:19 +08:00
parent 6a87bc42b5
commit f45ab114bd
2 changed files with 49 additions and 47 deletions

View File

@ -165,22 +165,14 @@ public class ParserHelper {
for (Document document : documentList) {
Element rootElement = document.getRootElement();
List<Element> chainList = rootElement.elements(CHAIN);
//先对继承自抽象Chain的chain进行字符串替换
chainList.stream()
.filter(e -> e.attributeValue(EXTENDS)!=null)
.forEach(e->{
String baseChainId = e.attributeValue(EXTENDS);
if(abstratChainMap.containsKey(baseChainId)) {
Element baseChain = abstratChainMap.get(baseChainId);
parseImplChain(baseChain,e,abstratChainMap,implChainSet);
}else{
throw new ChainNotFoundException(String.format("[abstract chain not found] chainName=%s", baseChainId));
}
});
//对所有非抽象chain进行解析
chainList.stream()
.filter(e -> e.attributeValue(ABSTRACT)==null || e.attributeValue(ABSTRACT).equals("false"))
.forEach(parseOneChainConsumer);
for(Element chain:chainList){
//首先需要对继承自抽象Chain的chain进行字符串替换
parseImplChain(abstratChainMap, implChainSet, chain);
//如果一个chain不为抽象chain则进行解析
if(chain.attributeValue(ABSTRACT) == null || !chain.attributeValue(ABSTRACT).equals("true")){
parseOneChainConsumer.accept(chain);
}
}
}
}
@ -257,15 +249,7 @@ public class ParserHelper {
while (chainIterator.hasNext()) {
JsonNode chainNode = chainIterator.next();
//首先需要对继承自抽象Chain的chain进行字符串替换
if(chainNode.hasNonNull(EXTENDS)){
String baseChainId = chainNode.get(EXTENDS).textValue();
if(abstratChainMap.containsKey(baseChainId)) {
JsonNode baseChain = abstratChainMap.get(baseChainId);
parseImplChain(baseChain,chainNode,abstratChainMap,implChainSet);
}else{
throw new ChainNotFoundException(String.format("[abstract chain not found] chainName=%s", baseChainId));
}
}
parseImplChain(abstratChainMap, implChainSet, chainNode);
//如果一个chain不为抽象chain则进行解析
if(chainNode.get(ABSTRACT) == null || !chainNode.get(ABSTRACT).asBoolean()){
parseOneChainConsumer.accept(chainNode);
@ -310,6 +294,42 @@ public class ParserHelper {
}
}
/**
* 解析一个带继承关系的Chain,xml格式
* @param chain 实现Chain
* @param abstratChainMap 所有的抽象Chain
* @param implChainSet 已经解析过的实现Chain
*/
private static void parseImplChain(Map<String, Element> abstratChainMap, Set<Element> implChainSet, Element chain) {
if(chain.attributeValue(EXTENDS)!=null){
String baseChainId = chain.attributeValue(EXTENDS);
if(abstratChainMap.containsKey(baseChainId)) {
Element baseChain = abstratChainMap.get(baseChainId);
internalParseImplChain(baseChain,chain,abstratChainMap,implChainSet);
}else{
throw new ChainNotFoundException(String.format("[abstract chain not found] chainName=%s", baseChainId));
}
}
}
/**
* 解析一个带继承关系的Chain,json格式
* @param chainNode 实现Chain
* @param abstratChainMap 所有的抽象Chain
* @param implChainSet 已经解析过的实现Chain
*/
private static void parseImplChain(Map<String, JsonNode> abstratChainMap, Set<JsonNode> implChainSet, JsonNode chainNode) {
if(chainNode.hasNonNull(EXTENDS)){
String baseChainId = chainNode.get(EXTENDS).textValue();
if(abstratChainMap.containsKey(baseChainId)) {
JsonNode baseChain = abstratChainMap.get(baseChainId);
internalParseImplChain(baseChain,chainNode,abstratChainMap,implChainSet);
}else{
throw new ChainNotFoundException(String.format("[abstract chain not found] chainName=%s", baseChainId));
}
}
}
/**
* 解析一个继承自baseChain的implChain,xml格式
* @param baseChain 父Chain
@ -317,19 +337,11 @@ public class ParserHelper {
* @param abstractChainMap 所有的抽象Chain
* @param implChainSet 已经解析过的实现Chain
*/
private static void parseImplChain(JsonNode baseChain,JsonNode implChain,Map<String,JsonNode> abstractChainMap,Set<JsonNode> implChainSet) {
private static void internalParseImplChain(JsonNode baseChain,JsonNode implChain,Map<String,JsonNode> abstractChainMap,Set<JsonNode> implChainSet) {
//如果已经解析过了就不再解析
if(implChainSet.contains(implChain)) return;
//如果baseChainId也是继承自其他的chain需要递归解析
if(baseChain.get(EXTENDS)!=null){
String pBaseChainId = baseChain.get(EXTENDS).textValue();
if(abstractChainMap.containsKey(pBaseChainId)) {
JsonNode pBaseChain = abstractChainMap.get(pBaseChainId);
parseImplChain(pBaseChain, baseChain, abstractChainMap, implChainSet);
}else{
throw new ChainNotFoundException(String.format("[abstract chain not found] chainName=%s", pBaseChainId));
}
}
parseImplChain(abstractChainMap, implChainSet, baseChain);
//否则根据baseChainId解析implChainId
String implChainEl = implChain.get(VALUE).textValue();
String baseChainEl = baseChain.get(VALUE).textValue();
@ -348,19 +360,11 @@ public class ParserHelper {
* @param abstractChainMap 所有的抽象Chain
* @param implChainSet 已经解析过的实现Chain
*/
private static void parseImplChain(Element baseChain,Element implChain,Map<String,Element> abstractChainMap,Set<Element> implChainSet) {
private static void internalParseImplChain(Element baseChain,Element implChain,Map<String,Element> abstractChainMap,Set<Element> implChainSet) {
//如果已经解析过了就不再解析
if(implChainSet.contains(implChain)) return;
//如果baseChainId也是继承自其他的chain需要递归解析
if(baseChain.attributeValue(EXTENDS)!=null){
String pBaseChainId = baseChain.attributeValue(EXTENDS);
if(abstractChainMap.containsKey(pBaseChainId)) {
Element pBaseChain = abstractChainMap.get(pBaseChainId);
parseImplChain(pBaseChain, baseChain, abstractChainMap, implChainSet);
}else{
throw new ChainNotFoundException(String.format("[abstract chain not found] chainName=%s", pBaseChainId));
}
}
parseImplChain(abstractChainMap, implChainSet, baseChain);
//否则根据baseChainId解析implChainId
String implChainEl = implChain.getText();
String baseChainEl = baseChain.getText();

View File

@ -23,6 +23,4 @@
{0}=THEN(a,b,{3});
{1}=SWITCH(f).to({4},k);
</chain>
</flow>