KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > beans > factory > xml > PluggableSchemaResolver


1 /*
2  * Copyright 2002-2007 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.beans.factory.xml;
18
19 import java.io.IOException JavaDoc;
20 import java.util.Properties JavaDoc;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.xml.sax.EntityResolver JavaDoc;
25 import org.xml.sax.InputSource JavaDoc;
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 /**
34  * {@link EntityResolver} implementation that attempts to resolve schema URLs into
35  * local {@link ClassPathResource classpath resources} using a set of mappings files.
36  *
37  * <p>By default, this class will look for mapping files in the classpath using the pattern:
38  * <code>META-INF/spring.schemas</code> allowing for multiple files to exist on the
39  * classpath at any one time.
40  *
41  * The format of <code>META-INF/spring.schemas</code> is a properties
42  * file where each line should be of the form <code>systemId=schema-location</code>
43  * where <code>schema-location</code> should also be a schema file in the classpath.
44  * Since systemId is commonly a URL, one must be careful to escape any ':' characters
45  * which are treated as delimiters in properties files.
46  *
47  * <p>The pattern for the mapping files can be overidden using the
48  * {@link #PluggableSchemaResolver(ClassLoader, String)} constructor
49  *
50  * @author Rob Harrop
51  * @author Juergen Hoeller
52  * @since 2.0
53  */

54 public class PluggableSchemaResolver implements EntityResolver JavaDoc {
55
56     /**
57      * The location of the file that defines schema mappings.
58      * Can be present in multiple JAR files.
59      */

60     public static final String JavaDoc 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 JavaDoc classLoader;
66
67     /** Stores the mapping of schema URL -> local schema path */
68     private final Properties JavaDoc schemaMappings;
69
70
71     /**
72      * Loads the schema URL -> schema file location mappings using the default
73      * mapping file pattern "META-INF/spring.schemas".
74      * @param classLoader the ClassLoader to use for loading
75      * (can be <code>null</code>) to use the default ClassLoader)
76      * @see PropertiesLoaderUtils#loadAllProperties(String, ClassLoader)
77      */

78     public PluggableSchemaResolver(ClassLoader JavaDoc classLoader) {
79         this(classLoader, DEFAULT_SCHEMA_MAPPINGS_LOCATION);
80     }
81
82     /**
83      * Loads the schema URL -> schema file location mappings using the given
84      * mapping file pattern.
85      * @param classLoader the ClassLoader to use for loading
86      * (can be <code>null</code>) to use the default ClassLoader)
87      * @param schemaMappingsLocation the location of the file that defines schema mappings
88      * (must not be empty)
89      * @see PropertiesLoaderUtils#loadAllProperties(String, ClassLoader)
90      */

91     public PluggableSchemaResolver(ClassLoader JavaDoc classLoader, String JavaDoc 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 JavaDoc e) {
105             throw new FatalBeanException(
106                     "Unable to load schema mappings from location [" + schemaMappingsLocation + "]", e);
107         }
108     }
109
110
111     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws IOException JavaDoc {
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 JavaDoc resourceLocation = this.schemaMappings.getProperty(systemId);
118             if (resourceLocation != null) {
119                 Resource resource = new ClassPathResource(resourceLocation, this.classLoader);
120                 InputSource JavaDoc source = new InputSource JavaDoc(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