Java Utililty Methods Script

List of utility methods to do Script

Description

The list of methods to do Script are organized into topic(s).

Method

DoubleevaluateExpression(String expr)
evaluate Expression
expr = expr.replace(",", ".");
try {
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("JavaScript");
    expr = engine.eval(expr).toString();
} catch (Exception e) {
return Double.parseDouble(expr);
...
doubleevaluateExpression(String s)
Converts a string to a mathematical expression and evaluates it with Javascript.
final ScriptEngineManager mgr = new ScriptEngineManager();
final ScriptEngine engine = mgr.getEngineByName("JavaScript");
final double result = Double.valueOf("" + engine.eval(s));
return result;
voidexecuteAppleScript(String script)
execute Apple Script
try {
    ScriptEngineManager mgr = new ScriptEngineManager();
    ScriptEngine engine = mgr.getEngineByName("AppleScript");
    engine.eval(script);
} catch (Throwable e) {
    _executeAppleScript(script);
ObjectexecuteScript(String script)
execute Script
if (_appleScriptEngine == null) {
    _scriptEngineManager = new ScriptEngineManager();
    _appleScriptEngine = _scriptEngineManager.getEngineByName("AppleScriptEngine");
    if (_appleScriptEngine == null) {
        _appleScriptEngine = _scriptEngineManager.getEngineByName("AppleScript");
        if (_appleScriptEngine == null) {
            throw new ScriptException("AppleScriptEngine not available");
return _appleScriptEngine.eval(script, _appleScriptEngine.getContext());
voidexecuteScript(String strScript)
execute Script
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval(strScript);
voidfillStringCollection(ScriptObjectMirror som, Collection collection)
fill String Collection
if (som == null)
    return;
if (!som.isArray())
    throw new ScriptException("The JS object is not an array");
som.values().forEach(o -> collection.add(o.toString()));
StringgenerateToken(String val)
generate Token
try {
    String script = "function tk(a) {"
            + "var TKK = ((function() {var a = 561666268;var b = 1526272306;return 406398 + '.' + (a + b); })());\n"
            + "function b(a, b) { for (var d = 0; d < b.length - 2; d += 3) { var c = b.charAt(d + 2), c = 'a' <= c ? c.charCodeAt(0) - 87 : Number(c), c = '+' == b.charAt(d + 1) ? a >>> c : a << c; a = '+' == b.charAt(d) ? a + c & 4294967295 : a ^ c } return a }\n"
            + "for (var e = TKK.split('.'), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {"
            + "var c = a.charCodeAt(f);"
            + "128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)"
            + "}" + "a = h;" + "for (d = 0; d < g.length; d++) a += g[d], a = b(a, '+-a^+6');"
...
StringgetBallerinaFromJsonModel(String jsonModel)
Generate ballerina source from json model using nashorn
ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("nashorn");
ArrayList<String> paths = new ArrayList<>();
String balHome = System.getProperty("bal.composer.home");
String basePath = balHome + File.separator + "resources" + File.separator + "composer" + File.separator
        + "web";
paths.add(balHome + File.separator + "resources" + File.separator + "composer" + File.separator + "web"
        + File.separator + "js" + File.separator + "source-gen-script" + File.separator + "readfully.js");
...
StringgetDynamicInput(String key)
get Dynamic Input
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
engine.eval(JSFUNCTION);
Invocable inv = (Invocable) engine;
return (String) inv.invokeFunction("getCode", key);
ScriptEnginegetScript(String name)
get Script
return new ScriptEngineManager().getEngineByName(name);