rhino/src/org/mozilla/javascript/commonjs/module/provider/ModuleSourceProvider.java
raw symbols SCIP| 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 | |
| 5 | package org.mozilla.javascript.commonjs.module.provider; |
| 6 | |
| 7 | import java.io.IOException; |
| 8 | import java.net.URI; |
| 9 | import java.net.URISyntaxException; |
| 10 | |
| 11 | import org.mozilla.javascript.Scriptable; |
| 12 | |
| 13 | /** |
| 14 | * Implemented by objects that can provide the source text for the script. The |
| 15 | * design of the interface supports cache revalidation. |
| 16 | * @author Attila Szegedi |
| 17 | * @version $Id: ModuleSourceProvider.java,v 1.3 2011/04/07 20:26:12 hannes%helma.at Exp $ |
| 18 | */ |
| 19 | public interface ModuleSourceProvider |
| 20 | { |
| 21 | /** |
| 22 | * A special return value for {@link #loadSource(String, Scriptable, |
| 23 | * Object)} and {@link #loadSource(URI, URI, Object)} that signifies that the |
| 24 | * cached representation is still valid according to the passed validator. |
| 25 | */ |
| 26 | public static final ModuleSource NOT_MODIFIED = new ModuleSource(null, |
| 27 | null, null, null, null); |
| 28 | |
| 29 | /** |
| 30 | * Returns the script source of the requested module. More specifically, it |
| 31 | * resolves the module ID to a resource. If it can not resolve it, null is |
| 32 | * returned. If the caller passes a non-null validator, and the source |
| 33 | * provider recognizes it, and the validator applies to the same resource |
| 34 | * that the provider would use to load the source, and the validator |
| 35 | * validates the current cached representation of the resource (using |
| 36 | * whatever semantics for validation that this source provider implements), |
| 37 | * then {@link #NOT_MODIFIED} should be returned. Otherwise, it should |
| 38 | * return a {@link ModuleSource} object with the actual source text of the |
| 39 | * module, preferrably a validator for it, and a security domain, where |
| 40 | * applicable. |
| 41 | * @param moduleId the ID of the module. An implementation must only accept |
| 42 | * an absolute ID, starting with a term. |
| 43 | * @param paths the value of the require() function's "paths" attribute. If |
| 44 | * the require() function is sandboxed, it will be null, otherwise it will |
| 45 | * be a JavaScript Array object. It is up to the provider implementation |
| 46 | * whether and how it wants to honor the contents of the array. |
| 47 | * @param validator a validator for an existing loaded and cached module. |
| 48 | * This will either be null, or an object that this source provider |
| 49 | * returned earlier as part of a {@link ModuleSource}. It can be used to |
| 50 | * validate the existing cached module and avoid reloading it. |
| 51 | * @return a script representing the code of the module. Null should be |
| 52 | * returned if the script is not found. {@link #NOT_MODIFIED} should be |
| 53 | * returned if the passed validator validates the current representation of |
| 54 | * the module (the currently cached module script). |
| 55 | * @throws IOException if there was an I/O problem reading the script |
| 56 | * @throws URISyntaxException if the final URI could not be constructed. |
| 57 | * @throws IllegalArgumentException if the module ID is syntactically not a |
| 58 | * valid absolute module identifier. |
| 59 | */ |
| 60 | public ModuleSource loadSource(String moduleId, Scriptable paths, Object validator) |
| 61 | throws IOException, URISyntaxException; |
| 62 | |
| 63 | /** |
| 64 | * Returns the script source of the requested module from the given URI. |
| 65 | * The URI is absolute but does not contain a file name extension such as |
| 66 | * ".js", which may be specific to the ModuleSourceProvider implementation. |
| 67 | * <p> |
| 68 | * If the resource is not found, null is returned. If the caller passes a |
| 69 | * non-null validator, and the source provider recognizes it, and the |
| 70 | * validator applies to the same resource that the provider would use to |
| 71 | * load the source, and the validator validates the current cached |
| 72 | * representation of the resource (using whatever semantics for validation |
| 73 | * that this source provider implements), then {@link #NOT_MODIFIED} |
| 74 | * should be returned. Otherwise, it should return a {@link ModuleSource} |
| 75 | * object with the actual source text of the module, preferrably a |
| 76 | * validator for it, and a security domain, where applicable. |
| 77 | * @param uri the absolute URI from which to load the module source, but |
| 78 | * without an extension such as ".js". |
| 79 | * @param baseUri the module path base URI from which <code>uri</code> |
| 80 | * was derived. |
| 81 | * @param validator a validator for an existing loaded and cached module. |
| 82 | * This will either be null, or an object that this source provider |
| 83 | * returned earlier as part of a {@link ModuleSource}. It can be used to |
| 84 | * validate the existing cached module and avoid reloading it. |
| 85 | * @return a script representing the code of the module. Null should be |
| 86 | * returned if the script is not found. {@link #NOT_MODIFIED} should be |
| 87 | * returned if the passed validator validates the current representation of |
| 88 | * the module (the currently cached module script). |
| 89 | * @throws IOException if there was an I/O problem reading the script |
| 90 | * @throws URISyntaxException if the final URI could not be constructed |
| 91 | */ |
| 92 | public ModuleSource loadSource(URI uri, URI baseUri, Object validator) |
| 93 | throws IOException, URISyntaxException; |
| 94 | } |