com.plotnix.jex
Class Context

java.lang.Object
  |
  +--com.plotnix.jex.Context

public class Context
extends java.lang.Object

Expressions are evaluated in a Context, which provides access to shared pieces of the environment: variables, locale, expression language compilers etc.

Version:
$Revision$ $Date$
Author:
Dmitri Plotnikov

Constructor Summary
Context()
          Creates a new Context
Context(Context parentContext)
          Creates a new Context with the given parent Context.
 
Method Summary
 void assign(java.lang.String expression, java.lang.Object value, java.lang.Object[] arguments)
          Invokes the context's interpreter to modify the value described by the supplied left-hand side expression.
 java.lang.Object evaluate(java.lang.String expression, java.lang.Object[] arguments)
          Invokes the context's interpreter to evaluate the supplied expression.
 java.lang.Object evaluate(java.lang.String expression, java.lang.Object[] arguments, java.lang.Class valueType)
          Evaluates the expression and casts the result to the supplied type.
 java.lang.Object findProperty(java.lang.String property)
          Returns the value of the specified property.
 Compiler getCompiler()
          Returns the compiler for the Context's expression language or a multilingual compiler if the Context is multilingual.
 java.lang.String getExpressionLanguage()
          Returns the name of this context's (default) expression language.
 Interpreter getInterpreter()
          Returns the interpreter for the Context's expression language or a multilingual interpreter if the Context is multilingual.
 java.util.Locale getLocale()
          Returns the locale set with setLocale or, if none was set parent context's locale or Locale.getDefault().
 Context getParentContext()
          Returns the Context's parent.
 java.lang.Object getProperty(java.lang.String property)
          Returns the value of the specified property.
 Variables getVariables()
          Returns the variable pool associated with the context.
 boolean isMultilingual()
          Returns true if the Context is multilingual.
 java.util.Iterator iterate(java.lang.String expression, java.lang.Object[] arguments)
          Invokes the context's interpreter to evaluate the supplied iteration expression.
 void setExpressionLanguage(java.lang.String language)
          Set the name of the expression language for this context.
 void setLocale(java.util.Locale locale)
          Set the locale for this context.
 void setMultilingual(boolean multilingual)
          If set to true, this context will support multiple expression languages.
 void setProperty(java.lang.String property, java.lang.Object value)
          Sets a configuration property that can be used by expression language implementations.
 void setVariables(Variables variables)
          Install a custom implementation of a Variable pool.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Context

public Context()
Creates a new Context

Context

public Context(Context parentContext)
Creates a new Context with the given parent Context. The child context will provide access to the parent's variable, extension functions etc. It can override and augment those. The use of nested contexts allows you to avoid re-configuring contexts all the time.
Parameters:
parentContext - is the parent context
Method Detail

getParentContext

public Context getParentContext()
Returns the Context's parent.
Returns:
The parent Context or null if there is no parent.

setExpressionLanguage

public void setExpressionLanguage(java.lang.String language)
Set the name of the expression language for this context. If the context is multilingual (see setMultilingual), this language will function as the default - expressions in this language will not require a "lang" prefix.
Parameters:
language - is the name of an expression language. The compiler for this language should be available.

getExpressionLanguage

public java.lang.String getExpressionLanguage()
Returns the name of this context's (default) expression language.
Returns:
the name of the language.

setMultilingual

public void setMultilingual(boolean multilingual)
If set to true, this context will support multiple expression languages. Expressions will start with the "lang:" prefix. If the default expression language has been specified, the expressions in that language do not require a language prefix.
Parameters:
multilingual - is the new setting

isMultilingual

public boolean isMultilingual()
Returns true if the Context is multilingual.
See Also:
setMultilingual(boolean)

getCompiler

public Compiler getCompiler()
Returns the compiler for the Context's expression language or a multilingual compiler if the Context is multilingual.

Will throw an exception if the Context is not multilingual and the expression language has not been specified.

Returns:
The compiler
See Also:
Compiler, MultilingualCompiler

getInterpreter

public Interpreter getInterpreter()
Returns the interpreter for the Context's expression language or a multilingual interpreter if the Context is multilingual.

Will throw an exception if the Context is not multilingual and the expression language has not been specified.

Returns:
The interpreter
See Also:
Interpreter, MultilingualInterpreter

setProperty

public void setProperty(java.lang.String property,
                        java.lang.Object value)
Sets a configuration property that can be used by expression language implementations. If the property is language-specific, the name of the property should start with "lang." by convention.
Parameters:
property - is the name of of the property
value - is the value of the property

getProperty

public java.lang.Object getProperty(java.lang.String property)
Returns the value of the specified property.
Parameters:
property - is a property name
Returns:
the value of the property

findProperty

public java.lang.Object findProperty(java.lang.String property)
Returns the value of the specified property. If the property is undefined on this context, tries to find it on the chain of parent contexts. Returns null if the property is undefined.
Parameters:
property - is a property name
Returns:
the value of the property

setVariables

public void setVariables(Variables variables)
Install a custom implementation of a Variable pool. Initially the context has the default variable pool, which is implemented with a simple HashMap.
Parameters:
variables - are custom Variables

getVariables

public Variables getVariables()
Returns the variable pool associated with the context. If no such pool was specified during the construction of the context, returns the default implementation of Variables which uses a HashMap.
Returns:
variables - default or custom

setLocale

public void setLocale(java.util.Locale locale)
Set the locale for this context. By default, Jex uses Locale.getDefault()
Parameters:
locale - is the locale this context is associated with

getLocale

public java.util.Locale getLocale()
Returns the locale set with setLocale or, if none was set parent context's locale or Locale.getDefault().
Returns:
The context's locale

evaluate

public java.lang.Object evaluate(java.lang.String expression,
                                 java.lang.Object[] arguments)
Invokes the context's interpreter to evaluate the supplied expression.
Parameters:
expression - is the expression that needs to be evaluated
arguments - are argument of the expression. Can be null if the expression does not take any arguments.
Returns:
the result of the evaluation of this expression
See Also:
getInterpreter()

evaluate

public java.lang.Object evaluate(java.lang.String expression,
                                 java.lang.Object[] arguments,
                                 java.lang.Class valueType)
Evaluates the expression and casts the result to the supplied type.
See Also:
evaluate(String, Object[])

iterate

public java.util.Iterator iterate(java.lang.String expression,
                                  java.lang.Object[] arguments)
Invokes the context's interpreter to evaluate the supplied iteration expression.
Parameters:
expression - is the expression that needs to be evaluated
arguments - are argument of the expression. Can be null if the expression does not take any arguments.
Returns:
the result of the evaluation of this expression
See Also:
getInterpreter()

assign

public void assign(java.lang.String expression,
                   java.lang.Object value,
                   java.lang.Object[] arguments)
Invokes the context's interpreter to modify the value described by the supplied left-hand side expression. Casts the type of the value if necessary.
Parameters:
expression - is the expression that needs to be evaluated
value - is the new value
arguments - are argument of the expression. Can be null if the expression does not take any arguments.
See Also:
getInterpreter()


Copyright (c) 2002 - Plotnix, Inc