| 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 | |
| 9 | import org.mozilla.javascript.Context; |
| 10 | import org.mozilla.javascript.Script; |
| 11 | import org.mozilla.javascript.Scriptable; |
| 12 | |
| 13 | /** |
| 14 | * A builder for {@link Require} instances. Useful when you're creating many |
| 15 | * instances of {@link Require} that are identical except for their top-level |
| 16 | * scope and current {@link Context}. Also useful if you prefer configuring it |
| 17 | * using named setters instead of passing many parameters in a constructor. |
| 18 | * Every setter returns "this", so you can easily chain their invocations for |
| 19 | * additional convenience. |
| 20 | * @author Attila Szegedi |
| 21 | * @version $Id: RequireBuilder.java,v 1.4 2011/04/07 20:26:11 hannes%helma.at Exp $ |
| 22 | */ |
| 23 | public class RequireBuilder implements Serializable |
| 24 | { |
| 25 | private static final long serialVersionUID = 1L; |
| 26 | |
| 27 | private boolean sandboxed = true; |
| 28 | private ModuleScriptProvider moduleScriptProvider; |
| 29 | private Script preExec; |
| 30 | private Script postExec; |
| 31 | |
| 32 | /** |
| 33 | * Sets the {@link ModuleScriptProvider} for the {@link Require} instances |
| 34 | * that this builder builds. |
| 35 | * @param moduleScriptProvider the module script provider for the |
| 36 | * {@link Require} instances that this builder builds. |
| 37 | * @return this, so you can chain ("fluidize") setter invocations |
| 38 | */ |
| 39 | public RequireBuilder setModuleScriptProvider( |
| 40 | ModuleScriptProvider moduleScriptProvider) |
| 41 | { |
| 42 | this.moduleScriptProvider = moduleScriptProvider; |
| 43 | return this; |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Sets the script that should execute in every module's scope after the |
| 48 | * module's own script has executed. |
| 49 | * @param postExec the post-exec script. |
| 50 | * @return this, so you can chain ("fluidize") setter invocations |
| 51 | */ |
| 52 | public RequireBuilder setPostExec(Script postExec) { |
| 53 | this.postExec = postExec; |
| 54 | return this; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Sets the script that should execute in every module's scope before the |
| 59 | * module's own script has executed. |
| 60 | * @param preExec the pre-exec script. |
| 61 | * @return this, so you can chain ("fluidize") setter invocations |
| 62 | */ |
| 63 | public RequireBuilder setPreExec(Script preExec) { |
| 64 | this.preExec = preExec; |
| 65 | return this; |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Sets whether the created require() instances will be sandboxed. |
| 70 | * See {@link Require#Require(Context, Scriptable, ModuleScriptProvider, |
| 71 | * Script, Script, boolean)} for explanation. |
| 72 | * @param sandboxed true if the created require() instances will be |
| 73 | * sandboxed. |
| 74 | * @return this, so you can chain ("fluidize") setter invocations |
| 75 | */ |
| 76 | public RequireBuilder setSandboxed(boolean sandboxed) { |
| 77 | this.sandboxed = sandboxed; |
| 78 | return this; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Creates a new require() function. You are still responsible for invoking |
| 83 | * either {@link Require#install(Scriptable)} or |
| 84 | * {@link Require#requireMain(Context, String)} to effectively make it |
| 85 | * available to its JavaScript program. |
| 86 | * @param cx the current context |
| 87 | * @param globalScope the global scope containing the JS standard natives. |
| 88 | * @return a new Require instance. |
| 89 | */ |
| 90 | public Require createRequire(Context cx, Scriptable globalScope) { |
| 91 | return new Require(cx, globalScope, moduleScriptProvider, preExec, |
| 92 | postExec, sandboxed); |
| 93 | } |
| 94 | } |