Skip to content

Commit 2de5faf

Browse files
committed
BeansDtdResolver resolves spring-beans.dtd declarations to spring-beans-2.0.dtd file
Issue: SPR-12836 (cherry picked from commit 8e4bfa9)
1 parent 5f95ff6 commit 2de5faf

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/xml/BeansDtdResolver.java

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,6 @@
1717
package org.springframework.beans.factory.xml;
1818

1919
import java.io.IOException;
20-
import java.util.Arrays;
2120

2221
import org.apache.commons.logging.Log;
2322
import org.apache.commons.logging.LogFactory;
@@ -45,7 +44,9 @@ public class BeansDtdResolver implements EntityResolver {
4544

4645
private static final String DTD_EXTENSION = ".dtd";
4746

48-
private static final String[] DTD_NAMES = {"spring-beans-2.0", "spring-beans"};
47+
private static final String DTD_FILENAME = "spring-beans-2.0";
48+
49+
private static final String DTD_NAME = "spring-beans";
4950

5051
private static final Log logger = LogFactory.getLog(BeansDtdResolver.class);
5152

@@ -58,30 +59,28 @@ public InputSource resolveEntity(String publicId, String systemId) throws IOExce
5859
}
5960
if (systemId != null && systemId.endsWith(DTD_EXTENSION)) {
6061
int lastPathSeparator = systemId.lastIndexOf("/");
61-
for (String DTD_NAME : DTD_NAMES) {
62-
int dtdNameStart = systemId.indexOf(DTD_NAME);
63-
if (dtdNameStart > lastPathSeparator) {
64-
String dtdFile = systemId.substring(dtdNameStart);
65-
if (logger.isTraceEnabled()) {
66-
logger.trace("Trying to locate [" + dtdFile + "] in Spring jar");
67-
}
68-
try {
69-
Resource resource = new ClassPathResource(dtdFile, getClass());
70-
InputSource source = new InputSource(resource.getInputStream());
71-
source.setPublicId(publicId);
72-
source.setSystemId(systemId);
73-
if (logger.isDebugEnabled()) {
74-
logger.debug("Found beans DTD [" + systemId + "] in classpath: " + dtdFile);
75-
}
76-
return source;
62+
int dtdNameStart = systemId.indexOf(DTD_NAME);
63+
if (dtdNameStart > lastPathSeparator) {
64+
String dtdFile = DTD_FILENAME + DTD_EXTENSION;
65+
if (logger.isTraceEnabled()) {
66+
logger.trace("Trying to locate [" + dtdFile + "] in Spring jar on classpath");
67+
}
68+
try {
69+
Resource resource = new ClassPathResource(dtdFile, getClass());
70+
InputSource source = new InputSource(resource.getInputStream());
71+
source.setPublicId(publicId);
72+
source.setSystemId(systemId);
73+
if (logger.isDebugEnabled()) {
74+
logger.debug("Found beans DTD [" + systemId + "] in classpath: " + dtdFile);
7775
}
78-
catch (IOException ex) {
79-
if (logger.isDebugEnabled()) {
80-
logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in class path", ex);
81-
}
76+
return source;
77+
}
78+
catch (IOException ex) {
79+
if (logger.isDebugEnabled()) {
80+
logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in classpath", ex);
8281
}
83-
8482
}
83+
8584
}
8685
}
8786

@@ -92,7 +91,7 @@ public InputSource resolveEntity(String publicId, String systemId) throws IOExce
9291

9392
@Override
9493
public String toString() {
95-
return "EntityResolver for DTDs " + Arrays.toString(DTD_NAMES);
94+
return "EntityResolver for spring-beans DTD";
9695
}
9796

9897
}

spring-beans/src/test/resources/org/springframework/beans/factory/xml/import.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd">
2+
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
33

44
<beans>
55

0 commit comments

Comments
 (0)