1 16 17 package org.springframework.beans.factory.xml; 18 19 import java.io.IOException ; 20 import java.util.Properties ; 21 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.xml.sax.EntityResolver ; 25 import org.xml.sax.InputSource ; 26 27 import org.springframework.beans.FatalBeanException; 28 import org.springframework.core.io.ClassPathResource; 29 import org.springframework.core.io.Resource; 30 import org.springframework.core.io.support.PropertiesLoaderUtils; 31 import org.springframework.util.Assert; 32 33 54 public class PluggableSchemaResolver implements EntityResolver { 55 56 60 public static final String DEFAULT_SCHEMA_MAPPINGS_LOCATION = "META-INF/spring.schemas"; 61 62 63 private static final Log logger = LogFactory.getLog(PluggableSchemaResolver.class); 64 65 private final ClassLoader classLoader; 66 67 68 private final Properties schemaMappings; 69 70 71 78 public PluggableSchemaResolver(ClassLoader classLoader) { 79 this(classLoader, DEFAULT_SCHEMA_MAPPINGS_LOCATION); 80 } 81 82 91 public PluggableSchemaResolver(ClassLoader classLoader, String schemaMappingsLocation) { 92 Assert.hasText(schemaMappingsLocation, "'schemaMappingsLocation' must not be empty"); 93 this.classLoader = classLoader; 94 if (logger.isDebugEnabled()) { 95 logger.debug("Loading schema mappings from [" + schemaMappingsLocation + "]"); 96 } 97 try { 98 this.schemaMappings = 99 PropertiesLoaderUtils.loadAllProperties(schemaMappingsLocation, classLoader); 100 if (logger.isDebugEnabled()) { 101 logger.debug("Loaded schema mappings: " + this.schemaMappings); 102 } 103 } 104 catch (IOException e) { 105 throw new FatalBeanException( 106 "Unable to load schema mappings from location [" + schemaMappingsLocation + "]", e); 107 } 108 } 109 110 111 public InputSource resolveEntity(String publicId, String systemId) throws IOException { 112 if (logger.isTraceEnabled()) { 113 logger.trace("Trying to resolve XML entity with public id [" + publicId + 114 "] and system id [" + systemId + "]"); 115 } 116 if (systemId != null) { 117 String resourceLocation = this.schemaMappings.getProperty(systemId); 118 if (resourceLocation != null) { 119 Resource resource = new ClassPathResource(resourceLocation, this.classLoader); 120 InputSource source = new InputSource (resource.getInputStream()); 121 source.setPublicId(publicId); 122 source.setSystemId(systemId); 123 if (logger.isDebugEnabled()) { 124 logger.debug("Found XML schema [" + systemId + "] in classpath: " + resourceLocation); 125 } 126 return source; 127 } 128 } 129 return null; 130 } 131 132 } 133 | Popular Tags |