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:
@@ -0,0 +1,62 @@
|
||||
/*
|
||||
* 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.util.xml;
|
||||
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.springframework.util.xml.XmlValidationModeDetector.VALIDATION_DTD;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link XmlValidationModeDetector}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 5.1.10
|
||||
*/
|
||||
public class XmlValidationModeDetectorTests {
|
||||
|
||||
private final XmlValidationModeDetector xmlValidationModeDetector = new XmlValidationModeDetector();
|
||||
|
||||
|
||||
@Test
|
||||
public void dtdWithTrailingComment() throws Exception {
|
||||
dtdDetection("dtdWithTrailingComment.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dtdWithLeadingComment() throws Exception {
|
||||
dtdDetection("dtdWithLeadingComment.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dtdWithCommentOnNextLine() throws Exception {
|
||||
dtdDetection("dtdWithCommentOnNextLine.xml");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dtdWithMultipleComments() throws Exception {
|
||||
dtdDetection("dtdWithMultipleComments.xml");
|
||||
}
|
||||
|
||||
private void dtdDetection(String fileName) throws Exception {
|
||||
InputStream inputStream = getClass().getResourceAsStream(fileName);
|
||||
assertEquals(VALIDATION_DTD, xmlValidationModeDetector.detectValidationMode(inputStream));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "https://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
<!-- comment on next line -->
|
||||
|
||||
<beans>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- leading comment --><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "https://www.springframework.org/dtd/spring-beans-2.0.dtd">
|
||||
|
||||
<beans>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!-- comment #1 --> <!-- comment #2 --> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "https://www.springframework.org/dtd/spring-beans-2.0.dtd"><!--
|
||||
trailing
|
||||
comment
|
||||
-->
|
||||
|
||||
<beans>
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "https://www.springframework.org/dtd/spring-beans-2.0.dtd"><!-- trailing comment -->
|
||||
|
||||
<beans>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user