KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.sax.EntityResolver JavaDoc;
22 import org.xml.sax.InputSource JavaDoc;
23 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc {
39
40     /** Suffix for DTD files */
41     public static final String JavaDoc DTD_SUFFIX = ".dtd";
42
43     /** Suffix for schema definition files */
44     public static final String JavaDoc XSD_SUFFIX = ".xsd";
45
46
47     private final EntityResolver JavaDoc dtdResolver;
48
49     private final EntityResolver JavaDoc schemaResolver;
50
51
52     /**
53      * Create a new DelegatingEntityResolver that delegates to
54      * a default {@link BeansDtdResolver} and a default {@link PluggableSchemaResolver}.
55      * <p>Configures the {@link PluggableSchemaResolver} with the supplied
56      * {@link ClassLoader}.
57      * @param classLoader the ClassLoader to use for loading
58      * @throws IllegalArgumentException if the supplied class loader is <code>null</code>
59      */

60     public DelegatingEntityResolver(ClassLoader JavaDoc classLoader) {
61         this.dtdResolver = new BeansDtdResolver();
62         this.schemaResolver = new PluggableSchemaResolver(classLoader);
63     }
64
65     /**
66      * Create a new DelegatingEntityResolver that delegates to
67      * the given {@link EntityResolver EntityResolvers}.
68      * @param dtdResolver the EntityResolver to resolve DTDs with
69      * @param schemaResolver the EntityResolver to resolve XML schemas with
70      * @throws IllegalArgumentException if either of the supplied resolvers is <code>null</code>
71      */

72     public DelegatingEntityResolver(EntityResolver JavaDoc dtdResolver, EntityResolver JavaDoc schemaResolver) {
73         Assert.notNull(dtdResolver);
74         Assert.notNull(schemaResolver);
75         this.dtdResolver = dtdResolver;
76         this.schemaResolver = schemaResolver;
77     }
78
79
80     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc {
81         if (systemId != null) {
82             if (systemId.endsWith(DTD_SUFFIX)) {
83                 return this.dtdResolver.resolveEntity(publicId, systemId);
84             }
85             else if (systemId.endsWith(XSD_SUFFIX)) {
86                 return this.schemaResolver.resolveEntity(publicId, systemId);
87             }
88         }
89         return null;
90     }
91
92 }
93
Popular Tags