KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > org > apache > xml > internal > resolver > helpers > BootstrapResolver


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

3 /*
4  * Copyright 2001-2004 The Apache Software Foundation or its licensors,
5  * as applicable.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */

19
20 package com.sun.org.apache.xml.internal.resolver.helpers;
21
22 import java.util.Hashtable JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import java.io.InputStream JavaDoc;
26
27 import javax.xml.transform.URIResolver JavaDoc;
28 import javax.xml.transform.Source JavaDoc;
29 import javax.xml.transform.sax.SAXSource JavaDoc;
30 import javax.xml.transform.TransformerException JavaDoc;
31
32 import org.xml.sax.EntityResolver JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34
35 /**
36  * A simple bootstrapping resolver.
37  *
38  * <p>This class is used as the entity resolver when reading XML Catalogs.
39  * It searches for the OASIS XML Catalog DTD, Relax NG Grammar and W3C XML Schema
40  * as resources (e.g., in the resolver jar file).</p>
41  *
42  * <p>If you have your own DTDs or schemas, you can extend this class and
43  * set the BootstrapResolver in your CatalogManager.</p>
44  *
45  * @see com.sun.org.apache.xml.internal.resolver.CatalogManager
46  *
47  * @author Norman Walsh
48  * <a HREF="mailto:Norman.Walsh@Sun.COM">Norman.Walsh@Sun.COM</a>
49  *
50  * @version 1.0
51  */

52 public class BootstrapResolver implements EntityResolver JavaDoc, URIResolver JavaDoc {
53   /** URI of the W3C XML Schema for OASIS XML Catalog files. */
54   public static final String JavaDoc xmlCatalogXSD = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.xsd";
55
56   /** URI of the RELAX NG Grammar for OASIS XML Catalog files. */
57   public static final String JavaDoc xmlCatalogRNG = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.rng";
58
59   /** Public identifier for OASIS XML Catalog files. */
60   public static final String JavaDoc xmlCatalogPubId = "-//OASIS//DTD XML Catalogs V1.0//EN";
61
62   /** System identifier for OASIS XML Catalog files. */
63   public static final String JavaDoc xmlCatalogSysId = "http://www.oasis-open.org/committees/entity/release/1.0/catalog.dtd";
64
65   /** Private hash used for public identifiers. */
66   private Hashtable JavaDoc publicMap = new Hashtable JavaDoc();
67
68   /** Private hash used for system identifiers. */
69   private Hashtable JavaDoc systemMap = new Hashtable JavaDoc();
70
71   /** Private hash used for URIs. */
72   private Hashtable JavaDoc uriMap = new Hashtable JavaDoc();
73
74   /** Constructor. */
75   public BootstrapResolver() {
76     URL JavaDoc url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.dtd");
77     if (url != null) {
78       publicMap.put(xmlCatalogPubId, url.toString());
79       systemMap.put(xmlCatalogSysId, url.toString());
80     }
81
82     url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.rng");
83     if (url != null) {
84       uriMap.put(xmlCatalogRNG, url.toString());
85     }
86
87     url = this.getClass().getResource("/com/sun/org/apache/xml/internal/resolver/etc/catalog.xsd");
88     if (url != null) {
89       uriMap.put(xmlCatalogXSD, url.toString());
90     }
91   }
92
93   /** SAX resolveEntity API. */
94   public InputSource JavaDoc resolveEntity (String JavaDoc publicId, String JavaDoc systemId) {
95     String JavaDoc resolved = null;
96
97     if (systemId != null && systemMap.containsKey(systemId)) {
98       resolved = (String JavaDoc) systemMap.get(systemId);
99     } else if (publicId != null && publicMap.containsKey(publicId)) {
100       resolved = (String JavaDoc) publicMap.get(publicId);
101     }
102
103     if (resolved != null) {
104       try {
105     InputSource JavaDoc iSource = new InputSource JavaDoc(resolved);
106     iSource.setPublicId(publicId);
107
108     // Ideally this method would not attempt to open the
109
// InputStream, but there is a bug (in Xerces, at least)
110
// that causes the parser to mistakenly open the wrong
111
// system identifier if the returned InputSource does
112
// not have a byteStream.
113
//
114
// It could be argued that we still shouldn't do this here,
115
// but since the purpose of calling the entityResolver is
116
// almost certainly to open the input stream, it seems to
117
// do little harm.
118
//
119
URL JavaDoc url = new URL JavaDoc(resolved);
120     InputStream JavaDoc iStream = url.openStream();
121     iSource.setByteStream(iStream);
122
123     return iSource;
124       } catch (Exception JavaDoc e) {
125     // FIXME: silently fail?
126
return null;
127       }
128     }
129
130     return null;
131   }
132
133   /** Transformer resolve API. */
134   public Source JavaDoc resolve(String JavaDoc href, String JavaDoc base)
135     throws TransformerException JavaDoc {
136
137     String JavaDoc uri = href;
138     String JavaDoc fragment = null;
139     int hashPos = href.indexOf("#");
140     if (hashPos >= 0) {
141       uri = href.substring(0, hashPos);
142       fragment = href.substring(hashPos+1);
143     }
144
145     String JavaDoc result = null;
146     if (href != null && uriMap.containsKey(href)) {
147       result = (String JavaDoc) uriMap.get(href);
148     }
149
150     if (result == null) {
151       try {
152     URL JavaDoc url = null;
153
154     if (base==null) {
155       url = new URL JavaDoc(uri);
156       result = url.toString();
157     } else {
158       URL JavaDoc baseURL = new URL JavaDoc(base);
159       url = (href.length()==0 ? baseURL : new URL JavaDoc(baseURL, uri));
160       result = url.toString();
161     }
162       } catch (java.net.MalformedURLException JavaDoc mue) {
163     // try to make an absolute URI from the current base
164
String JavaDoc absBase = makeAbsolute(base);
165     if (!absBase.equals(base)) {
166       // don't bother if the absBase isn't different!
167
return resolve(href, absBase);
168     } else {
169       throw new TransformerException JavaDoc("Malformed URL "
170                      + href + "(base " + base + ")",
171                      mue);
172     }
173       }
174     }
175
176     SAXSource JavaDoc source = new SAXSource JavaDoc();
177     source.setInputSource(new InputSource JavaDoc(result));
178     return source;
179   }
180
181   /** Attempt to construct an absolute URI */
182   private String JavaDoc makeAbsolute(String JavaDoc uri) {
183     if (uri == null) {
184       uri = "";
185     }
186
187     try {
188       URL JavaDoc url = new URL JavaDoc(uri);
189       return url.toString();
190     } catch (MalformedURLException JavaDoc mue) {
191       try {
192     URL JavaDoc fileURL = FileURL.makeURL(uri);
193     return fileURL.toString();
194       } catch (MalformedURLException JavaDoc mue2) {
195     // bail
196
return uri;
197       }
198     }
199   }
200 }
201
Popular Tags