Skip to content

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

Closed
@shooye

Description

@shooye

Affects: 5.3.14 and earlier

If you want to load a DTD configuration file, <! -- Different positions from --> will affect the loading of spring.

If my profile is written like this:

<?xml version="1.0" encoding="UTF-8"?>

<!--

DOCTYPE -->  <!--  -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

</beans>

Then, when Spring is used to load the configuration file, an exception will appear.

This is because there is a bug when determining the profile validation pattern.

In org.springframework.util.xml.XmlValidationModeDetector,Spring analyzes programs one line at a time.

However, each analysis focuses only on the first <!-- in each line. Spring will put <!-- The previous content is not considered as a comment.
Thus, the situation in the above example is ignored.

I think that can result in org.springframework.util.xml.XmlValidationModeDetector#consumeCommentTokens(String line) return to add the following code before:

@Nullable
private String consumeCommentTokens(String line) {
    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)) {
            int index = result.indexOf(END_COMMENT);
            if(index != -1) result = result.substring(index + END_COMMENT.length());
            return result + currLine;
        }
    }
    return null;
}

It can be removed in front of '-->'.

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions