Skip to content

Commit a6895e3

Browse files
committed
Merge branch '3.5.x' into 4.0.x
Closes gh-50289
2 parents 2c18012 + fd1b5c7 commit a6895e3

7 files changed

Lines changed: 87 additions & 24 deletions

File tree

build-plugin/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/AbstractPackagerMojo.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.Set;
2525
import java.util.function.Supplier;
2626

27+
import javax.xml.XMLConstants;
2728
import javax.xml.parsers.DocumentBuilder;
2829
import javax.xml.parsers.DocumentBuilderFactory;
2930

@@ -199,6 +200,8 @@ private CustomLayers getCustomLayers(File configuration) {
199200
private Document getDocumentIfAvailable(File xmlFile) throws Exception {
200201
InputSource inputSource = new InputSource(new FileInputStream(xmlFile));
201202
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
203+
factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
204+
factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
202205
factory.setNamespaceAware(true);
203206
DocumentBuilder builder = factory.newDocumentBuilder();
204207
return builder.parse(inputSource);

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,6 @@
2828

2929
import javax.xml.namespace.QName;
3030
import javax.xml.parsers.DocumentBuilder;
31-
import javax.xml.parsers.DocumentBuilderFactory;
32-
import javax.xml.parsers.ParserConfigurationException;
3331
import javax.xml.xpath.XPath;
3432
import javax.xml.xpath.XPathConstants;
3533
import javax.xml.xpath.XPathExpressionException;
@@ -50,6 +48,7 @@
5048
import org.springframework.boot.build.bom.ResolvedBom.JavadocLink;
5149
import org.springframework.boot.build.bom.ResolvedBom.Links;
5250
import org.springframework.boot.build.bom.ResolvedBom.ResolvedLibrary;
51+
import org.springframework.boot.build.xml.XmlDocument;
5352

5453
/**
5554
* Creates a {@link ResolvedBom resolved bom}.
@@ -67,12 +66,7 @@ class BomResolver {
6766
BomResolver(ConfigurationContainer configurations, DependencyHandler dependencies) {
6867
this.configurations = configurations;
6968
this.dependencies = dependencies;
70-
try {
71-
this.documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
72-
}
73-
catch (ParserConfigurationException ex) {
74-
throw new RuntimeException(ex);
75-
}
69+
this.documentBuilder = XmlDocument.builder();
7670
}
7771

7872
ResolvedBom resolve(BomExtension bomExtension) {

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

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

38-
import javax.xml.parsers.DocumentBuilder;
39-
import javax.xml.parsers.DocumentBuilderFactory;
4038
import javax.xml.xpath.XPath;
4139
import javax.xml.xpath.XPathFactory;
4240

@@ -51,6 +49,7 @@
5149

5250
import org.springframework.boot.build.bom.ResolvedBom.Id;
5351
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
52+
import org.springframework.boot.build.xml.XmlDocument;
5453

5554
/**
5655
* A collection of modules, Maven plugins, and Maven boms that are versioned and released
@@ -676,8 +675,7 @@ private List<Dependency> getBomDependencies(Library manager) {
676675

677676
private String propertyFrom(File pomFile) {
678677
try {
679-
DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
680-
Document document = documentBuilder.parse(pomFile);
678+
Document document = XmlDocument.parse(pomFile);
681679
XPath xpath = XPathFactory.newInstance().newXPath();
682680
return xpath.evaluate("/project/properties/" + this.name + "/text()", document);
683681
}

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

Lines changed: 2 additions & 6 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,7 +25,6 @@
2625
import java.util.TreeSet;
2726
import java.util.stream.Collectors;
2827

29-
import javax.xml.parsers.DocumentBuilderFactory;
3028
import javax.xml.xpath.XPathConstants;
3129
import javax.xml.xpath.XPathFactory;
3230

@@ -36,9 +34,9 @@
3634
import org.gradle.internal.artifacts.repositories.AuthenticationSupportedInternal;
3735
import org.w3c.dom.Document;
3836
import org.w3c.dom.NodeList;
39-
import org.xml.sax.InputSource;
4037

4138
import org.springframework.boot.build.bom.bomr.version.DependencyVersion;
39+
import org.springframework.boot.build.xml.XmlDocument;
4240
import org.springframework.http.HttpEntity;
4341
import org.springframework.http.HttpHeaders;
4442
import org.springframework.http.HttpMethod;
@@ -93,9 +91,7 @@ private Set<String> resolveVersions(String groupId, String artifactId, MavenArti
9391
}
9492
HttpEntity<Void> request = new HttpEntity<>(headers);
9593
String metadata = this.rest.exchange(url, HttpMethod.GET, request, String.class).getBody();
96-
Document metadataDocument = DocumentBuilderFactory.newInstance()
97-
.newDocumentBuilder()
98-
.parse(new InputSource(new StringReader(metadata)));
94+
Document metadataDocument = XmlDocument.parseContent(metadata);
9995
NodeList versionNodes = (NodeList) XPathFactory.newInstance()
10096
.newXPath()
10197
.evaluate("/metadata/versioning/versions/version", metadataDocument, XPathConstants.NODESET);

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

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

26-
import javax.xml.parsers.DocumentBuilderFactory;
2726
import javax.xml.xpath.XPath;
2827
import javax.xml.xpath.XPathConstants;
2928
import javax.xml.xpath.XPathExpressionException;
@@ -32,6 +31,8 @@
3231
import org.w3c.dom.Node;
3332
import org.w3c.dom.NodeList;
3433

34+
import org.springframework.boot.build.xml.XmlDocument;
35+
3536
/**
3637
* A parser for a Maven plugin's {@code plugin.xml} file.
3738
*
@@ -48,7 +49,7 @@ class PluginXmlParser {
4849

4950
Plugin parse(File pluginXml) {
5051
try {
51-
Node root = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(pluginXml);
52+
Node root = XmlDocument.parse(pluginXml);
5253
List<Mojo> mojos = parseMojos(root);
5354
return new Plugin(textAt("//plugin/groupId", root), textAt("//plugin/artifactId", root),
5455
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)