- 浏览: 343029 次
- 性别:
- 来自: 广州
-
文章分类
最新评论
JSFunction.java package com.milenfan.rhino; import org.mozilla.javascript.Function; public class JSFunction // extends ScriptableObject { private String name; private Function handle; public void setHandler(Function func) { this.handle = func; } public Function getHandler() { return this.handle; } public JSFunction(String s) { this.name = s; } public static void print(String s) { System.out.println(s); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
var swingNames = JavaImporter(); swingNames.importPackage(Packages.java.lang); swingNames.importPackage(Packages.com.milenfan.rhino); obj = { a : 1, b : [ 'x', 'y' ] } next = isPrime flag = isPrime(5) with (swingNames) { System.out.println("in javascript"); JSFunction.print("in JSFunction"); jsFunction = new JSFunction("caizhenyao"); var name = jsFunction.getName(); System.out.println("get name from java source: " + name); jsFunction.setHandler(log); } java.lang.System.out.println("not use swingNames"); function isPrime(num) { java.lang.System.out.println("in isPrime(num)"); if (num <= 1) { java.lang.System.out.println("Please enter a positive integer >= 2.") return false } var prime = true var sqrRoot = Math.round(Math.sqrt(num)) for ( var n = 2; prime & n <= sqrRoot; ++n) { prime = (num % n != 0) } return prime } function log(msg) { java.lang.System.out.println("in function log: " + msg); }
package com.milenfan.rhino; import java.io.FileReader; import java.io.LineNumberReader; import org.mozilla.javascript.Context; import org.mozilla.javascript.Function; import org.mozilla.javascript.Scriptable; public class JSExploration { private Context cx; private Scriptable scope; public JSExploration() { this.cx = Context.enter(); this.scope = cx.initStandardObjects(); } public Object runJavaScript(String filename) { String jsContent = this.getJsContent(filename); Object result = cx.evaluateString(scope, jsContent, filename, 1, null); return result; } private String getJsContent(String filename) { LineNumberReader reader; try { reader = new LineNumberReader(new FileReader(filename)); String s = null; StringBuffer sb = new StringBuffer(); while ((s = reader.readLine()) != null) { sb.append(s).append("\n"); } return sb.toString(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } } public Scriptable getScope() { return scope; } public static void main(String[] args) { String filename = "D:\\workplace\\WebProject\\src\\com\\milenfan\\rhino\\jsmap.js"; JSExploration jsExploration = new JSExploration(); Object result = jsExploration.runJavaScript(filename); Scriptable scope = jsExploration.getScope(); Scriptable obj = (Scriptable) scope.get("obj", scope); System.out.println("obj.a == " + obj.get("a", obj)); Scriptable b = (Scriptable) obj.get("b", obj); System.out.println("b[0] == " + b.get(0, b)); Boolean flag = (Boolean) scope.get("flag", scope); System.out.println(flag); Scriptable myobj = (Scriptable) scope.get("obj", scope); Boolean myflag = (Boolean) scope.get("flag", scope); System.out.println(myflag); Scriptable jsFunction = (Scriptable) scope.get("jsFunction", scope); Function fc = (Function) jsFunction.get("handler", jsFunction); Object isPrime = fc.call(Context.getCurrentContext(), jsFunction, fc, new Object[] { "this is my test" }); } }
发表评论
-
Mina重连
2014-05-26 21:29 2936import com.sun.swing.internal. ... -
面试经典
2014-05-24 09:29 6491.mysql innodb引擎,什么叫聚集索引,与非聚集索 ... -
一拍网网站系统架构图
2014-03-28 21:24 639一拍网网站系统架构图 -
Window下安装配置nginx
2013-08-12 16:53 821安装:http://www.cnblogs.com/wen ... -
使用线程池的好处
2013-07-18 14:41 1268使用线程池有两个好处: 1.可以创建和销毁线程所带来的系统 ... -
Java ThreadLocal使用浅析
2013-07-18 14:36 495ThreadLocal通过在其内部保存变量的副本,并且各个副本 ... -
MyBatis学习之简单增删改查操作、MyBatis存储过程、MyBatis分页、MyBatis一对一、MyBatis一对多
2013-07-05 13:06 1196http://blog.csdn.net/zhangwei ... -
分享一位网友的架构杂谈
2013-05-20 23:16 919不容类型的网站,并发处理不一样,例如针对sns这种类型的网站 ... -
EasyUI和EasyUI桌面App
2013-04-15 11:56 1216http://fxz-2008.iteye.com/blog/ ... -
Jquery进度条
2013-04-12 09:08 885http://www.cnblogs.com/lhb25/ h ... -
11个适合触摸事件开发的JavaScript库
2013-04-10 09:23 838http://www.codecto.com/2012/08/ ... -
JSP页面静态化
2013-04-08 09:20 897http://www.java-zone.org/644.ht ... -
Jquery插件
2013-03-22 18:33 799http://www.cnblogs.com/ywqu/arc ... -
Java compile to C++
2013-03-19 14:53 512http://code.google.com/a/eclips ... -
几个TCP Socket的通信框架
2013-03-19 12:26 1002http://www.oschina.net/p/simple ... -
宝贝鱼
2013-03-18 23:54 697http://code.google.com/p/cshbbr ... -
将Java程序注册成系统服务(NT服务)
2013-03-16 16:14 605http://blog.csdn.net/small____f ... -
HTML&JS MVC
2013-03-15 16:27 636http://www.bootcss.com/ http:// ... -
浏览器内核及Js引擎介绍
2013-03-15 16:18 749http://wenku.baidu.com/view/623 ... -
Java内存回收机制
2013-03-13 15:47 818http://www.iteye.com/blogs/tag/ ...
相关推荐
Rhinoceros,通常称为Rhino,是一个三维建模工具,广泛应用于工业设计、建筑、汽车设计、船舶设计、3D打印、珠宝设计等领域。它以其能够创建自由曲面的高质量模型和精确的几何表示而著称。而Rhino Python是指Rhino...
- 在Rhino中,你可以使用“曲线绕转”( Loft)命令,通过沿着一个路径将多个截面曲线绕转成一个连续的曲面。在这个例子中,可能需要先创建碗的横截面曲线,然后设置路径,最后执行Loft操作。 2. **建立路径,双轨...
以下是一个简单的Java类,展示了如何使用Rhino执行JavaScript代码: ```java import org.mozilla.javascript.Context; import org.mozilla.javascript.Scriptable; import org.mozilla.javascript.ScriptableObject...
以下是一个简单的例子,假设我们有一个`ICalculator`接口,包含一个`Add`方法: ```csharp public interface ICalculator { int Add(int a, int b); } ``` 我们可以使用Rhino Mocks创建一个模拟的`ICalculator`...
以下是一个简单的例子: ```java import org.mozilla.javascript.*; public class JavaToJsExample { public static void main(String[] args) { Context context = Context.enter(); try { Scriptable scope ...
这个例子展示了如何启动一个HtmlUnitDriver,导航到一个网页,并打印出页面标题。 请注意,具体的JAR文件版本可能会随着HTMLUnit的更新而变化,因此在实际使用时,最好参考HTMLUnit的官方文档或最新的Maven依赖来...
早期的成功案例包括Flakey(Konolige等人,1993),这是一个可以在SRI走廊中通过声音控制的机器人。此后,社会交互型机器人领域逐渐发展起来(Fong等人,2003)。 在能够用自然语言交流的交互式机器人中,经常被...
INT:中间代码,当一个源程序经过语法检查后编译产生一个可执行代码 IOF:Findit文档 IQY:Microsoft Internet查询文件 ISO:根据ISD 9660有关CD-ROM文件系统标准列出CD-ROM上的文件 ISP:X-Internet签字文件 ...