fix(测试跟踪): 功能用例导入xmind特殊字符解析报错
--bug=1032892 --user=陈建星 【测试跟踪】github#27543,xmind里有特殊符号,导致导入功能用例失败 https://www.tapd.cn/55049933/s/1436704
This commit is contained in:
parent
3e4ce1d488
commit
a72ba16ee9
|
@ -1,70 +0,0 @@
|
|||
package io.metersphere.utils;
|
||||
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.helpers.XMLFilterImpl;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
public class XmlUtils {
|
||||
public static final boolean IS_TRANS = false;
|
||||
|
||||
public static Document getDocument(InputStream source) throws DocumentException {
|
||||
SAXReader reader = new SAXReader();
|
||||
try {
|
||||
reader.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
|
||||
reader.setFeature("http://xml.org/sax/features/external-general-entities", false);
|
||||
reader.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
|
||||
reader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
|
||||
}catch (Exception e){
|
||||
LogUtil.error(e);
|
||||
}
|
||||
if (!IS_TRANS) {
|
||||
reader.setXMLFilter(XmlUtils.getFilter());
|
||||
}
|
||||
return reader.read(source);
|
||||
}
|
||||
|
||||
public static XMLFilterImpl getFilter() {
|
||||
return new XMLFilterImpl() {
|
||||
@Override
|
||||
public void characters(char[] ch, int start, int length) throws SAXException {
|
||||
String text = new String(ch, start, length);
|
||||
|
||||
if (length == 1) {
|
||||
if (StringUtils.equals("&", text)) {
|
||||
char[] escape = "&".toCharArray();
|
||||
super.characters(escape, start, escape.length);
|
||||
return;
|
||||
}
|
||||
if (StringUtils.equals("\"", text)) {
|
||||
char[] escape = """.toCharArray();
|
||||
super.characters(escape, start, escape.length);
|
||||
return;
|
||||
}
|
||||
if (StringUtils.equals("'", text)) {
|
||||
char[] escape = "'".toCharArray();
|
||||
super.characters(escape, start, escape.length);
|
||||
return;
|
||||
}
|
||||
if (StringUtils.equals("<", text)) {
|
||||
char[] escape = "<".toCharArray();
|
||||
super.characters(escape, start, escape.length);
|
||||
return;
|
||||
}
|
||||
if (StringUtils.equals(">", text)) {
|
||||
char[] escape = ">".toCharArray();
|
||||
super.characters(escape, start, escape.length);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
super.characters(ch, start, length);
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
|
@ -1,21 +1,12 @@
|
|||
package io.metersphere.xmind.parser;
|
||||
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.utils.XmlUtils;
|
||||
import org.apache.commons.lang.StringEscapeUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.dom4j.Document;
|
||||
import org.dom4j.DocumentException;
|
||||
import org.dom4j.Element;
|
||||
import org.dom4j.Node;
|
||||
import org.dom4j.io.SAXReader;
|
||||
import org.dom4j.*;
|
||||
import org.json.JSONObject;
|
||||
import org.json.XML;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -36,16 +27,12 @@ public class XmindLegacy {
|
|||
xmlContent = xmlContent.replace("xmlns:fo=\"http://www.w3.org/1999/XSL/Format\"", StringUtils.EMPTY);
|
||||
try {
|
||||
xmlContent = removeTopicsFromString(xmlContent);
|
||||
// 反转义 xml 中的 < > 字符
|
||||
xmlContent = StringEscapeUtils.unescapeXml(xmlContent);
|
||||
xmlContent = xmlContent.replace(">", ">");
|
||||
xmlContent = xmlContent.replace("<", "<");
|
||||
} catch (Exception e) {
|
||||
LogUtil.error("移除xml中的Topic出错:", e);
|
||||
}
|
||||
// 去除title中svg:width属性
|
||||
xmlContent = xmlContent.replaceAll("<title svg:width=\"[0-9]*\">", "<title>");
|
||||
Document document = XmlUtils.getDocument(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8.name())));// 读取XML文件,获得document对象
|
||||
Document document = DocumentHelper.parseText(xmlContent);// 读取XML文件,获得document对象
|
||||
Element root = document.getRootElement();
|
||||
List<Node> topics = root.selectNodes("//topic");
|
||||
|
||||
|
@ -54,7 +41,7 @@ public class XmindLegacy {
|
|||
xmlComments = xmlComments.replace("xmlns=\"urn:xmind:xmap:xmlns:comments:2.0\"", StringUtils.EMPTY);
|
||||
|
||||
// 添加评论到content中
|
||||
Document commentDocument = XmlUtils.getDocument(new ByteArrayInputStream(xmlComments.getBytes(StandardCharsets.UTF_8.name())));
|
||||
Document commentDocument = DocumentHelper.parseText(xmlComments);
|
||||
List<Node> commentsList = commentDocument.selectNodes("//comment");
|
||||
|
||||
for (Node topic : topics) {
|
||||
|
@ -107,7 +94,7 @@ public class XmindLegacy {
|
|||
* @throws Exception
|
||||
*/
|
||||
private static String removeTopicsFromString(String xmlContent) throws Exception {
|
||||
Document doc = XmlUtils.getDocument(new ByteArrayInputStream(xmlContent.getBytes(StandardCharsets.UTF_8.name())));
|
||||
Document doc = DocumentHelper.parseText(xmlContent);
|
||||
if (doc != null) {
|
||||
Element root = doc.getRootElement();
|
||||
List<Element> childrenElement = root.elements();
|
||||
|
|
Loading…
Reference in New Issue