/*
 * The contents of this file are subject to the Mozilla Public License Version 1.1
 * (the "License"); you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at <http://www.mozilla.org/MPL/>.
 * 
 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
 * language governing rights and limitations under the License.
 * 
 * The Original Code is the Venice Web Communities System.
 * 
 * The Initial Developer of the Original Code is Eric J. Bowersox <erbo@ricochet.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2004 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.velocity;

import java.util.*;
import org.apache.velocity.context.Context;

/**
 * An Adapter which allows a {@link com.silverwrist.venice.ui.velocity.VelocitySupplyParams VelocitySupplyParams}
 * to supply rendering parameters to Velocity as a standard <code>Context</code> object.
 */
class ContextWrapper implements Context
{
  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private VelocitySupplyParams m_vr;     // the parameter supplier we're adapting
  private Object[] m_arr = null;         // cache of the parameter names array

  /*--------------------------------------------------------------------------------
   * Constructor
   *--------------------------------------------------------------------------------
   */

  /**
   * Creates a new <code>ContextWrapper</code>.
   *
   * @param vr The {@link com.silverwrist.venice.ui.velocity.VelocitySupplyParams VelocitySupplyParams} to be
   *           wrapped by this object.
   */
  ContextWrapper(VelocitySupplyParams vr)
  {
    m_vr = vr;

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface Context
   *--------------------------------------------------------------------------------
   */

  /**
   * Adds a name/value pair to the context.  For this class, this is a no-op.
   *
   * @param key The name to key the provided value with.
   * @param value The corresponding value.
   * @return The previous value set for that key, or <code>null/code>.
   */
  public Object put(String key, Object value)
  {
    return null; // dummy method

  } // end put

  /**
   * Gets the value corresponding to the provided key from the context.
   *
   * @param key The name of the desired value.
   * @return The value corresponding to the provided key.
   */
  public Object get(String key)
  {
    if (m_vr==null)
      return null;
    return m_vr.getParameter(key);

  } // end get

  /**
   * Indicates whether the specified key is in the context.
   *
   * @param key The key to look for.
   * @return <code>true</code> if the key is in the context, <code>false</code> if not.
   */
  public boolean containsKey(Object key)
  {
    if (m_vr==null)
      return false;
    return (m_vr.getParameter(key.toString())!=null);

  } // end containsKey

  /**
   * Get all the keys for the values in the context.
   *
   * @return An array containing all the keys from this context.
   */
  public Object[] getKeys()
  {
    if (m_arr==null)
    { // fill in array
      if (m_vr==null)
	m_arr = new Object[0];
      else
	m_arr = m_vr.getParameterNames().toArray();

    } // end if

    return m_arr;

  } // end getKeys
  
  /**
   * Removes the value associated with the specified key from the context.  For this class, this is a no-op.
   *
   * @param key The name of the value to remove.
   * @return The value that the key was mapped to, or <code>null</code> if unmapped.
   */
  public Object remove(Object key)
  {
    return null;  // dummy method

  } // end remove

} // end class ContextWrapper
