List of usage examples for javax.script ScriptEngine getContext
public ScriptContext getContext();
ScriptContext
of the ScriptEngine
whose Bindings, Reader and Writers are used for script executions when no ScriptContext
is specified. From source file:Main.java
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);/* w w w . j a v a 2 s. c o m*/ }
From source file:Main.java
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);/* w ww. ja v a2s .c o m*/ ctx.setBindings(manager.getBindings(), ScriptContext.GLOBAL_SCOPE); engine.setContext(ctx); }
From source file:Main.java
public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); engine.eval("var msg = 'Hello globals'"); engine.eval("print(this.msg);"); ScriptContext ctx = new SimpleScriptContext(); ScriptContext defaultCtx = engine.getContext(); Bindings engineBindings = defaultCtx.getBindings(ENGINE_SCOPE); ctx.setBindings(engineBindings, ENGINE_SCOPE); engine.eval("print(this.msg);", ctx); }
From source file:Main.java
public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); File outputFile = new File("jsoutput.txt"); System.out.println("Script output will be written to " + outputFile.getAbsolutePath()); FileWriter writer = new FileWriter(outputFile); ScriptContext defaultCtx = engine.getContext(); defaultCtx.setWriter(writer);// w w w. ja v a 2 s . c o m String script = "print('Hello custom output writer')"; engine.eval(script); writer.close(); }
From source file:org.siphon.common.js.JsEngineUtil.java
public static Object getGlobal(ScriptEngine engine) { ScriptContext context = engine.getContext(); ScriptObjectMirror binding = (ScriptObjectMirror) context.getBindings(ScriptContext.ENGINE_SCOPE); Field globalField = null;/*from www .j av a2 s .co m*/ try { globalField = ScriptObjectMirror.class.getDeclaredField("global"); globalField.setAccessible(true); Object global = globalField.get(binding); return global; } catch (NoSuchFieldException e) { } catch (SecurityException e) { } catch (IllegalArgumentException e) { } catch (IllegalAccessException e) { } return null; }
From source file:org.netbeans.jcode.core.util.FileUtil.java
public static String expandTemplate(String template, Map<String, Object> values) { StringWriter writer = new StringWriter(); ScriptEngine eng = getScriptEngine(); Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE); if (values != null) { bind.putAll(values);/*from ww w . ja va2 s . c o m*/ } bind.put(ENCODING_PROPERTY_NAME, Charset.defaultCharset().name()); eng.getContext().setWriter(writer); Reader is = new StringReader(template); try { eng.eval(is); } catch (ScriptException ex) { Exceptions.printStackTrace(ex); } return writer.toString(); }
From source file:org.netbeans.jcode.core.util.FileUtil.java
private static void expandTemplate(InputStream template, Map<String, Object> values, Charset targetEncoding, Writer w) throws IOException { // Charset sourceEnc = FileEncodingQuery.getEncoding(template); ScriptEngine eng = getScriptEngine(); Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE); bind.putAll(values);//from w w w . ja va 2 s .c om bind.put(ENCODING_PROPERTY_NAME, targetEncoding.name()); Reader is = null; try { eng.getContext().setWriter(w); is = new InputStreamReader(template); eng.eval(is); } catch (ScriptException ex) { throw new IOException(ex); } finally { if (is != null) { is.close(); } } }
From source file:io.github.jeddict.jcode.util.FileUtil.java
public static void expandTemplate(Reader reader, Writer writer, Map<String, Object> values, Charset targetEncoding) throws IOException { ScriptEngine eng = getScriptEngine(); Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE); bind.putAll(values);//from w w w . j a v a 2s . com bind.put(ENCODING_PROPERTY_NAME, targetEncoding.name()); try { eng.getContext().setWriter(writer); eng.eval(reader); } catch (ScriptException ex) { throw new IOException(ex); } finally { if (reader != null) { reader.close(); } } }
From source file:io.github.jeddict.jcode.util.FileUtil.java
public static String expandTemplate(String inputTemplatePath, Map<String, Object> values) { InputStream contentStream = loadResource(inputTemplatePath); StringWriter writer = new StringWriter(); ScriptEngine eng = getScriptEngine(); Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE); if (values != null) { bind.putAll(values);//from ww w . j a v a2 s . c om } bind.put(ENCODING_PROPERTY_NAME, Charset.defaultCharset().name()); eng.getContext().setWriter(writer); Reader is = new InputStreamReader(contentStream); try { eng.eval(is); } catch (ScriptException ex) { Exceptions.printStackTrace(ex); } return writer.toString(); }
From source file:io.github.jeddict.jcode.util.FileUtil.java
/** * In-memory template api/*from w ww .j av a 2s . c om*/ * * @param templateContent * @param values * @return */ public static String expandTemplateContent(String templateContent, Map<String, Object> values) { StringWriter writer = new StringWriter(); ScriptEngine eng = getScriptEngine(); Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE); if (values != null) { bind.putAll(values); } bind.put(ENCODING_PROPERTY_NAME, Charset.defaultCharset().name()); eng.getContext().setWriter(writer); Reader is = new StringReader(templateContent); try { eng.eval(is); } catch (ScriptException ex) { Exceptions.printStackTrace(ex); } return writer.toString(); }