當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。