字符串批量处理中,情况多种多样,要在编译代码中预测所有可能的情况比较困难. 采用filter,plugin 的方式可以大大简化代码编写量,同时提供方便的扩展性.
这里主要讨论java 中如何掉用javascript 以及二者之间的变量传递关系,有于时间关系只看以下String 类型的数据.
bsf
rhino
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
import org.apache.bsf.*;
import org.apache.bsf.util.*;
public class TestU {
public static void main(String args[]){
String sourcehead = "function dealparm(){" +
" str = bsf.lookupBean('strparm');" +
"return str+\"javascript return;\";}";
try{
BSFManager mgr = new BSFManager ();
//设置
BSFEngine rhinoEngine = mgr.loadScriptingEngine ("javascript");
//rhinoEngine.eval(parm, arg1, arg2, args)
rhinoEngine.eval ("javascript", 0, 0, sourcehead);
mgr.registerBean("strparm", "testparm1");
Object result = rhinoEngine.eval("javascript", 0, 0, "dealparm();");
//输出显示
System.out.println(result);
mgr.registerBean("strparm", "testparm2");
result = rhinoEngine.eval("javascript", 0, 0, "dealparm();");
//输出显示
System.out.println(result);
}catch(Exception e){
e.printStackTrace();
}
}
}
输出结果:
testparm1javascript return;
testparm2javascript return;
BSFEngine.eval 说明
public java.lang.Object eval(java.lang.String source,
int lineNo,
int columnNo,
java.lang.Object expr)
throws
- This is used by an application to evaluate an expression. The expression may be string or some other type, depending on the language. (For example, for BML it'll be an org.w3c.dom.Element object.)
-
-
- Parameters:
source
- (context info) the source of this expression (e.g., filename)
lineNo
- (context info) the line number in source for expr
columnNo
- (context info) the column number in source for expr
expr
- the expression to evaluate
- Throws:
- if anything goes wrong while eval'ing a BSFException is thrown. The reason indicates the problem.
阅读(1989) | 评论(0) | 转发(0) |