本文介绍在 Java 环境中执行 Javascript 脚本的简单使用,具体包含以下内容
- Java 8 中的内置 Nashorn Javascript 引擎介绍 Rhino Javascript 引擎介绍以及对 XML 的处理介绍
Nashorn 是 Java 8 中内置的 Javascript 引擎,无需加入任何依赖。
Nashorn 基本使用步骤如下
- new 出 scriptEngineManager 对象 通过 scriptEngineManager 对象中的 getEngineByName 方法获取指定的 Javascript 引擎,返回 scriptEngine 对象 Java 8 中的默认的 Javascript 引擎包括:[nashorn, Nashorn, js, JS, Javascript, javascript, ECMAscript, ecmascript] 通过 scriptEngine 对象的 eval 方法来执行 Javascript 脚本。
Javascript 脚本中没有函数,没有返回值,通过 print 输出内容,这时需要通过 PrintWriter 获取读取脚本中的 print 输出,具体如下
@Test public void test_nashorn() { try { scriptEngine engine = new scriptEngineManager().getEngineByName("Nashorn"); scriptContext scriptContext = engine.getContext(); StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); scriptContext.setWriter(printWriter); String jsString = "var obj=JSON.parse(\'{\\"data\\":\\"7155\\",\\"sign\\":\\"success\\",\\"message\\":null}\');print(obj.sign=="success");"; stringWriter = new StringWriter(); printWriter = new PrintWriter(stringWriter); scriptContext.setWriter(printWriter); engine.eval(jsString); System.out.println(String.format("1 result = %s",stringWriter.toString())); jsString = "var obj=JSON.parse(\'{\\"data\\":\\"7157\\",\\"sign\\":\\"success\\",\\"message\\":null}\');print((function getData() { return obj.data;})())"; stringWriter = new StringWriter(); printWriter = new PrintWriter(stringWriter); scriptContext.setWriter(printWriter); engine.eval(jsString); System.out.println(String.format("2 result = %s",stringWriter.toString())); jsString = "var obj=JSON.parse(\'{\\"data\\":\\"7157\\",\\"sign\\":\\"success\\",\\"message\\":null}\');print((function getData() { return obj.data;})())"; stringWriter = new StringWriter(); printWriter = new PrintWriter(stringWriter); scriptContext.setWriter(printWriter); engine.eval(jsString); System.out.println(String.format("3 result = %s",stringWriter.toString())); } catch (Exception e) { e.printStackTrace(); } }
2.2 获取匿名函数的返回值 Javascript 脚本是一个匿名函数并且有返回值,通过 eval 函数可以直接
@Testpublic void test_js_function_return() { try { scriptEngine engine = new scriptEngineManager().getEngineByName("Nashorn"); String jsFunction = "(function(){var obj=JSON.parse(\'{\\"data\\":\\"7155\\",\\"sign\\":\\"success\\",\\"message\\":null}\');return obj.sign=="success"})();"; Boolean result = (Boolean) engine.eval(jsFunction); System.out.println(result); } catch (Exception e) { e.printStackTrace(); }}
2.3 调用 Javascript 脚本中指定的函数 Javascript 脚本中有变量,有多个函数,具体如下
@Testpublic void test_invoke_js_function() { try { scriptEngine engine = new scriptEngineManager().getEngineByName("Nashorn"); String jsFunction = "var obj=JSON.parse(\'{\\"data\\":\\"7155\\",\\"sign\\":\\"success\\",\\"message\\":null}\');function checkSign(){return obj.sign==\'success\'}function getData(){return obj.data}function calculate(a,b){return a+b}"; engine.eval(jsFunction); Invocable invocable = (Invocable) engine; Object result = invocable.invokeFunction("checkSign", null); System.out.println(result); result = invocable.invokeFunction("getData", null); System.out.println(result); result = invocable.invokeFunction("calculate", 2, 5); System.out.println(result); } catch (Exception e) { e.printStackTrace(); }}
输出如下 true71557.0
2.4 读取 Javascript 文件并执行 这种情况是读取 Javascript 文件,并执行,具体如下
src/test/resources/test.js@Testpublic void test_invoke_js_file() { try { scriptEngine engine = new scriptEngineManager().getEngineByName("Nashorn"); engine.eval(new FileReader(TestJSEngine.class.getResource("/test.js").getPath())); Invocable invocable = (Invocable) engine; Object result = invocable.invokeFunction("checkSign", null); System.out.println(result); result = invocable.invokeFunction("getData", null); System.out.println(result); result = invocable.invokeFunction("calculate", 2, 5); System.out.println(result); } catch (Exception e) { e.printStackTrace(); }}
文件内容和上面一样。
3 Rhino Javascript 引擎介绍默认的 Nashorn 引擎是无法解析 xml 的,像 DOMParser 这样的对象是浏览器内置的组件。
这里可以通过 Maven 依赖 Rhino 引擎来处理 xml。
Rhino Maven 依赖如下<dependency> <groupId>cat.inspiracio</groupId> <artifactId>rhino-js-engine</artifactId> <version>1.7.10</version></dependency>
使用的步骤和其他 Javascript 引擎一样,引擎的名称为 Rhino.
3.1 Rhino 对 xml 的解析这里通过读取文件的方式来加载和解析 Javascript 脚本,脚本中是对一段 xml 的解析的过程。
src/test/resources/xml.js 文件内容如下print("----------------------------------------");var e = new XML(\'<employees> <employee id="1"><name>Joe</name><age>20</age></employee> <employee id="2"><name>Sue</name><age>30</age></employee> </employees>\');// 获取所有的员工print("获取所有的员工:n" + e..name);// 名字叫 Joe 的员工print("名字叫 Joe 的员工:n" + e.employee.(name == "Joe"));// 员工的id 为 1 和 2print("员工的id 为 1 和 2:n" + e.employee.(@id == 1 || @id == 2));// 员工的id 为 1print("员工的id 为 1: " + e.employee.(@id == 1).name);print("----------------------------------------");
执行如下 @Testpublic void test_rhino_file_js() { try { scriptEngine engine = new scriptEngineManager().getEngineByName("rhino"); scriptContext scriptContext = engine.getContext(); StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); scriptContext.setWriter(printWriter); engine.eval(new FileReader(TestJSEngine.class.getResource("/xml.js").getPath())); System.out.println(String.format("xml result = %s",stringWriter.toString() )); } catch (Exception e) { e.printStackTrace(); }}
输出如下 xml result = ----------------------------------------All the employee names are:<name>Joe</name><name>Sue</name>The employee named Joe is:<employee id="1"> <name>Joe</name> <age>20</age></employee>Employees with ids 1 & 2:<employee id="1"> <name>Joe</name> <age>20</age></employee><employee id="2"> <name>Sue</name> <age>30</age></employee>Name of the the employee with ID=1: Joe----------------------------------------
3.2 测试 xml 内容如下 <CCardProcessSyncResponse> <RetCode>0</RetCode> <Message>操作成功!</Message> <RefundCycle /> <EpayRefundCycleMin>1</EpayRefundCycleMin> <EpayRefundCycleMax>7</EpayRefundCycleMax> <EpayRefundCycleUnitF /></CCardProcessSyncResponse>
具体测试如下 @Testpublic void test_rhino() { try { scriptEngine engine = new scriptEngineManager().getEngineByName("rhino"); String jsString = jsString = "var obj=new XML(\'<CCardProcessSyncResponse> <RetCode>0</RetCode> <Message>操作成功!</Message> <RefundCycle /> <EpayRefundCycleMin>1</EpayRefundCycleMin> <EpayRefundCycleMax>7</EpayRefundCycleMax> <EpayRefundCycleUnitF /> </CCardProcessSyncResponse>\');print(obj.Message == \'操作成功!\');"; scriptContext scriptContext = engine.getContext(); StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); scriptContext.setWriter(printWriter); engine.eval(jsString); System.out.println(String.format("xml result = %s",stringWriter.toString() )); } catch (Exception e) { e.printStackTrace(); }}
上面的输出结果如下
xml result = true