org.webmacro.engine
Class Variable

java.lang.Object
  |
  +--org.webmacro.engine.Variable
All Implemented Interfaces:
Macro, Visitable

public abstract class Variable
extends java.lang.Object
implements Macro, Visitable

A Variable is a reference into a Propertymap.

A variable name contains a list of names separated by dots, for example "$User.Identity.email.address" is the list: User, Identity, email, and address.

PLEASE NOTE: Case-sensitivity is enforced. "User" is the the same name as "user".

What that means: When a template is interpreted, it is interpreted in terms of data in a hashtable/map called the "context". This is actually a Map of type Map. The context contains all the local variables that have been set, as well as other information that Macros may use to evaluate the request.

Variable depends heavily on Property introspection: It is defined as a list of one or more names (separated by dots when written).

Those names are references to sub-objects within the context. The Variable instance, when interpreted, will decend through the context following fields, method references, or hash table look-ups based on its names.

For example, the variable "$User.Identity.email.address" implies that there is a "User" object under the Map--either it is a field within the map, or the map has a getUser() method, or the User can be obtained by calling Map.get("User").

The full expansion of $User.Identity.email.address might be:

 
    Map.get("User").getIdentity().get("email").address

 
. Variable (actually the Property class it uses) will figure out how to decend through the object like this until it finds the final reference--which is the "value" of the variable.

When searchin for subfields Variable prefers fields over getFoo() methods, and getFoo() over get("Foo").


Field Summary
protected  java.lang.Object[] _names
          The name as an array
protected  java.lang.String _vname
          The name of this variable.
static java.lang.Object LOCAL_TYPE
           
static java.lang.Object PROPERTY_TYPE
           
 
Method Summary
 void accept(TemplateVisitor v)
           
 java.lang.Object evaluate(Context context)
          Looks in the hashTable (context) for a value keyed to this variables name and returns the value string.
 java.lang.String getName()
          Like getPropertyNames, but only works if isSimpleName is true
 java.lang.String[] getPropertyNames()
           
abstract  java.lang.Object getValue(Context context)
          The code to get the value represented by the variable from the supplied context.
 java.lang.String getVariableName()
          Return the canonical name for this variable
 boolean isSimpleName()
          Returns true if the Variable describes a simple name (one with only one element)
abstract  void setValue(Context c, java.lang.Object v)
          The code to set the value represented by the variable in the supplied context.
abstract  java.lang.String toString()
          Return the String name of the variable prefixed with a string representing its type.
 void write(FastWriter out, Context context)
          Look in the hashtable (context) for a value keyed to this variables name and write its value to the stream.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

PROPERTY_TYPE

public static final java.lang.Object PROPERTY_TYPE

LOCAL_TYPE

public static final java.lang.Object LOCAL_TYPE

_vname

protected java.lang.String _vname
The name of this variable.

_names

protected java.lang.Object[] _names
The name as an array
Method Detail

getPropertyNames

public final java.lang.String[] getPropertyNames()

getName

public final java.lang.String getName()
Like getPropertyNames, but only works if isSimpleName is true

isSimpleName

public final boolean isSimpleName()
Returns true if the Variable describes a simple name (one with only one element)

evaluate

public final java.lang.Object evaluate(Context context)
                                throws PropertyException
Looks in the hashTable (context) for a value keyed to this variables name and returns the value string. If the resulting value is a Macro, recursively call its evaluate method.
Specified by:
evaluate in interface Macro
Returns:
String

write

public final void write(FastWriter out,
                        Context context)
                 throws PropertyException,
                        java.io.IOException
Look in the hashtable (context) for a value keyed to this variables name and write its value to the stream.
Specified by:
write in interface Macro
Throws:
PropertyException - is required data is missing
java.io.IOException - if could not write to output stream

getValue

public abstract java.lang.Object getValue(Context context)
                                   throws PropertyException
The code to get the value represented by the variable from the supplied context.

setValue

public abstract void setValue(Context c,
                              java.lang.Object v)
                       throws PropertyException
The code to set the value represented by the variable in the supplied context.

toString

public abstract java.lang.String toString()
Return the String name of the variable prefixed with a string representing its type. For example local:a.b.c
Overrides:
toString in class java.lang.Object

getVariableName

public java.lang.String getVariableName()
Return the canonical name for this variable

accept

public void accept(TemplateVisitor v)
Specified by:
accept in interface Visitable