KickJava   Java API By Example, From Geeks To Geeks.

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


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
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.xml.sax.EntityResolver JavaDoc;
24 import org.xml.sax.InputSource JavaDoc;
25
26 import org.springframework.core.io.ClassPathResource;
27 import org.springframework.core.io.Resource;
28
29 /**
30  * EntityResolver implementation for the Spring beans DTD,
31  * to load the DTD from the Spring class path (or JAR file).
32  *
33  * <p>Fetches "spring-beans-2.0.dtd" from the class path resource
34  * "/org/springframework/beans/factory/xml/spring-beans-2.0.dtd",
35  * no matter whether specified as some local URL that includes "spring-beans"
36  * in the DTD name or as "http://www.springframework.org/dtd/spring-beans-2.0.dtd".
37  *
38  * @author Juergen Hoeller
39  * @author Colin Sampaleanu
40  * @since 04.06.2003
41  * @see ResourceEntityResolver
42  */

43 public class BeansDtdResolver implements EntityResolver JavaDoc {
44     
45     private static final String JavaDoc DTD_EXTENSION = ".dtd";
46
47     private static final String JavaDoc[] DTD_NAMES = {"spring-beans-2.0", "spring-beans"};
48
49     private static final Log logger = LogFactory.getLog(BeansDtdResolver.class);
50
51
52     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws IOException JavaDoc {
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 JavaDoc 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 JavaDoc source = new InputSource JavaDoc(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 JavaDoc 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         // Use the default behavior -> download from website or wherever.
87
return null;
88     }
89
90 }
91
Popular Tags