| 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; |
| 6 | |
| 7 | import java.io.Serializable; |
| 8 | import java.net.URI; |
| 9 | |
| 10 | import org.mozilla.javascript.Script; |
| 11 | |
| 12 | /** |
| 13 | * Represents a compiled CommonJS module script. The {@link Require} functions |
| 14 | * use them and obtain them through a {@link ModuleScriptProvider}. Instances |
| 15 | * are immutable. |
| 16 | * @author Attila Szegedi |
| 17 | * @version $Id: ModuleScript.java,v 1.3 2011/04/07 20:26:11 hannes%helma.at Exp $ |
| 18 | */ |
| 19 | public class ModuleScript implements Serializable |
| 20 | { |
| 21 | private static final long serialVersionUID = 1L; |
| 22 | |
| 23 | private final Script script; |
| 24 | private final URI uri; |
| 25 | private final URI base; |
| 26 | |
| 27 | /** |
| 28 | * Creates a new CommonJS module. |
| 29 | * @param script the script representing the code of the module. |
| 30 | * @param uri the URI of the module. |
| 31 | * @param base the base URI, or null. |
| 32 | */ |
| 33 | public ModuleScript(Script script, URI uri, URI base) { |
| 34 | this.script = script; |
| 35 | this.uri = uri; |
| 36 | this.base = base; |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Returns the script object representing the code of the module. |
| 41 | * @return the script object representing the code of the module. |
| 42 | */ |
| 43 | public Script getScript(){ |
| 44 | return script; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * Returns the URI of the module. |
| 49 | * @return the URI of the module. |
| 50 | */ |
| 51 | public URI getUri() { |
| 52 | return uri; |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Returns the base URI from which this module source was loaded, or null |
| 57 | * if it was loaded from an absolute URI. |
| 58 | * @return the base URI, or null. |
| 59 | */ |
| 60 | public URI getBase() { |
| 61 | return base; |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Returns true if this script has a base URI and has a source URI that |
| 66 | * is contained within that base URI. |
| 67 | * @return true if this script is contained within its sandbox base URI. |
| 68 | */ |
| 69 | public boolean isSandboxed() { |
| 70 | return base != null |
| 71 | && uri != null |
| 72 | && !base.relativize(uri).isAbsolute(); |
| 73 | } |
| 74 | } |