KickJava   Java API By Example, From Geeks To Geeks.

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


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.File JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.net.URL JavaDoc;
22 import java.net.URLDecoder JavaDoc;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.xml.sax.InputSource JavaDoc;
27 import org.xml.sax.SAXException JavaDoc;
28
29 import org.springframework.core.io.Resource;
30 import org.springframework.core.io.ResourceLoader;
31
32 /**
33  * EntityResolver implementation that tries to resolve entity references
34  * through a {@link org.springframework.core.io.ResourceLoader} (usually,
35  * relative to the resource base of an ApplicationContext), if applicable.
36  * Extends {@link DelegatingEntityResolver} to also provide DTD and XSD lookup.
37  *
38  * <p>Allows to use standard XML entities to include XML snippets into an
39  * application context definition, for example to split a large XML file
40  * into various modules. The include paths can be relative to the
41  * application context's resource base as usual, instead of relative
42  * to the JVM working directory (the XML parser's default).
43  *
44  * <p>Note: In addition to relative paths, every URL that specifies a
45  * file in the current system root, i.e. the JVM working directory,
46  * will be interpreted relative to the application context too.
47  *
48  * @author Juergen Hoeller
49  * @since 31.07.2003
50  * @see org.springframework.core.io.ResourceLoader
51  * @see org.springframework.context.ApplicationContext
52  */

53 public class ResourceEntityResolver extends DelegatingEntityResolver {
54
55     private static final Log logger = LogFactory.getLog(ResourceEntityResolver.class);
56
57     private final ResourceLoader resourceLoader;
58
59
60     /**
61      * Create a ResourceEntityResolver for the specified ResourceLoader
62      * (usually, an ApplicationContext).
63      * @param resourceLoader the ResourceLoader (or ApplicationContext)
64      * to load XML entity includes with
65      */

66     public ResourceEntityResolver(ResourceLoader resourceLoader) {
67         super(resourceLoader.getClassLoader());
68         this.resourceLoader = resourceLoader;
69     }
70
71
72     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
73         InputSource JavaDoc source = super.resolveEntity(publicId, systemId);
74         if (source == null && systemId != null) {
75             String JavaDoc resourcePath = null;
76             try {
77                 String JavaDoc decodedSystemId = URLDecoder.decode(systemId);
78                 String JavaDoc givenUrl = new URL JavaDoc(decodedSystemId).toString();
79                 String JavaDoc systemRootUrl = new File JavaDoc("").toURL().toString();
80                 // Try relative to resource base if currently in system root.
81
if (givenUrl.startsWith(systemRootUrl)) {
82                     resourcePath = givenUrl.substring(systemRootUrl.length());
83                 }
84             }
85             catch (Exception JavaDoc ex) {
86                 // Typically a MalformedURLException or AccessControlException.
87
if (logger.isDebugEnabled()) {
88                     logger.debug("Could not resolve XML entity [" + systemId + "] against system root URL", ex);
89                 }
90                 // No URL (or no resolvable URL) -> try relative to resource base.
91
resourcePath = systemId;
92             }
93             if (resourcePath != null) {
94                 if (logger.isTraceEnabled()) {
95                     logger.trace("Trying to locate XML entity [" + systemId + "] as resource [" + resourcePath + "]");
96                 }
97                 Resource resource = this.resourceLoader.getResource(resourcePath);
98                 source = new InputSource JavaDoc(resource.getInputStream());
99                 source.setPublicId(publicId);
100                 source.setSystemId(systemId);
101                 if (logger.isDebugEnabled()) {
102                     logger.debug("Found XML entity [" + systemId + "]: " + resource);
103                 }
104             }
105         }
106         return source;
107     }
108
109 }
110
Popular Tags