Class GenericInterpreter
- All Implemented Interfaces:
Interpreter
- Direct Known Subclasses:
BSHInterpreter,GroovyInterpreter,JythonInterpreter,RhinoInterpreter
Interpreter).
Derive classes usually override exec(java.lang.String) instead of interpret(java.lang.String, org.zkoss.zk.ui.ext.Scope);
In addition, don't override getVariable(java.lang.String),
setVariable(java.lang.String, java.lang.Object) and unsetVariable(java.lang.String).
Instead, override get(String), contains(String),
set(String,Object) and unset(String) instead.
If an interpreter doesn't support hierarchical scopes,
it can simply implement a global scope, and then use
getFromNamespace(java.lang.String) to
retrieve variables from ZK's hierarchical scopes.
If it supports hierarchical scopes
(example: BSHInterpreter), it
can maintain a one-to-one relationship among interpreter's scopes
and ZK's IdSpace. Thus, it can retrieve
the correct scope by giving ZK's IdSpace, and vice versa.
It also has to implement get(Scope,String), contains(Scope,String)
set(Scope,String,Object) and unset(Scope,String).
Whether to support hierarchical scopes is optional.
- Author:
- tomyeh
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final ObjectUsed bygetFromNamespace(java.lang.String)to denote a variable is not defined. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidCalled afterexec(java.lang.String),get(java.lang.String)and many others.protected voidafterInterpret(Scope scope) Called afterexec(java.lang.String).protected voidCalled beforeexec(java.lang.String),get(java.lang.String)and many others.protected voidbeforeInterpret(Scope scope) Called beforeexec(java.lang.String).protected booleanTests whether a variable is defined in this interpreter.protected booleanTests whether a variable is defined in the interpreter's scope associated with the specified scope.booleancontainsVariable(String name) Tests whether the variable exists.booleancontainsVariable(Scope scope, String name) Tests whether the variable exists by using the specified name as a reference to identify the interpreter's scope.voiddestroy()Reset the owner (getOwner()) to null.protected abstract voidExecutes the specified script.protected ObjectGets the variable from the interpreter.protected ObjectGets the variable from the interpreter's scope associated with the giving scope.Returns null since retrieving class is not supported.protected ScopeReturns the current scope, or null if no scope is allowed.protected ObjectgetFromNamespace(String name) Returns the variable through scopes and variable resolvers, orUNDEFINEDif the variable not defined.protected ObjectgetFromNamespace(Scope scope, String name, boolean recurse) Returns the variable through the specified scopes and variable resolvers, orUNDEFINEDif the variable is not defined.org.zkoss.xel.FunctiongetFunction(String name, Class[] argTypes) Returns null since retrieving methods is not supported.org.zkoss.xel.FunctiongetFunction(Scope scope, String name, Class[] argTypes) Returns null since retrieving methods is not supported.protected static ObjectgetImplicit(String name) Returns the value of the implicit variables.Returns the scripting language this interpreter is associated with.getOwner()Returns the owner of this interpreter.getVariable(String name) Retrieve the variable.getVariable(Scope scope, String name) Returns the value of a variable defined in this interpreter's scope identified by the specified scope.voidInitializes the interpreter.voidHandles the scope and then invokeexec(java.lang.String).protected voidSets the variable to the interpreter.protected voidSets the variable to the interpreter's scope associated with the giving scope.final voidsetVariable(String name, Object value) Sets the variable to this interpreter.final voidsetVariable(Scope scope, String name, Object value) Sets the value of a variable to this interpreter's scope identified by the specified scope.protected voidRemoves the variable from the interpreter.protected voidRemoves the variable from the interpreter.final voidunsetVariable(String name) Removes the variable from this interpreter.final voidunsetVariable(Scope scope, String name) Removes the value of a variable defined in the interpreter's scope identified by the specified scope.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.zkoss.zk.scripting.Interpreter
getNativeInterpreter
-
Field Details
-
UNDEFINED
Used bygetFromNamespace(java.lang.String)to denote a variable is not defined.- Since:
- 2.4.0
-
-
Constructor Details
-
GenericInterpreter
protected GenericInterpreter()
-
-
Method Details
-
exec
Executes the specified script. Deriving class shall provide an implementation of this method, rather than overridinginterpret(java.lang.String, org.zkoss.zk.ui.ext.Scope). -
contains
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.- Since:
- 2.4.0
-
contains
Tests whether a variable is defined in the interpreter's scope associated with the specified scope. Optional. Implement it if the interpreter can tell the difference between null and undefined.By default, it tests whether
get(Scope, String)returns non-null.- Since:
- 5.0.0
-
get
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) scope is pushed so
getFromNamespace(java.lang.String)always returns null. -
get
Gets the variable from the interpreter's scope associated with the giving scope. 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 hierarchical scopes (
HierarchicalAware).Default: the same as
get(String).beforeExec()is called first, before this method is invoked.An empty (and fake) scope is pushed so
getFromNamespace(java.lang.String)always returns null.- Since:
- 5.0.0
-
set
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. -
set
Sets the variable to the interpreter's scope associated with the giving scope. 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 hierarchical scopes (
HierarchicalAware).Default: the same as
set(String, Object).beforeExec()is called first, before this method is invoked.- Since:
- 5.0.0
-
unset
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. -
unset
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 hierarchical scopes (
HierarchicalAware).Default: the same as
unset(String).beforeExec()is called first, before this method is invoked.- Since:
- 5.0.0
-
beforeInterpret
Called beforeexec(java.lang.String).Default: call
beforeExec()and push the scope as the active one.- Since:
- 5.0.0
-
afterInterpret
Called afterexec(java.lang.String).Default: call
afterExec().- Since:
- 5.0.0
-
beforeExec
protected void beforeExec() -
afterExec
protected void afterExec() -
getFromNamespace
Returns the variable through scopes and variable resolvers, orUNDEFINEDif the variable not defined.It is usually called to search scopes and variable resolvers, when the real interpreter failed to find a variable in its own scope.
Note: We use
UNDEFINEDto denote undefined since 2.4.0, while null is a valid value. -
getFromNamespace
Returns the variable through the specified scopes and variable resolvers, orUNDEFINEDif the variable is not defined.It is usually called to search scopes 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 hierarchical scopes (
HierarchicalAware).- Parameters:
scope- the scope to search from (never null). Note: ifgetCurrent()returns null, this method simply returns null (i.e., ignoring scope).recurse- whether to look for the parent scope, if any.- Since:
- 5.0.0
-
getImplicit
Returns the value of the implicit variables. It is called bygetFromNamespace(java.lang.String), so you don't need to invoke this method if you invokegetFromNamespace(java.lang.String). However, you have to invoke this method as the last step, if you implement your own getFromNamespace from scratch.- Since:
- 3.6.0
-
init
Description copied from interface:InterpreterInitializes the interpreter. It is called once when the new instance of interpreter is constructed.- Specified by:
initin interfaceInterpreterzslang- the language this interpreter is associated with
-
destroy
public void destroy()Reset the owner (getOwner()) to null.- Specified by:
destroyin interfaceInterpreter
-
getOwner
Description copied from interface:InterpreterReturns the owner of this interpreter.- Specified by:
getOwnerin interfaceInterpreter
-
getLanguage
Description copied from interface:InterpreterReturns the scripting language this interpreter is associated with.- Specified by:
getLanguagein interfaceInterpreter
-
interpret
Handles the scope and then invokeexec(java.lang.String).Don't override this method, rather, override
exec(java.lang.String).- Specified by:
interpretin interfaceInterpreterscope- the scope as the context to interpret the script. If null, the current scope is assumed. The current scope isScopes.getCurrent(org.zkoss.zk.ui.Page), which is the event target's scope, if the thread is processing an event. The event target isEvent.getTarget(). Otherwise, the current scope is the owner page (Interpreter.getOwner().- Since:
- 5.0.0
-
getClass
Returns null since retrieving class is not supported.- Specified by:
getClassin interfaceInterpreter
-
getFunction
Returns null since retrieving methods is not supported.- Specified by:
getFunctionin interfaceInterpreterargTypes- the list of argument (a.k.a., parameter) types. If null, Class[0] is assumed.- Since:
- 3.0.0
-
getFunction
Returns null since retrieving methods is not supported.- Since:
- 5.0.0
-
containsVariable
Tests whether the variable exists.Deriving class shall override
contains(String), instead of this method.- Specified by:
containsVariablein interfaceInterpreter- Since:
- 2.4.0
-
containsVariable
Tests whether the variable exists by using the specified name as a reference to identify the interpreter's scope.Deriving class shall override
contains(Scope,String), instead of this method.- Since:
- 5.0.0
-
getVariable
Retrieve the variable.Deriving class shall override
get(String), instead of this method.- Specified by:
getVariablein interfaceInterpreter
-
getVariable
Returns the value of a variable defined in this interpreter's scope identified by the specified scope. Note: it doesn't search the specified scope (Scope).Deriving class shall override
get(Scope,String), instead of this method.This method is part of
HierarchicalAware. It is defined here to simplify the implementation of the deriving classes, if they support the hierarchical scopes.- Since:
- 5.0.0
-
setVariable
Sets the variable to this interpreter.Deriving class shall override
set(String,Object), instead of this method.- Specified by:
setVariablein interfaceInterpreter
-
setVariable
Sets the value of a variable to this interpreter's scope identified by the specified scope.Deriving class shall override
set(Scope,String,Object), instead of this method.- Since:
- 5.0.0
-
unsetVariable
Removes the variable from this interpreter.Deriving class shall override
unset(String), instead of this method.- Specified by:
unsetVariablein interfaceInterpreter
-
unsetVariable
Removes the value of a variable defined in the interpreter's scope identified by the specified scope.Deriving class shall override
unset(Scope,String), instead of this method.- Since:
- 5.0.0
-
getCurrent
Returns the current scope, or null if no scope is allowed. Note: if this method returns null, it means the interpreter shall not search variables defined in ZK scope.This method will handle
Scopes.getCurrent(org.zkoss.zk.ui.Page)automatically.- Since:
- 5.0.0
-