com.google.clearsilver.jsilver.data
Class DefaultDataContext

java.lang.Object
  extended by com.google.clearsilver.jsilver.data.DefaultDataContext
All Implemented Interfaces:
DataContext

public class DefaultDataContext
extends Object
implements DataContext

This is the basic implementation of the DataContext. It stores the root Data node and a stack of Data objects that hold local variables. By definition, local variables are limited to single HDF names, with no '.' allowed. We use this to limit our search in the stack for the first occurence of the first chunk in the variable name.


Constructor Summary
DefaultDataContext(Data data)
           
 
Method Summary
 void createLocalVariableByPath(String name, String path)
          Creates a local variable that references a (possibly non-existent) Data node.
 void createLocalVariableByValue(String name, String value)
          Creates and sets a local variable in the current scope equal to the given value.
 void createLocalVariableByValue(String name, String value, boolean isFirst, boolean isLast)
          Creates and sets a local variable in the current scope equal to the given value.
 void createLocalVariableByValue(String name, String value, EscapeMode mode)
          Creates and sets a local variable in the current scope equal to the given value.
 Data findVariable(String name, boolean create)
          Searches the variable map stack for the specified variable name.
 EscapeMode findVariableEscapeMode(String name)
          Searches the variable map stack for the specified variable name, and returns its EscapeMode.
 Data getRootData()
          Returns the main Data object this RenderingContext was defined with.
 void popVariableScope()
          Removes the current variable scope and references to the variables in it.
 void pushVariableScope()
          Starts a new variable scope.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

DefaultDataContext

public DefaultDataContext(Data data)
Method Detail

getRootData

public Data getRootData()
Description copied from interface: DataContext
Returns the main Data object this RenderingContext was defined with.

Specified by:
getRootData in interface DataContext

pushVariableScope

public void pushVariableScope()
Starts a new variable scope. It is illegal to call this twice in a row without declaring a local variable.

Specified by:
pushVariableScope in interface DataContext

popVariableScope

public void popVariableScope()
Removes the current variable scope and references to the variables in it. It is illegal to call this more times than pushVariableScope() has been called.

Specified by:
popVariableScope in interface DataContext

createLocalVariableByValue

public void createLocalVariableByValue(String name,
                                       String value)
Description copied from interface: DataContext
Creates and sets a local variable in the current scope equal to the given value.

Specified by:
createLocalVariableByValue in interface DataContext
Parameters:
name - the name of the local variable to fetch or create.
value - The String value to store at the local variable.

createLocalVariableByValue

public void createLocalVariableByValue(String name,
                                       String value,
                                       EscapeMode mode)
Description copied from interface: DataContext
Creates and sets a local variable in the current scope equal to the given value. Also set the EscapeMode that was applied to its value. This may be used by the template renderer to decide whether to autoescape the variable.

Specified by:
createLocalVariableByValue in interface DataContext
Parameters:
name - the name of the local variable to fetch or create.
value - The String value to store at the local variable.
mode - EscapeMode that describes the escaping this variable has. EscapeMode.ESCAPE_NONE if the variable was not escaped. EscapeMode.ESCAPE_IS_CONSTANT if the variable was populated with a string or numeric literal.

createLocalVariableByValue

public void createLocalVariableByValue(String name,
                                       String value,
                                       boolean isFirst,
                                       boolean isLast)
Description copied from interface: DataContext
Creates and sets a local variable in the current scope equal to the given value.

This method is a helper method for supporting the first() and last() functions on loops without requiring loops create a full Data tree.

Specified by:
createLocalVariableByValue in interface DataContext
Parameters:
name - the name of the local variable to fetch or create.
value - The String value to store at the local variable.
isFirst - What the local variable should return for Data.isFirstSibling()
isLast - What the local variable should return for Data.isLastSibling()

createLocalVariableByPath

public void createLocalVariableByPath(String name,
                                      String path)
Description copied from interface: DataContext
Creates a local variable that references a (possibly non-existent) Data node. When the Data object for this variable is requested, it will return the Data object at the path location or null, if it does not exist. If DataContext.findVariable(java.lang.String, boolean) is called with create == true, then if no Data object exists at the path location, it will be created.

Specified by:
createLocalVariableByPath in interface DataContext
Parameters:
name - the name of the local variable to fetch or create.
path - The path to the Data object

findVariable

public Data findVariable(String name,
                         boolean create)
Description copied from interface: DataContext
Searches the variable map stack for the specified variable name. If not found, it then searches the root Data object. If not found then and create is true, then a new node is created with the given name in the root Data object and that node is returned.

Note: This only creates nodes in the root Data object, not in any local variable map. To do that, use the Data node returned by DataContext.pushVariableScope().

Specified by:
findVariable in interface DataContext
Parameters:
name - the name of the variable to find and/or create.
create - if true then a new node will be created if an existing Data node with the given name does not exist.
Returns:
The first Data node in the variable map stack that matches the given name, or a Data node in the root Data object matching the given name, or null if no such node exists and create is false.

findVariableEscapeMode

public EscapeMode findVariableEscapeMode(String name)
Description copied from interface: DataContext
Searches the variable map stack for the specified variable name, and returns its EscapeMode.

Specified by:
findVariableEscapeMode in interface DataContext
Returns:
If the variable is found, returns its EscapeMode, otherwise returns EscapeMode.ESCAPE_NONE.


Copyright © 2010-2012 Google. All Rights Reserved.