Support trailing comment in DTD declaration in XML config
Prior to this commit, Spring failed to determine that an XML config file was DTD-based if the DTD declaration was followed by a comment. This commit fixes this by modifying the consumeCommentTokens(String) algorithm in XmlValidationModeDetector so that both leading and trailing comments are properly consumed without losing any XML content. Closes gh-23605
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2017 the original author or authors.
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,6 +30,7 @@ import org.springframework.util.StringUtils;
|
||||
*
|
||||
* @author Rob Harrop
|
||||
* @author Juergen Hoeller
|
||||
* @author Sam Brannen
|
||||
* @since 2.0
|
||||
*/
|
||||
public class XmlValidationModeDetector {
|
||||
@@ -142,20 +143,27 @@ public class XmlValidationModeDetector {
|
||||
}
|
||||
|
||||
/**
|
||||
* Consumes all the leading comment data in the given String and returns the remaining content, which
|
||||
* may be empty since the supplied content might be all comment data. For our purposes it is only important
|
||||
* to strip leading comment content on a line since the first piece of non comment content will be either
|
||||
* the DOCTYPE declaration or the root element of the document.
|
||||
* 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.
|
||||
*/
|
||||
@Nullable
|
||||
private String consumeCommentTokens(String line) {
|
||||
if (!line.contains(START_COMMENT) && !line.contains(END_COMMENT)) {
|
||||
int indexOfStartComment = line.indexOf(START_COMMENT);
|
||||
if (indexOfStartComment == -1 && !line.contains(END_COMMENT)) {
|
||||
return line;
|
||||
}
|
||||
|
||||
String result = "";
|
||||
String currLine = line;
|
||||
if (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 currLine;
|
||||
return result + currLine;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
Reference in New Issue
Block a user