|
@@ -3,11 +3,18 @@ package com.zkqy.common.utils.bpm;
|
|
|
import cn.hutool.core.util.XmlUtil;
|
|
|
import com.zkqy.common.utils.uuid.IdUtils;
|
|
|
import org.w3c.dom.*;
|
|
|
+import org.xml.sax.SAXException;
|
|
|
|
|
|
+import javax.xml.parsers.ParserConfigurationException;
|
|
|
import javax.xml.transform.Transformer;
|
|
|
import javax.xml.transform.TransformerFactory;
|
|
|
import javax.xml.transform.dom.DOMSource;
|
|
|
import javax.xml.transform.stream.StreamResult;
|
|
|
+import javax.xml.xpath.XPath;
|
|
|
+import javax.xml.xpath.XPathConstants;
|
|
|
+import javax.xml.xpath.XPathExpressionException;
|
|
|
+import javax.xml.xpath.XPathFactory;
|
|
|
+import java.io.IOException;
|
|
|
import java.io.StringWriter;
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.HashMap;
|
|
@@ -184,6 +191,7 @@ public class XmlDataParserUtils {
|
|
|
return resultMap;
|
|
|
}
|
|
|
|
|
|
+ //递归查找下一节点
|
|
|
private static Map<String,Integer> recursiveParse(NodeList nodeList, String currentId ,Map<String,Integer> map, List<String> list) {
|
|
|
for (int i = 0; i < nodeList.getLength(); i++) {
|
|
|
Node node = nodeList.item(i);
|
|
@@ -202,5 +210,214 @@ public class XmlDataParserUtils {
|
|
|
return map;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 清空异常节点
|
|
|
+ * @param xmlData xml内容
|
|
|
+ * @param currentNodeId 当前节点ID
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String clearExceptionTask(String xmlData,String currentNodeId) throws Exception{
|
|
|
+ //将xml文件内容转成Document对象
|
|
|
+ Document document = XmlUtil.parseXml(xmlData);
|
|
|
+
|
|
|
+ //得到当前所有节点ID
|
|
|
+ Map<String, Object> allNodeKey = getAllNodeKey(xmlData);
|
|
|
+ List<String> list = (List<String>) allNodeKey.get("nodeList");
|
|
|
+ //截取当前节点后的所有ID
|
|
|
+ list = new ArrayList<>(list.subList(list.indexOf(currentNodeId),list.size()));
|
|
|
+
|
|
|
+ //获取所有异常节点,筛选出正常节点和异常节点
|
|
|
+ List<String> exceptionList = new ArrayList<>();
|
|
|
+ for (int i = 1; i < list.size(); i++){
|
|
|
+ Element node = getNodeById(document, list.get(i));
|
|
|
+ String nodeTagName = node.getTagName();
|
|
|
+ if (nodeTagName.equals("bpmn:exceptionTask")){
|
|
|
+ exceptionList.add(list.get(i));
|
|
|
+ list.remove(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ System.err.println(exceptionList.toString());
|
|
|
+
|
|
|
+ // 循环正常节点,修改下一个指向
|
|
|
+ for (int i = 0; i < list.size() - 1; i++ ) {
|
|
|
+
|
|
|
+ //查看下一节点标签是否异常节点,如果是异常节点,先找到当前节点的序列线,记录下来
|
|
|
+ Element nextNode = getNodeById(document, list.get(i + 1));
|
|
|
+ String nodeTagName = nextNode.getTagName();
|
|
|
+// if (nodeTagName.equals("bpmn:userTask")) {
|
|
|
+// continue;
|
|
|
+// } else {
|
|
|
+ // 得到上一个正常节点得序列流元素,修改targetRef= 下一个节点ID
|
|
|
+ Element currentSequenceFlow = getNodeById1(document,list.get(i));
|
|
|
+ currentSequenceFlow.setAttribute("targetRef",list.get(i+1));
|
|
|
+ //修改下一个节点入口incoming=第一个节点序列流ID
|
|
|
+ Element elementOutgoing = XmlUtil.getElement(nextNode,"bpmn:incoming");
|
|
|
+ elementOutgoing.setTextContent(currentSequenceFlow.getAttributes().getNamedItem("id").getNodeValue());
|
|
|
+// }
|
|
|
+
|
|
|
+ }
|
|
|
+ //删除标签
|
|
|
+ for (int i = 0; i < exceptionList.size(); i++) {
|
|
|
+ System.err.println(exceptionList.get(i));
|
|
|
+ Element node = getNodeById(document, exceptionList.get(i));
|
|
|
+ System.err.println(node.getTagName());
|
|
|
+// System.err.println(node.getAttributes().getNamedItem("id").getNodeValue());
|
|
|
+ node.getParentNode().removeChild(node);
|
|
|
+
|
|
|
+ Element nodeXian = getNodeById1(document, exceptionList.get(i));
|
|
|
+ System.err.println(nodeXian.getTagName());
|
|
|
+ System.err.println(nodeXian.getAttributes().getNamedItem("id").getNodeValue());
|
|
|
+ nodeXian.getParentNode().removeChild(nodeXian);
|
|
|
+ }
|
|
|
+
|
|
|
+//将document对象转成字符串
|
|
|
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
|
|
|
+ Transformer transformer = transformerFactory.newTransformer();
|
|
|
+ DOMSource source = new DOMSource(document);
|
|
|
+ StreamResult result = new StreamResult(new StringWriter()); // 使用StringWriter来将结果输出为字符串
|
|
|
+ transformer.transform(source, result);
|
|
|
+ String data = result.getWriter().toString();
|
|
|
+
|
|
|
+// for (int i = 1; i < list.size(); i++){
|
|
|
+// //查看下一节点标签是否异常节点,如果是异常节点,先找到当前节点的序列线,记录下来
|
|
|
+// String nodeType = getNodeById(xmlData, list.get(i));
|
|
|
+//
|
|
|
+// if(nodeType.equals("bpmn:userTask")){
|
|
|
+// continue;
|
|
|
+// } else {
|
|
|
+// // 得到上一个正常节点得序列流ID
|
|
|
+// String currentSequenceFlow = getNodeById1(xmlData,list.get(i-1));
|
|
|
+//
|
|
|
+//
|
|
|
+// }
|
|
|
+// }
|
|
|
+ return data;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Element getNodeById(Document document,String nodeId){
|
|
|
+// Document document= XmlUtil.parseXml(xmlData);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 创建XPath表达式来查找所有id属性为nodeId的元素
|
|
|
+// String expression = "//*[local-name()='userTask' and namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@id='Activity_db1ad3cc-30d2-44c8-9371-647a9bcbb7e5']";
|
|
|
+ String expression = "//*[namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@id='"+nodeId+"']";
|
|
|
+ XPath xpath = XPathFactory.newInstance().newXPath();
|
|
|
+ NodeList nodeList = (NodeList) xpath.compile(expression).evaluate(document, XPathConstants.NODESET);
|
|
|
+
|
|
|
+ // 遍历节点列表并获取第一个匹配的元素
|
|
|
+ Element userTaskElement = null;
|
|
|
+ for (int i = 0; i < nodeList.getLength(); i++) {
|
|
|
+ Node node = nodeList.item(i);
|
|
|
+ if (node instanceof Element) {
|
|
|
+ userTaskElement = (Element) node;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否找到了匹配的元素
|
|
|
+ if (userTaskElement != null) {
|
|
|
+// System.out.println("Found user task element: " + userTaskElement.getTagName());
|
|
|
+// System.out.println("Name attribute: " + userTaskElement.getAttribute("name")); // 打印name属性值
|
|
|
+// return userTaskElement.getTagName();
|
|
|
+ return userTaskElement;
|
|
|
+ } else {
|
|
|
+ System.out.println("No matching user task element found.");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static Element getNodeById1(Document document,String nodeId){
|
|
|
+// Document document= XmlUtil.parseXml(xmlData);
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 创建XPath表达式来查找所有id属性为nodeId的元素
|
|
|
+// String expression = "//*[local-name()='userTask' and namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@id='Activity_db1ad3cc-30d2-44c8-9371-647a9bcbb7e5']";
|
|
|
+ String expression = "//*[namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@sourceRef='"+nodeId+"']";
|
|
|
+ XPath xpath = XPathFactory.newInstance().newXPath();
|
|
|
+ NodeList nodeList = (NodeList) xpath.compile(expression).evaluate(document, XPathConstants.NODESET);
|
|
|
+
|
|
|
+ // 遍历节点列表并获取第一个匹配的元素
|
|
|
+ Element userTaskElement = null;
|
|
|
+ for (int i = 0; i < nodeList.getLength(); i++) {
|
|
|
+ Node node = nodeList.item(i);
|
|
|
+ if (node instanceof Element) {
|
|
|
+ userTaskElement = (Element) node;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查是否找到了匹配的元素
|
|
|
+ if (userTaskElement != null) {
|
|
|
+// System.out.println("Found user task element: " + userTaskElement.getTagName());
|
|
|
+// System.out.println("Name attribute: " + userTaskElement.getAttribute("name")); // 打印name属性值
|
|
|
+// return userTaskElement.getAttribute("id");
|
|
|
+ return userTaskElement;
|
|
|
+ } else {
|
|
|
+ System.out.println("No matching user task element found.");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+// private static String getNodeById2(String xmlData,String nodeId){
|
|
|
+// Document document= XmlUtil.parseXml(xmlData);
|
|
|
+//
|
|
|
+// try {
|
|
|
+// // 创建XPath表达式来查找所有id属性为nodeId的元素
|
|
|
+//// String expression = "//*[local-name()='userTask' and namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@id='Activity_db1ad3cc-30d2-44c8-9371-647a9bcbb7e5']";
|
|
|
+// String expression = "//*[namespace-uri()='http://www.omg.org/spec/BPMN/20100524/MODEL'][@targetRef='"+nodeId+"']";
|
|
|
+// XPath xpath = XPathFactory.newInstance().newXPath();
|
|
|
+// NodeList nodeList = (NodeList) xpath.compile(expression).evaluate(document, XPathConstants.NODESET);
|
|
|
+//
|
|
|
+// // 遍历节点列表并获取第一个匹配的元素
|
|
|
+// Element userTaskElement = null;
|
|
|
+// for (int i = 0; i < nodeList.getLength(); i++) {
|
|
|
+// Node node = nodeList.item(i);
|
|
|
+// if (node instanceof Element) {
|
|
|
+// userTaskElement = (Element) node;
|
|
|
+// break;
|
|
|
+// }
|
|
|
+// }
|
|
|
+//
|
|
|
+// // 检查是否找到了匹配的元素
|
|
|
+// if (userTaskElement != null) {
|
|
|
+// System.out.println("Found user task element: " + userTaskElement.getTagName());
|
|
|
+// System.out.println("Name attribute: " + userTaskElement.getAttribute("name")); // 打印name属性值
|
|
|
+// return userTaskElement.getAttribute("id");
|
|
|
+// } else {
|
|
|
+// System.out.println("No matching user task element found.");
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+//
|
|
|
+// } catch (Exception e) {
|
|
|
+// e.printStackTrace();
|
|
|
+// }
|
|
|
+// return null;
|
|
|
+// }
|
|
|
+
|
|
|
+
|
|
|
+// public Map<String,String> getAaa(String xmlData,List<String> list,Map<String,String> map){
|
|
|
+// map.put("",getNodeById2(xmlData,list.get(0)));
|
|
|
+// for (String s : list){
|
|
|
+//// map.put("rukou",);
|
|
|
+// String str = getNodeById(xmlData,s);
|
|
|
+// if(!str.equals("bpmn:exceptionTask")){
|
|
|
+// break;
|
|
|
+// }else {
|
|
|
+//
|
|
|
+// }
|
|
|
+// }
|
|
|
+// return map;
|
|
|
+// }
|
|
|
+
|
|
|
|
|
|
}
|