| 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 javax.xml.transform.Source; |
| 11 | import javax.xml.transform.TransformerException; |
| 12 | import javax.xml.transform.URIResolver; |
| 13 | |
| 14 | /** |
| 15 | * The standard JAXP URIResolver is given a relative URI and a base URI and returns the resource |
| 16 | * identified by this combination. However, to support a stable implementation of the doc() function, |
| 17 | * Saxon needs to know what the absolute URI is before the resource is fetched, so it can determine whether |
| 18 | * a document with that absolute URI actually exists. |
| 19 | * <p>This extended interface defines a URIResolver that separates the two functions of resolving a relative URI |
| 20 | * against a base URI, and fetching a resource with that absolute URI. If the URI resolver supplied to Saxon |
| 21 | * implements this interface, the absolute URI associated with a loaded document will be the URI returned by |
| 22 | * this resolver.</p> |
| 23 | * <p>The particular motivation for providing this interface is to allow a URIResolver to wrap a .NET XmlResolver, |
| 24 | * which has additional capability not present in the JAXP interface.</p> |
| 25 | */ |
| 26 | |
| 27 | public interface RelativeURIResolver extends URIResolver { |
| 28 | |
| 29 | /** |
| 30 | * Create an absolute URI from a relative URI and a base URI. This method performs the |
| 31 | * process which is correctly called "URI resolution": this is purely a syntactic operation |
| 32 | * on the URI strings, and does not retrieve any resources. |
| 33 | * |
| 34 | * @param href A relative or absolute URI, to be resolved against the specified base URI |
| 35 | * @param base The base URI against which the first argument will be made |
| 36 | * absolute if the absolute URI is required. |
| 37 | * @return A string containing the absolute URI that results from URI resolution. If the resource |
| 38 | * needs to be fetched, this absolute URI will be supplied as the href parameter in a subsequent |
| 39 | * call to the <code>resolve</code> method. |
| 40 | * @throws javax.xml.transform.TransformerException |
| 41 | * if any failure occurs |
| 42 | */ |
| 43 | |
| 44 | String makeAbsolute(String href, String base) |
| 45 | throws TransformerException; |
| 46 | |
| 47 | /** |
| 48 | * Called by the processor when it encounters |
| 49 | * an xsl:include, xsl:import, or document() function. |
| 50 | * |
| 51 | * @param uri The absolute URI to be dereferenced |
| 52 | * @return A Source object, or null if the href cannot be dereferenced, |
| 53 | * and the processor should try to resolve the URI itself. |
| 54 | * @throws javax.xml.transform.TransformerException |
| 55 | * if an error occurs when trying to |
| 56 | * resolve the URI. |
| 57 | */ |
| 58 | Source dereference(String uri) |
| 59 | throws TransformerException; |
| 60 | |
| 61 | /** |
| 62 | * Called by the processor when it encounters |
| 63 | * an xsl:include, xsl:import, or document() function. |
| 64 | * <p>Despite the name, the main purpose of this method is to dereference the URI, not merely |
| 65 | * to resolve it.</p> |
| 66 | * <p>This method is provided because it is required by the interface. When using a RelativeURIResolver, |
| 67 | * the single-argument dereference() method is preferred. The result of calling this method should be the |
| 68 | * same as the result of calling <code>dereference(makeAbsolute(href, base))</code></p> |
| 69 | * |
| 70 | * @param href An href attribute, which may be relative or absolute. |
| 71 | * @param base The base URI against which the first argument will be made |
| 72 | * absolute if the absolute URI is required. |
| 73 | * @return A Source object, or null if the href cannot be resolved, |
| 74 | * and the processor should try to resolve the URI itself. |
| 75 | * @throws javax.xml.transform.TransformerException |
| 76 | * if an error occurs when trying to |
| 77 | * resolve the URI. |
| 78 | */ |
| 79 | Source resolve(String href, String base) |
| 80 | throws TransformerException; |
| 81 | |
| 82 | |
| 83 | } |
| 84 |