《运用BeanShell脚本语言进行逻辑判断.doc》由会员分享,可在线阅读,更多相关《运用BeanShell脚本语言进行逻辑判断.doc(5页珍藏版)》请在金锄头文库上搜索。
1、运用BeanShell脚本语言进行逻辑判断Wang,XinyuBAS-DIT-Javaxin-Proprietary NoticeThis documentation is the property of, and contains confidential information of, Hewlett-Packard Company or its affiliates. Possession and use of this documentation is authorized only as specified in Exhibit SS5 and the HP Terms and Co
2、nditions of Sale and Services, HP Business Terms or HP Global Agreement or pursuant to the license accompanying this documentation. Keyword BeanSshell Category Java Description 运用BeanShell脚本语言进行逻辑判断 How to Use IT 1. 简述: BeanShell是一种脚本语言,一种完全符合Java语法的java脚本语言,并且又拥有自己的一些语法和方法,BeanShell是一种松散类型的脚本语言(这点和
3、JS类似)。实际工程中可以与用BeanShell脚本语言完成所需的简单或复杂对象的逻辑判断下载地址:http:/www.beanshell.org2. 环境设置: 1)配置BeanShellA. 把;bsh-xx.jar放到$JAVA_HOME/jre/lib/ext文件夹下Bunix: export CLASSPATH=$CLASSPATH:bsh-xx.jarC Windows: set classpath %classpath%;bsh-xx.jar本文中使用的是方法A,将bsh-2.0b4.jar放到C:jdk1.5.0_15jrelibext目录下2)检测配置打开dos窗口,键入:j
4、ava bsh.Console命令出现BeanShell图片代表设置成功,BeanShell开始运行3. 具体应用: 1)在Eclipse中建立工程TestBeanShell2)添加所需Java类文件建立Bsh1.java,此文件中记录用使用BeanShell编写的方法。建立Person.java,此文件记录一个简单Java类对象。建立ServiceContext.java,此文件记录一个复杂Java类对象。3)BeanShell方法实现详解Person.javapackage bhs.shell;public class Person private String pname;public
5、Person()super();public String getPname() return pname;public void setPname(String pname) this.pname = pname;ServiceContext.javapackage bhs.shell;import java.util.HashMap;import java.util.Map;public class ServiceContext public static final String EVENT_ID = EVENT_ID; public static final String MESSAG
6、E_ID = MESSAGE_ID;private Map context = new HashMap();public void setAttribute(String key, Object value) context.put(key, value);public Object getAttribute(String key) return context.get(key);public void removeAttribute(String key) context.remove(key);Bsh1.javapackage bhs.shell;import java.util.*;im
7、port bsh.EvalError;import bsh.Interpreter;import bsh.UtilEvalError;public class Bsh1 public static void main(String args) throws EvalError, UtilEvalError Bsh1.testBshSimpleType();Bsh1.testBshComplexType();Bsh1.testBshComplexExpression(); public static boolean testBshSimpleType() throws EvalError Int
8、erpreter i = new Interpreter(); / Construct an interpreterBoolean flag = false;i.set(input, 3);i.set(b, 7);/ Eval a statement and get the resultflag = (Boolean) i.eval(b input);System.out.print(Simple Type result: +flag+n);return flag; public static boolean testBshComplexType()throws EvalError, Util
9、EvalError ServiceContext context = new ServiceContext();List list= new ArrayList();Person p1=new Person();p1.setPname(eva);Person p2=new Person();p2.setPname(jason);list.add(p1);list.add(p2);context.setAttribute(input, list);Boolean flag = false;flag=(Boolean)validateCondition(Person)context.getAttr
10、ibute(input).get(1).getPname().equals(jason), context);System.out.print(Complex Type result: +flag+n);return flag; public static boolean testBshComplexExpression()throws EvalError, UtilEvalError ServiceContext context = new ServiceContext(); context.setAttribute(a, 3);context.setAttribute(b, 4);cont
11、ext.setAttribute(c, 6);context.setAttribute(d, null);Boolean flag = false;flag=(Boolean) validateCondition(int)context.getAttribute(a)(int)context.getAttribute(b)&(context.getAttribute(d)=null),context);System.out.print(Complex Expression result: +flag+n);return flag; public static boolean validateC
12、ondition(String condition, ServiceContext context) throws EvalError, UtilEvalError Interpreter i = new Interpreter();/ construct an InterpreterBoolean flag = false;/ record the result expression estimatedi.getNameSpace().importPackage(bhs.shell);/ import whole package or import special class with importClass(Person.class.getName()i.set(context, context);flag = (Boolean) i.eval(condition);return flag;