rhino

rhino-java中调用javascript

无人久伴 提交于 2019-12-09 17:28:57
在有些情况下需要在java里面执行javascript,这时rhino就可以帮忙了。 mozilla的一个开源产品。 官网 https://developer.mozilla.org/en-US/docs/Rhino 之前的一篇博客 http://my.oschina.net/yybear/blog/101493 里面介绍的事件模块处理程序就有利用javascript定义eventHandler,然后解释javascript执行。 先看一个简单的应用: public class JavaScriptHandle { private Scriptable global; private int optimizationLevel = -1; // 介于-1到9之间,负值表示使用解释性执行,不会生成class private Script script; void handle() { Context ctx = Context.enter(); try { ctx.setOptimizationLevel(optimizationLevel); if (script == null) { script = ctx.compileString("var str = 'xx'", "firstRhino", 0, null); } Scriptable scope = new

Java 8新特性探究(十二)Nashorn :新犀牛

五迷三道 提交于 2019-12-09 17:28:47
Nashorn是什么 Nashorn,发音“nass-horn”,是德国二战时一个坦克的命名,同时也是java8新一代的javascript引擎--替代老旧,缓慢的 Rhino , 符合 ECMAScript-262 5.1 版语言规范。 你可能想javascript是运行在web浏览器,提供对html各种dom操作,但是Nashorn不支持浏览器DOM的对象。这个需要注意的一个点。 关于Nashorn的入门 主要是两个方面,jjs工具以及javax.script包下面的API: jjs是在java_home/bin下面自带的,作为例子,让我们创建一个func.js, 内容如下: function f() {return 1;}; print( f() + 1 ); 运行这个文件,把这个文件作为参数传给jjs jjs func.js 输出结果: 2 另一个方面是javax.script,也是以前Rhino余留下来的API ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName( "JavaScript" ); System.out.println( engine.getClass().getName() ); System.out

java调用javascript :js引擎rhino

老子叫甜甜 提交于 2019-12-09 17:15:24
前段时间,在浏览javaeye论坛,看见有人征集如何在java中运行数学表达式。 结果方案五花八门: 1.jakarta commons JEXL. 2.Beanshell 3.Java Math Expression Parser jep 4.parse combinator jparsec 5.jdk 6.0的 script 6.利用SQL 7.自己写语法分析 如果自己写语法分析,没有个2000行估计搞不定。有人用sql来运行数学表达式,比较另类。 不过由于前段时间较深入的学习了一些java的javascript引擎,我给出的方案就是用javascript来计算了。 java中比较著名的js引擎当属mozilla开源的rhino,不过jdk6已经把它收归帐下,成了正规军。 public class MathEval { public static void main(String[] args) { Context cx = Context.enter(); try { Scriptable scope = cx.initStandardObjects(); String str = "9*(1+2)"; Object result = cx.evaluateString(scope, str, null, 1, null); double res = Context

Importing a map in javax.scripting javascript environment

自闭症网瘾萝莉.ら 提交于 2019-12-08 15:55:46
问题 I'm seeing some odd behavior in the javax.scripting map implementation. The online examples show an example of adding to a list within the js environment: ScriptEngineManager mgr = new ScriptEngineManager(); ScriptEngine jsEngine = mgr.getEngineByName("JavaScript"); List<String> namesList = new ArrayList<String>(); namesList.add("Jill"); namesList.add("Bob"); namesList.add("Laureen"); namesList.add("Ed"); jsEngine.put("namesListKey", namesList); System.out.println("Executing in script

Attempted to read or write protected memory

浪子不回头ぞ 提交于 2019-12-08 06:16:55
问题 I have a sample ASP.NET MVC 3 web application that is following Jonathan McCracken's Test-Drive Asp.NET MVC (great book , by the way) and I have stumbled upon a problem. Note that I'm using MVCContrib, Rhino and NUnit. [Test] public void ShouldSetLoggedInUserToViewBag() { var todoController = new TodoController(); var builder = new TestControllerBuilder(); builder.InitializeController(todoController); builder.HttpContext.User = new GenericPrincipal(new GenericIdentity("John Doe"), null);

Android, Javascript, Rhino, JSON

女生的网名这么多〃 提交于 2019-12-08 05:14:09
问题 After long search in repo folders I found rhino1_7R2.jar for Android at http://code.google.com/p/android-scripting/source/browse/rhino/rhino1_7R2.jar Unfortunately 1_7R3 is not there. The script I'm using uses JSON.stringify function which is not present in 1_7R2. There is the JSON2.js file for Rhino but I don't know the proper way to load it at run time. Documentation and example codes are weak. Should I load it as a string and prepend on the running script? Or there is a better way? I'm

Dojo not getting loaded into rhino emmbedded in java

笑着哭i 提交于 2019-12-08 03:54:30
问题 I'm trying to render the dojo charts server side. I came across the Rhino and envjs for server side browser emulation. when I tried to an example program to load the dojo.js in the rhino embedded in the java impl, the exception is thrown, Exception in thread "main" javax.script.ScriptException: sun.org.mozilla.javascript.EcmaError: ReferenceError: "location" is not defined. (#15) in at line number 15. My code is as follows: import javax.script.*; import java.io.*; public class

Different result from JavaScript RegExp in “rhino1.7.6” vs “V8”

让人想犯罪 __ 提交于 2019-12-07 22:58:03
问题 currently, I found the same JavaScript RegExp might generate different result in different JavaScript Engines, here is an example: In Chrome V8 JS engine, /\x3c/.test("\x3c") --> returns true /\x3c/.test(function() { return "\x3c" }) -->returns ***false*** In rhino1.7.6,I typed the command like this: >java -jar js.jar Rhino 1.7.6 2015 04 15 js> /\x3c/.test(function() { return "\x3c" }) true js> And I tested these two: /\x3c/.test("\x3c") --> returns true /\x3c/.test(function() { return "\x3c"

Rhino.Security and IEntityInformationExtractor

让人想犯罪 __ 提交于 2019-12-07 21:15:34
问题 I've recently downloaded Rhino.Security and I am trying to implement permissions on a entity. Since I like Ninject (v2) I would like to put together a simple example to start with. In my NinjectModule I've bound the repository and the services: Bind<ISessionFactory>() .ToProvider(new SessionFactoryProvider()) .InSingletonScope(); Bind<ISession>().ToProvider(new SessionProvider()) .InSingletonScope(); Bind<IAuthorizationRepository>() .To<AuthorizationRepository>() .InSingletonScope(); Bind

using recent Rhino in ant script

懵懂的女人 提交于 2019-12-07 16:10:32
问题 I'm trying to use a recent version of Rhino in an ant tag, but it seems to be using the version of Rhino that comes bundled with the JVM instead. I've tried adjusting the classpath to specify the Rhino script jar. Currently, I've tried the following: <project default="hello" name="hello-world" basedir="."> <target name="hello"> <script language="javascript"> <classpath> <pathelement location="js.jar"/> </classpath><![CDATA[ x=<hello><world/></hello> ]]></script> </target> </project> Notice