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

voidaddAttribute(final ScriptContext context, final String key, final Object value)
add Attribute
context.setAttribute(key, value, ScriptContext.ENGINE_SCOPE);
StringadjustKey(String key)
adjust Key
if (key.equals(ScriptEngine.ARGV)) {
    return "ARGV";
if ("ARGV".equals(key)) {
    return ScriptEngine.ARGV;
} else {
    return key;
voidcdInShell(IResource resource)
cd In Shell
try {
    String containerPath = getParentOfResource(resource).replaceAll(" ", "\\ ");
    String openInTerminalString = "tell application \"Terminal\"\n do script \"cd \\\"" + containerPath
            + "\\\"\"\n activate\nend tell";
    executeAppleScript(openInTerminalString);
} catch (Throwable t) {
    t.printStackTrace();
Objectconvert(Object from)
convert
if (from instanceof JSObject) {
    if (((JSObject) from).isArray()) {
        return convertArray((Bindings) from);
if (from instanceof Map) {
    return dbObjectFromMap((Map<String, Object>) from);
return from;
BasicDBObjectdbObjectFromMap(Map from)
db Object From Map
BasicDBObject result = new BasicDBObject();
for (Entry<String, Object> entry : from.entrySet()) {
    result.append(entry.getKey(), convert(entry.getValue()));
return result;
StringdecodeB64_TO_UTF8(String encodedStr)
decode TUTF
String decodeStr = new String(Base64.getDecoder().decode(encodedStr.getBytes()), "UTF-8");
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
String result = (String) engine.eval("unescape('" + java.net.URLDecoder.decode(decodeStr, "UTF-8") + "')");
return result;
StringescapeJavascript(String str)
Encoding if need escaping %$&+,/:;=?@<>#%
try {
    String n = engine.eval(String.format("escape(\"%s\")", str.replaceAll("%20", " "))).toString();
    for (Entry<String, String> e : jsReplaces.entrySet()) {
        n = n.replaceAll(e.getKey(), e.getValue());
    return n;
} catch (ScriptException ex) {
    return null;
...
Objecteval(String expression, Object... arguments)
eval
ScriptEngineManager engineManager = new ScriptEngineManager();
ScriptEngine engine = engineManager.getEngineByName("JavaScript");
for (int i = 0; i < arguments.length; i++) {
    expression = expression.replaceAll("%" + (i + 1), arguments[i].toString());
return engine.eval(expression);
Objecteval(String name, Map params)
eval
CompiledScript script = scripts.get(name);
if (script == null) {
    throw new ScriptException("no such methor!");
Bindings b = engine.createBindings();
if (params != null) {
    b.putAll(params);
return script.eval(b);
Stringeval(String script)
eval
ScriptEngineManager sem = new ScriptEngineManager();
ScriptEngine se = sem.getEngineByName("javascript");
String res = "";
try {
    res = String.valueOf(se.eval(script));
} catch (ScriptException e) {
    e.printStackTrace();
return res;