package org.springframework.beans.factory.xml;

import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;

import org.springframework.core.io.ClassPathResource;

/**
 * EntityResolver implementation for the Spring beans DTD,
 * to load the DTD from the Spring classpath resp. JAR file.
 *
 * <p>Fetches "spring-beans.dtd" from the classpath resource
 * "/org/springframework/beans/factory/xml/spring-beans.dtd",
 * no matter if specified as some local URL or as
 * "http://www.springframework.org/dtd/spring-beans.dtd".
 *
 * @author Juergen Hoeller
 * @since 04.06.2003
 */
public class BeansDtdResolver implements EntityResolver {

	private static final String DTD_NAME = "spring-beans";

	private static final String SEARCH_PACKAGE = "org/springframework/beans/factory/xml/";

	protected final Log logger = LogFactory.getLog(getClass());

	public InputSource resolveEntity(String publicId, String systemId) throws IOException {
		logger.debug("Trying to resolve XML entity with public ID [" + publicId +
								 "] and system ID [" + systemId + "]");

		if (systemId != null && systemId.indexOf(DTD_NAME) > systemId.lastIndexOf("/")) {
			String dtdFile = systemId.substring(systemId.indexOf(DTD_NAME));
			// Search for DTD
			logger.debug("Trying to locate [" + dtdFile + "] under [" + SEARCH_PACKAGE + "]");
			InputStream is = (new ClassPathResource(SEARCH_PACKAGE + dtdFile)).getInputStream();
			if (is != null) {
				logger.debug("Found beans DTD [" + systemId + "] in class path");
				InputSource source = new InputSource(is);
				source.setPublicId(publicId);
				source.setSystemId(systemId);
				return source;
			}
			else {
				logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in class path");
			}
		}
		// use the default behaviour -> download from website or wherever
		return null;
	}

}

@@ -72,36 +72,8 @@
 ion;
-%0Aimport java.io.InputStream;
 %0A%0Aim
@@ -276,16 +276,61 @@
 esource;
+%0Aimport org.springframework.core.io.Resource;
 %0A%0A/**%0A *
@@ -899,16 +899,17 @@
 KAGE = %22
+/
 org/spri
@@ -1217,17 +1217,16 @@
 + %22%5D%22);%0A
-%0A
 %09%09if (sy
@@ -1373,29 +1373,8 @@
 ));%0A
-%09%09%09// Search for DTD%0A
 %09%09%09l
@@ -1462,26 +1462,38 @@
 %0A%09%09%09
-InputStream is
+try %7B%0A%09%09%09%09Resource resource
  = 
-(
 new 
@@ -1538,117 +1538,20 @@
 File
-)).getInputStream();%0A%09%09%09if (is != null) %7B%0A%09%09%09%09logger.debug(%22Found beans DTD %5B%22 + systemId + %22%5D in class path%22
+, getClass()
 );%0A%09
@@ -1594,10 +1594,33 @@
 rce(
-is
+resource.getInputStream()
 );%0A%09
@@ -1682,24 +1682,93 @@
 (systemId);%0A
+%09%09%09%09logger.debug(%22Found beans DTD %5B%22 + systemId + %22%5D in classpath%22);%0A
 %09%09%09%09return s
@@ -1786,12 +1786,30 @@
 %0A%09%09%09
-else
+catch (IOException ex)
  %7B%0A%09
@@ -1895,14 +1895,17 @@
 lass
- 
 path%22
+, ex
 );%0A%09

load DTD with classloader of BeansDtdResolver instead of thread context classloader