本文整理汇总了Java中javax.script.ScriptEngine.setBindings方法的典型用法代码示例。如果您正苦于以下问题:Java ScriptEngine.setBindings方法的具体用法?Java ScriptEngine.setBindings怎么用?Java ScriptEngine.setBindings使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类javax.script.ScriptEngine
的用法示例。
在下文中一共展示了ScriptEngine.setBindings方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: main
import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
System.out.println("\nTest3\n");
final Reader reader = new FileReader(
new File(System.getProperty("test.src", "."), "Test3.js"));
ScriptEngineManager m = new ScriptEngineManager();
final ScriptEngine engine = Helper.getJsEngine(m);
if (engine == null) {
System.out.println("Warning: No js engine found; test vacuously passes.");
return;
}
Bindings en = new SimpleBindings();
engine.setBindings(en, ScriptContext.ENGINE_SCOPE);
en.put("key", "engine value");
Bindings gn = new SimpleBindings();
engine.setBindings(gn, ScriptContext.GLOBAL_SCOPE);
gn.put("key", "global value");
engine.eval(reader);
}
示例2: getScriptEngine
import javax.script.ScriptEngine; //导入方法依赖的package包/类
private PacScriptEngine getScriptEngine(String pacSource) throws PacParsingException {
try {
String helperJSScript = getHelperJsScriptSource();
LOGGER.log(Level.FINER, "PAC Helper JavaScript :\n{0}", helperJSScript);
ScriptEngine engine;
if (nashornJava8u40Available) {
engine = getNashornJSScriptEngine();
} else {
engine = getGenericJSScriptEngine();
}
LOGGER.log(Level.FINE, "PAC script evaluator using: {0}", getEngineInfo(engine));
PacHelperMethods pacHelpers = Lookup.getDefault().lookup(PacHelperMethods.class);
if (pacHelpers == null) { // this should be redundant but we take no chances
pacHelpers = new NbPacHelperMethods();
}
Bindings b = engine.createBindings();
b.put(JS_HELPER_METHODS_INSTANCE_NAME, pacHelpers);
engine.setBindings(b, ScriptContext.ENGINE_SCOPE);
engine.eval(pacSource);
engine.eval(helperJSScript);
// Do some minimal testing of the validity of the PAC Script.
final PacJsEntryFunction jsMainFunction;
if (nashornJava8u40Available) {
jsMainFunction = testScriptEngine(engine, true);
} else {
jsMainFunction = testScriptEngine(engine, false);
}
return new PacScriptEngine(engine, jsMainFunction);
} catch (ScriptException ex) {
throw new PacParsingException(ex);
}
}
示例3: init
import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static ScriptEngine init(String programName) {
ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn");
nashorn.setBindings(new ThreadSafeBindings(), ScriptContext.ENGINE_SCOPE);
Parser parser = new Parser(new Tokenizer(loadSrcForName(programName)));
Node programNode = parser.parse();
ClassNode classNode = programNode.getFirstChild(ClassNode.class);
String script = classNode.genCode();
try {
nashorn.eval(script);
} catch (Exception e) {
throw new RuntimeException(e);
}
return nashorn;
}
示例4: init
import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static ScriptEngine init(String programName) {
ScriptEngine nashorn = new ScriptEngineManager().getEngineByName("nashorn");
nashorn.setBindings(new ThreadSafeBindings(), ScriptContext.ENGINE_SCOPE);
Parser parser = new Parser(new Tokenizer(loadSrcForName(programName)));
Node programNode = parser.parse();
safe(() -> nashorn.eval(programNode.genCode()));
return nashorn;
}
示例5: invokeJS
import javax.script.ScriptEngine; //导入方法依赖的package包/类
private List<String> invokeJS(String js, int y, int ch) {
ArrayList<String> pagsList = new ArrayList<String>();
String str = js.substring(0, js.indexOf("var pt="));
str = str.replace("ge('TheImg').src", "var src");
String unuseScript = StringUtility.substring(str, "\'.jpg\';", "break;");
str = str.replace(unuseScript, "");
String varSrc = null;
if(str.indexOf("ci = i;") != -1){
varSrc = StringUtility.substring(str, "ci = i;", "break;");
}else if(str.indexOf("ci=i;") != -1){
varSrc = StringUtility.substring(str, "ci=i;", "break;");
}
String getPageJS = String.format(buildGetPagesJS(), varSrc);
str = str.replace(varSrc, "");
str = str.replace("break;", getPageJS);
String script = "function sp2(ch, y){" + str + "} " + buildNviewJS();
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
Bindings bind = engine.createBindings();
bind.put("pagsList", pagsList);
engine.setBindings(bind, ScriptContext.ENGINE_SCOPE);
engine.eval(script);
Invocable inv = (Invocable) engine;
inv.invokeFunction("sp2", ch, y);
} catch (Exception e) {
e.printStackTrace();
}
return pagsList;
}
示例6: invokeJS
import javax.script.ScriptEngine; //导入方法依赖的package包/类
public static List<String> invokeJS(String js, int y, int ch) {
ArrayList<String> pagsList = new ArrayList<String>();
System.out.println(StringUtility.substring(js, ";for(var i=0;i<", ";i++"));
String str = js.substring(0, js.indexOf("var pt="));
str = str.replace("ge('TheImg').src", "var src");
String unuseScript = StringUtility.substring(str, "\'.jpg\';", "break;");
System.out.println(str);
System.out.println("unuseScript==" + unuseScript);
str = str.replace(unuseScript, "");
System.out.println("str-->" + str);
String varSrc = StringUtility.substring(str, "ci = i; ", "break;");
System.out.println("varSrc-->" + varSrc);
String getPageJS = String.format(buildGetPagesJS(), varSrc);
str = str.replace(varSrc, "");
str = str.replace("break;", getPageJS);
String script = "function sp2(ch, y){" + str + "} " + buildNviewJS();
System.out.println("script-->" + script);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
try {
Bindings bind = engine.createBindings();
bind.put("pagsList", pagsList);
engine.setBindings(bind, ScriptContext.ENGINE_SCOPE);
engine.eval(script);
Invocable inv = (Invocable) engine;
inv.invokeFunction("sp2", ch, y);
} catch (Exception e) {
e.printStackTrace();
}
return pagsList;
}