List of usage examples for javax.script ScriptEngineManager ScriptEngineManager
public ScriptEngineManager()
ScriptEngineManager(Thread.currentThread().getContextClassLoader())
. From source file:com.github.sebhoss.contract.verifier.JavaScriptConfiguration.java
/** * @return A new ScriptEngineManager.//w w w.j a v a 2s . co m */ @Bean @SuppressWarnings(CompilerWarnings.STATIC_METHOD) public ScriptEngineManager scriptManager() { return new ScriptEngineManager(); }
From source file:Engine.Lua.PlayerLua.java
public PlayerLua(String luaFile) { ScriptEngineManager sem = new ScriptEngineManager(); ScriptEngine scriptEngine = sem.getEngineByName("luaj"); ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream("Scripts/" + luaFile); InputStreamReader isr = new InputStreamReader(is); CompiledScript script;//from w ww .ja va 2 s .co m try { script = ((Compilable) scriptEngine).compile(isr); isr.close(); is.close(); sb = new SimpleBindings(); script.eval(sb); // Put the Lua functions into the sb environment } catch (ScriptException ex) { Logger.getLogger(PlayerLua.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(PlayerLua.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:org.paxml.test.SelfTest.java
public void testSyntax() throws Exception { ScriptEngine runtime = new ScriptEngineManager().getEngineByName("javascript"); Bindings bindings = runtime.getBindings(ScriptContext.ENGINE_SCOPE); bindings.put("util", "xx"); runtime.setBindings(new SimpleBindings() { }, ScriptContext.ENGINE_SCOPE); }
From source file:com.pamarin.game24.Equations.java
private ScriptEngine getEngine() { if (engine == null) { ScriptEngineManager manager = new ScriptEngineManager(); engine = manager.getEngineByName("js"); }//w ww. ja v a 2 s .c om return engine; }
From source file:Hyperbole.java
public Hyperbole(String applicationTitle, String chartTitle) { super(applicationTitle); setSize(500, 270);/*from ww w.ja va 2 s . c om*/ setVisible(true); setFocusable(true); setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); manager = new ScriptEngineManager(); engine = manager.getEngineByName("js"); eq = new JButton("Add equation"); XYDataset dataset = createDataset(); JFreeChart chart = createChart(dataset, chartTitle); ChartPanel chartPanel = new ChartPanel(chart); JPanel panel1 = new JPanel(); panel1.add(chartPanel); JPanel panel2 = new JPanel(); panel2.add(eq); JPanel container = new JPanel(); container.add(panel1); container.add(panel2); getContentPane().add(container); eq.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { clean(); draw(JOptionPane.showInputDialog("New equation")); } }); pack(); }
From source file:com.mnxfst.stream.pipeline.element.script.ScriptEvaluatorPipelineElementTest.java
/** * This is not a test case but more a kind of a sandbox for fiddling around with the script engine *//*from w ww. ja v a2 s . c om*/ @Test public void testEvaluateScriptWithReturn() throws Exception { ScriptEngineManager factory = new ScriptEngineManager(); ScriptEngine engine = factory.getEngineByName("JavaScript"); StringWriter writer = new StringWriter(); PrintWriter pw = new PrintWriter(writer, true); engine.getContext().setWriter(pw); engine.eval(new FileReader("/home/mnxfst/git/stream-analyzer/src/main/resources/spahql.js")); String script = "var result0 = '1';var result1 = '2';"; engine.eval(script); StringBuffer sb = writer.getBuffer(); System.out.println("StringBuffer contains: " + sb + " - " + engine.get("test")); System.out.println(engine.get("content")); for (int i = 0; i < Integer.MAX_VALUE; i++) { String content = (String) engine.get("result" + i); if (StringUtils.isBlank(content)) break; System.out.println(content); } ObjectMapper mapper = new ObjectMapper(); StreamEventMessage message = new StreamEventMessage(); message.setIdentifier("test-id"); message.setOrigin("test-origin"); message.setTimestamp("2014-03-05"); message.setEvent("10"); // message.addCustomAttribute("test-key-1", "3"); // message.addCustomAttribute("test-key-2", "value-1"); // message.addCustomAttribute("test-key-3", "19"); // message.addCustomAttribute("errors", (String)engine.get("test")); String json = mapper.writeValueAsString(message); System.out.println(json); StreamEventMessage msg = mapper.readValue(json, StreamEventMessage.class); System.out.println(message.getCustomAttributes().get("json")); }
From source file:org.zols.datastore.validator.TV4.java
public TV4() { mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); ScriptEngineManager manager = new ScriptEngineManager(); engine = manager.getEngineByName("JavaScript"); try {/*from www . j a va 2 s .c om*/ json = engine.eval("JSON"); loadJavaScriptFile("/tv4.js"); loadJavaScriptFile("/tv4Wrapper.js"); } catch (Exception e) { e.printStackTrace(); } }
From source file:cc.osint.graphd.script.GScriptEngine.java
public GScriptEngine(String vm, String vm_type) throws Exception { this.vmName = vm; this.vmType = vm_type; log.info("new GScriptEngine(" + vm + ", " + vm_type + ")"); manager = new ScriptEngineManager(); engine = manager.getEngineByName(this.vmType); try {//from w ww . jav a 2 s. co m invocableEngine = (Invocable) engine; onlyEval = false; } catch (Exception ex) { ex.printStackTrace(); invocableEngine = null; onlyEval = true; } }
From source file:org.gatherdata.alert.detect.bsf.internal.BsfEventDetector.java
public BsfEventDetector() { this.scriptEngineManager = new ScriptEngineManager(); List<ScriptEngineFactory> engineFactories = scriptEngineManager.getEngineFactories(); log.info("Available ScriptEngineFactories..."); for (ScriptEngineFactory factory : engineFactories) { log.info("\t" + factory.getEngineName() + " - " + factory.getLanguageName() + " " + "ext:" + factory.getExtensions() + " " + "names:" + factory.getNames()); }//from w w w . j a va 2 s . c o m }
From source file:io.lightlink.translator.JSBeautifyPostProcessor.java
@Override public String process(String script) throws IOException { String beautifier = IOUtils.toString(Thread.currentThread().getContextClassLoader() .getResourceAsStream("io/lightlink/translator/beautify.js")); ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn"); try {//w w w. java 2 s . com engine.eval(beautifier); engine.getBindings(ScriptContext.ENGINE_SCOPE).put("$originalScript", script); return engine.eval("js_beautify($originalScript, {})") + ""; } catch (ScriptException e) { LOG.error(e.toString(), e); return script; } }