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:controllers.ExpertApp.java

public static Result searchTag() {
    String st = request().getQueryString("st");
    String p = StringUtils.defaultIfBlank(request().getQueryString("p"), "1|1");
    String type = StringUtils.defaultIfBlank(request().getQueryString("type"), "html");
    ScriptEngine engine = SemUtils.getEngine();
    try {//from  w  w w . j  a v a2  s  .  c om
        if (StringUtils.isNotBlank(st))
            st = engine.eval("decodeURIComponent('" + st + "')").toString();
    } catch (ScriptException e) {
        e.printStackTrace();
    }
    Transformer trf = new Transformer(st, p, null, null, null, null, null, null);
    String resultJson = SearchHttpClient.advancedQuery(trf.tranMustTagNVP());
    EPage<ExpertListVO> pageObj = trf.pageFromJson(resultJson, null, Constants.HOME_EXPERT_PAGE_SIZE);
    pageObj.setFt(st);
    if (!type.equals("json")) {
        return ok(views.html.expert.customerservice.render(pageObj));
    } else {
        return ok(play.libs.Json.toJson(pageObj));
    }
}

From source file:controllers.ExpertApp.java

@Transactional(readOnly = true)
public static Result search() {
    User user = User.getFromSession(session());
    DynamicForm requestData = Form.form().bindFromRequest();
    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 cf = requestData.get("cf");
    String ssf = requestData.get("ssf");
    String ef = requestData.get("ef");
    String gf = requestData.get("gf");
    String o = requestData.get("o");
    String ot = requestData.get("ot");
    ScriptEngine engine = SemUtils.getEngine();
    try {/* w  w  w.j a  v a2  s . c o  m*/
        if (StringUtils.isNotBlank(ft))
            ft = engine.eval("decodeURIComponent('" + ft + "')").toString();
        if (StringUtils.isNotBlank(cf))
            cf = engine.eval("decodeURIComponent('" + cf + "')").toString();
    } catch (ScriptException e) {
        e.printStackTrace();
    }
    Transformer trf = new Transformer(ft, p, cf, ssf, ef, gf, o, ot);
    String resultJson = SearchHttpClient.advancedQuery(trf.tranAdSearchNVP(Constants.HOME_EXPERT_PAGE_SIZE));
    EPage<ExpertListVO> pageObj = null;
    if (StringUtils.isNotBlank(resultJson))
        pageObj = trf.pageFromJson(resultJson, user, Constants.HOME_EXPERT_PAGE_SIZE);
    else
        pageObj = new EPage(null, 0L, 1, 1);

    pageObj.setCf(cf);
    pageObj.setEf(ef);
    pageObj.setFt(ft);
    pageObj.setGf(gf);
    pageObj.setO(o);
    pageObj.setOt(ot);
    pageObj.setSsf(ssf);

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

    if (!type.equals("json")) {
        return ok(views.html.expert.search.render(pageObj));
    } else {
        return ok(play.libs.Json.toJson(pageObj));
    }
}

From source file:com.netsteadfast.greenstep.util.ScriptExpressionUtils.java

private static void executeRenjin(String scriptExpression, Map<String, Object> results,
        Map<String, Object> parameters) throws Exception {
    ScriptEngine engine = buildRenjinScriptEngine();
    if (parameters != null) {
        for (Map.Entry<String, Object> entry : parameters.entrySet()) {
            engine.put(entry.getKey(), entry.getValue());
        }//from ww  w .j  av  a  2 s . c o  m
    }
    engine.eval(scriptExpression);
    if (results != null) {
        for (Map.Entry<String, Object> entry : results.entrySet()) {
            Object res = engine.get(entry.getKey());
            if (res instanceof AbstractAtomicVector) {
                if (((AbstractAtomicVector) res).length() > 0) {
                    entry.setValue(((AbstractAtomicVector) res).getElementAsObject(0));
                }
            } else {
                entry.setValue(res);
            }
        }
    }
}

From source file:org.siphon.common.js.JsEngineUtil.java

public static Object eval(ScriptEngine jsEngine, String srcFile) throws ScriptException, IOException {
    jsEngine.put(ScriptEngine.FILENAME, srcFile);
    return jsEngine.eval(FileUtils.readFileToString(new File(srcFile)));
}

From source file:org.siphon.common.js.JsEngineUtil.java

public static Object eval(ScriptEngine jsEngine, String srcFile, String code) throws ScriptException {

    jsEngine.put(ScriptEngine.FILENAME, srcFile);
    return jsEngine.eval(code);
}

From source file:controllers.skilltag.SkillTagApp.java

@Transactional(readOnly = true)
public static Result change(Long i, int seq) {
    List<SkillTag> skillTags = SkillTag.getAll(i, seq, "tag");
    List<TagListVo> svs = new ArrayList<TagListVo>();
    try {//w w  w .  j a v  a 2s. c  o  m
        ScriptEngine engine = SemUtils.getEngine();
        for (SkillTag st : skillTags) {
            TagListVo tv = new TagListVo();
            tv.setTagName(st.tagName);
            // tv.setIsCurr(true);
            tv.setHref(controllers.skilltag.routes.SkillTagApp.list().url() + "?p=1&i=" + i + "&s="
                    + engine.eval("encodeURIComponent('" + tv.getTagName() + "')").toString());
            svs.add(tv);
        }
    } catch (ScriptException e) {
        e.printStackTrace();
    }
    return ok(play.libs.Json.toJson(svs));
}

From source file:io.github.jeddict.jcode.util.FileUtil.java

public static void expandTemplate(Reader reader, Writer writer, Map<String, Object> values,
        Charset targetEncoding) throws IOException {
    ScriptEngine eng = getScriptEngine();
    Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    bind.putAll(values);/*from  www. ja v a  2 s.  co m*/
    bind.put(ENCODING_PROPERTY_NAME, targetEncoding.name());

    try {
        eng.getContext().setWriter(writer);
        eng.eval(reader);
    } catch (ScriptException ex) {
        throw new IOException(ex);
    } finally {
        if (reader != null) {
            reader.close();
        }
    }
}

From source file:org.netbeans.jcode.core.util.FileUtil.java

private static void expandTemplate(InputStream template, Map<String, Object> values, Charset targetEncoding,
        Writer w) throws IOException {
    //        Charset sourceEnc = FileEncodingQuery.getEncoding(template);
    ScriptEngine eng = getScriptEngine();
    Bindings bind = eng.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
    bind.putAll(values);/*from  ww w.ja va2s .c  o  m*/
    bind.put(ENCODING_PROPERTY_NAME, targetEncoding.name());

    Reader is = null;
    try {
        eng.getContext().setWriter(w);
        is = new InputStreamReader(template);
        eng.eval(is);
    } catch (ScriptException ex) {
        throw new IOException(ex);
    } finally {
        if (is != null) {
            is.close();
        }
    }
}

From source file:com.linkedin.drelephant.util.Utils.java

/**
 * Returns the configured thresholds after evaluating and verifying the levels.
 *
 * @param rawLimits A comma separated string of threshold limits
 * @param thresholdLevels The number of threshold levels
 * @return The evaluated threshold limits
 *///from  w ww  .j ava  2 s.c  om
public static double[] getParam(String rawLimits, int thresholdLevels) {
    double[] parsedLimits = null;

    if (rawLimits != null && !rawLimits.isEmpty()) {
        String[] thresholds = rawLimits.split(",");
        if (thresholds.length != thresholdLevels) {
            logger.error("Could not find " + thresholdLevels + " threshold levels in " + rawLimits);
            parsedLimits = null;
        } else {
            // Evaluate the limits
            parsedLimits = new double[thresholdLevels];
            ScriptEngineManager mgr = new ScriptEngineManager(null);
            ScriptEngine engine = mgr.getEngineByName("JavaScript");
            for (int i = 0; i < thresholdLevels; i++) {
                try {
                    parsedLimits[i] = Double.parseDouble(engine.eval(thresholds[i]).toString());
                } catch (ScriptException e) {
                    logger.error("Could not evaluate " + thresholds[i] + " in " + rawLimits);
                    parsedLimits = null;
                }
            }
        }
    }

    return parsedLimits;
}

From source file:tk.tomby.tedit.services.ScriptingManager.java

/**
 * DOCUMENT ME!//from   w w  w  .j a v  a 2 s .c om
 *
 * @param lang DOCUMENT ME!
 * @param script DOCUMENT ME!
 * @param buffer DOCUMENT ME!
 *
 * @return DOCUMENT ME!
 */
public static Object eval(String lang, String script, IBuffer buffer) {
    Object result = null;

    try {
        ScriptEngine engine = manager.getEngineByName(lang);

        if (buffer != null) {
            Bindings bindings = engine.createBindings();
            bindings.put("buffer", new BufferDecorator(buffer));
            engine.setBindings(bindings, ScriptContext.ENGINE_SCOPE);
        }

        result = engine.eval(script);
    } catch (ScriptException e) {
        log.error("error in script excution", e);
    }

    return result;
}