KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > util > xml > catalog > helpers > BootstrapResolver


1 // BootstrapResolver.java - Resolve entities and URIs internally
2

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

56
57 package org.jboss.util.xml.catalog.helpers;
58
59 import java.util.Hashtable JavaDoc;
60 import java.net.URL JavaDoc;
61 import java.net.MalformedURLException JavaDoc;
62 import java.io.InputStream JavaDoc;
63
64 import javax.xml.transform.URIResolver JavaDoc;
65 import javax.xml.transform.Source JavaDoc;
66 import javax.xml.transform.sax.SAXSource JavaDoc;
67 import javax.xml.transform.TransformerException JavaDoc;
68
69 import org.xml.sax.EntityResolver JavaDoc;
70 import org.xml.sax.InputSource JavaDoc;
71
72 /**
73  * A simple bootstrapping resolver.
74  *
75  * <p>This class is used as the entity resolver when reading XML Catalogs.
76  * It searches for the OASIS XML Catalog DTD, Relax NG Grammar and W3C XML Schema
77  * as resources (e.g., in the resolver jar file).</p>
78  *
79  * <p>If you have your own DTDs or schemas, you can extend this class and
80  * set the BootstrapResolver in your CatalogManager.</p>
81  *
82  * @see org.jboss.util.xml.catalog.CatalogManager
83  *
84  * @author Norman Walsh
85  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
86  *
87  * @version 1.0
88  */

89 public class BootstrapResolver implements EntityResolver JavaDoc, URIResolver JavaDoc {
90   /** URI of the W3C XML Schema for OASIS XML Catalog files. */
91   public static final String JavaDoc xmlCatalogXSD = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.xsd";
92
93   /** URI of the RELAX NG Grammar for OASIS XML Catalog files. */
94   public static final String JavaDoc xmlCatalogRNG = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.rng";
95
96   /** Public identifier for OASIS XML Catalog files. */
97   public static final String JavaDoc xmlCatalogPubId = "-//OASIS//DTD XML Catalogs V1.0//EN";
98
99   /** System identifier for OASIS XML Catalog files. */
100   public static final String JavaDoc xmlCatalogSysId = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd";
101
102   /** Private hash used for public identifiers. */
103   private Hashtable JavaDoc publicMap = new Hashtable JavaDoc();
104
105   /** Private hash used for system identifiers. */
106   private Hashtable JavaDoc systemMap = new Hashtable JavaDoc();
107
108   /** Private hash used for URIs. */
109   private Hashtable JavaDoc uriMap = new Hashtable JavaDoc();
110
111   /** Constructor. */
112   public BootstrapResolver() {
113     URL JavaDoc url = this.getClass().getResource("/org/apache/xml/resolver/etc/catalog.dtd");
114     if (url != null) {
115       publicMap.put(xmlCatalogPubId, url.toString());
116       systemMap.put(xmlCatalogSysId, url.toString());
117     }
118
119     url = this.getClass().getResource("/org/apache/xml/resolver/etc/catalog.rng");
120     if (url != null) {
121       uriMap.put(xmlCatalogRNG, url.toString());
122     }
123
124     url = this.getClass().getResource("/org/apache/xml/resolver/etc/catalog.xsd");
125     if (url != null) {
126       uriMap.put(xmlCatalogXSD, url.toString());
127     }
128   }
129
130   /** SAX resolveEntity API. */
131   public InputSource JavaDoc resolveEntity (String JavaDoc publicId, String JavaDoc systemId) {
132     String JavaDoc resolved = null;
133
134     if (systemId != null && systemMap.containsKey(systemId)) {
135       resolved = (String JavaDoc) systemMap.get(systemId);
136     } else if (publicId != null && publicMap.containsKey(publicId)) {
137       resolved = (String JavaDoc) publicMap.get(publicId);
138     }
139
140     if (resolved != null) {
141       try {
142     InputSource JavaDoc iSource = new InputSource JavaDoc(resolved);
143     iSource.setPublicId(publicId);
144
145     // Ideally this method would not attempt to open the
146
// InputStream, but there is a bug (in Xerces, at least)
147
// that causes the parser to mistakenly open the wrong
148
// system identifier if the returned InputSource does
149
// not have a byteStream.
150
//
151
// It could be argued that we still shouldn't do this here,
152
// but since the purpose of calling the entityResolver is
153
// almost certainly to open the input stream, it seems to
154
// do little harm.
155
//
156
URL JavaDoc url = new URL JavaDoc(resolved);
157     InputStream JavaDoc iStream = url.openStream();
158     iSource.setByteStream(iStream);
159
160     return iSource;
161       } catch (Exception JavaDoc e) {
162     // FIXME: silently fail?
163
return null;
164       }
165     }
166
167     return null;
168   }
169
170   /** Transformer resolve API. */
171   public Source JavaDoc resolve(String JavaDoc href, String JavaDoc base)
172     throws TransformerException JavaDoc {
173
174     String JavaDoc uri = href;
175     String JavaDoc fragment = null;
176     int hashPos = href.indexOf("#");
177     if (hashPos >= 0) {
178       uri = href.substring(0, hashPos);
179       fragment = href.substring(hashPos+1);
180     }
181
182     String JavaDoc result = null;
183     if (href != null && uriMap.containsKey(href)) {
184       result = (String JavaDoc) uriMap.get(href);
185     }
186
187     if (result == null) {
188       try {
189     URL JavaDoc url = null;
190
191     if (base==null) {
192       url = new URL JavaDoc(uri);
193       result = url.toString();
194     } else {
195       URL JavaDoc baseURL = new URL JavaDoc(base);
196       url = (href.length()==0 ? baseURL : new URL JavaDoc(baseURL, uri));
197       result = url.toString();
198     }
199       } catch (java.net.MalformedURLException JavaDoc mue) {
200     // try to make an absolute URI from the current base
201
String JavaDoc absBase = makeAbsolute(base);
202     if (!absBase.equals(base)) {
203       // don't bother if the absBase isn't different!
204
return resolve(href, absBase);
205     } else {
206       throw new TransformerException JavaDoc("Malformed URL "
207                      + href + "(base " + base + ")",
208                      mue);
209     }
210       }
211     }
212
213     SAXSource JavaDoc source = new SAXSource JavaDoc();
214     source.setInputSource(new InputSource JavaDoc(result));
215     return source;
216   }
217
218   /** Attempt to construct an absolute URI */
219   private String JavaDoc makeAbsolute(String JavaDoc uri) {
220     if (uri == null) {
221       uri = "";
222     }
223
224     try {
225       URL JavaDoc url = new URL JavaDoc(uri);
226       return url.toString();
227     } catch (MalformedURLException JavaDoc mue) {
228       String JavaDoc dir = System.getProperty("user.dir");
229       String JavaDoc file = "";
230
231       if (dir.endsWith("/")) {
232     file = "file://" + dir + uri;
233       } else {
234     file = "file://" + dir + "/" + uri;
235       }
236
237       try {
238     URL JavaDoc fileURL = new URL JavaDoc(file);
239     return fileURL.toString();
240       } catch (MalformedURLException JavaDoc mue2) {
241     // bail
242
return uri;
243       }
244     }
245   }
246 }
247
Popular Tags