腳本引擎在上下文或環(huán)境中執(zhí)行腳本。
ScriptContext
接口的實(shí)例表示腳本的上下文。
SimpleScriptContext
類是 ScriptContext
接口的實(shí)現(xiàn)。
腳本上下文由四個(gè)組件組成:
以下代碼使用SimpleScriptContext類創(chuàng)建ScriptContext。
SimpleScriptContext類的一個(gè)實(shí)例維護(hù)Bindings的兩個(gè)實(shí)例:一個(gè)用于引擎范圍,一個(gè)用于全局范圍。
默認(rèn)情況下,SimpleScriptContext類分別將上下文的輸入閱讀器,輸出寫入程序和錯(cuò)誤寫入程序初始化為 System.in
, System.out
和 System.err
。
ScriptContext接口的getReader(),getWriter()和getErrorWriter()方法分別返回來(lái)自ScriptContext的讀者,寫者和錯(cuò)誤寫入者的引用。
Setter方法可以設(shè)置讀寫器。
以下代碼顯示如何獲取讀寫器。它還顯示如何設(shè)置寫入器到FileWriter以將腳本輸出寫入文件:
import java.io.FileWriter; import java.io.Reader; import java.io.Writer; import javax.script.ScriptContext; import javax.script.SimpleScriptContext; public class Main { public static void main(String[] args) throws Exception{ ScriptContext ctx = new SimpleScriptContext(); // Get the reader and writers from the script context Reader inputReader = ctx.getReader(); Writer outputWriter = ctx.getWriter(); Writer errWriter = ctx.getErrorWriter(); // Write all script outputs to an out.txt file Writer fileWriter = new FileWriter("out.txt"); ctx.setWriter(fileWriter); } }
創(chuàng)建 SimpleScriptContext
后,您可以開始在引擎范圍綁定中存儲(chǔ)鍵值對(duì)。
setAttribute()
方法用于向Bindings添加鍵值對(duì)。
以下代碼顯示如何添加三個(gè)鍵值對(duì)。
import javax.script.ScriptContext; import javax.script.SimpleScriptContext; public class Main { public static void main(String[] args) throws Exception{ ScriptContext ctx = new SimpleScriptContext(); ctx.setAttribute("year", 2015, ScriptContext.ENGINE_SCOPE); ctx.setAttribute("month", 9, ScriptContext.ENGINE_SCOPE); ctx.setAttribute("day", 21, ScriptContext.ENGINE_SCOPE); } }
要將鍵值對(duì)添加到全局范圍中的綁定,請(qǐng)首先創(chuàng)建和設(shè)置綁定。
import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.SimpleBindings; import javax.script.SimpleScriptContext; public class Main { public static void main(String[] args) throws Exception { ScriptContext ctx = new SimpleScriptContext(); Bindings globalBindings = new SimpleBindings(); ctx.setBindings(globalBindings, ScriptContext.GLOBAL_SCOPE); ctx.setAttribute("year", 2015, ScriptContext.GLOBAL_SCOPE); ctx.setAttribute("name", "Java", ScriptContext.GLOBAL_SCOPE); } }
import static javax.script.ScriptContext.ENGINE_SCOPE; import static javax.script.ScriptContext.GLOBAL_SCOPE; import java.util.List; import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.SimpleBindings; import javax.script.SimpleScriptContext; public class Main { public static void main(String[] args) { ScriptContext ctx = new SimpleScriptContext(); List<Integer> scopes = ctx.getScopes(); System.out.println("Supported Scopes: " + scopes); ctx.setAttribute("year", 2015, ENGINE_SCOPE); Bindings globalBindings = new SimpleBindings(); ctx.setBindings(globalBindings, GLOBAL_SCOPE); ctx.setAttribute("year", 2014, GLOBAL_SCOPE); ctx.setAttribute("name", "Jack", GLOBAL_SCOPE); String nameValue = (String) ctx.getAttribute("name"); System.out.println("nameValue = " + nameValue); int engineScopeYear = (Integer) ctx.getAttribute("year", ENGINE_SCOPE); int globalScopeYear = (Integer) ctx.getAttribute("year", GLOBAL_SCOPE); System.out.println("engineScopeYear = " + engineScopeYear); System.out.println("globalScopeYear = " + globalScopeYear); } }
上面的代碼生成以下結(jié)果。
使用由同一ScriptEngineManager創(chuàng)建的引擎的全局和引擎范圍綁定。
import static javax.script.ScriptContext.ENGINE_SCOPE; import static javax.script.ScriptContext.GLOBAL_SCOPE; import javax.script.Bindings; import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import javax.script.SimpleScriptContext; public class Main { public static void main(String[] args) throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); manager.put("n1", 1); String script = "var sum = n1 + n2;print(msg + " + "" n1=" + n1 + ", n2=" + n2 + " + "", sum=" + sum);"; engine.put("n2", 2); engine.put("msg", "a string"); engine.eval(script); Bindings bindings = engine.createBindings(); bindings.put("n2", 3); bindings.put("msg", "another string"); engine.eval(script, bindings); ScriptContext ctx = new SimpleScriptContext(); Bindings ctxGlobalBindings = engine.createBindings(); ctx.setBindings(ctxGlobalBindings, GLOBAL_SCOPE); ctx.setAttribute("n1", 4, GLOBAL_SCOPE); ctx.setAttribute("n2", 5, ENGINE_SCOPE); ctx.setAttribute("msg", "ScriptContext:", ENGINE_SCOPE); engine.eval(script, ctx); engine.eval(script); } }
上面的代碼生成以下結(jié)果。
分別使用getContext()和setContext()方法獲取和設(shè)置ScriptEngine的默認(rèn)上下文,如下所示:
import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import javax.script.SimpleScriptContext; public class Main { public static void main(String[] args) throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); ScriptContext defaultCtx = engine.getContext(); // Work with defaultCtx here // Create a new context ScriptContext ctx = new SimpleScriptContext(); // Configure ctx here engine.setContext(ctx); } }
要在新的默認(rèn)上下文中使用ScriptEngineManager的綁定,請(qǐng)如下所示明確設(shè)置:
import javax.script.ScriptContext; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import javax.script.SimpleScriptContext; public class Main { public static void main(String[] args) throws ScriptException { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); ScriptContext defaultCtx = engine.getContext(); // Work with defaultCtx here // Create a new context ScriptContext ctx = new SimpleScriptContext(); // Configure ctx here // Set ctx as the new default context for the engine engine.setContext(ctx); ctx.setBindings(manager.getBindings(), ScriptContext.GLOBAL_SCOPE); engine.setContext(ctx); } }
更多建議: