当前位置: 首页>>代码示例>>Java>>正文


Java ScriptEngine.get方法代码示例

本文整理汇总了Java中javax.script.ScriptEngine.get方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptEngine.get方法的具体用法?Java ScriptEngine.get怎么用?Java ScriptEngine.get使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.script.ScriptEngine的用法示例。


在下文中一共展示了ScriptEngine.get方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: main

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    System.out.println("\nTest4\n");
    ScriptEngineManager m = new ScriptEngineManager();
    ScriptEngine e  = Helper.getJsEngine(m);
    if (e == null) {
        System.out.println("Warning: No js engine found; test vacuously passes.");
        return;
    }
    e.eval(new FileReader(
        new File(System.getProperty("test.src", "."), "Test4.js")));
    Invocable inv = (Invocable)e;
    Runnable run1 = (Runnable)inv.getInterface(Runnable.class);
    run1.run();
    // use methods of a specific script object
    Object intfObj = e.get("intfObj");
    Runnable run2 = (Runnable)inv.getInterface(intfObj, Runnable.class);
    run2.run();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:19,代码来源:Test4.java

示例2: test6

import javax.script.ScriptEngine; //导入方法依赖的package包/类
@Test
    public void test6() throws Exception {
        Document document = Jsoup.parse(FileUtils.readFileToString(new File("/Users/eddy/Desktop/content")));
        Elements elements = document.getElementsByTag("script");
//        String result = elements.stream().filter(e -> e.data().contains("globalRepeatSubmitToken") && e.childNodes().size() > 0)
//                .findFirst().map(e -> e.childNode(0).outerHtml()).orElse(StringUtils.EMPTY);

        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        ScriptEngine engine = scriptEngineManager.getEngineByExtension("js");
//        engine.eval(result);
//        Object o = engine.get("globalRepeatSubmitToken");
//        System.out.println(o);

//        ticketInfoForPassengerForm
        String ticketInfo = elements.stream().filter(e -> e.data().contains("ticketInfoForPassengerForm") && e.childNodes().size() > 0)
                .findFirst().map(e -> e.childNode(0).outerHtml()).orElse(StringUtils.EMPTY);
        ticketInfo = ticketInfo.substring(0, ticketInfo.lastIndexOf("var"));
        engine.eval(ticketInfo);
        ScriptObjectMirror o2 = (ScriptObjectMirror) engine.get("ticketInfoForPassengerForm");
        System.out.println(o2.get("purpose_codes"));

    }
 
开发者ID:justice-code,项目名称:Thrush,代码行数:23,代码来源:LoginTest.java

示例3: isJsFunctionAvailable

import javax.script.ScriptEngine; //导入方法依赖的package包/类
private boolean isJsFunctionAvailable(ScriptEngine eng, String functionName, boolean doDeepTest) {
    // We want to test if the function is there, but without actually 
    // invoking it.        
    Object obj = eng.get(functionName);
    
    if (!doDeepTest && obj != null) {  
        // Shallow test. We've established that there's
        // "something" in the ENGINE_SCOPE with a name like
        // functionName, and we *hope* it is a function, but we really don't
        // know, therefore we call it a shallow test.
        return true;
    }
    
    // For Nashorn post JDK8u40 we can do even deeper validation
    // using the ScriptObjectMirror class. This will not work for Rhino.
    if (doDeepTest && obj != null) {
        if (obj instanceof ScriptObjectMirror) {
                ScriptObjectMirror  som = (ScriptObjectMirror) obj;
                if (som.isFunction()) {
                    return true;
                }
        }
    }        
    return false;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:26,代码来源:NbPacScriptEvaluator.java

示例4: executeJsExpress

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * 执行js表达式并返回执行后的结果
 * @param express
 * 表达式
 * @param value
 * 原值
 * @return
 * 返回新值或返回原值(执行失败时)
 */
public static Object executeJsExpress(String express, Object value){
    Object newValue = value;
    if(!StringUtils.isEmpty(express)){
        try {
            ScriptEngineManager manager = new ScriptEngineManager();
            ScriptEngine engine = manager.getEngineByName("nashorn");
            engine.put("value", value);
            engine.put("newValue", "");
            engine.getBindings(ScriptContext.ENGINE_SCOPE);
            engine.eval(express);
            newValue = engine.get("newValue");
        } catch (ScriptException e) {
            log.error("执行js表达式错误",e);
        }
    }

    return newValue;
}
 
开发者ID:DevopsJK,项目名称:SuitAgent,代码行数:28,代码来源:MetricsCommon.java

示例5: main

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    System.out.println("\nTest8\n");
    ScriptEngineManager m = new ScriptEngineManager();
    ScriptEngine e  = Helper.getJsEngine(m);
    if (e == null) {
        System.out.println("Warning: No js engine found; test vacuously passes.");
        return;
    }
    e.eval(new FileReader(
        new File(System.getProperty("test.src", "."), "Test8.js")));
    Invocable inv = (Invocable)e;
    inv.invokeFunction("main", "Mustang");
    // use method of a specific script object
    Object scriptObj = e.get("scriptObj");
    inv.invokeMethod(scriptObj, "main", "Mustang");
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:17,代码来源:Test8.java

示例6: main

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
    System.out.println("\nTest7\n");
    File file =
        new File(System.getProperty("test.src", "."), "Test7.js");
    Reader r = new FileReader(file);
    ScriptEngineManager m = new ScriptEngineManager();
    ScriptEngine eng = Helper.getJsEngine(m);
    if (eng == null) {
        System.out.println("Warning: No js engine found; test vacuously passes.");
        return;
    }
    eng.put("filename", file.getAbsolutePath());
    eng.eval(r);
    String str = (String)eng.get("firstLine");
    // do not change first line in Test7.js -- we check it here!
    if (!str.equals("//this is the first line of Test7.js")) {
        throw new RuntimeException("unexpected first line");
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:20,代码来源:Test7.java

示例7: main

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(final String[] args) throws Exception {
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine engine = manager.getEngineByName("nashorn");

    // JavaScript code in a String
    final String script = "var obj = new Object(); obj.run = function() { print('run method called'); }";

    // evaluate script
    engine.eval(script);

    // get script object on which we want to implement the interface with
    final Object obj = engine.get("obj");

    final Invocable inv = (Invocable) engine;

    // get Runnable interface object from engine. This interface methods
    // are implemented by script methods of object 'obj'
    final Runnable r = inv.getInterface(obj, Runnable.class);

    // start a new thread that runs the script implemented
    // runnable interface
    final Thread th = new Thread(r);
    th.start();
    th.join();
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:RunnableImplObject.java

示例8: main

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(final String[] args) throws Exception {
    final ScriptEngineManager manager = new ScriptEngineManager();
    final ScriptEngine engine = manager.getEngineByName("nashorn");

    // JavaScript code in a String. This code defines a script object 'obj'
    // with one method called 'hello'.
    final String script = "var obj = new Object(); obj.hello = function(name) { print('Hello, ' + name); }";
    // evaluate script
    engine.eval(script);

    // javax.script.Invocable is an optional interface.
    // Check whether your script engine implements or not!
    // Note that the JavaScript engine implements Invocable interface.
    final Invocable inv = (Invocable) engine;

    // get script object on which we want to call the method
    final Object obj = engine.get("obj");

    // invoke the method named "hello" on the script object "obj"
    inv.invokeMethod(obj, "hello", "Script Method !!" );
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:22,代码来源:InvokeScriptMethod.java

示例9: getToken

import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static String getToken(String html) throws ScriptException {
    Document document = Jsoup.parse(html);

    Elements elements = document.getElementsByTag("script");
    String jsContent = elements.stream().filter(e -> e.data().contains("globalRepeatSubmitToken") && e.childNodes().size() > 0)
            .findFirst().map(e -> e.childNode(0).outerHtml()).orElse(StringUtils.EMPTY);

    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine engine = scriptEngineManager.getEngineByExtension("js");
    engine.eval(jsContent);
    return (String) engine.get("globalRepeatSubmitToken");
}
 
开发者ID:justice-code,项目名称:Thrush,代码行数:13,代码来源:TokenUtil.java

示例10: getTicketInfoForPassengerForm

import javax.script.ScriptEngine; //导入方法依赖的package包/类
private static String getTicketInfoForPassengerForm(String html, String key) throws ScriptException {
    Document document = Jsoup.parse(html);

    Elements elements = document.getElementsByTag("script");
    String jsContent = elements.stream().filter(e -> e.data().contains("ticketInfoForPassengerForm") && e.childNodes().size() > 0)
            .findFirst().map(e -> e.childNode(0).outerHtml()).orElse(StringUtils.EMPTY);
    jsContent = jsContent.substring(0, jsContent.lastIndexOf("var"));

    ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
    ScriptEngine engine = scriptEngineManager.getEngineByExtension("js");
    engine.eval(jsContent);
    ScriptObjectMirror objectMirror = (ScriptObjectMirror) engine.get("ticketInfoForPassengerForm");
    return (String) objectMirror.get(key);
}
 
开发者ID:justice-code,项目名称:Thrush,代码行数:15,代码来源:TokenUtil.java

示例11: prettifyJson

import javax.script.ScriptEngine; //导入方法依赖的package包/类
private String prettifyJson(String json) {
    try {
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine scriptEngine = manager.getEngineByName("JavaScript");
        scriptEngine.put("jsonString", json);
        scriptEngine.eval("result = JSON.stringify(JSON.parse(jsonString), null, 2)");

        return (String) scriptEngine.get("result");
    } catch (ScriptException e) {
        throw new RuntimeException(e);
    }
}
 
开发者ID:amvnetworks,项目名称:amv-access-api-poc,代码行数:13,代码来源:SwaggerRestApiDocumentation.java

示例12: getAsBoolean

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Retrieves the value of the given JavaScript-Variable and
 * returns it as boolean.
 *
 * @param js  JavaScript-Engine that contains the variable.
 * @param key Name of the variable.
 * @return Value of the variable as a Boolean.
 */
private static boolean getAsBoolean(ScriptEngine js, String key) {
    Objects.requireNonNull(js);
    Objects.requireNonNull(key);

    Object o = js.get(key);
    if (o instanceof Boolean) {
        return (boolean) o;
    }

    throw new IllegalArgumentException("JavaScript-object '" + key + "' with value '" + o + "' is not a Boolean.");
}
 
开发者ID:lsinfo3,项目名称:mo-vnfcp,代码行数:20,代码来源:Config.java

示例13: getAsInt

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Retrieves the value of the given JavaScript-Variable and
 * returns it as int.
 * Valid Object types are Integer and Double.
 *
 * @param js  JavaScript-Engine that contains the variable.
 * @param key Name of the variable.
 * @return Value of the variable as an Integer.
 */
private static int getAsInt(ScriptEngine js, String key) {
    Objects.requireNonNull(js);
    Objects.requireNonNull(key);

    Object o = js.get(key);
    if (o instanceof Integer || o instanceof Double) {
        return (int) o;
    }

    throw new IllegalArgumentException("JavaScript-object '" + key + "' with value '" + o + "' is not a number.");
}
 
开发者ID:lsinfo3,项目名称:mo-vnfcp,代码行数:21,代码来源:Config.java

示例14: getAsDouble

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Retrieves the value of the given JavaScript-Variable and
 * returns it as double.
 * Valid Object types are Integer and Double.
 *
 * @param js  JavaScript-Engine that contains the variable.
 * @param key Name of the variable.
 * @return Value of the variable as double.
 */
private static double getAsDouble(ScriptEngine js, String key) {
    Objects.requireNonNull(js);
    Objects.requireNonNull(key);

    Object o = js.get(key);
    if (o instanceof Integer) {
        return ((Integer) o).doubleValue();
    }
    if (o instanceof Double) {
        return (double) o;
    }

    throw new IllegalArgumentException("JavaScript-object '" + key + "' with value '" + o + "' is not a number.");
}
 
开发者ID:lsinfo3,项目名称:mo-vnfcp,代码行数:24,代码来源:Config.java

示例15: getAsString

import javax.script.ScriptEngine; //导入方法依赖的package包/类
/**
 * Retrieves the value of the given JavaScript-Variable and
 * returns it as String.
 *
 * @param js  JavaScript-Engine that contains the variable.
 * @param key Name of the variable.
 * @return Value of the variable as String.
 */
private static String getAsString(ScriptEngine js, String key) {
    Objects.requireNonNull(js);
    Objects.requireNonNull(key);

    Object o = js.get(key);
    if (o instanceof String) {
        return (String) o;
    }

    throw new IllegalArgumentException("JavaScript-object '" + key + "' with value '" + o + "' is not a String.");
}
 
开发者ID:lsinfo3,项目名称:mo-vnfcp,代码行数:20,代码来源:Config.java


注:本文中的javax.script.ScriptEngine.get方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。