Skip to content

fix:Spring fails to determine XML is XSD-based if DOCTYPE appears in a comment #27927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public class XmlValidationModeDetector {
* Indicates whether or not the current parse position is inside an XML comment.
*/
private boolean inComment;
// private boolean nowInComment;


/**
Expand All @@ -95,7 +96,7 @@ public int detectValidationMode(InputStream inputStream) throws IOException {
String content;
while ((content = reader.readLine()) != null) {
content = consumeCommentTokens(content);
if (this.inComment || !StringUtils.hasText(content)) {
if (!StringUtils.hasText(content)) {
continue;
}
if (hasDoctype(content)) {
Expand Down Expand Up @@ -147,22 +148,28 @@ private boolean hasOpeningTag(String content) {
private String consumeCommentTokens(String line) {
int indexOfStartComment = line.indexOf(START_COMMENT);
if (indexOfStartComment == -1 && !line.contains(END_COMMENT)) {
if(inComment) return null;
return line;
}

String result = "";
String currLine = line;
if (indexOfStartComment >= 0) {
if (indexOfStartComment >= 0 && !this.inComment) {
result = line.substring(0, indexOfStartComment);
currLine = line.substring(indexOfStartComment);
}

int index = endComment(result);
if(index != -1) {
result = result.substring(index);
}

String resultCurrLine = null;
while ((currLine = consume(currLine)) != null) {
if (!this.inComment && !currLine.trim().startsWith(START_COMMENT)) {
return result + currLine;
}
resultCurrLine = currLine;
}
return null;

return result + resultCurrLine;
}

/**
Expand All @@ -171,8 +178,18 @@ private String consumeCommentTokens(String line) {
*/
@Nullable
private String consume(String line) {
int index = (this.inComment ? endComment(line) : startComment(line));
return (index == -1 ? null : line.substring(index));
if(this.inComment) {
int endIndex;
if((endIndex = endComment(line)) == -1)
return null;
return line.substring(endIndex);
}else {
int startIndex, endIndex;
if((startIndex = startComment(line)) == -1)
return null;
endIndex = endComment(line);
return line.substring(0, startIndex - START_COMMENT.length()) + (endIndex == -1 ? "" : line.substring(endIndex));
}
}

/**
Expand Down