public abstract class GenericInterpreter extends java.lang.Object implements Interpreter
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.
Modifier and Type | Field and Description |
---|---|
static java.lang.Object |
UNDEFINED
Used by
getFromNamespace(java.lang.String) to denote a variable is not defined. |
Modifier | Constructor and Description |
---|---|
protected |
GenericInterpreter() |
Modifier and Type | Method and Description |
---|---|
protected void |
afterExec()
Called after
exec(java.lang.String) , get(java.lang.String) and many others. |
protected void |
afterInterpret(Scope scope)
Called after
exec(java.lang.String) . |
protected void |
beforeExec()
Called before
exec(java.lang.String) , get(java.lang.String) and many others. |
protected void |
beforeInterpret(Scope scope)
Called before
exec(java.lang.String) . |
protected boolean |
contains(Scope scope,
java.lang.String name)
Tests whether a variable is defined in the interpreter's scope
associated with the specified scope.
|
protected boolean |
contains(java.lang.String name)
Tests whether a variable is defined in this interpreter.
|
boolean |
containsVariable(Scope scope,
java.lang.String name)
Tests whether the variable exists by using the specified name
as a reference to identify the interpreter's scope.
|
boolean |
containsVariable(java.lang.String name)
Tests whether the variable exists.
|
void |
destroy()
Reset the owner (
getOwner() ) to null. |
protected abstract void |
exec(java.lang.String script)
Executes the specified script.
|
protected java.lang.Object |
get(Scope scope,
java.lang.String name)
Gets the variable from the interpreter's scope associated with
the giving scope.
|
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.
|
protected Scope |
getCurrent()
Returns the current scope, or null if no scope is allowed.
|
protected java.lang.Object |
getFromNamespace(Scope scope,
java.lang.String name,
boolean recurse)
Returns the variable through the specified scopes and
variable resolvers, or
UNDEFINED if the variable is not
defined. |
protected java.lang.Object |
getFromNamespace(java.lang.String name)
Returns the variable through scopes and variable resolvers,
or
UNDEFINED if the variable not defined. |
Function |
getFunction(Scope scope,
java.lang.String name,
java.lang.Class[] argTypes)
Returns null since retrieving methods is not supported.
|
Function |
getFunction(java.lang.String name,
java.lang.Class[] argTypes)
Returns null since retrieving methods is not supported.
|
protected static java.lang.Object |
getImplicit(java.lang.String name)
Returns the value of the implicit variables.
|
java.lang.String |
getLanguage()
Returns the scripting language this interpreter is associated with.
|
Page |
getOwner()
Returns the owner of this interpreter.
|
java.lang.Object |
getVariable(Scope scope,
java.lang.String name)
Returns the value of a variable defined in this interpreter's
scope identified by the specified scope.
|
java.lang.Object |
getVariable(java.lang.String name)
Retrieve the variable.
|
void |
init(Page owner,
java.lang.String zslang)
Initializes the interpreter.
|
void |
interpret(java.lang.String script,
Scope scope)
Handles the scope and then invoke
exec(java.lang.String) . |
protected void |
set(Scope scope,
java.lang.String name,
java.lang.Object value)
Sets the variable to the interpreter's scope associated with the
giving scope.
|
protected void |
set(java.lang.String name,
java.lang.Object value)
Sets the variable to the interpreter.
|
void |
setVariable(Scope scope,
java.lang.String name,
java.lang.Object value)
Sets the value of a variable to this interpreter's scope
identified by the specified scope.
|
void |
setVariable(java.lang.String name,
java.lang.Object value)
Sets the variable to this interpreter.
|
protected void |
unset(Scope scope,
java.lang.String name)
Removes the variable from the interpreter.
|
protected void |
unset(java.lang.String name)
Removes the variable from the interpreter.
|
void |
unsetVariable(Scope scope,
java.lang.String name)
Removes the value of a variable defined in the interpreter's
scope identified by the specified scope.
|
void |
unsetVariable(java.lang.String name)
Removes the variable from this interpreter.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
getNativeInterpreter
public static final java.lang.Object UNDEFINED
getFromNamespace(java.lang.String)
to denote a variable is not defined.protected abstract void exec(java.lang.String script)
interpret(java.lang.String, org.zkoss.zk.ui.ext.Scope)
.protected boolean contains(java.lang.String name)
By default, it tests whether get(String)
returns non-null.
protected boolean contains(Scope scope, java.lang.String name)
By default, it tests whether get(Scope, String)
returns non-null.
protected java.lang.Object get(java.lang.String name)
beforeExec()
is called first, before this method is invoked.
An empty (and fake) scope is pushed so getFromNamespace(java.lang.String)
always returns null.
protected java.lang.Object get(Scope scope, java.lang.String name)
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.
protected void set(java.lang.String name, java.lang.Object value)
beforeExec()
is called first, before this method is invoked.
protected void set(Scope scope, java.lang.String name, java.lang.Object value)
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.
protected void unset(java.lang.String name)
beforeExec()
is called first, before this method is invoked.
protected void unset(Scope scope, java.lang.String name)
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.
protected void beforeInterpret(Scope scope)
exec(java.lang.String)
.
Default: call beforeExec()
and push the scope
as the active one.
protected void afterInterpret(Scope scope)
exec(java.lang.String)
.
Default: call afterExec()
.
protected void beforeExec()
protected void afterExec()
protected java.lang.Object getFromNamespace(java.lang.String name)
UNDEFINED
if 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 UNDEFINED
to denote undefined since 2.4.0,
while null is a valid value.
protected java.lang.Object getFromNamespace(Scope scope, java.lang.String name, boolean recurse)
UNDEFINED
if 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
).
scope
- the scope to search from (never null).
Note: if getCurrent()
returns null, this method simply returns
null (i.e., ignoring scope).recurse
- whether to look for the parent scope, if any.protected static java.lang.Object getImplicit(java.lang.String name)
getFromNamespace(java.lang.String)
, so you don't need to
invoke this method if you invoke getFromNamespace(java.lang.String)
.
However, you have to invoke this method as the last step, if you
implement your own getFromNamespace from scratch.public void init(Page owner, java.lang.String zslang)
Interpreter
init
in interface Interpreter
zslang
- the language this interpreter is associated withpublic void destroy()
getOwner()
) to null.destroy
in interface Interpreter
public Page getOwner()
Interpreter
getOwner
in interface Interpreter
public java.lang.String getLanguage()
Interpreter
getLanguage
in interface Interpreter
public void interpret(java.lang.String script, Scope scope)
exec(java.lang.String)
.
Don't override this method, rather, override exec(java.lang.String)
.
interpret
in interface Interpreter
scope
- the scope as the context to interpret the script.
If null, the current scope is assumed.
The current scope is Scopes.getCurrent(org.zkoss.zk.ui.Page)
, which
is the event target's scope, if the thread is processing an event.
The event target is Event.getTarget()
.
Otherwise, the current scope is the owner page (Interpreter.getOwner()
.public java.lang.Class getClass(java.lang.String clsnm)
getClass
in interface Interpreter
public Function getFunction(java.lang.String name, java.lang.Class[] argTypes)
getFunction
in interface Interpreter
argTypes
- the list of argument (a.k.a., parameter) types.
If null, Class[0] is assumed.public Function getFunction(Scope scope, java.lang.String name, java.lang.Class[] argTypes)
public boolean containsVariable(java.lang.String name)
Deriving class shall override contains(String)
, instead of this method.
containsVariable
in interface Interpreter
public boolean containsVariable(Scope scope, java.lang.String name)
Deriving class shall override contains(Scope,String)
, instead of this method.
public java.lang.Object getVariable(java.lang.String name)
Deriving class shall override get(String)
, instead of this method.
getVariable
in interface Interpreter
public java.lang.Object getVariable(Scope scope, java.lang.String name)
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.
public final void setVariable(java.lang.String name, java.lang.Object value)
Deriving class shall override set(String,Object)
, instead of this method.
setVariable
in interface Interpreter
public final void setVariable(Scope scope, java.lang.String name, java.lang.Object value)
Deriving class shall override set(Scope,String,Object)
,
instead of this method.
public final void unsetVariable(java.lang.String name)
Deriving class shall override unset(String)
, instead of this method.
unsetVariable
in interface Interpreter
public final void unsetVariable(Scope scope, java.lang.String name)
Deriving class shall override unset(Scope,String)
, instead of this method.
protected Scope getCurrent()
This method will handle Scopes.getCurrent(org.zkoss.zk.ui.Page)
automatically.
Copyright © 2005-2023 Potix Corporation. All Rights Reserved.