org.apache.velocity.tools.generic

## Class ContextTool

- java.lang.Object
- - org.apache.velocity.tools.generic.SafeConfig
  - - org.apache.velocity.tools.generic.ContextTool

All Implemented Interfaces:
  Serializable

    @DefaultKey(value="context")
     @InvalidScope(value={"application","session"})
    public class ContextTool
    extends SafeConfig
    implements Serializable

Tool for convenient access to `Context` data and meta-data.

Template example(s):

      #foreach( $key in $context.keys )
        $key = $context.get($key)
      #end

     Toolbox configuration:
     <tools>
       <toolbox scope="request">
         <tool class="org.apache.velocity.tools.generic.ContextTool"/>
       </toolbox>
     </tools>

This class is only designed for use as a request-scope tool.

Since:
  VelocityTools 2.0
Version:
  $Id: ContextTool.java 385122 2006-03-11 18:37:42Z nbubna $
Author:
  Nathan Bubna
See Also:
  Serialized Form

### Field Summary

Fields
`protected org.apache.velocity.context.Context` `context`
`protected Map<String,Object>` `toolbox`

### Fields inherited from class org.apache.velocity.tools.generic.SafeConfig

`LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY`

### Constructor Summary

Constructors
Constructor and Description
`ContextTool()`

### Method Summary

All Methods Instance Methods Concrete Methods
`protected void` `configure(ValueParser parser)` Initializes this instance for the current request.
`boolean` `contains(Object refName)` Returns `true` if the context contains a value for the specified reference name (aka context key).
`protected void` `fillKeyset(Set keys)` Actually do the work of filling in the set of keys for `getKeys()` here so subclasses can add keys too.
`Object` `get(Object refName)` Retrieves the value for the specified reference name (aka context key).
`Set` `getKeys()` Return a `Set` of the available reference keys in the current context.
`org.apache.velocity.context.Context` `getThis()` Returns the context being analyzed by this tool.
`Map<String,Object>` `getToolbox()` Returns a read-only view of the toolbox `Map` for this context.
`Set` `getValues()` Return a `Set` of the available values in the current context.

### Methods inherited from class org.apache.velocity.tools.generic.SafeConfig

`configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

### Field Detail

#### context

    protected org.apache.velocity.context.Context context

#### toolbox

    protected Map<String,Object> toolbox

### Constructor Detail

#### ContextTool

    public ContextTool()

### Method Detail

#### configure

    protected void configure(ValueParser parser)

Initializes this instance for the current request. Also looks for a safe-mode configuration setting. By default, safeMode is true and thus keys with '.' in them are hidden.

Overrides:
  `configure` in class `SafeConfig`
Parameters:
  `parser` - configuration values

#### getThis

    public org.apache.velocity.context.Context getThis()

Returns the context being analyzed by this tool.

Returns:
  analyzed context

#### getToolbox

    public Map<String,Object> getToolbox()

Returns a read-only view of the toolbox `Map` for this context.

Returns:
  a map of all available tools for this request or `null` if such a map is not available

#### getKeys

    public Set getKeys()

Return a `Set` of the available reference keys in the current context.

Returns:
  keys set

#### fillKeyset

    protected void fillKeyset(Set keys)

Actually do the work of filling in the set of keys for `getKeys()` here so subclasses can add keys too.

Parameters:
  `keys` - set to fill with keys

#### getValues

    public Set getValues()

Return a `Set` of the available values in the current context.

Returns:
  values set

#### contains

    public boolean contains(Object refName)

Returns `true` if the context contains a value for the specified reference name (aka context key).

Parameters:
  `refName` - context key
Returns:
  `true` if key is present in the context

#### get

    public Object get(Object refName)

Retrieves the value for the specified reference name (aka context key).

Parameters:
  `refName` - context key
Returns:
  found value, or null