test.js
function Transfer(content, baseurl) { //TODO var result = ""; if(content.indexOf('<body>') > -1 && content.indexOf('</body>') > -1) { result = content.substring(content.indexOf('<body>') + 6,content.indexOf('</body>')); } return result; }
java中的代码
public void excueteJs() {
Context ct = Context.enter();
Scriptable scope = ct.initStandardObjects();
String filename = "E:\\test\\test.js";
String jsFunction = "Transfer";
try {
LineNumberReader reader = new LineNumberReader(new FileReader(filename));
String temp = null;
StringBuffer sb = new StringBuffer();
while ((temp = reader.readLine()) != null) {
sb.append(temp).append("\n");
}
ct.evaluateString(scope, sb.toString(), null, 1, null);
Object result = ct.evaluateString(scope, jsFunction, null, 1, null);
System.out.println("my"+result.toString());
} catch (Exception e) {
e.printStackTrace();
} finally {
ct.exit();
}
}
在给个复杂点的例子, 没有什么逻辑,纯技术展示
JSFunction.java:
package co.test;
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;
}
}
JSExploration.java:
package co.test;
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 = System.getProperty("user.dir") + "/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" });
}
}
js脚本:jsmap.js
- var swingNames = JavaImporter();
- swingNames.importPackage(Packages.java.lang);
- swingNames.importPackage(Packages.co.test);
- 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("lichunlei");
- 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);
- }