KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > query > StandardModuleURIResolver


1 package net.sf.saxon.query;
2 import net.sf.saxon.StandardURIResolver;
3 import net.sf.saxon.trans.StaticError;
4 import net.sf.saxon.trans.XPathException;
5
6 import javax.xml.transform.stream.StreamSource JavaDoc;
7 import java.io.BufferedInputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.InputStream JavaDoc;
10 import java.io.InputStreamReader JavaDoc;
11 import java.net.URI JavaDoc;
12 import java.net.URISyntaxException JavaDoc;
13 import java.net.URL JavaDoc;
14 import java.net.URLConnection JavaDoc;
15
16
17 /**
18  * This class is the standard ModuleURIResolver used to implement the "import module" declaration
19  * in a Query Prolog. It is used when no user-defined ModuleURIResolver has been specified, or when
20  * the user-defined ModuleURIResolver decides to delegate to the standard ModuleURIResolver.
21  * @author Michael H. Kay
22 */

23
24 public class StandardModuleURIResolver implements ModuleURIResolver {
25
26     /**
27      * Resolve a module URI and associated location hints.
28      * @param moduleURI The module namespace URI of the module to be imported; or null when
29      * loading a non-library module.
30      * @param baseURI The base URI of the module containing the "import module" declaration;
31      * null if no base URI is known
32      * @param locations The set of URIs specified in the "at" clause of "import module",
33      * which serve as location hints for the module
34      * @return an array of StreamSource objects each identifying the contents of a module to be
35      * imported. Each StreamSource must contain a
36      * non-null absolute System ID which will be used as the base URI of the imported module,
37      * and either an InputSource or a Reader representing the text of the module. The method
38      * may also return null, in which case the system attempts to resolve the URI using the
39      * standard module URI resolver.
40      * @throws net.sf.saxon.trans.XPathException if the module cannot be located
41     */

42
43     public StreamSource JavaDoc[] resolve(String JavaDoc moduleURI, String JavaDoc baseURI, String JavaDoc[] locations) throws XPathException {
44         if (locations.length == 0) {
45             StaticError err = new StaticError("Cannot locate module for namespace " + moduleURI);
46             err.setErrorCode("XQST0059");
47             throw err;
48         } else {
49             // One or more locations given: import modules from all these locations
50
StreamSource JavaDoc[] sources = new StreamSource JavaDoc[locations.length];
51             for (int m=0; m<locations.length; m++) {
52                 String JavaDoc href = locations[m];
53                 URI JavaDoc absoluteURI;
54                 try {
55                     absoluteURI = StandardURIResolver.makeAbsolute(href, baseURI);
56                 } catch (URISyntaxException JavaDoc err) {
57                     StaticError se = new StaticError("Cannot resolve relative URI " + href, err);
58                     se.setErrorCode("XQST0059");
59                     throw se;
60                 }
61                 sources[m] = getQuerySource(absoluteURI);
62             }
63             return sources;
64         }
65     }
66
67     /**
68       * Get a StreamSource object representing the source of a query, given its URI.
69       * This method attempts to discover the encoding by reading any HTTP headers.
70       * If the encoding can be determined, it returns a StreamSource containing a Reader that
71       * performs the required decoding. Otherwise, it returns a StreamSource containing an
72       * InputSource, leaving the caller to sort out encoding problems.
73       * @param absoluteURI the absolute URI of the source query
74       * @return a StreamSource containing a Reader or InputSource, as well as a systemID representing
75       * the base URI of the query.
76       * @throws net.sf.saxon.trans.StaticError if the URIs are invalid or cannot be resolved or dereferenced, or
77       * if any I/O error occurs
78       */

79
80      public static StreamSource JavaDoc getQuerySource(URI JavaDoc absoluteURI)
81              throws StaticError {
82
83          try {
84              InputStream JavaDoc is;
85              URL JavaDoc absoluteURL = absoluteURI.toURL();
86              URLConnection JavaDoc connection = absoluteURL.openConnection();
87              connection.connect();
88              is = connection.getInputStream();
89
90              if (!is.markSupported()) {
91                  is = new BufferedInputStream JavaDoc(is);
92              }
93
94              // Get any external (HTTP) encoding label.
95
String JavaDoc contentType;
96              String JavaDoc encoding = null;
97
98              // The file:// URL scheme gives no useful information...
99
if (!"file".equals(connection.getURL().getProtocol())) {
100
101                  // Use the contentType from the HTTP header if available
102
contentType = connection.getContentType();
103
104                  if (contentType != null) {
105                      int pos = contentType.indexOf("charset");
106                      if (pos>=0) {
107                          pos = contentType.indexOf('=', pos + 7);
108                          if (pos>=0) {
109                              contentType = contentType.substring(pos + 1);
110                          }
111                          if ((pos = contentType.indexOf(';')) > 0) {
112                              contentType = contentType.substring(0, pos);
113                          }
114
115                          // attributes can have comment fields (RFC 822)
116
if ((pos = contentType.indexOf('(')) > 0) {
117                              contentType = contentType.substring(0, pos);
118                          }
119                          // ... and values may be quoted
120
if ((pos = contentType.indexOf('"')) > 0) {
121                              contentType = contentType.substring(pos + 1,
122                                      contentType.indexOf('"', pos + 2));
123                          }
124                          encoding = contentType.trim();
125                      }
126                  }
127              }
128              StreamSource JavaDoc ss = new StreamSource JavaDoc();
129              if (encoding == null) {
130                  ss.setInputStream(is);
131              } else {
132                  ss.setReader(new InputStreamReader JavaDoc(is, encoding));
133              }
134              ss.setSystemId(absoluteURL.toString());
135              return ss;
136          } catch (IOException JavaDoc err) {
137              StaticError se = new StaticError(err);
138              se.setErrorCode("XQST0059");
139              throw se;
140          }
141
142      }
143 }
144
145 //
146
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
147
// you may not use this file except in compliance with the License. You may obtain a copy of the
148
// License at http://www.mozilla.org/MPL/
149
//
150
// Software distributed under the License is distributed on an "AS IS" basis,
151
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
152
// See the License for the specific language governing rights and limitations under the License.
153
//
154
// The Original Code is: all this file.
155
//
156
// The Initial Developer of the Original Code is Michael H. Kay
157
//
158
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
159
//
160
// Contributor(s): none.
161
//
162
Popular Tags