001: /*
002: * Copyright 2002-2007 the original author or authors.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.springframework.beans.factory.xml;
018:
019: import java.io.IOException;
020:
021: import org.apache.commons.logging.Log;
022: import org.apache.commons.logging.LogFactory;
023: import org.xml.sax.EntityResolver;
024: import org.xml.sax.InputSource;
025:
026: import org.springframework.core.io.ClassPathResource;
027: import org.springframework.core.io.Resource;
028:
029: /**
030: * EntityResolver implementation for the Spring beans DTD,
031: * to load the DTD from the Spring class path (or JAR file).
032: *
033: * <p>Fetches "spring-beans-2.0.dtd" from the class path resource
034: * "/org/springframework/beans/factory/xml/spring-beans-2.0.dtd",
035: * no matter whether specified as some local URL that includes "spring-beans"
036: * in the DTD name or as "http://www.springframework.org/dtd/spring-beans-2.0.dtd".
037: *
038: * @author Juergen Hoeller
039: * @author Colin Sampaleanu
040: * @since 04.06.2003
041: * @see ResourceEntityResolver
042: */
043: public class BeansDtdResolver implements EntityResolver {
044:
045: private static final String DTD_EXTENSION = ".dtd";
046:
047: private static final String[] DTD_NAMES = { "spring-beans-2.0",
048: "spring-beans" };
049:
050: private static final Log logger = LogFactory
051: .getLog(BeansDtdResolver.class);
052:
053: public InputSource resolveEntity(String publicId, String systemId)
054: throws IOException {
055: if (logger.isTraceEnabled()) {
056: logger
057: .trace("Trying to resolve XML entity with public ID ["
058: + publicId
059: + "] and system ID ["
060: + systemId
061: + "]");
062: }
063: if (systemId != null && systemId.endsWith(DTD_EXTENSION)) {
064: int lastPathSeparator = systemId.lastIndexOf("/");
065: for (int i = 0; i < DTD_NAMES.length; ++i) {
066: int dtdNameStart = systemId.indexOf(DTD_NAMES[i]);
067: if (dtdNameStart > lastPathSeparator) {
068: String dtdFile = systemId.substring(dtdNameStart);
069: if (logger.isTraceEnabled()) {
070: logger.trace("Trying to locate [" + dtdFile
071: + "] in Spring jar");
072: }
073: try {
074: Resource resource = new ClassPathResource(
075: dtdFile, getClass());
076: InputSource source = new InputSource(resource
077: .getInputStream());
078: source.setPublicId(publicId);
079: source.setSystemId(systemId);
080: if (logger.isDebugEnabled()) {
081: logger.debug("Found beans DTD [" + systemId
082: + "] in classpath: " + dtdFile);
083: }
084: return source;
085: } catch (IOException ex) {
086: if (logger.isDebugEnabled()) {
087: logger
088: .debug(
089: "Could not resolve beans DTD ["
090: + systemId
091: + "]: not found in class path",
092: ex);
093: }
094: }
095:
096: }
097: }
098: }
099:
100: // Use the default behavior -> download from website or wherever.
101: return null;
102: }
103:
104: }
|