All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.loocme.sys.util.ScriptUtil Maven / Gradle / Ivy

There is a newer version: 7.1.11
Show newest version
package com.loocme.sys.util;

import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * @author liuchi
 */
public class ScriptUtil
{

    private static final ScriptEngineManager ENGINE_MANAGER;
    private static final String WEB_JAVASCRIPT_DATE_CLASSNAME = "class sun.org.mozilla.javascript.internal.NativeDate";

//    private static final Context cx = Context.enter();

    static
    {
        ENGINE_MANAGER = new ScriptEngineManager();
    }

    public static Object evalScript(String engineName, String script)
            throws ScriptException
    {
        ScriptEngine engine = ENGINE_MANAGER.getEngineByName(engineName);
        return engine.eval(script);
    }

    public static Object loadScript(String engineName, String script)
            throws ScriptException
    {
        ScriptEngine engine = ENGINE_MANAGER.getEngineByName(engineName);
        if (null == engine)
        {
            return null;
        }

        engine.eval(script);
        if (engine instanceof Invocable)
        {
            return (Invocable) engine;
        }
        return null;
    }

    @SuppressWarnings("unchecked")
    public static  T loadScript(String engineName, String script,
            Class clz) throws ScriptException
    {
        Object obj = loadScript(engineName, script);
        if (null == obj)
        {
            return null;
        }

        if (clz.isAssignableFrom(obj.getClass()))
        {
            return (T) obj;
        }
        else
        {
            return null;
        }
    }

    @SuppressWarnings("static-access")
    public static Object execScript(Invocable invo, String funcName,
            Object[] paramArr) throws ScriptException, NoSuchMethodException
    {
        Object retValue = null;
        if (null == paramArr)
        {
            paramArr = new Object[0];
        }
        switch (paramArr.length)
        {
            case 0:
                retValue = invo.invokeFunction(funcName);
                break;
            case 1:
                retValue = invo.invokeFunction(funcName, paramArr[0]);
                break;
            case 2:
                retValue = invo.invokeFunction(funcName, paramArr[0],
                        paramArr[1]);
                break;
            case 3:
                retValue = invo.invokeFunction(funcName, paramArr[0],
                        paramArr[1], paramArr[2]);
                break;
            case 4:
                retValue = invo.invokeFunction(funcName, paramArr[0],
                        paramArr[1], paramArr[2], paramArr[3]);
                break;
            case 5:
                retValue = invo.invokeFunction(funcName, paramArr[0],
                        paramArr[1], paramArr[2], paramArr[3], paramArr[4]);
                break;
            case 6:
                retValue = invo.invokeFunction(funcName, paramArr[0],
                        paramArr[1], paramArr[2], paramArr[3], paramArr[4],
                        paramArr[5]);
                break;
            case 7:
                retValue = invo.invokeFunction(funcName, paramArr[0],
                        paramArr[1], paramArr[2], paramArr[3], paramArr[4],
                        paramArr[5], paramArr[6]);
                break;
            case 8:
                retValue = invo.invokeFunction(funcName, paramArr[0],
                        paramArr[1], paramArr[2], paramArr[3], paramArr[4],
                        paramArr[5], paramArr[6], paramArr[7]);
                break;
            case 9:
                retValue = invo.invokeFunction(funcName, paramArr[0],
                        paramArr[1], paramArr[2], paramArr[3], paramArr[4],
                        paramArr[5], paramArr[6], paramArr[7], paramArr[8]);
                break;
            default:
                break;
        }

        if (null != retValue)
        {
            if (WEB_JAVASCRIPT_DATE_CLASSNAME
                    .equals(retValue.getClass().toString()))
            {
//                retValue = cx.jsToJava(retValue, Date.class);
            }
        }

        return retValue;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy