List of utility methods to do Script
void | addAttribute(final ScriptContext context, final String key, final Object value) add Attribute context.setAttribute(key, value, ScriptContext.ENGINE_SCOPE); |
String | adjustKey(String key) adjust Key if (key.equals(ScriptEngine.ARGV)) { return "ARGV"; if ("ARGV".equals(key)) { return ScriptEngine.ARGV; } else { return key; |
void | cdInShell(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(); |
Object | convert(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; |
BasicDBObject | dbObjectFromMap(Map db Object From Map BasicDBObject result = new BasicDBObject(); for (Entry<String, Object> entry : from.entrySet()) { result.append(entry.getKey(), convert(entry.getValue())); return result; |
String | decodeB64_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; |
String | escapeJavascript(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; ... |
Object | eval(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); |
Object | eval(String name, Map 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); |
String | eval(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; |