Example usage for javax.script ScriptEngine eval

List of usage examples for javax.script ScriptEngine eval

Introduction

In this page you can find the example usage for javax.script ScriptEngine eval.

Prototype

public Object eval(Reader reader) throws ScriptException;

Source Link

Document

Same as eval(String) except that the source of the script is provided as a Reader

Usage

From source file:fr.assoba.open.sel.generator.LanguageExecutor.java

public static void execute(List<Namespace> namespaceList, IO io, String... languages)
        throws IOException, ScriptException {
    ScriptEngineManager factory = new ScriptEngineManager();
    ScriptEngine jsEngine = factory.getEngineByName("JavaScript");
    jsEngine.put("IO", io);
    jsEngine.eval(io.readFile("underscore.js"));
    jsEngine.eval(io.readFile("handlebars-v1.3.0.js"));
    ObjectMapper mapper = new ObjectMapper();
    jsEngine.eval("namespaces=" + mapper.writeValueAsString(namespaceList));
    for (String lang : languages) {
        if (generatorMap.containsKey(lang)) {
            generatorMap.get(lang).generate(namespaceList, io);
        } else {//  w  ww .  j ava 2  s .c  o  m
            jsEngine.eval(io.readFile(lang + ".js"));
        }
    }
}

From source file:org.wso2.carbon.uuf.renderablecreator.hbs.internal.serialize.JsonSerializerTest.java

private static Bindings executeJavaScript(String jsScript) throws ScriptException {
    NashornScriptEngineFactory scriptEngineFactory = new NashornScriptEngineFactory();
    ScriptEngine engine = scriptEngineFactory.getScriptEngine("-strict", "--optimistic-types");
    engine.eval(jsScript);
    return engine.getBindings(ScriptContext.ENGINE_SCOPE);
}

From source file:Main.java

public static void execute(ScriptEngineManager manager, String engineName, String script) {

    ScriptEngine engine = manager.getEngineByName(engineName);
    if (engine == null) {
        System.out.println(engineName + " is not available.");
        return;//  w ww .  ja v a2  s. c o m
    }

    try {
        engine.eval(script);
    } catch (ScriptException e) {
        e.printStackTrace();
    }
}

From source file:com.intbit.util.ServletUtil.java

public static String getServerName(ServletContext context) {
    try {//from   www.j a v  a 2s. c om
        ScriptEngineManager manager = new ScriptEngineManager();
        ScriptEngine engine = manager.getEngineByName("JavaScript");
        String path = context.getRealPath("") + "/js/configurations.js";
        // read script file
        engine.eval(Files.newBufferedReader(Paths.get(path), StandardCharsets.UTF_8));

        Invocable inv = (Invocable) engine;
        // call function from script file
        return inv.invokeFunction("getHost", "").toString();
    } catch (Exception ex) {
        return "http://clients.brndbot.com/BrndBot/";
    }
}

From source file:org.eclairjs.nashorn.TestUtils.java

public static void evalJSResource(ScriptEngine engine, String resource) throws Exception {
    String src = IOUtils.toString(new InputStreamReader(TestUtils.class.getResourceAsStream(resource)));
    src += "\n//@ sourceURL=" + resource;

    engine.eval(src);
}

From source file:controllers.require.RequireApp.java

/**
 * ??// ww  w  . j av a  2 s  .c om
 */
@Transactional(readOnly = true)
public static Result search() throws ScriptException {
    DynamicForm requestData = Form.form().bindFromRequest();
    User me = User.getFromSession(session());
    String p = requestData.get("p") == null ? "1|1" : requestData.get("p");
    String type = requestData.get("type") == null ? "html" : requestData.get("type");
    String ft = requestData.get("ft") == null ? "" : requestData.get("ft").trim();
    String inf = requestData.get("inf");
    String cf = requestData.get("cf");
    String gf = requestData.get("gf");
    String o = requestData.get("o");
    String ot = requestData.get("ot");
    ScriptEngine engine = SemUtils.getEngine();
    if (StringUtils.isNotBlank(ft))
        ft = engine.eval("decodeURIComponent('" + ft + "')").toString();
    RTransformer gtrf = new RTransformer(ft, p, inf, cf, gf, o, ot);
    String resultJson = SearchHttpClient.advancedQuery(gtrf.tranAdSearchNVP(Constants.HOME_EXPERT_PAGE_SIZE));
    RPage<RequireListVO> sPage = null;
    if (StringUtils.isNotBlank(resultJson))
        sPage = gtrf.pageFromJson(resultJson, Constants.HOME_EXPERT_PAGE_SIZE, me);
    else
        sPage = new RPage(null, 0L, 1, 1);

    sPage.setGf(gf);
    sPage.setCf(cf);
    sPage.setInf(inf);
    sPage.setFt(ft);
    sPage.setO(o);
    sPage.setOt(ot);

    List<String> countryList = SkillTag.getCountryNameWithCache();
    sPage.setCountryList(countryList);

    ListOrderedMap cts = SkillTag.getCacheCategory();
    if (StringUtils.isNotBlank(inf))
        sPage.setInfStr((String) cts.get(new Long(inf)));
    else
        sPage.setInfStr("");

    if (StringUtils.isNotBlank(cf))
        sPage.setCfStr(cf);
    else
        sPage.setCfStr("");

    if (!type.equals("json")) {
        return ok(views.html.require.search.render(sPage, cts));
    }
    sPage.setCountryList(null);
    return ok(play.libs.Json.toJson(sPage));
}

From source file:controllers.services.ServicesApp.java

/**
 * ?/* ww w  .  j  ava  2  s .  c om*/
 * @throws ScriptException 
 */
@Transactional(readOnly = true)
public static Result search() throws ScriptException {
    DynamicForm requestData = Form.form().bindFromRequest();
    User me = User.getFromSession(session());
    String p = requestData.get("p") == null ? "1|1" : requestData.get("p");
    String type = requestData.get("type") == null ? "html" : requestData.get("type");
    String ft = requestData.get("ft") == null ? "" : requestData.get("ft").trim();
    String inf = requestData.get("inf");
    String cf = requestData.get("cf");
    String gf = requestData.get("gf");
    String o = requestData.get("o");
    String ot = requestData.get("ot");
    ScriptEngine engine = SemUtils.getEngine();
    if (StringUtils.isNotBlank(ft))
        ft = engine.eval("decodeURIComponent('" + ft + "')").toString();
    STransformer gtrf = new STransformer(ft, p, inf, cf, gf, o, ot);
    String resultJson = SearchHttpClient.advancedQuery(gtrf.tranAdSearchNVP(Constants.HOME_EXPERT_PAGE_SIZE));
    SPage<ServiceListVO> sPage = null;
    if (StringUtils.isNotBlank(resultJson))
        sPage = gtrf.pageFromJson(resultJson, Constants.HOME_EXPERT_PAGE_SIZE, me);
    else
        sPage = new SPage(null, 0L, 1, 1);

    sPage.setGf(gf);
    sPage.setCf(cf);
    sPage.setInf(inf);
    sPage.setFt(ft);
    sPage.setO(o);
    sPage.setOt(ot);

    List<String> countryList = SkillTag.getCountryNameWithCache();
    sPage.setCountryList(countryList);

    ListOrderedMap cts = SkillTag.getCacheCategory();
    if (StringUtils.isNotBlank(inf))
        sPage.setInfStr((String) cts.get(new Long(inf)));
    else
        sPage.setInfStr("");

    if (StringUtils.isNotBlank(cf))
        sPage.setCfStr(cf);
    else
        sPage.setCfStr("");

    if (!type.equals("json")) {
        return ok(views.html.services.search.render(sPage, cts));
    }
    sPage.setCountryList(null);
    return ok(play.libs.Json.toJson(sPage));
}

From source file:controllers.group.GroupApp.java

/**
 * ?//from ww  w  .  j  a v a2  s  .  co m
 * @throws ScriptException 
 */
@Transactional(readOnly = true)
public static Result searchResult() throws ScriptException {
    DynamicForm requestData = Form.form().bindFromRequest();
    User me = User.getFromSession(session());
    String p = requestData.get("p") == null ? "1|1" : requestData.get("p");
    String type = requestData.get("type") == null ? "html" : requestData.get("type");
    String ft = requestData.get("ft") == null ? "" : requestData.get("ft").trim();
    String inf = requestData.get("inf");
    String gpf = requestData.get("gpf");
    String o = requestData.get("o");
    String ot = requestData.get("ot");
    ScriptEngine engine = SemUtils.getEngine();
    if (StringUtils.isNotBlank(ft))
        ft = engine.eval("decodeURIComponent('" + ft + "')").toString();
    GTransformer gtrf = new GTransformer(ft, p, inf, gpf, o, ot);
    String resultJson = SearchHttpClient.advancedQuery(gtrf.tranAdSearchNVP(Constants.HOME_EXPERT_PAGE_SIZE));
    GPage<GroupListVO> gPage = null;
    if (StringUtils.isNotBlank(resultJson))
        gPage = gtrf.pageFromJson(resultJson, Constants.HOME_EXPERT_PAGE_SIZE, me);
    else
        gPage = new GPage(null, 0L, 1, 1);

    gPage.setGpf(gpf);
    gPage.setInf(inf);
    gPage.setFt(ft);
    gPage.setO(o);
    gPage.setOt(ot);

    ListOrderedMap cts = SkillTag.getCacheCategory();
    if (StringUtils.isNotBlank(inf))
        gPage.setInfStr((String) cts.get(new Long(inf)));
    else
        gPage.setInfStr("");

    if (!type.equals("json")) {
        return ok(views.html.group.searchresult.render(gPage, cts));
    } else {
        return ok(play.libs.Json.toJson(gPage));
    }
}

From source file:jef.jre5support.script.JavaScriptUtil.java

public static void importPackage(ScriptEngine e, Package pkg, Bindings... b) {
    try {//  ww  w  .j a  v a2s.  c  om
        if (b.length == 0) {
            e.eval("importPackage(Packages." + pkg.getName() + ")");
        } else {
            e.eval("importPackage(Packages." + pkg.getName() + ")", b[0]);
        }
    } catch (ScriptException e1) {
        throw new RuntimeException(e1);
    }
}

From source file:jef.jre5support.script.JavaScriptUtil.java

public static void importClass(ScriptEngine e, Class<?> pkg, Bindings... b) {
    try {/* w ww.  j  av  a  2 s.com*/
        if (b.length == 0) {
            e.eval("importClass(Packages." + pkg.getName() + ")");
        } else {
            e.eval("importClass(Packages." + pkg.getName() + ")", b[0]);
        }
    } catch (ScriptException e1) {
        throw new RuntimeException(e1);
    }
}