Debian Codegraph @

net/sf/saxon/lib/StandardModuleURIResolver.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.lib;
9
10 import net.sf.saxon.Configuration;
11 import net.sf.saxon.functions.ResolveURI;
12 import net.sf.saxon.trans.XPathException;
13
14 import javax.xml.transform.stream.StreamSource;
15 import java.io.BufferedInputStream;
16 import java.io.IOException;
17 import java.io.InputStream;
18 import java.io.InputStreamReader;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.net.URL;
22 import java.net.URLConnection;
23
24
25 /**
26 * This class is the standard ModuleURIResolver used to implement the "import module" declaration
27 * in a Query Prolog. It is used when no user-defined ModuleURIResolver has been specified, or when
28 * the user-defined ModuleURIResolver decides to delegate to the standard ModuleURIResolver.
29 * It relies on location hints being supplied in the "import module" declaration, and attempts
30 * to locate a module by dereferencing the URI given as the location hint. It accepts standard
31 * URIs recognized by the Java URL class, including the <code>jar</code> URI scheme; it also
32 * accepts <code>classpath</code> URIs as defined in the Spring framework.
33 *
34 * @author Michael H. Kay
35 */
36
37 public class StandardModuleURIResolver implements ModuleURIResolver {
38
39 Configuration config;
40
41 /**
42 * Create a StandardModuleURIResolver. Although the class is generally used as a singleton,
43 * a public constructor is provided so that the class can be named in configuration files and
44 * instantiated in the same way as user-written module URI resolvers.
45 */
46
47 public StandardModuleURIResolver() {
48
49 }
50
51
52 /**
53 * Create a StandardModuleURIResolver. Although the class is generally used as a singleton,
54 * a public constructor is provided so that the class can be named in configuration files and
55 * instantiated in the same way as user-written module URI resolvers.
56 */
57
58 public StandardModuleURIResolver(Configuration config) {
59 this.config = config;
60 }
61
62 /**
63 * Resolve a module URI and associated location hints.
64 *
65 * @param moduleURI The module namespace URI of the module to be imported; or null when
66 * loading a non-library module.
67 * @param baseURI The base URI of the module containing the "import module" declaration;
68 * null if no base URI is known
69 * @param locations The set of URIs specified in the "at" clause of "import module",
70 * which serve as location hints for the module
71 * @return an array of StreamSource objects each identifying the contents of a module to be
72 * imported. Each StreamSource must contain a
73 * non-null absolute System ID which will be used as the base URI of the imported module,
74 * and either an InputSource or a Reader representing the text of the module.
75 * @throws XPathException (error XQST0059) if the module cannot be located
76 */
77
78 public StreamSource[] resolve(String moduleURI, String baseURI, String[] locations) throws XPathException {
79 if (locations.length == 0) {
80 XPathException err = new XPathException("Cannot locate module for namespace " + moduleURI);
81 err.setErrorCode("XQST0059");
82 err.setIsStaticError(true);
83 throw err;
84 } else {
85 // One or more locations given: import modules from all these locations
86 StreamSource[] sources = new StreamSource[locations.length];
87 for (int m = 0; m < locations.length; m++) {
88 String href = locations[m];
89 URI absoluteURI;
90 try {
91 absoluteURI = ResolveURI.makeAbsolute(href, baseURI);
92 } catch (URISyntaxException err) {
93 XPathException se = new XPathException("Cannot resolve relative URI " + href, err);
94 se.setErrorCode("XQST0059");
95 se.setIsStaticError(true);
96 throw se;
97 }
98 sources[m] = getQuerySource(absoluteURI);
99 }
100 return sources;
101 }
102 }
103
104 /**
105 * Get a StreamSource object representing the source of a query, given its URI.
106 * This method attempts to discover the encoding by reading any HTTP headers.
107 * If the encoding can be determined, it returns a StreamSource containing a Reader that
108 * performs the required decoding. Otherwise, it returns a StreamSource containing an
109 * InputSource, leaving the caller to sort out encoding problems.
110 *
111 * @param absoluteURI the absolute URI of the source query
112 * @return a StreamSource containing a Reader or InputSource, as well as a systemID representing
113 * the base URI of the query.
114 * @throws XPathException if the URIs are invalid or cannot be resolved or dereferenced, or
115 * if any I/O error occurs
116 */
117
118 /*@NotNull*/
119 protected StreamSource getQuerySource(URI absoluteURI)
120 throws XPathException {
121
122 String encoding = null;
123 try {
124 InputStream is;
125 if ("classpath".equals(absoluteURI.getScheme())) {
126 String path = absoluteURI.getPath();
127 is = config.getDynamicLoader().getResourceAsStream(path);
128 if (is == null) {
129 XPathException se = new XPathException("Cannot locate module " + path + " on class path");
130 se.setErrorCode("XQST0059");
131 se.setIsStaticError(true);
132 throw se;
133 }
134 } else {
135 URL absoluteURL = absoluteURI.toURL();
136 URLConnection connection = absoluteURL.openConnection();
137 connection.connect();
138 is = connection.getInputStream();
139
140 // Get any external (HTTP) encoding label.
141 String contentType;
142
143 // The file:// URL scheme gives no useful information...
144 if (!"file".equals(connection.getURL().getProtocol())) {
145
146 // Use the contentType from the HTTP header if available
147 contentType = connection.getContentType();
148
149 if (contentType != null) {
150 int pos = contentType.indexOf("charset");
151 if (pos >= 0) {
152 pos = contentType.indexOf('=', pos + 7);
153 if (pos >= 0) {
154 contentType = contentType.substring(pos + 1);
155 }
156 if ((pos = contentType.indexOf(';')) > 0) {
157 contentType = contentType.substring(0, pos);
158 }
159
160 // attributes can have comment fields (RFC 822)
161 if ((pos = contentType.indexOf('(')) > 0) {
162 contentType = contentType.substring(0, pos);
163 }
164 // ... and values may be quoted
165 if ((pos = contentType.indexOf('"')) > 0) {
166 contentType = contentType.substring(pos + 1,
167 contentType.indexOf('"', pos + 2));
168 }
169 encoding = contentType.trim();
170 }
171 }
172 }
173 }
174
175 if (!is.markSupported()) {
176 is = new BufferedInputStream(is);
177 }
178
179
180 StreamSource ss = new StreamSource();
181 if (encoding == null) {
182 ss.setInputStream(is);
183 } else {
184 ss.setReader(new InputStreamReader(is, encoding));
185 }
186 ss.setSystemId(absoluteURI.toString());
187 return ss;
188 } catch (IOException err) {
189 XPathException se = new XPathException(err);
190 se.setErrorCode("XQST0059");
191 se.setIsStaticError(true);
192 throw se;
193 }
194
195 }
196 }
197