純java 的javascript引擎:rhino

前段時間,在瀏覽javaeye論壇,看見有人徵集如何在java中運行數學表達式。
結果方案五花八門:
1.jakarta commons JEXL.
2.Beanshell
3.Java Math Expression Parser jep
4.parse combinator jparsec
5.jdk 6.0的 script
6.利用SQL
7.自己寫語法分析
如果自己寫語法分析,沒有個2000行估計搞不定。有人用sql來運行數學表達式,比較另類。
不過由於前段時間較深入的學習了一些java的javascript引擎,我給出的方案就是用javascript來計算了。
java中比較著名的js引擎當屬mozilla開源的rhino,不過jdk6已經把它收歸帳下,成了正規軍。

  1. public class MathEval   
  2. {   
  3.     public static void main(String[] args)   
  4.     {   
  5.         Context cx = Context.enter();   
  6.         try  
  7.         {   
  8.             Scriptable scope = cx.initStandardObjects();   
  9.             String str = "9*(1+2)";   
  10.             Object result = cx.evaluateString(scope, str, null1null);   
  11.             double res = Context.toNumber(result);   
  12.             System.out.println(res);   
  13.         }   
  14.         finally  
  15.         {   
  16.             Context.exit();   
  17.         }   
  18.     }   
  19. }  

下面總結一下前段時間學習rhino的心得(給自己的程序添加腳本功能,其實是很酷的):

一:環境配置及運行js腳本:
在 http://www.mozilla.org/rhino/ 下載rhino:
把js.jar加入系統CLASSPATH中
可以以交互模式調用js解釋器:
java org.mozilla.javascript.tools.shell.Main
然後您應該會看到解釋器的版本號,後面跟着提示符 js>
用法如下:
比如:有一個js文件:
D:\eclipse-workshop\rhinoExample\src\isPrime.js
內容如下:

js 代碼
  1. function isPrime (num)   
  2. {   
  3.     if (num <= 1) {   
  4.         print("Please enter a positive integer >= 2.")   
  5.         return false  
  6.     }   
  7.        
  8.     var prime = true  
  9.     var sqrRoot = Math.round(Math.sqrt(num))   
  10.        
  11.     for (var n = 2; prime & n <= sqrRoot; ++n) {   
  12.         prime = (num % n != 0)   
  13.     }   
  14.        
  15.     return prime   
  16. }  


如何運行呢:
1:在命令行下鍵入:
java org.mozilla.javascript.tools.shell.Main
2:在js〉下鍵入:
load("D:/eclipse-workshop/rhinoExample/src/isPrime.js");
注意:是“/”而不是“\”
3:鍵入:
isPrime(77);
可看見返回結果爲false。
鍵入:
isPrime(71);返回true

再給個例子,腳本如下:

  1. person = {   
  2.   name:"Mike Squillace",   
  3.   age:37,   
  4.   position:"software engineer",   
  5.   getFirstName:function () {return this.name.split(" ")[0]}   
  6. }   
  7. person.getFirstName()  

js產生swing的例子:
load("D:/eclipse-workshop/rhinoExample/src/SwingApplication.js");
怎麼樣?看見效果沒?是不是很強悍?其中SwingApplication.js是rhnio自帶的例子。


Rhino還有一個js腳本的調試器:
Rhino JavaScript Debugger:
java org.mozilla.javascript.tools.debugger.Main [options] [filename.js] [script-arguments]

只須運行java org.mozilla.javascript.tools.debugger.Main,就可以看到調試器的界面了。


爲了加快js文件運行的速度,可以把它編譯爲class文件:
compile:
java org.mozilla.javascript.tools.jsc.Main D:/eclipse-workshop/rhinoExample/src/FirstCompile.js
編譯產生FirstCompile.class文件
在D:/eclipse-workshop/rhinoExample/src/下運行該class文件:
java FirstCompile

二:在實際應用中不可避免的需要遇到java代碼如何和javascript腳本相互訪問的問題:
這是一個最簡單的例子:(liveConnect.js是rhnio自帶的例子):
load("D:/eclipse-workshop/rhinoExample/src/liveConnect.js");

在給個複雜點的例子, 沒有什麼邏輯,純技術展示,呵呵:
JSFunction.java:

java 代碼
  1. package co.test;   
  2.   
  3. import org.mozilla.javascript.Function;   
  4.   
  5. public class JSFunction  //extends ScriptableObject   
  6. {   
  7.  private String name;    
  8.     
  9.  private Function handle;   
  10.     
  11.  public void setHandler(Function func)   
  12.  {   
  13.   this.handle = func;   
  14.  }   
  15.     
  16.  public Function getHandler()   
  17.  {   
  18.   return this.handle;   
  19.  }   
  20.   
  21.     
  22.  public JSFunction(String s)   
  23.  {   
  24.   this.name = s;   
  25.  }    
  26.     
  27.  public static void print(String s)   
  28.  {   
  29.   System.out.println(s);   
  30.  }   
  31.   
  32.  public String getName() {   
  33.   return name;   
  34.  }   
  35.  public void setName(String name) {   
  36.   this.name = name;   
  37.  }   
  38.   
  39. }   
  40.   

JSExploration.java:

java 代碼
  1. package co.test;   
  2.   
  3. import java.io.FileReader;   
  4. import java.io.LineNumberReader;   
  5.   
  6. import org.mozilla.javascript.Context;   
  7. import org.mozilla.javascript.Function;   
  8. import org.mozilla.javascript.Scriptable;   
  9.   
  10. public class JSExploration   
  11. {   
  12.     private Context cx;   
  13.   
  14.     private Scriptable scope;   
  15.   
  16.     public JSExploration()   
  17.     {   
  18.         this.cx = Context.enter();   
  19.         this.scope = cx.initStandardObjects();   
  20.     }   
  21.   
  22.     public Object runJavaScript(String filename)   
  23.     {   
  24.         String jsContent = this.getJsContent(filename);   
  25.         Object result = cx.evaluateString(scope, jsContent, filename, 1null);   
  26.         return result;   
  27.     }   
  28.   
  29.     private String getJsContent(String filename)   
  30.     {   
  31.         LineNumberReader reader;   
  32.         try  
  33.         {   
  34.             reader = new LineNumberReader(new FileReader(filename));   
  35.             String s = null;   
  36.             StringBuffer sb = new StringBuffer();   
  37.             while ((s = reader.readLine()) != null)   
  38.             {   
  39.                 sb.append(s).append("\n");   
  40.             }   
  41.             return sb.toString();   
  42.         }   
  43.         catch (Exception e)   
  44.         {   
  45.             // TODO Auto-generated catch block   
  46.             e.printStackTrace();   
  47.             return null;   
  48.         }   
  49.     }   
  50.   
  51.   
  52.     public Scriptable getScope()   
  53.     {   
  54.         return scope;   
  55.     }   
  56.   
  57.     public static void main(String[] args)   
  58.     {   
  59.         String filename = System.getProperty("user.dir") + "/jsmap.js";   
  60.         JSExploration jsExploration = new JSExploration();   
  61.         Object result = jsExploration.runJavaScript(filename);   
  62.         Scriptable scope = jsExploration.getScope();   
  63.         Scriptable obj = (Scriptable) scope.get("obj", scope);   
  64.         System.out.println("obj.a == " + obj.get("a", obj));   
  65.         Scriptable b = (Scriptable) obj.get("b", obj);   
  66.         System.out.println("b[0] == " + b.get(0, b));   
  67.         Boolean flag = (Boolean) scope.get("flag", scope);   
  68.         System.out.println(flag);   
  69.   
  70.         Scriptable myobj = (Scriptable) scope.get("obj", scope);   
  71.         Boolean myflag = (Boolean) scope.get("flag", scope);   
  72.         System.out.println(myflag);   
  73.   
  74.         Scriptable jsFunction = (Scriptable) scope.get("jsFunction", scope);   
  75.         Function fc = (Function) jsFunction.get("handler", jsFunction);   
  76.         Object isPrime = fc.call(Context.getCurrentContext(), jsFunction, fc, new Object[] { "this is my test" });   
  77.     }   
  78. }   
  79.   

js腳本:jsmap.js

js 代碼
  1. var swingNames = JavaImporter();   
  2.   
  3. swingNames.importPackage(Packages.java.lang);   
  4. swingNames.importPackage(Packages.co.test);   
  5.   
  6. obj = {a:1, b:['x','y']}   
  7. next = isPrime   
  8. flag = isPrime(5)   
  9. with (swingNames) {   
  10.  System.out.println("in javascript");   
  11.  JSFunction.print("in JSFunction");   
  12.   jsFunction = new JSFunction("lichunlei");   
  13.  var name = jsFunction.getName();   
  14.  System.out.println("get name from java source: " + name);   
  15.  jsFunction.setHandler(log);    
  16. }    
  17.   
  18. java.lang.System.out.println("not use swingNames");   
  19. function isPrime (num)   
  20. {   
  21.  java.lang.System.out.println("in isPrime(num)");   
  22.     if (num <= 1) {   
  23.         java.lang.System.out.println("Please enter a positive integer >= 2.")   
  24.         return false  
  25.     }   
  26.        
  27.     var prime = true  
  28.     var sqrRoot = Math.round(Math.sqrt(num))   
  29.        
  30.     for (var n = 2; prime & n <= sqrRoot; ++n) {   
  31.         prime = (num % n != 0)   
  32.     }   
  33.        
  34.     return prime   
  35. }   
  36.   
  37. function log(msg)   
  38. {   
  39.  java.lang.System.out.println("in function log: " + msg);   
  40. }   
  41.   
  42.     

js 代碼

java 代碼
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章