Beanshell翻译1

上一篇 / 下一篇  2008-09-02 20:08:43 / 个人分类:Beanshell

Beanshell翻译1
开源框架Beanshell翻译

http://user.qzone.qq.com/281696143/blog/1197940650 Ronger
1.bsh.Interpreter:
说明:The BeanShell scrīpt interpreter. An instance of Interpreter can be used to source scrīpts and evaluate statements or expressions.
Beanshell脚本解释者。一个解释的实例可以用作发起脚本或者计算申明或者表达式。
Here are some example:
  Interpeter bsh = new Interpreter();
// Evaluate statements and expressions计算表达式
  bsh.eval("foo=Math.sin(0.5)");
  bsh.eval("bar=foo*5; bar=Math.cos(bar);");
  bsh.eval("for(i=0; i<10; i++) { print(\"hello\"); }");
  // same as above using java syntax and apis only像上面一样使用java语法和句法。
   bsh.eval("for(int i=0; i<10; i++) { System.out.println(\"hello\"); }");
  // Source from files or streams从文件或者流中发起。
   bsh.source("myscrīpt.bsh");  // or bsh.eval("source(\"myscrīpt.bsh\")");
  // Use set() and get() to pass objects in and out of variables
使用set()和get()方法让对象进入和变量返回。
bsh.set( "date", new Date() );
Date date = (Date)bsh.get( "date" );
// This would also work:下面的也可以实现。
Date date = (Date)bsh.eval( "date" );
bsh.eval("year = date.getYear()");
Integer year = (Integer)bsh.get("year");
// primitives use wrappers最初使用包装类
// With Java1.3+ scrīpts can implement arbitrary interfaces...
JDK为java1.3以上脚本可以实现任意的接口。
// scrīpt an awt event handler (or source it from a file, more likely)
使用一个awt事件助手(或者从一个文件发起,更多类似)
bsh.eval( "actionPerformed( e ) { print( e ); }");
// Get a reference to the scrīpt object (implementing the interface)
取得一个指向脚本对象的引用。
  ActionListener scrīptedHandler =
         (ActionListener)bsh.eval("return (ActionListener)this");
// Use the scrīpted event handler normally...
通常使用已经定义好的事件助手。
new JButton.addActionListener( scrīpt );

In the above examples we showed a single interpreter instance, however you may wish to use many instances, depending on the application and how you structure your scrīpts. Interpreter instances are very light weight to create, however if you are going to execute the same scrīpt repeatedly and require maximum performance you should consider scrīpting the code as a method and invoking the scrīpted method each time on the same interpreter instance (using eval()).
我上面的例子中,我们演示了一个解释者实例。但是你会希望依靠应用程序和定义脚本的结构来使用很多的实例。解释者实例是轻量级的。但是如果你准备重复的执行一段脚本,你就应该将脚本代码定义成一个方法并且没有都用同一个解释者实例来调用这个脚本方法(使用eval())。
2.bsh.NameSpace
A namespace in which methods, variables, and imports (class names) live. This is package public because it is used in the implementation of some bsh commands. However for normal use you should be using methods on bsh.Interpreter to interact with your scrīpts.
A bsh.This object is a thin layer over a NameSpace that associates it with an Interpreter instance. Together they comprise a Bsh scrīpted object context.
一个放置方法,变量,和导入类名的名字空间。这是一个公共包,因为它通常用在一些bsh命令的执行上。但是对于通常的使用你必须使用bsh.Interpreter中的方法来执行你的脚本。
一个bsh.This对象是一个超过和Interpreter实例联系的NameSpace的薄层。
bsh.This对象和NameSpace合起来是一个bsh脚本对象上下文。
(理解:这句话的意思就是可以通过NameSpace取到脚本中的所有对象。)
3.目前已经实现了一个demo,就是已知软件系统的请求报文,通过Beanshell的脚本,得到应答报文:
3.1.条件:请求报文(XML),寄存器中的变量(以一个XML来模拟)。
3.2.实现思路:
3.2.1通过程序将请求报文和寄存器报文中的内容全部读出来。将内容全部放到以in_和reg_开头的变量中,同时将变量全部传入Interpreter。
3.2.2.通过Interpreter中提供的命令执行脚本。
3.2.3.通过NameSpace中提供的命令,就脚本中定义的变量的名字和值全部返回。同时写入XML文件中,也就是应答报文。

4.报文格式:
4.1.请求报文格式:
<?xml version="1.0" encoding="utf-8"?>
<REQ_HQXH>
  <requester_id>1</requester_id>
  <channel_id>10</channel_id>
  <branch_no>100080</branch_no>
  <jym>101100</jym>
  <khlx>0</khlx>
  <zh>955880010132429877</zh>
  <pzh>8001</pzh>
  <wdh>1008</wdh>
  <reqdate>20080703</reqdate>
  <reqtime>153000</reqtime>
</REQ_HQXH>
4.2.寄存器报文格式:
<?xml version="1.0" encoding="utf-8"?>
<register>
  <lsh>1234567</lsh>
  <ye>120000.3</ye>
  <lxjs>0.15</lxjs>
  <lv>0.15</lv>
  <sl>0.15</sl>
</register>
4.3.脚本格式:
4.3.1.公共脚本(函数脚本)。
toNum(String param){
  return Double.parseDouble(param);
}
sysdate(){
  Date date=new Date();
  return date.toLocaleString();
}
systime(){
  Date date=new Date();
  return String.valueOf(date.getTime());
}
double random(int length){    
  return Math.random();
}
4.3.2.业务脚本:
out_zh=in_zh;  
//应答报文中的帐号取输入报文中的帐号
out_lsh=reg_lsh;    
//应答报文中的流水号从寄存器中获取。
out_ye=reg_ye;    
//应答报文中的余额从寄存器中获取。
lxjs=reg_lxjs;          
//应答报文中的利息积数从寄存器中获取。
out_lxjs= lxjs;  
lx=toNum(reg_lv)*toNum(lxjs);  
//利息=利率?á利息积数。
out_lx=lx;
lxs=toNum(reg_sl)*lx;      
//利息税=税率?á利息
out_lxs=lxs;    
out_xhrq=sysdate();  //销户日期=系统日期。
out_askdate=sysdate();
out_asktime=systime();  //应答时间=系统时间      
out_zdh=random();  //终端号=随机数。
out_hh=in_zh.substring(0,5);  
//行号=输入报文中帐号的前五位。
if(toNum(reg_ye)>0)
  out_status="complete";
else
  out_status="fail";
4.4.生成的应答报文的格式:
<?xml version="1.0" encoding="utf-8"?>
<ASK_HQXH>
<zh>955880010132429877</zh>
<lxjs>0.15</lxjs>
<lx>0.0225</lx>
<zdh>0.5654736967697099</zdh>
<hh>95588</hh>
<status>complete</status>
<ye>120000.3</ye>
<xhrq>2008-7-9 13:11:49</xhrq>
<lxs>0.003375</lxs>
<asktime>1215580309281</asktime>
<askdate>2008-7-9 13:11:49</askdate>
<lsh>1234567</lsh>
</ASK_HQXH>

               回答这个技术有何作用----细细品味方知其中滋味20080709北京。


TAG: Beanshell

 

评分:0

我来说两句

日历

« 2024-05-13  
   1234
567891011
12131415161718
19202122232425
262728293031 

数据统计

  • 访问量: 26138
  • 日志数: 25
  • 建立时间: 2008-08-27
  • 更新时间: 2008-09-02

RSS订阅

Open Toolbar