From e3d3b34f8f7557edebaf3b19de7741200b653f43 Mon Sep 17 00:00:00 2001 From: CaptainB Date: Tue, 8 Feb 2022 13:50:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95=E8=BD=AC=E5=87=BAjmx?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E5=8C=96=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../parse/EngineSourceParserFactory.java | 34 ++++++++++++++++--- .../xml/reader/JmeterDocumentParser.java | 5 +-- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/backend/src/main/java/io/metersphere/performance/parse/EngineSourceParserFactory.java b/backend/src/main/java/io/metersphere/performance/parse/EngineSourceParserFactory.java index 143b647dd5..450e772aa6 100644 --- a/backend/src/main/java/io/metersphere/performance/parse/EngineSourceParserFactory.java +++ b/backend/src/main/java/io/metersphere/performance/parse/EngineSourceParserFactory.java @@ -1,19 +1,25 @@ package io.metersphere.performance.parse; import io.metersphere.commons.constants.FileType; +import io.metersphere.commons.utils.LogUtil; import io.metersphere.performance.parse.xml.reader.JmeterDocumentParser; import org.apache.commons.lang3.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentException; +import org.dom4j.Node; +import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; +import org.dom4j.io.XMLWriter; import org.xml.sax.SAXException; import org.xml.sax.helpers.XMLFilterImpl; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.InputStream; -import java.nio.charset.StandardCharsets; +import java.util.List; public class EngineSourceParserFactory { - public static final boolean IS_TRANS = true; + public static final boolean IS_TRANS = false; public static EngineSourceParser createEngineSourceParser(String type) { @@ -35,8 +41,28 @@ public class EngineSourceParserFactory { } public static byte[] getBytes(Document document) throws Exception { - // todo 格式化代码会导致前后置脚本缩进有问题,先使用基本方式 - return document.asXML().getBytes(StandardCharsets.UTF_8); + OutputFormat format = new OutputFormat(); + format.setIndentSize(2); + format.setNewlines(true); + format.setPadText(true); + format.setTrimText(false); + try ( + ByteArrayOutputStream out = new ByteArrayOutputStream(); + ) { + // 删除空白的行 + List nodes = document.selectNodes("//text()[normalize-space(.)='']"); + nodes.forEach(node -> node.setText("")); + + XMLWriter xw = new XMLWriter(out, format); + xw.setEscapeText(IS_TRANS); + xw.write(document); + xw.flush(); + xw.close(); + return out.toByteArray(); + } catch (IOException e) { + LogUtil.error(e); + } + return new byte[0]; } public static XMLFilterImpl getFilter() { diff --git a/backend/src/main/java/io/metersphere/performance/parse/xml/reader/JmeterDocumentParser.java b/backend/src/main/java/io/metersphere/performance/parse/xml/reader/JmeterDocumentParser.java index 2a5c083996..aff168cb46 100644 --- a/backend/src/main/java/io/metersphere/performance/parse/xml/reader/JmeterDocumentParser.java +++ b/backend/src/main/java/io/metersphere/performance/parse/xml/reader/JmeterDocumentParser.java @@ -1127,9 +1127,6 @@ public class JmeterDocumentParser implements EngineSourceParser { } private void removeChildren(Element node) { - List elements = node.elements(); - for (Element ele : elements) { - node.remove(ele); - } + node.setText(""); } }