Save This Page
Home » zk-src-3.0.6 » org.zkoss.zk.scripting.util » [javadoc | source]
org.zkoss.zk.scripting.util
abstract public class: GenericInterpreter [javadoc | source]
java.lang.Object
   org.zkoss.zk.scripting.util.GenericInterpreter

All Implemented Interfaces:
    Interpreter

Direct Known Subclasses:
    JythonInterpreter, RhinoInterpreter, JRubyInterpreter, GroovyInterpreter, BSHInterpreter

A skeletal class for implementing a interpreter (Interpreter ) that support Namespace .

Derive classes usually override #exec instead of #interpret ; In addition, don't override #getVariable , #setVariable and #unsetVariable . Instead, override #get(String) , #contains(String) , #set(String,Object) and #unset(String) instead.

If an interpreter doesn't support hierachical scopes, it can simply implement a global scope, and then use #getFromNamespace to retrieve variables from ZK's hierachical namespaces.

If it supports hierachical scopes (example: org.zkoss.zk.scripting.bsh.BSHInterpreter ), it can maintain a one-to-one relationship among interpreter's scopes and ZK's Namespace . Thus, it can retrieve the correct scope by giving ZK's Namespace , and vice versa. It also has to implement #get(Namespace,String) , #contains(Namespace,String) #set(Namespace,String,Object) and #unset(Namespace,String) .

Whether to support hierachical namespaces is optional.

Field Summary
public static final  Object UNDEFINED    Used by #getFromNamespace to denote a variable is not defined.
    since: 2.4.0 -
 
Constructor:
 protected GenericInterpreter() 
Method from org.zkoss.zk.scripting.util.GenericInterpreter Summary:
afterExec,   afterInterpret,   beforeExec,   beforeInterpret,   contains,   contains,   containsVariable,   containsVariable,   destroy,   exec,   get,   get,   getClass,   getCurrent,   getFromNamespace,   getFromNamespace,   getFunction,   getFunction,   getLanguage,   getOwner,   getVariable,   getVariable,   init,   interpret,   set,   set,   setVariable,   setVariable,   unset,   unset,   unsetVariable,   unsetVariable
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.zkoss.zk.scripting.util.GenericInterpreter Detail:
 protected  void afterExec() 
    Called after #exec , #get and many others.

    Default: does nothing.

 protected  void afterInterpret(Namespace ns) 
 protected  void beforeExec() 
    Called before #exec , #get and many others.

    Default: does nothing.

 protected  void beforeInterpret(Namespace ns) 
    Called before #exec .

    Default: call #beforeExec and push the namespace as the active one.

 protected boolean contains(String name) 
    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 #get(String) returns non-null.

 protected boolean contains(Namespace ns,
    String name) 
    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 #get(Namespace, String) returns non-null.

 public boolean containsVariable(String name) 
    Tests whether the variable exists.

    Deriving class shall override #contains(String) , instead of this method.

 public boolean containsVariable(Namespace ns,
    String name) 
    Tests whether the variable exists by using the specified name as a reference to identify the interpreter's scope.

    Deriving class shall override #contains(Namespace,String) , instead of this method.

 public  void destroy() 
 abstract protected  void exec(String script)
    Executes the specified script. Deriving class shall provide an implementation of this method, rather than overriding #interpret .
 protected Object get(String name) 
    Gets the variable from the interpreter. Optional. Implement it if you want to expose variables defined in the interpreter to Java codes.

    #beforeExec is called first, before this method is invoked.

    An empty (and fake) namespace is pushed so #getFromNamespace always returns null.

 protected Object get(Namespace ns,
    String name) 
    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 (org.zkoss.zk.scripting.HierachicalAware ).

    Default: the same as #get(String) .

    #beforeExec is called first, before this method is invoked.

    An empty (and fake) namespace is pushed so #getFromNamespace always returns null.

 public Class getClass(String clsnm) 
    Returns null since retrieving class is not supported.
 protected Namespace getCurrent() 
    Returns the current namespace, or null if no namespace is allowed. Note: if this method returns null, it means the interpreter shall not search variables defined in ZK namespace.

    This method will handle Namespaces#getCurrent automatically.

 protected Object getFromNamespace(String name) 
    Returns the variable through namespaces and variable resolvers, or #UNDEFINED if the variable not defined.

    It is usually called to search namespaces and variable resolvers, when the real interpreter failed to find a variable in its own scope.

    Note: We use #UNDEFINED to denote undefined since 2.4.0, while null is a valid value.

 protected Object getFromNamespace(Namespace ns,
    String name) 
    Returns the variable through the specified namespaces and variable resolvers, or #UNDEFINED if the variable is not defined.

    It is usually called to search namespaces and variable resolvers, when the real interpreter failed to find a variable in its own scope.

    This method is used with the interpreter that supports hierachical scopes (org.zkoss.zk.scripting.HierachicalAware ).

 public Function getFunction(String name,
    Class[] argTypes) 
    Returns null since retrieving methods is not supported.
 public Function getFunction(Namespace ns,
    String name,
    Class[] argTypes) 
    Returns null since retrieving methods is not supported.
 public String getLanguage() 
 public Page getOwner() 
 public Object getVariable(String name) 
    Retrieve the variable.

    Deriving class shall override #get(String) , instead of this method.

 public Object getVariable(Namespace ns,
    String name) 
    Returns the value of a variable defined in this interpreter's scope identified by the specified namespace. Note: it doesn't search the specified namespace (Namespace ).

    Deriving class shall override #get(Namespace,String) , instead of this method.

    This method is part of org.zkoss.zk.scripting.HierachicalAware . It is defined here to simplify the implementation of the deriving classes, if they support the hierachical scopes.

 public  void init(Page owner,
    String zslang) 
 public  void interpret(String script,
    Namespace ns) 
    Handles the namespace and then invoke #exec .

    Don't override this method, rather, override #exec .

 protected  void set(String name,
    Object value) 
    Sets the variable to the interpreter. Optional. Implement it if you want to allow Java codes to define a variable in the interpreter.

    #beforeExec is called first, before this method is invoked.

 protected  void set(Namespace ns,
    String name,
    Object value) 
    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 (org.zkoss.zk.scripting.HierachicalAware ).

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

    #beforeExec is called first, before this method is invoked.

 public final  void setVariable(String name,
    Object value) 
    Sets the variable to this interpreter.

    Deriving class shall override #set(String,Object) , instead of this method.

 public final  void setVariable(Namespace ns,
    String name,
    Object value) 
    Sets the value of a variable to this interpreter's scope identified by the specified namespace.

    Deriving class shall override #set(Namespace,String,Object) , instead of this method.

 protected  void unset(String name) 
    Removes the variable from the interpreter. Optional. Implement it if you want to allow Java codes to undefine a variable from the interpreter.

    #beforeExec is called first, before this method is invoked.

 protected  void unset(Namespace ns,
    String name) 
    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 (org.zkoss.zk.scripting.HierachicalAware ).

    Default: the same as #unset(String) .

    #beforeExec is called first, before this method is invoked.

 public final  void unsetVariable(String name) 
    Removes the variable from this interpreter.

    Deriving class shall override #unset(String) , instead of this method.

 public final  void unsetVariable(Namespace ns,
    String name) 
    Removes the value of a variable defined in the interpreter's scope identified by the specified namespace.

    Deriving class shall override #unset(Namespace,String) , instead of this method.