01: /*
02: * Copyright 2002-2007 the original author or authors.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
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;
20:
21: import org.xml.sax.EntityResolver;
22: import org.xml.sax.InputSource;
23: import org.xml.sax.SAXException;
24:
25: import org.springframework.util.Assert;
26:
27: /**
28: * {@link EntityResolver} implementation that delegates to a {@link BeansDtdResolver}
29: * and a {@link PluggableSchemaResolver} for DTDs and XML schemas, respectively.
30: *
31: * @author Rob Harrop
32: * @author Juergen Hoeller
33: * @author Rick Evans
34: * @since 2.0
35: * @see BeansDtdResolver
36: * @see PluggableSchemaResolver
37: */
38: public class DelegatingEntityResolver implements EntityResolver {
39:
40: /** Suffix for DTD files */
41: public static final String DTD_SUFFIX = ".dtd";
42:
43: /** Suffix for schema definition files */
44: public static final String XSD_SUFFIX = ".xsd";
45:
46: private final EntityResolver dtdResolver;
47:
48: private final EntityResolver schemaResolver;
49:
50: /**
51: * Create a new DelegatingEntityResolver that delegates to
52: * a default {@link BeansDtdResolver} and a default {@link PluggableSchemaResolver}.
53: * <p>Configures the {@link PluggableSchemaResolver} with the supplied
54: * {@link ClassLoader}.
55: * @param classLoader the ClassLoader to use for loading
56: * (can be <code>null</code>) to use the default ClassLoader)
57: */
58: public DelegatingEntityResolver(ClassLoader classLoader) {
59: this .dtdResolver = new BeansDtdResolver();
60: this .schemaResolver = new PluggableSchemaResolver(classLoader);
61: }
62:
63: /**
64: * Create a new DelegatingEntityResolver that delegates to
65: * the given {@link EntityResolver EntityResolvers}.
66: * @param dtdResolver the EntityResolver to resolve DTDs with
67: * @param schemaResolver the EntityResolver to resolve XML schemas with
68: */
69: public DelegatingEntityResolver(EntityResolver dtdResolver,
70: EntityResolver schemaResolver) {
71: Assert.notNull(dtdResolver, "'dtdResolver' is required");
72: Assert.notNull(schemaResolver, "'schemaResolver' is required");
73: this .dtdResolver = dtdResolver;
74: this .schemaResolver = schemaResolver;
75: }
76:
77: public InputSource resolveEntity(String publicId, String systemId)
78: throws SAXException, IOException {
79: if (systemId != null) {
80: if (systemId.endsWith(DTD_SUFFIX)) {
81: return this.dtdResolver.resolveEntity(publicId,
82: systemId);
83: } else if (systemId.endsWith(XSD_SUFFIX)) {
84: return this.schemaResolver.resolveEntity(publicId,
85: systemId);
86: }
87: }
88: return null;
89: }
90:
91: }
|