KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > catalina > util > SchemaResolver


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17 package org.apache.catalina.util;
18
19
20 import java.util.HashMap JavaDoc;
21
22 import org.apache.tomcat.util.digester.Digester;
23 import org.xml.sax.InputSource JavaDoc;
24 import org.xml.sax.EntityResolver JavaDoc;
25 import org.xml.sax.SAXException JavaDoc;
26
27 /**
28  * This class implements a local SAX's <code>EntityResolver</code>. All
29  * DTDs and schemas used to validate the web.xml file will re-directed
30  * to a local file stored in the servlet-api.jar and jsp-api.jar.
31  *
32  * @author Jean-Francois Arcand
33  */

34 public class SchemaResolver implements EntityResolver JavaDoc {
35
36     /**
37      * The disgester instance for which this class is the entity resolver.
38      */

39     protected Digester digester;
40
41
42     /**
43      * The URLs of dtds and schemas that have been registered, keyed by the
44      * public identifier that corresponds.
45      */

46     protected HashMap JavaDoc entityValidator = new HashMap JavaDoc();
47
48
49     /**
50      * The public identifier of the DTD we are currently parsing under
51      * (if any).
52      */

53     protected String JavaDoc publicId = null;
54
55
56     /**
57      * Extension to make the difference between DTD and Schema.
58      */

59     protected String JavaDoc schemaExtension = "xsd";
60
61
62     /**
63      * Create a new <code>EntityResolver</code> that will redirect
64      * all remote dtds and schema to a locat destination.
65      * @param digester The digester instance.
66      */

67     public SchemaResolver(Digester digester) {
68         this.digester = digester;
69     }
70
71
72     /**
73      * Register the specified DTD/Schema URL for the specified public
74      * identifier. This must be called before the first call to
75      * <code>parse()</code>.
76      *
77      * When adding a schema file (*.xsd), only the name of the file
78      * will get added. If two schemas with the same name are added,
79      * only the last one will be stored.
80      *
81      * @param publicId Public identifier of the DTD to be resolved
82      * @param entityURL The URL to use for reading this DTD
83      */

84      public void register(String JavaDoc publicId, String JavaDoc entityURL) {
85          String JavaDoc key = publicId;
86          if (publicId.indexOf(schemaExtension) != -1)
87              key = publicId.substring(publicId.lastIndexOf('/')+1);
88          entityValidator.put(key, entityURL);
89      }
90
91
92     /**
93      * Resolve the requested external entity.
94      *
95      * @param publicId The public identifier of the entity being referenced
96      * @param systemId The system identifier of the entity being referenced
97      *
98      * @exception SAXException if a parsing exception occurs
99      *
100      */

101     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId)
102         throws SAXException JavaDoc {
103
104         if (publicId != null) {
105             this.publicId = publicId;
106             digester.setPublicId(publicId);
107         }
108
109         // Has this system identifier been registered?
110
String JavaDoc entityURL = null;
111         if (publicId != null) {
112             entityURL = (String JavaDoc) entityValidator.get(publicId);
113         }
114
115         // Redirect the schema location to a local destination
116
String JavaDoc key = null;
117         if (entityURL == null && systemId != null) {
118             key = systemId.substring(systemId.lastIndexOf('/')+1);
119             entityURL = (String JavaDoc)entityValidator.get(key);
120         }
121
122         if (entityURL == null) {
123            return (null);
124         }
125
126         try {
127             return (new InputSource JavaDoc(entityURL));
128         } catch (Exception JavaDoc e) {
129             throw new SAXException JavaDoc(e);
130         }
131
132     }
133
134 }
135
Popular Tags