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: * @throws IllegalArgumentException if the supplied class loader is <code>null</code>
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: * @throws IllegalArgumentException if either of the supplied resolvers is <code>null</code>
69: */
70: public DelegatingEntityResolver(EntityResolver dtdResolver,
71: EntityResolver schemaResolver) {
72: Assert.notNull(dtdResolver);
73: Assert.notNull(schemaResolver);
74: this .dtdResolver = dtdResolver;
75: this .schemaResolver = schemaResolver;
76: }
77:
78: public InputSource resolveEntity(String publicId, String systemId)
79: throws SAXException, IOException {
80: if (systemId != null) {
81: if (systemId.endsWith(DTD_SUFFIX)) {
82: return this.dtdResolver.resolveEntity(publicId,
83: systemId);
84: } else if (systemId.endsWith(XSD_SUFFIX)) {
85: return this.schemaResolver.resolveEntity(publicId,
86: systemId);
87: }
88: }
89: return null;
90: }
91:
92: }
|