Skip to content

Commit 7bcb932

Browse files
committed
Reuse XML factories in collect results
1 parent d4716b7 commit 7bcb932

1 file changed

Lines changed: 28 additions & 12 deletions

File tree

.gitlab/collect-result/JUnitReport.java

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ final class JUnitReport {
2020
private static final String FINAL_STATUS_PROPERTY = "dd_tags[test.final_status]";
2121
private static final Pattern HASH_CODE = Pattern.compile("@[0-9a-f]{5,}");
2222
private static final Pattern LOCALHOST_PORT = Pattern.compile("localhost:[0-9]{2,5}");
23+
private static final DocumentBuilderFactory DOCUMENT_BUILDER_FACTORY =
24+
newDocumentBuilderFactory();
25+
private static final TransformerFactory TRANSFORMER_FACTORY = newTransformerFactory();
2326

2427
private final Document document;
2528

@@ -28,14 +31,8 @@ private JUnitReport(Document document) {
2831
}
2932

3033
static JUnitReport parse(Path xmlFile) throws Exception {
31-
var dbf = DocumentBuilderFactory.newInstance();
32-
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
33-
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
34-
dbf.setFeature("http://xml.org/sax/features/external-general-entities", false);
35-
dbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
36-
dbf.setXIncludeAware(false);
37-
dbf.setExpandEntityReferences(false);
38-
return new JUnitReport(dbf.newDocumentBuilder().parse(xmlFile.toFile()));
34+
var document = DOCUMENT_BUILDER_FACTORY.newDocumentBuilder().parse(xmlFile.toFile());
35+
return new JUnitReport(document);
3936
}
4037

4138
boolean addFileAttribute(String sourceFile) {
@@ -104,10 +101,7 @@ void write(Path xmlFile) throws Exception {
104101
Files.createDirectories(xmlFile.getParent());
105102
var tmpFile = Files.createTempFile(xmlFile.getParent(), "collect-results-", ".xml");
106103
try (OutputStream output = Files.newOutputStream(tmpFile)) {
107-
var transformerFactory = TransformerFactory.newInstance();
108-
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
109-
transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
110-
var transformer = transformerFactory.newTransformer();
104+
var transformer = TRANSFORMER_FACTORY.newTransformer();
111105
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
112106
transformer.transform(new DOMSource(document), new StreamResult(output));
113107
} catch (Exception e) {
@@ -117,6 +111,28 @@ void write(Path xmlFile) throws Exception {
117111
Files.move(tmpFile, xmlFile, StandardCopyOption.REPLACE_EXISTING);
118112
}
119113

114+
private static DocumentBuilderFactory newDocumentBuilderFactory() {
115+
try {
116+
var factory = DocumentBuilderFactory.newInstance();
117+
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
118+
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
119+
factory.setFeature("http://xml.org/sax/features/external-general-entities", false);
120+
factory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
121+
factory.setXIncludeAware(false);
122+
factory.setExpandEntityReferences(false);
123+
return factory;
124+
} catch (Exception e) {
125+
throw new ExceptionInInitializerError(e);
126+
}
127+
}
128+
129+
private static TransformerFactory newTransformerFactory() {
130+
var factory = TransformerFactory.newInstance();
131+
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
132+
factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
133+
return factory;
134+
}
135+
120136
private List<Element> testcases() {
121137
var testcases = document.getElementsByTagName("testcase");
122138
var elements = new ArrayList<Element>(testcases.getLength());

0 commit comments

Comments
 (0)