Skip to content

Commit 05a6447

Browse files
committed
Polish "Resolve errors in layers.xsd"
Validate loaded `layer.xml` files against the XSD and additionally update `<layers>` sub elements to have a 'minOccurs' of 0. See gh-31126
1 parent 0d78323 commit 05a6447

File tree

7 files changed

+61
-16
lines changed

7 files changed

+61
-16
lines changed

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/build.gradle

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,29 @@ dependencies {
6464
versionProperties(project(path: ":spring-boot-project:spring-boot-dependencies", configuration: "effectiveBom"))
6565
}
6666

67+
ext {
68+
versionElements = version.split("\\.")
69+
xsdVersion = versionElements[0] + "." + versionElements[1]
70+
}
71+
6772
syncDocumentationSourceForAsciidoctor {
6873
from(documentPluginGoals) {
6974
into "asciidoc/goals"
7075
}
7176
}
7277

7378
sourceSets {
79+
main {
80+
output.dir("${buildDir}/generated/resources/xsd", builtBy: "xsdResources")
81+
}
7482
intTest {
7583
output.dir("${buildDir}/generated-resources", builtBy: "extractVersionProperties")
7684
}
7785
}
7886

7987
tasks.withType(org.asciidoctor.gradle.jvm.AbstractAsciidoctorTask) {
8088
doFirst {
81-
def versionEl = version.split("\\.")
82-
attributes "spring-boot-xsd-version": versionEl[0] + '.' + versionEl[1]
89+
attributes "spring-boot-xsd-version" : project.ext.xsdVersion
8390
}
8491
}
8592

@@ -129,6 +136,12 @@ task zip(type: Zip) {
129136
}
130137
}
131138

139+
task xsdResources(type: Sync) {
140+
from "src/main/xsd/layers-${project.ext.xsdVersion}.xsd"
141+
into "${buildDir}/generated/resources/xsd/org/springframework/boot/maven"
142+
rename { fileName -> "layers.xsd" }
143+
}
144+
132145
prepareMavenBinaries {
133146
versions "3.8.1", "3.6.3", "3.5.4"
134147
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -171,6 +171,7 @@ private CustomLayers getCustomLayers(File configuration) {
171171
private Document getDocumentIfAvailable(File xmlFile) throws Exception {
172172
InputSource inputSource = new InputSource(new FileInputStream(xmlFile));
173173
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
174+
factory.setNamespaceAware(true);
174175
DocumentBuilder builder = factory.newDocumentBuilder();
175176
return builder.parse(inputSource);
176177
}

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/java/org/springframework/boot/maven/CustomLayersProvider.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2020 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,24 @@
1616

1717
package org.springframework.boot.maven;
1818

19+
import java.io.IOException;
1920
import java.util.ArrayList;
2021
import java.util.Collections;
2122
import java.util.List;
2223
import java.util.function.Function;
2324
import java.util.stream.Collectors;
2425

26+
import javax.xml.XMLConstants;
27+
import javax.xml.transform.dom.DOMSource;
28+
import javax.xml.validation.Schema;
29+
import javax.xml.validation.SchemaFactory;
30+
import javax.xml.validation.Validator;
31+
2532
import org.w3c.dom.Document;
2633
import org.w3c.dom.Element;
2734
import org.w3c.dom.Node;
2835
import org.w3c.dom.NodeList;
36+
import org.xml.sax.SAXException;
2937

3038
import org.springframework.boot.loader.tools.Layer;
3139
import org.springframework.boot.loader.tools.Library;
@@ -45,13 +53,35 @@
4553
class CustomLayersProvider {
4654

4755
CustomLayers getLayers(Document document) {
56+
validate(document);
4857
Element root = document.getDocumentElement();
4958
List<ContentSelector<String>> applicationSelectors = getApplicationSelectors(root);
5059
List<ContentSelector<Library>> librarySelectors = getLibrarySelectors(root);
5160
List<Layer> layers = getLayers(root);
5261
return new CustomLayers(layers, applicationSelectors, librarySelectors);
5362
}
5463

64+
private void validate(Document document) {
65+
Schema schema = loadSchema();
66+
try {
67+
Validator validator = schema.newValidator();
68+
validator.validate(new DOMSource(document));
69+
}
70+
catch (SAXException | IOException ex) {
71+
throw new IllegalStateException("Invalid layers.xml configuration", ex);
72+
}
73+
}
74+
75+
private Schema loadSchema() {
76+
try {
77+
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
78+
return factory.newSchema(getClass().getResource("layers.xsd"));
79+
}
80+
catch (SAXException ex) {
81+
throw new IllegalStateException("Unable to load layers XSD");
82+
}
83+
}
84+
5585
private List<ContentSelector<String>> getApplicationSelectors(Element root) {
5686
return getSelectors(root, "application", (element) -> getSelector(element, ApplicationContentFilter::new));
5787
}

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/xsd/layers-2.3.xsd

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<xsd:element name="layers" type="layersType" />
77
<xsd:complexType name="layersType">
88
<xsd:sequence>
9-
<xsd:element name="application" type="applicationType" />
10-
<xsd:element name="dependencies" type="dependenciesType" />
11-
<xsd:element name="layerOrder" type="layerOrderType" />
9+
<xsd:element name="application" type="applicationType" minOccurs="0"/>
10+
<xsd:element name="dependencies" type="dependenciesType" minOccurs="0"/>
11+
<xsd:element name="layerOrder" type="layerOrderType" minOccurs="0"/>
1212
</xsd:sequence>
1313
</xsd:complexType>
1414
<xsd:complexType name="applicationType">

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/xsd/layers-2.4.xsd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<xsd:element name="layers" type="layersType" />
77
<xsd:complexType name="layersType">
88
<xsd:sequence>
9-
<xsd:element name="application" type="applicationType" />
10-
<xsd:element name="dependencies" type="dependenciesType" />
11-
<xsd:element name="layerOrder" type="layerOrderType" />
9+
<xsd:element name="application" type="applicationType" minOccurs="0"/>
10+
<xsd:element name="dependencies" type="dependenciesType" minOccurs="0"/>
11+
<xsd:element name="layerOrder" type="layerOrderType" minOccurs="0"/>
1212
</xsd:sequence>
1313
</xsd:complexType>
1414
<xsd:complexType name="applicationType">
@@ -92,7 +92,7 @@
9292
]]></xsd:documentation>
9393
</xsd:annotation>
9494
</xsd:element>
95-
</xsd:choice>
95+
</xsd:choice>
9696
</xsd:extension>
9797
</xsd:complexContent>
9898
</xsd:complexType>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/main/xsd/layers-2.5.xsd

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66
<xsd:element name="layers" type="layersType" />
77
<xsd:complexType name="layersType">
88
<xsd:sequence>
9-
<xsd:element name="application" type="applicationType" />
10-
<xsd:element name="dependencies" type="dependenciesType" />
11-
<xsd:element name="layerOrder" type="layerOrderType" />
9+
<xsd:element name="application" type="applicationType" minOccurs="0"/>
10+
<xsd:element name="dependencies" type="dependenciesType" minOccurs="0"/>
11+
<xsd:element name="layerOrder" type="layerOrderType" minOccurs="0"/>
1212
</xsd:sequence>
1313
</xsd:complexType>
1414
<xsd:complexType name="applicationType">
@@ -92,7 +92,7 @@
9292
]]></xsd:documentation>
9393
</xsd:annotation>
9494
</xsd:element>
95-
</xsd:choice>
95+
</xsd:choice>
9696
</xsd:extension>
9797
</xsd:complexContent>
9898
</xsd:complexType>

spring-boot-project/spring-boot-tools/spring-boot-maven-plugin/src/test/java/org/springframework/boot/maven/CustomLayersProviderTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2021 the original author or authors.
2+
* Copyright 2012-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -97,6 +97,7 @@ private Document getDocument(String resourceName) throws Exception {
9797
ClassPathResource resource = new ClassPathResource(resourceName);
9898
InputSource inputSource = new InputSource(resource.getInputStream());
9999
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
100+
factory.setNamespaceAware(true);
100101
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
101102
return documentBuilder.parse(inputSource);
102103
}

0 commit comments

Comments
 (0)