Beanshell翻译12

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

Beanshell翻译12

http://user.qzone.qq.com/281696143/blog/1219126057  Ronger
1.Files and Directories 文件和目录
The following commands work with files, directories, and the working directory:
下面的命令和文件,目录,工作目录一起工作:
cd(), pwd(), dir(), rm(), mv(), cat()
Unix Style file commands.
Unix 样式文件命令。
pathToFile()
Translate a relative path
to an absolute path taking into account the BeanShell current working directory.
通过Beanshell当前的工作路径,将相对路径转变为绝对路径。
Desktop and Class Browser 桌面和类浏览器
The following commands work with GUI tools:
下面的命令是和GUI工具一起工作的:
classBrowser(), browseClass() Open a class browser window or browse a specific class or object.
打开一个类浏览器窗口,或者展示一个特殊类或对象。
desktop() Launch the BeanShell GUI desktop.
desktop() 弹出Beanshell的GUI桌面。
setNameCompletion() Turn on or off name completion in the GUI console.
在GUI控制台中,打开或者关闭名字。
Note: 注意
The dir() command is written in Java;
dir()命令在Java中被重写;
2.Adding BeanShell Commands 增加Beanshell命令
BeanShell Commands are scrīpted methods or compiled Java classes
which are dynamically loaded from the classpath to implement a method.
All of the standard commands we discuss in this manual live in the
BeanShell JAR file under the path /bsh/commands.
Beanshell命令是脚本函数或者编译的Java类,这个Java类是从类路径中动态加载的。
我们讨论的Beanshell的JAR文件下的所有标准命令,都在路径/bsh/commands下面。
Adding to the set of "prefab" commands supplied with BeanShell
is as easy as writing any other BeanShell methods.
You simply have to place your scrīpt into a file named with the same name as the command
and place the file in the classpath.
You may then "import" the commands with the importCommands() method.
在Beanshell中增加"prefab"命令的设置就像写一个其它的Beanshell函数一样简单。
你可以简单的将你的脚本放到,一个名字和这个命令一样名字的文件中,并且将这个文件放在类路径中。
你也可以通过importCommands()函数来导入命令。
Command files can be placed anywhere in the BeanShell classpath.
You can use even use the addClassPath() or setClassPath() commands
to add new command directories or JARs containing commands to your scrīpt at any time.
命令文件可以放在Beanshell类路径中的任何地方。
你甚至可以使用addClassPath()或者setClassPath()命令,
任何时间都可以增加一个新的命令目录或者包含命令的JARs,到你的脚本中。
Hello World
For example, let's make a helloWorld() command:
例如,让我们做一个helloWorld()命令:
// File: helloWorld.bsh
helloWorld() {
print("Hello World!");
}
Place the command file helloWorld.bsh in a directory or JAR in your classpath
and import it with the importCommands() command.
You can either set the classpath externally for Java or inside of BeanShell with
the addClassPath() command.
For example, suppose we have placed the file in the path:
/home/pat/mycommands/helloWorld.bsh. We could then do:
将这个命令文件helloWorld.bsh放在你的类路径的路径中,并且使用importCommands()命令导入它。
你不仅可以为Java设置第三方类路径,也可以使用addClassPath()命令进入Beanshell.
例如,假设我们已经将文件放在路径中:/home/pat/mycommands/helloWorld.bsh.我们可以这么做:
addClassPath("/home/pat"); // If it's not already in our classpath 如果它不总是在我们的类路径中
importCommands("/mycommands");
We can now use helloWorld() just like any other BeanShell command.
我们现在可以像其它的Beanshell命令一样使用helloWorld()。
helloWorld(); // prints "Hello World!" 打印"Hello World!"
importCommands() will accept either a "resource path" style path name or a Java package name.
Either one is simply converted to a resource path or Java package name as required
to load scrīpts or compiled BeanShell command classes.
A relative path (e.g. "mycommands") is turned into an absolute path by prepending "/".
You may import "loose" commands (like unpackaged classes)
at the top of the classpath by importing "/".
importCommands()将接受一个"resource path"式样的路径名字或者一个Java包名。
任一个都是简单的转变成一个源路径或者需要的Java包名,
去加载脚本或者编译Beanshell命令类。
一个相对的路径(比如"mycommands"),是通过一个预先的"/"来转变成一个绝对路径。
你可以通过在类路径的前面导入"/",导入"loose"命令(就像没有包装的类)。
If for example you have placed BeanShell commands
along with your other classes in a Java package called com.xyz.utils in your classpath,
you can import those commands with:
如果在例子中,你通过一个Java包来将Beanshell命令放到你的其它类中,
你可以像下面一样导入命令:
// equivalent 等价于
importCommands("com.xyz.utils");
importCommands("/com/xyz/utils");
Imported commands are scoped just like imported classes.
So if you import commands in a method or object they are local to that scope.
导入命令的有效性和导入的类一致。
所以如果你在一个方法或者对象中导入命令,他们都有一个本地的作用域。
3.Overloaded Commands 重新加载命令
BeanShell command scrīpts can contain any number of overloaded forms of the command method,e.g.:
Beansehll命令脚本包含一些重新加载样式的命令函数,例如:
// File: helloWorld.bsh 文件:helloWorld.bsh
helloWorld() {
print("Hello World!");
}
helloWorld( String msg ) {
print("Hello World: "+msg);
}
BeanShell will select the appropriate method based on the usual rules for methods selection.
Beanshell将选择可供专用的方法,是建立在方法选择的通用规则的基础上。
4.Compiled Commands 编译命令
You can also implement BeanShell commands as compiled classes instead of scrīpts if you wish.
Your class name must simply be the name of the command (matching case as well)
and it must implement one or more static invoke() methods who's signatures match a pattern.
The first two arguments of the invoke() method must be the bsh.Interpreter and bsh.CallStack objects
that provide context to all BeanShell scrīpts.
Then any number (possibly zero) of arguments,
which are the arguments of the command may follow.
BeanShell will select the appropriate method based on the usual rules for methods selection.
如果你想这样做的话,你也可以用编译好的类代替脚本来实现Beanshell命令。
你的类名必须简单的和命令的名字一样(最好是匹配的),
并且它必须实现一个或者多个静态invoke()方法,方法的签名匹配一个模式。
这个invoke()函数的前面两个参数必须为bsh.Interpreter和bsh.CallStack对象,
对象是为所有的Beanshell脚本提供环境。
那么一些数量的参数(有可能为0),这些参数是命令的参数。
Beanshell将选择可供专用的方法,是建立在方法选择的通用规则的基础上。
The dir() command is an example of a BeanShell command that is implemented in Java.
Let's look at a snippet from it
to see how it implements a pair of invoke() methods
for the dir() and dir(path) commands.
dir()命令是一个Beanshell命令的例子,这个命令是在Java中实现的。
我们来看看为了dir()和dir(path)命令儿做的片断,看看它是如何实现invoke()函数的。
/**
Implement dir() command. 实现dir()命令
*/
public static void invoke( Interpreter env, CallStack callstack )
{
String dir = ".";
invoke( env, callstack, dir );
}
/**
Implement dir( String directory ) command. 实现dir(String directory)命令。
*/
public static void invoke(
Interpreter env, CallStack callstack, String dir )
{
...
}
5.User Defined Commands with invoke() 用户通过invoke()定义命令
It is useful to note that the invoke() meta.method
which we described in the section "scrīpting Interfaces"
can be used directly in scope as well as through an object reference
and one could use this to load arbitrary commands or implement arbitrary behavīor
for commands (undefined method calls). For example:
它是有用并且需要注意的,我们在章节"scrīpting Interfaces"中提到的invoke()方法
meta.method可以直接用在一个对象引用的范围中,
并且可以使用这个来加载任意的命令或者为命令实现任意的行为(未定义的方法调用)。例如:
invoke( String methodName, Object [] arguments ) {
print("You invoked the method: "+ methodName );
}
// invoke() will be called to handle noSuchMethod()
invoke()将被调用去处理noSuchMethod()
noSuchMethod("foo");
invoke() is called to handle any method invocations for undefined methods within its scope.
In this case we have declared it at the global scope.
为未定义的函数,invoke()被调用处理一些函数。
在这个例子中,我们可以在全局的作用域中定义它。
6.Commands Scope 命令作用域
scrīpted BeanShell commands are loaded when no existing method matches the command name.
When a command scrīpt is loaded
it is sourced (evaluated) in the 'global' scope of the interpreter.
This means that once the command is loaded the methods declared in the command scrīpt
are then defined in the interpreter's global scope
and subsequent calls to the command
are simply handled by the those methods as any other scrīpted method.
当不存在方法匹配命令的名字的时候,脚本的Beanshell命令被加载。
当一个命令脚本被加载,它是解释器的全局作用域的源。
这个的意思就是,命令中加载定义在命令脚本中的函数,是定义在解释器的全局作用域中的。
并且后来对这个命令的调用,是简单的被其它的函数处理。
Note: 注意:
Note that this means that currently scrīpted commands may only be loaded once
and then they are effectively cached.
注意这个的意思就是,当前脚本命令会被马上加载,并且他们会马上被储藏起来。


TAG: Beanshell

 

评分:0

我来说两句

日历

« 2024-03-15  
     12
3456789
10111213141516
17181920212223
24252627282930
31      

数据统计

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

RSS订阅

Open Toolbar