KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > apache > xerces > readers > XMLCatalogHandler


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 1999 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "Xerces" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 1999, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.enhydra.apache.xerces.readers;
59
60 import java.io.IOException JavaDoc;
61 import java.util.Enumeration JavaDoc;
62 import java.util.Hashtable JavaDoc;
63
64 import org.xml.sax.EntityResolver JavaDoc;
65 import org.xml.sax.InputSource JavaDoc;
66 import org.xml.sax.SAXException JavaDoc;
67
68 /**
69  * Interface for implementing basic catalog support in the parser.
70  * To implement and use a Catalog, implement this interface and
71  * install your catalog instance as the EntityResolver in the
72  * parser's entity handler. For example:
73  * <pre>
74  * XMLParser parser = new AnyParser();
75  * <font color="blue">parser.addCatalogHandler(new MyCatalog());</font>
76  * </pre>
77  * <p>
78  * This default catalog implementation does not provide a method
79  * for loading multiple catalogs from various input sources.
80  * Instead, it is a convenient base class for other catalog
81  * implementations.
82  * <p>
83  * To create a catalog implementation, simply extend this class
84  * and implement the <tt>loadCatalog</tt> method. Public and system
85  * identifier mappings can be stored and accessed using the
86  * convenient public methods on this class.
87  *
88  * @author Andy Clark, IBM
89  * @version
90  *
91  * @see org.xml.sax.EntityResolver
92  */

93 public abstract class XMLCatalogHandler implements EntityResolver JavaDoc {
94
95     /**
96      * Loads the catalog stream specified by the given input source and
97      * appends the contents to the catalog.
98      *
99      * @param source The catalog source.
100      *
101      * @exception java.lang.Exception Throws an exception if an error
102      * occurs while loading the catalog source.
103      */

104     public abstract void loadCatalog(InputSource JavaDoc source) throws Exception JavaDoc;
105
106
107     //
108
// Data
109
//
110

111     /** Public identifier mappings. */
112     private Hashtable JavaDoc publicMap = new Hashtable JavaDoc();
113
114     /** System identifier mappings (aliases). */
115     private Hashtable JavaDoc systemMap = new Hashtable JavaDoc();
116
117     //
118
// Public methods
119
//
120

121     /**
122      * Adds a public to system identifier mapping.
123      *
124      * @param publicId The public identifier, or "key".
125      * @param systemId The system identifier, or "value".
126      */

127     public void addPublicMapping(String JavaDoc publicId, String JavaDoc systemId) {
128         publicMap.put(publicId, systemId);
129     }
130
131     /**
132      * Removes a public identifier mapping.
133      *
134      * @param publicId The public identifier to remove.
135      */

136     public void removePublicMapping(System JavaDoc publicId) {
137         publicMap.remove(publicId);
138     }
139
140     /** Returns an enumeration of public identifier mapping keys. */
141     public Enumeration JavaDoc getPublicMappingKeys() {
142         return publicMap.keys();
143     }
144
145     /**
146      * Returns a public identifier mapping.
147      *
148      * @param publicId The public identifier, or "key".
149      *
150      * @return Returns the system identifier value or null if there
151      * is no mapping defined.
152      */

153     public String JavaDoc getPublicMapping(String JavaDoc publicId) {
154         return (String JavaDoc)publicMap.get(publicId);
155     }
156
157     /**
158      * Adds a system identifier alias.
159      *
160      * @param publicId The system identifier "key".
161      * @param systemId The system identifier "value".
162      */

163     public void addSystemMapping(String JavaDoc systemId1, String JavaDoc systemId2) {
164         systemMap.put(systemId1, systemId2);
165     }
166
167     /**
168      * Removes a system identifier alias.
169      *
170      * @param systemId The system identifier to remove.
171      */

172     public void removeSystemMapping(String JavaDoc systemId) {
173         systemMap.remove(systemId);
174     }
175
176     /** Returns an enumeration of system identifier mapping keys. */
177     public Enumeration JavaDoc getSystemMappingKeys() {
178         return systemMap.keys();
179     }
180
181     /**
182      * Returns a system identifier alias.
183      *
184      * @param systemId The system identifier "key".
185      *
186      * @return Returns the system identifier alias value or null if there
187      * is no alias defined.
188      */

189     public String JavaDoc getSystemMapping(String JavaDoc systemId) {
190         return (String JavaDoc)systemMap.get(systemId);
191     }
192
193     /**
194      * Resolves external entities.
195      *
196      * @param publicId The public identifier used for entity resolution.
197      * @param systemId If the publicId is not null, this systemId is
198      * to be considered the default system identifier;
199      * else a system identifier alias mapping is
200      * requested.
201      *
202      * @return Returns the input source of the resolved entity or null
203      * if no resolution is possible.
204      *
205      * @exception org.xml.sax.SAXException Exception thrown on SAX error.
206      * @exception java.io.IOException Exception thrown on i/o error.
207      */

208     public InputSource JavaDoc resolveEntity(String JavaDoc publicId, String JavaDoc systemId) throws SAXException JavaDoc, IOException JavaDoc
209     {
210         
211         // public id -> system id
212
if (publicId != null) {
213             String JavaDoc value = getPublicMapping(publicId);
214             if (value != null) {
215                 return new InputSource JavaDoc(value);
216             }
217         }
218
219         // system id(1) -> system id(2)
220
if (systemId != null) {
221             String JavaDoc value = getSystemMapping(systemId);
222             if (value == null) {
223                 value = systemId;
224             }
225
226             return new InputSource JavaDoc(value);
227         }
228
229         return null;
230
231     }
232 }
233
Popular Tags