@Override public void setErrorWriter(Writer writer) { context.setErrorWriter(writer); }
public void applyTo ( final ScriptContext scriptContext ) { scriptContext.setWriter ( new PrintWriter ( new OutputStreamWriter ( this.writerStream ) ) ); scriptContext.setErrorWriter ( this.errorPrintWriter ); }
@Override public void setStdErr( Writer writer ) { engine.getContext().setErrorWriter( writer ); }
public void applyTo ( final ScriptContext scriptContext ) { scriptContext.setWriter ( new PrintWriter ( new OutputStreamWriter ( this.writerStream ) ) ); scriptContext.setErrorWriter ( this.errorPrintWriter ); }
public InteractiveConsole(String kernel) { ScriptEngineManager manager = new ScriptEngineManager(); engine = manager.getEngineByName(kernel); if (engine == null) { System.out.println("Classpath is " + System.getProperty("java.class.path")); throw new RuntimeException("ScriptEngine '"+kernel+"' not found. Please check your classpath."); } engine.getContext().setWriter(stdoutWriter); engine.getContext().setErrorWriter(stderrWriter); }
public EmbeddedScriptEngine(String name, ScriptEngine engine) { this.name = name; this.engine = engine; this.ctx = new SimpleScriptContext(); ctx.setWriter(new StringWriter()); ctx.setErrorWriter(new StringWriter()); ctx.setReader(new StringReader("")); }
public void testErrorWriter() throws ScriptException { final StringWriter stdout = new StringWriter(); final StringWriter stderr = new StringWriter(); pythonEngine.getContext().setWriter(stdout); pythonEngine.getContext().setErrorWriter(stderr); final String testString1 = "It is a wonderful world."; final String testString2 = "Stuff happens!"; assertNull(pythonEngine.eval("import sys")); assertNull(pythonEngine.eval("sys.stdout.write('" + testString1 + "')")); assertNull(pythonEngine.eval("sys.stderr.write('" + testString2 + "')")); assertEquals(testString1, stdout.toString()); assertEquals(testString2, stderr.toString()); }
public void testEvalWithReader() throws ScriptException, FileNotFoundException { //Check that multiple evals don't cause an NPE. //See issue http://bugs.jython.org/issue1536 final ScriptEngineManager manager = new ScriptEngineManager(); final String engineType = "jython"; final ScriptEngine engine = manager.getEngineByName(engineType); final StringWriter stdout = new StringWriter(); final StringWriter stderr = new StringWriter(); engine.getContext().setWriter(stdout); engine.getContext().setErrorWriter(stderr); final Bindings bindings = new SimpleBindings(); bindings.put("firstLevelNodes", 10); bindings.put("secondLevelNodes", 5); engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE); final Reader dfsScript = new FileReader("tests/python/dfs.py"); for (int i = 1; i <= 10; i++) { engine.eval(dfsScript); assertEquals(61, engine.get("result")); } }
public InteractiveConsole(ScriptEngine engine) { this.engine = engine; this.engine.getContext().setWriter(stdoutWriter); this.engine.getContext().setErrorWriter(stderrWriter); }
public ScriptHandler(GVRContext gvrContext) { prompt = ""; mScriptEngine = gvrContext.getScriptManager().getEngine(GVRScriptManager.LANG_JAVASCRIPT); mScriptContext = mScriptEngine.getContext(); mWriter = new StringWriter(); mScriptContext.setWriter(mWriter); mScriptContext.setErrorWriter(mWriter); }
@Override public SpicyPlate spicyPlate( SpicyContext globalContext, String template ) { NullArgumentException.ensureNotNull( "Global context", globalContext ); NullArgumentException.ensureNotEmpty( "Template", template ); StringWriter errorWriter = new StringWriter(); try { ScriptContext scriptContext = new SimpleScriptContext(); scriptContext.setErrorWriter( errorWriter ); scriptContext.setAttribute( "__spicy_plates_raw_template", template, ScriptContext.ENGINE_SCOPE ); Object erb = ruby.eval( new InputStreamReader( getClass().getResourceAsStream( "template_reader.rb" ) ), scriptContext ); return new ERubySpicyPlate( ruby, globalContext, erb ); } catch ( ScriptException ex ) { throw new SpicyPlatesFailure( "Unable to create ERB object from template content", ex ).andLogError( "Ruby error output was:\n" + Strings.indent( errorWriter.toString(), 1, " ", "| " ) ); } }
/** * initialize this instance. creates a javascript engine and loads the * javascript files. Instances of this class are not thread-safe. * * @param loader * @param sling */ public void initialize(ScriptCollectionLoader loader, Object sling) { ScriptEngineManager scriptEngineManager = new ScriptEngineManager(null); engine = scriptEngineManager.getEngineByName("nashorn"); engine.getContext().setErrorWriter(new Print()); engine.getContext().setWriter(new Print()); engine.put("console", new Console()); engine.put("Sling", sling); this.loader = loader; scriptChecksums = new HashMap<>(); updateJavascriptLibrary(); }
@Override public void render( SpicyContext context, Writer output ) { StringWriter errorWriter = new StringWriter(); try { ScriptContext scriptContext = new SimpleScriptContext(); scriptContext.setWriter( output ); scriptContext.setErrorWriter( errorWriter ); for ( Map.Entry<String, Object> eachGlobalAttribute : globalContext.entrySet() ) { // We use ScriptContext.ENGINE_SCOPE on purpose here, it seems JRuby do not support ScriptContext.GLOBAL_SCOPE. // It seems http://jira.codehaus.org/browse/JRUBY-4944 strikes again in the used version. // Next loop will override existing entries anyway. scriptContext.setAttribute( eachGlobalAttribute.getKey(), eachGlobalAttribute.getValue(), ScriptContext.ENGINE_SCOPE ); } for ( Map.Entry<String, Object> eachLocalAttribute : context.entrySet() ) { scriptContext.setAttribute( eachLocalAttribute.getKey(), eachLocalAttribute.getValue(), ScriptContext.ENGINE_SCOPE ); } scriptContext.setAttribute( "__spicy_plates_erb", erb, ScriptContext.ENGINE_SCOPE ); ruby.eval( new InputStreamReader( getClass().getResourceAsStream( "template_renderer.rb" ) ), scriptContext ); } catch ( ScriptException ex ) { throw new SpicyPlatesFailure( "Unable to render ERB template", ex ).andLogError( "Ruby error output was:\n" + Strings.indent( errorWriter.toString(), 1, " ", "| " ) ); } }
private ScriptEngine getJsEngine(Writer output) throws IOException, ScriptException { ScriptEngine ret = null; if (jsEngine != null) { ret = jsEngine.get(); } if (ret == null) { ret = new ScriptEngineManager().getEngineByName("javascript"); ScriptContext ctx = new SimpleScriptContext(); Bindings globalScope = ret.createBindings(); ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); initTv4(ret, globalScope); ret.setContext(ctx); jsEngine = new WeakReference<>(ret); } ret.getContext().setWriter(output); ret.getContext().setErrorWriter(output); return ret; }
private ScriptEngine getJsEngine(Writer output) throws IOException, ScriptException { ScriptEngine ret = null; if (jsEngine != null) { ret = jsEngine.get(); } if (ret == null) { ret = new ScriptEngineManager().getEngineByName("javascript"); ScriptContext ctx = new SimpleScriptContext(); Bindings globalScope = ret.createBindings(); ctx.setBindings(globalScope, ScriptContext.GLOBAL_SCOPE); initTv4(ret, globalScope); ret.setContext(ctx); jsEngine = new WeakReference<>(ret); } ret.getContext().setWriter(output); ret.getContext().setErrorWriter(output); return ret; }
protected ScriptContext getScriptContext(Bindings bindings) { if (bindings == null) { throw new NullPointerException("null bindings in engine scope"); } ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); Bindings global = getBindings(ScriptContext.GLOBAL_SCOPE); if (global != null) { newContext.setBindings(global, ScriptContext.GLOBAL_SCOPE); } newContext.setReader(context.getReader()); newContext.setWriter(context.getWriter()); newContext.setErrorWriter(context.getErrorWriter()); return newContext; }
protected ScriptContext getScriptContext(Bindings bindings) { if (bindings == null) { throw new NullPointerException("null bindings in engine scope"); } ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); Bindings global = getBindings(ScriptContext.GLOBAL_SCOPE); if (global != null) { newContext.setBindings(global, ScriptContext.GLOBAL_SCOPE); } newContext.setReader(context.getReader()); newContext.setWriter(context.getWriter()); newContext.setErrorWriter(context.getErrorWriter()); return newContext; }
protected ScriptContext getScriptContext(Bindings bindings) { if (bindings == null) { throw new NullPointerException("null bindings in engine scope"); } ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); Bindings global = getBindings(ScriptContext.GLOBAL_SCOPE); if (global != null) { newContext.setBindings(global, ScriptContext.GLOBAL_SCOPE); } newContext.setReader(context.getReader()); newContext.setWriter(context.getWriter()); newContext.setErrorWriter(context.getErrorWriter()); return newContext; }
protected ScriptContext getScriptContext(Bindings bindings) { if (bindings == null) { throw new NullPointerException("null bindings in engine scope"); } ScriptContext newContext = new SimpleScriptContext(); newContext.setBindings(bindings, ScriptContext.ENGINE_SCOPE); Bindings global = getBindings(ScriptContext.GLOBAL_SCOPE); if (global != null) { newContext.setBindings(global, ScriptContext.GLOBAL_SCOPE); } newContext.setReader(context.getReader()); newContext.setWriter(context.getWriter()); newContext.setErrorWriter(context.getErrorWriter()); return newContext; }
protected ScriptContext getScriptContext(Bindings bindings) { ScriptContext result = new SimpleScriptContext(); if (bindings != null) { result.setBindings(bindings, ScriptContext.ENGINE_SCOPE); } if (context.getBindings(ScriptContext.GLOBAL_SCOPE) != null) { result.setBindings(context.getBindings(ScriptContext.GLOBAL_SCOPE), ScriptContext.GLOBAL_SCOPE); } result.setReader(context.getReader()); result.setWriter(context.getWriter()); result.setErrorWriter(context.getErrorWriter()); return result; } }