diff --git a/spring-core/src/main/java/org/springframework/util/xml/XmlValidationModeDetector.java b/spring-core/src/main/java/org/springframework/util/xml/XmlValidationModeDetector.java index 32e81c6f1f..869f9ca051 100644 --- a/spring-core/src/main/java/org/springframework/util/xml/XmlValidationModeDetector.java +++ b/spring-core/src/main/java/org/springframework/util/xml/XmlValidationModeDetector.java @@ -97,7 +97,7 @@ public class XmlValidationModeDetector { String content; while ((content = reader.readLine()) != null) { content = consumeCommentTokens(content); - if (this.inComment || !StringUtils.hasText(content)) { + if (!StringUtils.hasText(content)) { continue; } if (hasDoctype(content)) { @@ -143,11 +143,10 @@ public class XmlValidationModeDetector { } /** - * Consume all leading and trailing comments in the given String and return - * the remaining content, which may be empty since the supplied content might - * be all comment data. + * Consume all comments in the given String and return the remaining content, + * which may be empty since the supplied content might be all comment data. + *

This method takes the current "in comment" parsing state into account. */ - @Nullable private String consumeCommentTokens(String line) { int indexOfStartComment = line.indexOf(START_COMMENT); if (indexOfStartComment == -1 && !line.contains(END_COMMENT)) { @@ -156,17 +155,15 @@ public class XmlValidationModeDetector { String result = ""; String currLine = line; - if (indexOfStartComment >= 0) { + if (!this.inComment && (indexOfStartComment >= 0)) { result = line.substring(0, indexOfStartComment); currLine = line.substring(indexOfStartComment); } - while ((currLine = consume(currLine)) != null) { - if (!this.inComment && !currLine.trim().startsWith(START_COMMENT)) { - return result + currLine; - } + if ((currLine = consume(currLine)) != null) { + result += consumeCommentTokens(currLine); } - return null; + return result; } /** diff --git a/spring-core/src/test/java/org/springframework/util/xml/XmlValidationModeDetectorTests.java b/spring-core/src/test/java/org/springframework/util/xml/XmlValidationModeDetectorTests.java index 5951d29830..35a5e13222 100644 --- a/spring-core/src/test/java/org/springframework/util/xml/XmlValidationModeDetectorTests.java +++ b/spring-core/src/test/java/org/springframework/util/xml/XmlValidationModeDetectorTests.java @@ -24,6 +24,7 @@ import org.junit.jupiter.params.provider.ValueSource; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.util.xml.XmlValidationModeDetector.VALIDATION_DTD; +import static org.springframework.util.xml.XmlValidationModeDetector.VALIDATION_XSD; /** * Unit tests for {@link XmlValidationModeDetector}. @@ -37,12 +38,29 @@ class XmlValidationModeDetectorTests { @ParameterizedTest - @ValueSource(strings = { "dtdWithTrailingComment.xml", "dtdWithLeadingComment.xml", "dtdWithCommentOnNextLine.xml", - "dtdWithMultipleComments.xml" }) + @ValueSource(strings = { + "dtdWithNoComments.xml", + "dtdWithLeadingComment.xml", + "dtdWithTrailingComment.xml", + "dtdWithTrailingCommentAcrossMultipleLines.xml", + "dtdWithCommentOnNextLine.xml", + "dtdWithMultipleComments.xml" + }) void dtdDetection(String fileName) throws Exception { assertValidationMode(fileName, VALIDATION_DTD); } + @ParameterizedTest + @ValueSource(strings = { + "xsdWithNoComments.xml", + "xsdWithMultipleComments.xml", + "xsdWithDoctypeInComment.xml", + "xsdWithDoctypeInOpenCommentWithAdditionalCommentOnSameLine.xml" + }) + void xsdDetection(String fileName) throws Exception { + assertValidationMode(fileName, VALIDATION_XSD); + } + private void assertValidationMode(String fileName, int expectedValidationMode) throws IOException { try (InputStream inputStream = getClass().getResourceAsStream(fileName)) { diff --git a/spring-core/src/test/resources/org/springframework/util/xml/dtdWithNoComments.xml b/spring-core/src/test/resources/org/springframework/util/xml/dtdWithNoComments.xml new file mode 100644 index 0000000000..299c52abef --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/util/xml/dtdWithNoComments.xml @@ -0,0 +1,6 @@ + + + + + + diff --git a/spring-core/src/test/resources/org/springframework/util/xml/dtdWithTrailingCommentAcrossMultipleLines.xml b/spring-core/src/test/resources/org/springframework/util/xml/dtdWithTrailingCommentAcrossMultipleLines.xml new file mode 100644 index 0000000000..68776b4f61 --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/util/xml/dtdWithTrailingCommentAcrossMultipleLines.xml @@ -0,0 +1,6 @@ + + + + + diff --git a/spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInComment.xml b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInComment.xml new file mode 100644 index 0000000000..3f0fd05ed9 --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInComment.xml @@ -0,0 +1,8 @@ + + + + + diff --git a/spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInOpenCommentWithAdditionalCommentOnSameLine.xml b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInOpenCommentWithAdditionalCommentOnSameLine.xml new file mode 100644 index 0000000000..3d831932a9 --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithDoctypeInOpenCommentWithAdditionalCommentOnSameLine.xml @@ -0,0 +1,9 @@ + + + + + diff --git a/spring-core/src/test/resources/org/springframework/util/xml/xsdWithMultipleComments.xml b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithMultipleComments.xml new file mode 100644 index 0000000000..8843539e4a --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithMultipleComments.xml @@ -0,0 +1,10 @@ + + + + diff --git a/spring-core/src/test/resources/org/springframework/util/xml/xsdWithNoComments.xml b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithNoComments.xml new file mode 100644 index 0000000000..4f178c702f --- /dev/null +++ b/spring-core/src/test/resources/org/springframework/util/xml/xsdWithNoComments.xml @@ -0,0 +1,8 @@ + + + + +