基于mozilla的js.jar包定义javascript的解析ScriptTool脚本工具类实现执...

package test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable;
public class ScriptTool {
    /**
     * @see 执行js函数,返回js函数执行结果
     * @param jsFile
     * @param funName
     * @param args
     * @return
     */
    public static Object callFunction(String jsFile,String funName,Object[] args){
           String jsPath = jsFile;
            String jsFunction = funName;
            Object result=null;
            Context cx = Context.enter();
            java.io.FileReader fr = null;
            try {
                Scriptable scope = cx.initStandardObjects();
                fr = new java.io.FileReader(jsPath);
                cx.evaluateReader(scope, fr, '<cmd>'1null);
                Object fObj = scope.get(jsFunction, scope);
                if (!(fObj instanceof Function)) {
                    System.out.println('Function not found' +jsFunction);
                else {
                    Function f = (Function)fObj;
                    result = f.call(cx, scope, scope, args);
                    System.out.println('return:'+Context.toString(result));
                }
            
            catch(Exception e){
                e.printStackTrace();
            }
            finally {
                Context.exit();
                if(fr != null)
                    try {
                        fr.close();
                    catch (IOException e) {
                        e.printStackTrace();
                    }
            }
        return result;
    }
    /**
     * @see 执行出入的js串,返回变量值Map
     * @param jString
     * @param varNames 返回map中变量名称
     * @return Map 变量值
     */
    public static Map eval(String jString,String[] varNames){
         Map result=new HashMap();
         Context cx = Context.enter();
            try {
                Scriptable scope = cx.initStandardObjects();
                cx.evaluateString(scope, jString, '<cmd>'1null);
                for(int i=0;i<varNames.length;i++){
                    result.put(varNames[i],scope.get(varNames[i], scope));
                }
                
            
            catch(Exception e){
                e.printStackTrace();
            }
            finally {
                Context.exit();
            }
        return result;
    }
    public static void main(String[] args) {
         
        try {
            //执行js函数
            String jsFile='c:\\temp\\mactest.js';
            String funName='mactest';
            Object[] arges={'m1','m2'};
            Object result=ScriptTool.callFunction(jsFile, funName, arges);
            System.out.println(result);
             
            //执行js脚本
            StringBuffer jString=new StringBuffer();
            jString.append('var i=1;');
            jString.append('var j=2;');
            jString.append('var c=(i+j)*100;');
            Map varsMap=ScriptTool.eval(jString.toString(),new String[]{'i','j','c'});
            System.out.println(varsMap);
         
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
(0)

相关推荐