BeansDtdResolver resolves spring-beans.dtd declarations to spring-beans-2.0.dtd file

Issue: SPR-12836
master
Juergen Hoeller 10 years ago
parent 898c24fcdd
commit 8e4bfa9cc1
  1. 49
      spring-beans/src/main/java/org/springframework/beans/factory/xml/BeansDtdResolver.java
  2. 2
      spring-beans/src/test/resources/org/springframework/beans/factory/xml/import.xml

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,7 +17,6 @@
package org.springframework.beans.factory.xml; package org.springframework.beans.factory.xml;
import java.io.IOException; import java.io.IOException;
import java.util.Arrays;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -45,7 +44,9 @@ public class BeansDtdResolver implements EntityResolver {
private static final String DTD_EXTENSION = ".dtd"; private static final String DTD_EXTENSION = ".dtd";
private static final String[] DTD_NAMES = {"spring-beans-2.0", "spring-beans"}; private static final String DTD_FILENAME = "spring-beans-2.0";
private static final String DTD_NAME = "spring-beans";
private static final Log logger = LogFactory.getLog(BeansDtdResolver.class); private static final Log logger = LogFactory.getLog(BeansDtdResolver.class);
@ -58,30 +59,28 @@ public class BeansDtdResolver implements EntityResolver {
} }
if (systemId != null && systemId.endsWith(DTD_EXTENSION)) { if (systemId != null && systemId.endsWith(DTD_EXTENSION)) {
int lastPathSeparator = systemId.lastIndexOf("/"); int lastPathSeparator = systemId.lastIndexOf("/");
for (String DTD_NAME : DTD_NAMES) { int dtdNameStart = systemId.indexOf(DTD_NAME);
int dtdNameStart = systemId.indexOf(DTD_NAME); if (dtdNameStart > lastPathSeparator) {
if (dtdNameStart > lastPathSeparator) { String dtdFile = DTD_FILENAME + DTD_EXTENSION;
String dtdFile = systemId.substring(dtdNameStart); if (logger.isTraceEnabled()) {
if (logger.isTraceEnabled()) { logger.trace("Trying to locate [" + dtdFile + "] in Spring jar on classpath");
logger.trace("Trying to locate [" + dtdFile + "] in Spring jar"); }
} try {
try { Resource resource = new ClassPathResource(dtdFile, getClass());
Resource resource = new ClassPathResource(dtdFile, getClass()); InputSource source = new InputSource(resource.getInputStream());
InputSource source = new InputSource(resource.getInputStream()); source.setPublicId(publicId);
source.setPublicId(publicId); source.setSystemId(systemId);
source.setSystemId(systemId); if (logger.isDebugEnabled()) {
if (logger.isDebugEnabled()) { logger.debug("Found beans DTD [" + systemId + "] in classpath: " + dtdFile);
logger.debug("Found beans DTD [" + systemId + "] in classpath: " + dtdFile);
}
return source;
} }
catch (IOException ex) { return source;
if (logger.isDebugEnabled()) { }
logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in class path", ex); catch (IOException ex) {
} if (logger.isDebugEnabled()) {
logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in classpath", ex);
} }
} }
} }
} }
@ -92,7 +91,7 @@ public class BeansDtdResolver implements EntityResolver {
@Override @Override
public String toString() { public String toString() {
return "EntityResolver for DTDs " + Arrays.toString(DTD_NAMES); return "EntityResolver for spring-beans DTD";
} }
} }

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd"> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans> <beans>

Loading…
Cancel
Save