Skip to content

Commit f8ecad5

Browse files
committed
Polish "Align DocumentBuilderFactory configuration"
See gh-50282
1 parent eb90d0d commit f8ecad5

6 files changed

Lines changed: 84 additions & 38 deletions

File tree

buildSrc/src/main/java/org/springframework/boot/build/bom/BomResolver.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,8 @@
2626
import java.util.Set;
2727
import java.util.function.Function;
2828

29-
import javax.xml.XMLConstants;
3029
import javax.xml.namespace.QName;
3130
import javax.xml.parsers.DocumentBuilder;
32-
import javax.xml.parsers.DocumentBuilderFactory;
33-
import javax.xml.parsers.ParserConfigurationException;
3431
import javax.xml.xpath.XPath;
3532
import javax.xml.xpath.XPathConstants;
3633
import javax.xml.xpath.XPathExpressionException;
@@ -51,6 +48,7 @@
5148
import org.springframework.boot.build.bom.ResolvedBom.JavadocLink;
5249
import org.springframework.boot.build.bom.ResolvedBom.Links;
5350
import org.springframework.boot.build.bom.ResolvedBom.ResolvedLibrary;
51+
import org.springframework.boot.build.xml.XmlDocument;
5452

5553
/**
5654
* Creates a {@link ResolvedBom resolved bom}.
@@ -68,15 +66,7 @@ class BomResolver {
6866
BomResolver(ConfigurationContainer configurations, DependencyHandler dependencies) {
6967
this.configurations = configurations;
7068
this.dependencies = dependencies;
71-
try {
72-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
73-
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
74-
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
75-
this.documentBuilder = factory.newDocumentBuilder();
76-
}
77-
catch (ParserConfigurationException ex) {
78-
throw new RuntimeException(ex);
79-
}
69+
this.documentBuilder = XmlDocument.builder();
8070
}
8171

8272
ResolvedBom resolve(BomExtension bomExtension) {

buildSrc/src/main/java/org/springframework/boot/build/bom/Library.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,6 @@
3535
import java.util.regex.Pattern;
3636
import java.util.stream.Stream;
3737

38-
import javax.xml.XMLConstants;
39-
import javax.xml.parsers.DocumentBuilder;
40-
import javax.xml.parsers.DocumentBuilderFactory;
4138
import javax.xml.xpath.XPath;
4239
import javax.xml.xpath.XPathFactory;
4340

@@ -52,6 +49,7 @@
5249

5350
import org.springframework.boot.build.bom.ResolvedBom.Id;
5451
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
52+
import org.springframework.boot.build.xml.XmlDocument;
5553

5654
/**
5755
* A collection of modules, Maven plugins, and Maven boms that are versioned and released
@@ -677,11 +675,7 @@ private List<Dependency> getBomDependencies(Library manager) {
677675

678676
private String propertyFrom(File pomFile) {
679677
try {
680-
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
681-
dbf.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
682-
dbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
683-
DocumentBuilder documentBuilder = dbf.newDocumentBuilder();
684-
Document document = documentBuilder.parse(pomFile);
678+
Document document = XmlDocument.parse(pomFile);
685679
XPath xpath = XPathFactory.newInstance().newXPath();
686680
return xpath.evaluate("/project/properties/" + this.name + "/text()", document);
687681
}

buildSrc/src/main/java/org/springframework/boot/build/bom/bomr/MavenMetadataVersionResolver.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.boot.build.bom.bomr;
1818

19-
import java.io.StringReader;
2019
import java.net.URI;
2120
import java.util.Collection;
2221
import java.util.Collections;
@@ -26,8 +25,6 @@
2625
import java.util.TreeSet;
2726
import java.util.stream.Collectors;
2827

29-
import javax.xml.XMLConstants;
30-
import javax.xml.parsers.DocumentBuilderFactory;
3128
import javax.xml.xpath.XPathConstants;
3229
import javax.xml.xpath.XPathFactory;
3330

@@ -37,9 +34,9 @@
3734
import org.gradle.internal.artifacts.repositories.AuthenticationSupportedInternal;
3835
import org.w3c.dom.Document;
3936
import org.w3c.dom.NodeList;
40-
import org.xml.sax.InputSource;
4137

4238
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
39+
import org.springframework.boot.build.xml.XmlDocument;
4340
import org.springframework.http.HttpEntity;
4441
import org.springframework.http.HttpHeaders;
4542
import org.springframework.http.HttpMethod;
@@ -94,10 +91,7 @@ private Set<String> resolveVersions(String groupId, String artifactId, MavenArti
9491
}
9592
HttpEntity<Void> request = new HttpEntity<>(headers);
9693
String metadata = this.rest.exchange(url, HttpMethod.GET, request, String.class).getBody();
97-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
98-
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
99-
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
100-
Document metadataDocument = factory.newDocumentBuilder().parse(new InputSource(new StringReader(metadata)));
94+
Document metadataDocument = XmlDocument.parseContent(metadata);
10195
NodeList versionNodes = (NodeList) XPathFactory.newInstance()
10296
.newXPath()
10397
.evaluate("/metadata/versioning/versions/version", metadataDocument, XPathConstants.NODESET);

buildSrc/src/main/java/org/springframework/boot/build/mavenplugin/PluginXmlParser.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,6 @@
2323
import java.util.List;
2424
import java.util.Map;
2525

26-
import javax.xml.XMLConstants;
27-
import javax.xml.parsers.DocumentBuilderFactory;
2826
import javax.xml.xpath.XPath;
2927
import javax.xml.xpath.XPathConstants;
3028
import javax.xml.xpath.XPathExpressionException;
@@ -33,6 +31,8 @@
3331
import org.w3c.dom.Node;
3432
import org.w3c.dom.NodeList;
3533

34+
import org.springframework.boot.build.xml.XmlDocument;
35+
3636
/**
3737
* A parser for a Maven plugin's {@code plugin.xml} file.
3838
*
@@ -49,10 +49,7 @@ class PluginXmlParser {
4949

5050
Plugin parse(File pluginXml) {
5151
try {
52-
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
53-
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
54-
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
55-
Node root = factory.newDocumentBuilder().parse(pluginXml);
52+
Node root = XmlDocument.parse(pluginXml);
5653
List<Mojo> mojos = parseMojos(root);
5754
return new Plugin(textAt("//plugin/groupId", root), textAt("//plugin/artifactId", root),
5855
textAt("//plugin/version", root), textAt("//plugin/goalPrefix", root), mojos);
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2026 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.build.xml;
18+
19+
import java.io.File;
20+
import java.io.IOException;
21+
import java.io.StringReader;
22+
23+
import javax.xml.XMLConstants;
24+
import javax.xml.parsers.DocumentBuilder;
25+
import javax.xml.parsers.DocumentBuilderFactory;
26+
import javax.xml.parsers.ParserConfigurationException;
27+
28+
import org.w3c.dom.Document;
29+
import org.xml.sax.InputSource;
30+
import org.xml.sax.SAXException;
31+
32+
/**
33+
* XML {@link Document} builder and parsing.
34+
*
35+
* @author Phillip Webb
36+
* @author Sebastien Tardif
37+
*/
38+
public final class XmlDocument {
39+
40+
private static final DocumentBuilderFactory factory;
41+
static {
42+
try {
43+
factory = DocumentBuilderFactory.newInstance();
44+
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
45+
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
46+
}
47+
catch (ParserConfigurationException ex) {
48+
throw new IllegalStateException(ex);
49+
}
50+
}
51+
52+
private XmlDocument() {
53+
}
54+
55+
public static Document parseContent(String content) throws SAXException, IOException {
56+
return builder().parse(new InputSource(new StringReader(content)));
57+
}
58+
59+
public static Document parse(File file) throws SAXException, IOException {
60+
return builder().parse(file);
61+
}
62+
63+
public static DocumentBuilder builder() {
64+
try {
65+
return factory.newDocumentBuilder();
66+
}
67+
catch (ParserConfigurationException ex) {
68+
throw new IllegalStateException(ex);
69+
}
70+
}
71+
72+
}

buildSrc/src/test/java/org/springframework/boot/build/assertj/NodeAssert.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.File;
2020

21-
import javax.xml.parsers.DocumentBuilderFactory;
2221
import javax.xml.xpath.XPath;
2322
import javax.xml.xpath.XPathConstants;
2423
import javax.xml.xpath.XPathExpressionException;
@@ -30,15 +29,15 @@
3029
import org.w3c.dom.Document;
3130
import org.w3c.dom.Node;
3231

32+
import org.springframework.boot.build.xml.XmlDocument;
33+
3334
/**
3435
* AssertJ {@link AssertProvider} for {@link Node} assertions.
3536
*
3637
* @author Andy Wilkinson
3738
*/
3839
public class NodeAssert extends AbstractAssert<NodeAssert, Node> implements AssertProvider<NodeAssert> {
3940

40-
private static final DocumentBuilderFactory FACTORY = DocumentBuilderFactory.newInstance();
41-
4241
private final XPathFactory xpathFactory = XPathFactory.newInstance();
4342

4443
private final XPath xpath = this.xpathFactory.newXPath();
@@ -53,7 +52,7 @@ public NodeAssert(Node actual) {
5352

5453
private static Document read(File xmlFile) {
5554
try {
56-
return FACTORY.newDocumentBuilder().parse(xmlFile);
55+
return XmlDocument.parse(xmlFile);
5756
}
5857
catch (Exception ex) {
5958
throw new RuntimeException(ex);

0 commit comments

Comments
 (0)