List of usage examples for javax.script Bindings get
public Object get(Object key);
From source file:Main.java
public static void main(String[] args) throws Exception { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); engine.put("a", 1); engine.put("b", 5); Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); Object a = bindings.get("a"); Object b = bindings.get("b"); System.out.println("a = " + a); System.out.println("b = " + b); Object result = engine.eval("c = a + b;"); System.out.println("a + b = " + result); }
From source file:Main.java
public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("js"); engine.put("a", 1); engine.put("b", 5); Bindings bindings = engine.getBindings(ScriptContext.ENGINE_SCOPE); Object a = bindings.get("a"); Object b = bindings.get("b"); System.out.println("a = " + a); System.out.println("b = " + b); Object result;/*from www . ja v a 2s .com*/ try { result = engine.eval("c = aaaa + bbbb;"); System.out.println("a + b = " + result); } catch (ScriptException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:Main.java
public static void main(String[] args) { Bindings params = new SimpleBindings(); params.put("stringKey", "Hello"); params.put("valueKey", 2015); Object msg = params.get("stringKey"); Object year = params.get("valueKey"); System.out.println("stringKey" + msg); System.out.println("valueKey = " + year); params.remove("valueKey"); year = params.get("valueKey"); boolean containsYear = params.containsKey("valueKey"); System.out.println("valueKey = " + year); System.out.println("params contains year = " + containsYear); }
From source file:Main.java
public static void main(String[] args) { ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName("JavaScript"); Bindings params = engine.createBindings(); params.put("stringKey", "Hello"); params.put("valueKey", 2015); Object msg = params.get("stringKey"); Object year = params.get("valueKey"); System.out.println("stringKey" + msg); System.out.println("valueKey = " + year); params.remove("valueKey"); year = params.get("valueKey"); boolean containsYear = params.containsKey("valueKey"); System.out.println("valueKey = " + year); System.out.println("params contains year = " + containsYear); }
From source file:org.apache.sling.scripting.sightly.js.impl.Utils.java
public static SlingScriptHelper getHelper(Bindings bindings) { return (SlingScriptHelper) bindings.get(SlingBindings.SLING); }
From source file:GetToKnowBindingsAndScopes.java
public static void dumpBindings(Bindings bindings) { if (bindings == null) System.out.println(" No bindings"); else/*ww w . j av a 2 s. co m*/ for (String key : bindings.keySet()) System.out.println(" " + key + ": " + bindings.get(key)); System.out.println(); }
From source file:org.apache.sling.scripting.sightly.js.impl.Utils.java
public static Resource getScriptResource(Resource caller, String path, Bindings bindings) { Resource scriptResource = caller.getChild(path); if (scriptResource == null) { Resource componentCaller = ResourceResolution.getResourceForRequest(caller.getResourceResolver(), (SlingHttpServletRequest) bindings.get(SlingBindings.REQUEST)); if (isResourceOverlay(caller, componentCaller)) { scriptResource = ResourceResolution.getResourceFromSearchPath(componentCaller, path); } else {//from ww w . j a va2 s . com scriptResource = ResourceResolution.getResourceFromSearchPath(caller, path); } } if (scriptResource == null) { throw new SightlyException("Required script resource could not be located: " + path + ". The caller is " + caller.getPath()); } return scriptResource; }
From source file:org.mycontroller.standalone.scripts.McScriptEngineUtils.java
public static HashMap<String, Object> getBindings(Bindings bindings) { HashMap<String, Object> engineScopes = new HashMap<String, Object>(); for (String key : bindings.keySet()) { //Do not add mcApi and __builtins__ if (!key.equals(MC_API) && !key.startsWith("__")) { engineScopes.put(key, bindings.get(key)); }//from w ww .j a v a2 s. c o m } return engineScopes; }
From source file:at.alladin.rmbt.qos.testscript.TestScriptInterpreter.java
/** * // w w w. j ava 2 s . c o m * @param args * @param hstore * @param object * @return * @throws ScriptException */ private static Object eval(String[] args, Hstore hstore, AbstractResult<?> object) throws ScriptException { try { final Bindings bindings = new SimpleBindings(object.getResultMap()); //System.out.println(object.getResultMap().toString()); jsEngine.eval(args[0], bindings); final Object result = bindings.get("result"); EvalResult evalResult = null; if (result != null) { if (result.getClass().getCanonicalName().equals("sun.org.mozilla.javascript.NativeObject") || result .getClass().getCanonicalName().equals("sun.org.mozilla.javascript.internal.NativeObject")) { if (!alredayLookedForGetter && jsEngineNativeObjectGetter == null) { alredayLookedForGetter = true; System.out.println("js getter is null, trying to get methody with reflections..."); try { jsEngineNativeObjectGetter = result.getClass().getMethod("get", Object.class); System.out.println("method found: " + jsEngineNativeObjectGetter.getName()); } catch (Exception e) { System.out.println("method not found: " + e.getMessage()); } } if (jsEngineNativeObjectGetter != null) { final String type = (String) jsEngineNativeObjectGetter.invoke(result, "type"); final String key = (String) jsEngineNativeObjectGetter.invoke(result, "key"); evalResult = new EvalResult(EvalResultType.valueOf(type.toUpperCase(Locale.US)), key); //System.out.println("Result: " + evalResult); } } } return evalResult == null ? (result == null ? "" : result) : evalResult; } catch (Exception e) { e.printStackTrace(); throw new ScriptException(e.getMessage()); } }
From source file:com.adobe.acs.commons.widgets.MultiFieldPanelWCMUse.java
@Override public void init(Bindings bindings) { Resource resource = (Resource) bindings.get(SlingBindings.RESOURCE); Object location = bindings.get("location"); if (location != null) { if (location instanceof Resource) { resource = (Resource) location; } else {// www. j a v a 2 s.c om if (location instanceof String) { resource = resource.getResourceResolver().getResource((String) location); } } } String name = (String) bindings.get("name"); if (StringUtils.isBlank(name)) { log.info("Invalid property name"); return; } values = MultiFieldPanelFunctions.getMultiFieldPanelValues(resource, name); }