@Test public void testRealJavaScriptExampleWithRecord() throws ScriptException { JavaScriptEnv jsEnv = new JavaScriptEnv(); Map<String, String> record = new HashMap<>(); record.put("fieldA", "value1"); record.put("fieldB", "value2"); record.put("fieldC", "value1"); assertTrue(jsEnv.evalFilter("record.fieldA == 'value1'", record)); assertTrue(jsEnv.evalFilter("record.fieldA == record['fieldC']", record)); assertFalse(jsEnv.evalFilter("record.fieldA == record.fieldB", record)); }
public boolean evalFilter(String s, Map<String, String> record) throws ScriptException { Gson gson = new Gson(); ScriptEngine engine = this.factory.getEngineByName("JavaScript"); String recordJson = gson.toJson(record); engine.put("record_json", recordJson); try { s = "var record = new Object(JSON.parse(record_json));" + s; return (boolean) engine.eval(s); } catch (NullPointerException e) { throw new NullPointerException("The record filter should return either true or false! -- " + e.toString()); } }
JavaScriptEnv { public boolean evalFilter(String s, Map<String, String> record) throws ScriptException { Gson gson = new Gson(); ScriptEngine engine = this.factory.getEngineByName("JavaScript"); String recordJson = gson.toJson(record); engine.put("record_json", recordJson); try { s = "var record = new Object(JSON.parse(record_json));" + s; return (boolean) engine.eval(s); } catch (NullPointerException e) { throw new NullPointerException("The record filter should return either true or false! -- " + e.toString()); } } }
JavaScriptEnv { public boolean evalFilter(String s, Map<String, String> record) throws ScriptException { Gson gson = new Gson(); ScriptEngine engine = this.factory.getEngineByName("JavaScript"); String recordJson = gson.toJson(record); engine.put("record_json", recordJson); try { s = "var record = new Object(JSON.parse(record_json));" + s; return (boolean) engine.eval(s); } catch (NullPointerException e) { throw new NullPointerException("The record filter should return either true or false! -- " + e.toString()); } } JavaScriptEnv(); }
JavaScriptEnv { public boolean evalFilter(String s, Map<String, String> record) throws ScriptException { Gson gson = new Gson(); ScriptEngine engine = this.factory.getEngineByName("JavaScript"); String recordJson = gson.toJson(record); engine.put("record_json", recordJson); try { s = "var record = new Object(JSON.parse(record_json));" + s; return (boolean) engine.eval(s); } catch (NullPointerException e) { throw new NullPointerException("The record filter should return either true or false! -- " + e.toString()); } } JavaScriptEnv(); boolean evalFilter(String s, Map<String, String> record); }
JavaScriptEnv { public boolean evalFilter(String s, Map<String, String> record) throws ScriptException { Gson gson = new Gson(); ScriptEngine engine = this.factory.getEngineByName("JavaScript"); String recordJson = gson.toJson(record); engine.put("record_json", recordJson); try { s = "var record = new Object(JSON.parse(record_json));" + s; return (boolean) engine.eval(s); } catch (NullPointerException e) { throw new NullPointerException("The record filter should return either true or false! -- " + e.toString()); } } JavaScriptEnv(); boolean evalFilter(String s, Map<String, String> record);  }