List of usage examples for javax.script ScriptEngine eval
public Object eval(Reader reader) throws ScriptException;
eval(String)
except that the source of the script is provided as a Reader
From source file:org.eclairjs.nashorn.sql.JSUDF6.java
@SuppressWarnings({ "null", "unchecked" }) @Override// w w w . j a v a 2s.co m public Object call(Object o, Object o2, Object o3, Object o4, Object o5, Object o6) throws Exception { ScriptEngine e = NashornEngineSingleton.getEngine(); if (this.fn == null) { this.fn = e.eval(func); if (this.args != null && this.args.length > 0) { this.argsJS = new Object[this.args.length]; for (int i = 0; i < this.args.length; i++) this.argsJS[i] = Utils.javaToJs(this.args[i], e); } } Invocable invocable = (Invocable) e; Object params[] = { o, o2, o3, o4, o5, o6 }; if (this.args != null && this.args.length > 0) { params = ArrayUtils.addAll(params, this.args); } for (int i = 0; i < params.length; i++) params[i] = Utils.javaToJs(params[i], e); // Object ret = invocable.invokeFunction("myTestFunc", params); Object ret = ((ScriptObjectMirror) this.fn).call(null, params); // Object ret = invocable.invokeFunction("Utils_invoke", params); //ret = Utils.jsToJava(ret); ret = this.castValueToReturnType(ret); return ret; }
From source file:temp.groovy.java
@Test public void foo() { JexlEngine eng = new JexlEngine(); eng.setLenient(false);//from www. ja va 2s. c om eng.setSilent(false); Expression expr = eng.createExpression("a == '9' and a =~ ['7','9']"); System.out.println(expr.dump()); JexlContext ctx = new MapContext(); ctx.set("a", "9"); Boolean result = (Boolean) expr.evaluate(ctx); try { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("jython"); engine.put("first", 9); result = (Boolean) engine.eval("first not in [7,9]"); result = (Boolean) engine.eval("first in ['7','9']"); assertEquals(true, result); //This next example illustrates calling an invokable function: String fact = "def factorial(n) { n == 1 ? 1 : n * factorial(n - 1) }"; engine.eval(fact); Invocable inv = (Invocable) engine; Object[] params = { 5 }; Object oresult = inv.invokeFunction("factorial", params); assertEquals(120, oresult); } catch (ScriptException | NoSuchMethodException ex) { Logger.getLogger(groovy.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.siphon.common.js.JsEngineUtil.java
/** * /*from w w w . java 2 s. co m*/ * @param jsEngine * @param libs [path, path, ...] */ public static void initEngine(ScriptEngine jsEngine, Object[] libs) { try { jsEngine.put("engine", jsEngine); NashornScriptEngine nashornScriptEngine = (NashornScriptEngine) jsEngine; JsTypeUtil jsTypeUtil = new JsTypeUtil(jsEngine); ScriptObjectMirror importedFiles = jsTypeUtil.newObject(); jsEngine.put("IMPORTED_FILES", importedFiles); ScriptObjectMirror stk = jsTypeUtil.newArray(); jsEngine.put("IMPORTS_PATH_STACK", stk); ScriptObjectMirror defaults = (libs.length > 0 && libs[0] != null && libs[0] instanceof String && ((String) libs[0]).length() > 0) ? jsTypeUtil.newArray(libs) : jsTypeUtil.newArray(); jsEngine.put("DEFAULT_IMPORTS_PATHS", defaults); jsEngine.put(ScriptEngine.FILENAME, "common/engine.js"); jsEngine.eval(importsFn); } catch (ScriptException e) { e.printStackTrace(); } }
From source file:org.myperl.MathService.java
@POST @Path("/") @Produces(MediaType.APPLICATION_JSON)/* w ww .ja v a2s . c o m*/ @Consumes(MediaType.APPLICATION_JSON) public String post(String payload) throws ScriptException, ParseException { JSONParser jsonParser = new JSONParser(); JSONObject jsonObject = (JSONObject) jsonParser.parse(payload); String expression = (String) jsonObject.get("exper"); ScriptEngine engine = manager.getEngineByName("nashorn"); JSONObject result = new JSONObject(); result.put("result", engine.eval(expression).toString()); return result.toString(); }
From source file:com.asual.lesscss.compiler.NashornCompiler.java
public NashornCompiler(LessOptions options, ResourceLoader loader, URL less, URL env, URL engine, URL cssmin, URL sourceMap) throws IOException { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine scriptEngine = factory.getEngineByName("nashorn"); try {//from ww w .java 2 s .c o m scriptEngine.eval(new InputStreamReader(sourceMap.openConnection().getInputStream())); scriptEngine.eval(new InputStreamReader(env.openConnection().getInputStream())); ScriptObjectMirror lessenv = (ScriptObjectMirror) scriptEngine.get("lessenv"); lessenv.put("charset", options.getCharset()); lessenv.put("css", options.isCss()); lessenv.put("lineNumbers", options.getLineNumbers()); lessenv.put("optimization", options.getOptimization()); lessenv.put("sourceMap", options.isSourceMap()); lessenv.put("sourceMapRootpath", options.getSourceMapRootpath()); lessenv.put("sourceMapBasepath", options.getSourceMapBasepath()); lessenv.put("sourceMapURL", options.getSourceMapUrl()); lessenv.put("loader", loader); lessenv.put("paths", options.getPaths()); scriptEngine.eval(new InputStreamReader(less.openConnection().getInputStream())); scriptEngine.eval(new InputStreamReader(cssmin.openConnection().getInputStream())); scriptEngine.eval(new InputStreamReader(engine.openConnection().getInputStream())); compile = (ScriptObjectMirror) scriptEngine.get("compile"); } catch (ScriptException e) { logger.error(e.getMessage(), e); } }
From source file:org.eclairjs.nashorn.JSComparator.java
@SuppressWarnings({ "null", "unchecked" }) @Override/* w ww.ja v a 2 s .c o m*/ public boolean equals(Object o) { boolean ret = false; try { ScriptEngine e = NashornEngineSingleton.getEngine(); if (this.fn == null) { this.fn = e.eval(func); } Invocable invocable = (Invocable) e; Object params[] = { this.fn, o }; if (this.args != null && this.args.length > 0) { params = ArrayUtils.addAll(params, this.args); } ret = Boolean.valueOf(invocable.invokeFunction(this.functionName, params).toString()); } catch (Exception exc) { // do nothing for now } return ret; }
From source file:org.callimachusproject.fluid.consumers.HttpJavaScriptResponseWriter.java
public HttpJavaScriptResponseWriter() throws ScriptException { String systemId = getSystemId("SCRIPT"); ScriptEngineManager man = new ScriptEngineManager(); ScriptEngine engine = man.getEngineByName("rhino"); engine.put(ScriptEngine.FILENAME, systemId); engine.eval(SCRIPT); this.engine = (Invocable) engine; this.delegate = new HttpMessageWriter(); }
From source file:org.jumpmind.metl.core.runtime.component.ModelAttributeScriptHelper.java
public static Object eval(Message message, ComponentContext context, ModelAttribute attribute, Object value, ModelEntity entity, EntityData data, String expression) { ScriptEngine engine = scriptEngine.get(); if (engine == null) { ScriptEngineManager factory = new ScriptEngineManager(); engine = factory.getEngineByName("groovy"); scriptEngine.set(engine);/* www. ja va 2 s .c om*/ } engine.put("value", value); engine.put("data", data); engine.put("entity", entity); engine.put("attribute", attribute); engine.put("message", message); engine.put("context", context); try { String importString = "import org.jumpmind.metl.core.runtime.component.ModelAttributeScriptHelper;\n"; String code = String.format( "return new ModelAttributeScriptHelper(message, context, attribute, entity, data, value) { public Object eval() { return %s } }.eval()", expression); return engine.eval(importString + code); } catch (ScriptException e) { throw new RuntimeException("Unable to evaluate groovy script. Attribute ==> " + attribute.getName() + ". Value ==> " + value.toString() + "." + e.getCause().getMessage(), e); } }
From source file:net.landora.video.filerenaming.RenameScriptManager.java
RenamingScript createRenamingScript(String folderScript, String fileScript, boolean reportExceptions) { try {/*from w ww . j a va 2 s .c o m*/ StringBuilder str = new StringBuilder(); str.append(getClassScript("RenameScript.py")); str.append("\n"); str.append(createScript("findFolderName", folderScript)); str.append("\n"); str.append(createScript("findFilename", fileScript)); ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("jython"); engine.eval(str.toString()); Invocable inv = (Invocable) engine; return inv.getInterface(RenamingScript.class); } catch (Exception ex) { if (reportExceptions) { LoggerFactory.getLogger(getClass()).error("Error getting rename script.", ex); } return null; } }
From source file:org.eclairjs.nashorn.JSBaseFunction.java
@SuppressWarnings({ "null", "unchecked" }) protected Object callScript(Object params[]) throws Exception { ScriptEngine e = NashornEngineSingleton.getEngine(); if (this.fn == null) { this.fn = e.eval(func); if (this.args != null && this.args.length > 0) { this.argsJS = new Object[this.args.length]; for (int i = 0; i < this.args.length; i++) this.argsJS[i] = Utils.javaToJs(this.args[i], e); }/* w ww .j a va 2s . com*/ } Invocable invocable = (Invocable) e; if (this.args != null && this.args.length > 0) { params = ArrayUtils.addAll(params, this.args); } for (int i = 0; i < params.length; i++) params[i] = Utils.javaToJs(params[i], e); Object ret = ((ScriptObjectMirror) this.fn).call(null, params); ret = Utils.jsToJava(ret); return ret; }