From c0bb1525252f2d7347ca937ba55445f89f0d33e4 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Tue, 2 Aug 2022 16:11:44 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8Dxmind=E8=A7=A3=E6=9E=90=E4=B8=8D=E6=94=AF?= =?UTF-8?q?=E6=8C=81xmind=E7=BB=8F=E5=85=B8=E7=89=88=E6=9C=AC=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复xmind解析不支持xmind经典版本的问题 --- .../metersphere/xmind/parser/XmindLegacy.java | 69 +++++++++++++++++-- 1 file changed, 62 insertions(+), 7 deletions(-) diff --git a/backend/src/main/java/io/metersphere/xmind/parser/XmindLegacy.java b/backend/src/main/java/io/metersphere/xmind/parser/XmindLegacy.java index ad140a55b4..437cc4def5 100644 --- a/backend/src/main/java/io/metersphere/xmind/parser/XmindLegacy.java +++ b/backend/src/main/java/io/metersphere/xmind/parser/XmindLegacy.java @@ -1,7 +1,12 @@ package io.metersphere.xmind.parser; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.performance.parse.EngineSourceParserFactory; -import org.dom4j.*; +import org.apache.commons.lang3.StringUtils; +import org.dom4j.Document; +import org.dom4j.DocumentException; +import org.dom4j.Element; +import org.dom4j.Node; import org.json.JSONObject; import org.json.XML; @@ -25,15 +30,14 @@ public class XmindLegacy { // 删除content.xml里面不能识别的字符串 xmlContent = xmlContent.replace("xmlns=\"urn:xmind:xmap:xmlns:content:2.0\"", ""); xmlContent = xmlContent.replace("xmlns:fo=\"http://www.w3.org/1999/XSL/Format\"", ""); - // 删除节点 - xmlContent = xmlContent.replace("", ""); - xmlContent = xmlContent.replace("", ""); + try { + xmlContent = removeTopicsFromString(xmlContent); + } catch (Exception e) { + LogUtil.error("移除xml中的Topic出错:", e); + } // 去除title中svg:width属性 xmlContent = xmlContent.replaceAll("", "<title>"); - //去除自由风格主题 - xmlContent = xmlContent.replaceAll("<topics type=\"detached\">", ""); - Document document = EngineSourceParserFactory.getDocument(new ByteArrayInputStream(xmlContent.getBytes("utf-8")));// 读取XML文件,获得document对象 Element root = document.getRootElement(); List<Node> topics = root.selectNodes("//topic"); @@ -86,4 +90,55 @@ public class XmindLegacy { // 设置缩进 return sheets; } + + /** + * 删除topics节点 + * + * @param xmlContent + * @return + * @throws Exception + */ + private static String removeTopicsFromString(String xmlContent) throws Exception { + Document doc = EngineSourceParserFactory.getDocument(new ByteArrayInputStream(xmlContent.getBytes("utf-8"))); + if (doc != null) { + Element root = doc.getRootElement(); + List<Element> childrenElement = root.elements(); + for (Element child : childrenElement) { + removeTopicsFromElement(child); + } + xmlContent = doc.asXML(); + } + return xmlContent; + } + + /** + * 递归删除topics节点 + * + * @param element + */ + private static void removeTopicsFromElement(Element element) { + if (element != null) { + List<Element> childrenElement = element.elements(); + List<Element> removeElements = new ArrayList<>(); + List<Element> addElements = new ArrayList<>(); + for (Element child : childrenElement) { + if (StringUtils.equalsIgnoreCase("topics", child.getName()) && StringUtils.equalsAnyIgnoreCase(child.attributeValue("type"), "attached", "detached")) { + removeElements.add(child); + addElements.addAll(child.elements()); + } + } + removeElements.forEach(item -> { + item.getParent().remove(item); + }); + addElements.forEach(item -> { + item.setParent(null); + element.add(item); + }); + childrenElement = element.elements(); + for (Element child : childrenElement) { + removeTopicsFromElement(child); + } + } + + } }