| 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.trans.XPathException; |
| 11 | |
| 12 | import javax.xml.transform.stream.StreamSource; |
| 13 | |
| 14 | |
| 15 | /** |
| 16 | * A ModuleURIResolver is used when resolving references to |
| 17 | * query modules. It takes as input a URI that identifies the module to be loaded, and a set of |
| 18 | * location hints, and returns one or more StreamSource objects containing the queries |
| 19 | * to be imported. |
| 20 | * |
| 21 | * @author Michael H. Kay |
| 22 | */ |
| 23 | |
| 24 | public interface ModuleURIResolver { |
| 25 | |
| 26 | /** |
| 27 | * Locate a query module, or a set of query modules, given the identifying URI and |
| 28 | * a set of associated location hints. |
| 29 | * <p>The module URI resolver is generally invoked when locating library modules. When a |
| 30 | * module import declaration is encountered in the query source, Saxon first expands any |
| 31 | * relative URIs appearing in the location hints to absolute URIs by resolving against |
| 32 | * the base URI of the module. It then discards any absolutized location hints that have |
| 33 | * previously been used in processing other module import declarations. If there are no |
| 34 | * remaining location hints, and if a module with the requested module URI has already |
| 35 | * been loaded, then the module URI resolver is not called (it is assumed this is a duplicate |
| 36 | * request). In all other cases, the module URI resolver is called, passing the requested |
| 37 | * module namespace URI and the list of absolutized location hints excluding any that have |
| 38 | * been used in a previous request.</p> |
| 39 | * <p>The module URI resolver is also |
| 40 | * invoked when loading the main query module in cases where this is provided in the form |
| 41 | * of a URI: specifically, when running a query from the command line specifying the -u |
| 42 | * option, or with a supplied query file name that starts with "http:" or "file:". When |
| 43 | * locating the main query module, the moduleURI and baseURI parameters will be null, and |
| 44 | * the resolver must either return null (which delegates to the standard ModuleURIResolver), |
| 45 | * or must return a singleton StreamSource, that is, an array of length 1.</p> |
| 46 | * |
| 47 | * @param moduleURI the module URI of the module to be imported; or null when |
| 48 | * loading a non-library module. |
| 49 | * @param baseURI The base URI of the module containing the "import module" declaration; |
| 50 | * null if no base URI is known |
| 51 | * @param locations The set of URIs specified in the "at" clause of "import module", |
| 52 | * which serve as location hints for the module. The values supplied are absolute URIs |
| 53 | * formed by resolving the relative URI appearing in the query against the base URI of the query. |
| 54 | * @return an array of {@link StreamSource} objects each identifying the contents of a query module to be |
| 55 | * imported. Each StreamSource must contain a |
| 56 | * non-null absolute System ID which will be used as the base URI of the imported module, |
| 57 | * and either an {@link java.io.InputStream} or an {@link java.io.Reader} representing the text of the module. |
| 58 | * <p>The contained InputStream or Reader must be positioned at the start of the |
| 59 | * content to be read; it will be consumed by the system and will be closed after use.</p> |
| 60 | * <p>The method may alternatively return null, in which case the system attempts to resolve the URI using the |
| 61 | * standard module URI resolver. The standard module URI resolver attempts to dereference each one of the |
| 62 | * location hints to locate a query module. If there are no location hints, or if any location hint |
| 63 | * cannot be dereferenced, it reports a fatal error.</p> |
| 64 | * @throws XPathException if the module cannot be located, and if delegation to the default |
| 65 | * module resolver is not required. |
| 66 | */ |
| 67 | |
| 68 | StreamSource[] resolve(String moduleURI, String baseURI, String[] locations) throws XPathException; |
| 69 | |
| 70 | } |
| 71 |