org.zkoss.zk.scripting.bsh
Class BSHInterpreter

java.lang.Object
  extended by org.zkoss.zk.scripting.util.GenericInterpreter
      extended by org.zkoss.zk.scripting.bsh.BSHInterpreter
All Implemented Interfaces:
HierachicalAware, Interpreter, SerializableAware

public class BSHInterpreter
extends GenericInterpreter
implements SerializableAware, HierachicalAware

The interpreter that uses BeanShell to interpret zscript codes.

Unlike many other implementations, it supports the hierachical scopes (HierachicalAware). That is, it uses an independent BeanShell NameSpace (aka. interpreter's scope) to store the variables/classes/methods defined in BeanShell script for each ZK namespace (Namespace). Since one-to-one relationship between BeanShell's scope and ZK namespace, the invocation of BeanShell methods can execute correctly without knowing what namespace it is. However, if you want your codes portable across different interpreters, you had better to call Namespaces.beforeInterpret(java.util.Map, org.zkoss.zk.ui.Component, boolean) to prepare the proper namespace, before calling any method defined in zscript.

Author:
tomyeh

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.zkoss.zk.scripting.SerializableAware
SerializableAware.Filter
 
Field Summary
 
Fields inherited from class org.zkoss.zk.scripting.util.GenericInterpreter
UNDEFINED
 
Constructor Summary
BSHInterpreter()
           
 
Method Summary
protected  boolean contains(Namespace ns, java.lang.String name)
          Tests whether a variable is defined in the interpreter's scope associated with the specified namespace.
protected  boolean contains(java.lang.String name)
          Tests whether a variable is defined in this interpreter.
 void destroy()
          Reset the owner (GenericInterpreter.getOwner()) to null.
protected  void exec(java.lang.String script)
          Executes the specified script.
protected  java.lang.Object get(Namespace ns, java.lang.String name)
          Gets the variable from the interpreter's scope associated with the giving namespace.
protected  java.lang.Object get(java.lang.String name)
          Gets the variable from the interpreter.
 java.lang.Class getClass(java.lang.String clsnm)
          Returns null since retrieving class is not supported.
 Method getMethod(Namespace ns, java.lang.String name, java.lang.Class[] argTypes)
          Returns null since retrieving methods is not supported.
 Method getMethod(java.lang.String name, java.lang.Class[] argTypes)
          Returns null since retrieving methods is not supported.
 void init(Page owner, java.lang.String zslang)
          Initializes the interpreter.
 void read(java.io.ObjectInputStream s)
          Reads the name and value of the variable from the specified input stream.
protected  void set(Namespace ns, java.lang.String name, java.lang.Object val)
          Sets the variable to the interpreter's scope associated with the giving namespace.
protected  void set(java.lang.String name, java.lang.Object val)
          Sets the variable to the interpreter.
protected  void unset(Namespace ns, java.lang.String name)
          Removes the variable from the interpreter.
protected  void unset(java.lang.String name)
          Removes the variable from the interpreter.
 void write(java.io.ObjectOutputStream s, SerializableAware.Filter filter)
          Writes the name and value of the variables of this namespace to the specified stream.
 
Methods inherited from class org.zkoss.zk.scripting.util.GenericInterpreter
afterExec, afterInterpret, beforeExec, beforeInterpret, containsVariable, containsVariable, getCurrent, getFromNamespace, getFromNamespace, getLanguage, getOwner, getVariable, getVariable, interpret, setVariable, setVariable, unsetVariable, unsetVariable
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.zkoss.zk.scripting.HierachicalAware
containsVariable, getVariable, setVariable, unsetVariable
 

Constructor Detail

BSHInterpreter

public BSHInterpreter()
Method Detail

exec

protected void exec(java.lang.String script)
Description copied from class: GenericInterpreter
Executes the specified script. Deriving class shall provide an implementation of this method, rather than overriding GenericInterpreter.interpret(java.lang.String, org.zkoss.zk.scripting.Namespace).

Specified by:
exec in class GenericInterpreter

contains

protected boolean contains(java.lang.String name)
Description copied from class: GenericInterpreter
Tests whether a variable is defined in this interpreter. Optional. Implement it if the interpreter can tell the difference between null and undefined.

By default, it tests whether GenericInterpreter.get(String) returns non-null.

Overrides:
contains in class GenericInterpreter

get

protected java.lang.Object get(java.lang.String name)
Description copied from class: GenericInterpreter
Gets the variable from the interpreter. Optional. Implement it if you want to expose variables defined in the interpreter to Java codes.

GenericInterpreter.beforeExec() is called first, before this method is invoked.

An empty (and fake) namespace is pushed so GenericInterpreter.getFromNamespace(java.lang.String) always returns null.

Overrides:
get in class GenericInterpreter

set

protected void set(java.lang.String name,
                   java.lang.Object val)
Description copied from class: GenericInterpreter
Sets the variable to the interpreter. Optional. Implement it if you want to allow Java codes to define a variable in the interpreter.

GenericInterpreter.beforeExec() is called first, before this method is invoked.

Overrides:
set in class GenericInterpreter

unset

protected void unset(java.lang.String name)
Description copied from class: GenericInterpreter
Removes the variable from the interpreter. Optional. Implement it if you want to allow Java codes to undefine a variable from the interpreter.

GenericInterpreter.beforeExec() is called first, before this method is invoked.

Overrides:
unset in class GenericInterpreter

contains

protected boolean contains(Namespace ns,
                           java.lang.String name)
Description copied from class: GenericInterpreter
Tests whether a variable is defined in the interpreter's scope associated with the specified namespace. Optional. Implement it if the interpreter can tell the difference between null and undefined.

By default, it tests whether GenericInterpreter.get(Namespace, String) returns non-null.

Overrides:
contains in class GenericInterpreter

get

protected java.lang.Object get(Namespace ns,
                               java.lang.String name)
Description copied from class: GenericInterpreter
Gets the variable from the interpreter's scope associated with the giving namespace. Optional. Implement it if you want to expose variables defined in the interpreter to Java codes.

This method is implemented only if the interpreter that supports hierachical scopes (HierachicalAware).

Default: the same as GenericInterpreter.get(String).

GenericInterpreter.beforeExec() is called first, before this method is invoked.

An empty (and fake) namespace is pushed so GenericInterpreter.getFromNamespace(java.lang.String) always returns null.

Overrides:
get in class GenericInterpreter

set

protected void set(Namespace ns,
                   java.lang.String name,
                   java.lang.Object val)
Description copied from class: GenericInterpreter
Sets the variable to the interpreter's scope associated with the giving namespace. Optional. Implement it if you want to allow Java codes to define a variable in the interpreter.

This method is implemented only if the interpreter that supports hierachical scopes (HierachicalAware).

Default: the same as GenericInterpreter.set(String, Object).

GenericInterpreter.beforeExec() is called first, before this method is invoked.

Overrides:
set in class GenericInterpreter

unset

protected void unset(Namespace ns,
                     java.lang.String name)
Description copied from class: GenericInterpreter
Removes the variable from the interpreter. Optional. Implement it if you want to allow Java codes to undefine a variable from the interpreter.

This method is implemented only if the interpreter that supports hierachical scopes (HierachicalAware).

Default: the same as GenericInterpreter.unset(String).

GenericInterpreter.beforeExec() is called first, before this method is invoked.

Overrides:
unset in class GenericInterpreter

init

public void init(Page owner,
                 java.lang.String zslang)
Description copied from interface: Interpreter
Initializes the interpreter. It is called once when the new instance of interpreter is constructed.

Specified by:
init in interface Interpreter
Overrides:
init in class GenericInterpreter
zslang - the language this interpreter is associated with

destroy

public void destroy()
Description copied from class: GenericInterpreter
Reset the owner (GenericInterpreter.getOwner()) to null.

Specified by:
destroy in interface Interpreter
Overrides:
destroy in class GenericInterpreter

getClass

public java.lang.Class getClass(java.lang.String clsnm)
Description copied from class: GenericInterpreter
Returns null since retrieving class is not supported.

Specified by:
getClass in interface Interpreter
Overrides:
getClass in class GenericInterpreter

getMethod

public Method getMethod(java.lang.String name,
                        java.lang.Class[] argTypes)
Description copied from class: GenericInterpreter
Returns null since retrieving methods is not supported.

Specified by:
getMethod in interface Interpreter
Overrides:
getMethod in class GenericInterpreter
argTypes - the list of argument (aka., parameter) types. If null, Class[0] is assumed.

getMethod

public Method getMethod(Namespace ns,
                        java.lang.String name,
                        java.lang.Class[] argTypes)
Description copied from class: GenericInterpreter
Returns null since retrieving methods is not supported.

Specified by:
getMethod in interface HierachicalAware
Overrides:
getMethod in class GenericInterpreter
Parameters:
ns - the namespace used as a reference to identify the correct scope for searching the method. Note: this method doesn't look for any variable stored in ns.
argTypes - the list of argument (aka., parameter) types. If null, Class[0] is assumed.

write

public void write(java.io.ObjectOutputStream s,
                  SerializableAware.Filter filter)
           throws java.io.IOException
Description copied from interface: SerializableAware
Writes the name and value of the variables of this namespace to the specified stream.

If the variable's value is not serializable, it won't be written.

To read back, use SerializableAware.read(java.io.ObjectInputStream).

Specified by:
write in interface SerializableAware
Throws:
java.io.IOException

read

public void read(java.io.ObjectInputStream s)
          throws java.io.IOException,
                 java.lang.ClassNotFoundException
Description copied from interface: SerializableAware
Reads the name and value of the variable from the specified input stream.

Specified by:
read in interface SerializableAware
Throws:
java.io.IOException
java.lang.ClassNotFoundException
See Also:
SerializableAware.write(java.io.ObjectOutputStream, org.zkoss.zk.scripting.SerializableAware.Filter)


Copyright © 2005-2007 Potix Corporation. All Rights Reserved.