1 16 17 package org.springframework.beans.factory.xml; 18 19 import java.io.IOException ; 20 21 import org.apache.commons.logging.Log; 22 import org.apache.commons.logging.LogFactory; 23 import org.xml.sax.EntityResolver ; 24 import org.xml.sax.InputSource ; 25 26 import org.springframework.core.io.ClassPathResource; 27 import org.springframework.core.io.Resource; 28 29 43 public class BeansDtdResolver implements EntityResolver { 44 45 private static final String DTD_EXTENSION = ".dtd"; 46 47 private static final String [] DTD_NAMES = {"spring-beans-2.0", "spring-beans"}; 48 49 private static final Log logger = LogFactory.getLog(BeansDtdResolver.class); 50 51 52 public InputSource resolveEntity(String publicId, String systemId) throws IOException { 53 if (logger.isTraceEnabled()) { 54 logger.trace("Trying to resolve XML entity with public ID [" + publicId + 55 "] and system ID [" + systemId + "]"); 56 } 57 if (systemId != null && systemId.endsWith(DTD_EXTENSION)) { 58 int lastPathSeparator = systemId.lastIndexOf("/"); 59 for (int i = 0; i < DTD_NAMES.length; ++i) { 60 int dtdNameStart = systemId.indexOf(DTD_NAMES[i]); 61 if (dtdNameStart > lastPathSeparator) { 62 String dtdFile = systemId.substring(dtdNameStart); 63 if (logger.isTraceEnabled()) { 64 logger.trace("Trying to locate [" + dtdFile + "] in Spring jar"); 65 } 66 try { 67 Resource resource = new ClassPathResource(dtdFile, getClass()); 68 InputSource source = new InputSource (resource.getInputStream()); 69 source.setPublicId(publicId); 70 source.setSystemId(systemId); 71 if (logger.isDebugEnabled()) { 72 logger.debug("Found beans DTD [" + systemId + "] in classpath: " + dtdFile); 73 } 74 return source; 75 } 76 catch (IOException ex) { 77 if (logger.isDebugEnabled()) { 78 logger.debug("Could not resolve beans DTD [" + systemId + "]: not found in class path", ex); 79 } 80 } 81 82 } 83 } 84 } 85 86 return null; 88 } 89 90 } 91 | Popular Tags |