/**
 * Formic installer framework.
 * Copyright (C) 2005 - 2008  Eric Van Dewoestine
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.formic;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import org.apache.tools.ant.Project;

/**
 * Context that contains the current state of the install process including any
 * user inputed values.
 *
 * @author Eric Van Dewoestine
 */
public class InstallContext
{
  private Map values = new HashMap();

  /**
   * Determines if this context contains the supplied key.
   *
   * @param key The key.
   * @return true if contains the key, false otherwise.
   */
  public boolean containsKey(Object key)
  {
    return values.containsKey(key);
  }

  /**
   * Gets the value stored under the supplied key.
   *
   * @param key The key the value is stored under.
   * @return The value or null if not found.
   */
  public Object getValue(Object key)
  {
    return values.get(key);
  }

  /**
   * Gets all keys that have the specified prefix.
   *
   * @param keyPrefix The prefix.
   * @return array of keys.
   */
  public String[] getKeysByPrefix(String keyPrefix)
  {
    return getKeysByPrefix(keyPrefix, false);
  }

  /**
   * Gets all keys that have the specified prefix.
   *
   * @param keyPrefix The prefix.
   * @return array of keys.
   */
  public String[] getKeysByPrefix(String keyPrefix, boolean sort)
  {
    ArrayList results = new ArrayList();
    for (Iterator ii = values.keySet().iterator(); ii.hasNext();){
      String key = (String)ii.next();
      if(key.startsWith(keyPrefix)){
        results.add(key);
      }
    }

    if (sort){
      Collections.sort(results);
    }

    return (String[])results.toArray(new String[results.size()]);
  }

  /**
   * Stores a value using the supplied key.
   *
   * @param key The key to store the value under.
   * @param value The value to store.
   */
  public void setValue(Object key, Object value)
  {
    values.put(key, value);
    Installer.getProject().setProperty(
        key.toString(), value != null ? value.toString() : (String)value);
  }

  /**
   * Removes the value stored at the supplied key.
   *
   * @param key The key.
   * @return The value removed or null if none.
   */
  public Object removeValue(Object key)
  {
    return values.remove(key);
  }

  /**
   * Gets Iterator to iterate over all available keys.
   *
   * @return Iterator of available keys.
   */
  public Iterator keys()
  {
    return values.keySet().iterator();
  }

  /**
   * Gets the current ant project.
   *
   * @return The ant project.
   */
  public Project getProject()
  {
    return Installer.getProject();
  }
}

@@ -58,10 +58,10 @@
 - 20
+1
 0
-8
   Er
@@ -926,16 +926,44 @@
 ashMap;%0A
+import java.util.Hashtable;%0A
 import j
@@ -1723,16 +1723,88 @@
  values.
+containsKey(key) ?%0A      values.get(key) : getProject().getProperties().
 get(key)
@@ -1876,36 +1876,33 @@
   *%0A   * @param 
-keyP
+p
 refix The prefix
@@ -1974,28 +1974,25 @@
 efix(String 
-keyP
+p
 refix)%0A  %7B%0A 
@@ -2017,20 +2017,17 @@
 yPrefix(
-keyP
+p
 refix, f
@@ -2112,20 +2112,17 @@
  @param 
-keyP
+p
 refix Th
@@ -2206,20 +2206,17 @@
 (String 
-keyP
+p
 refix, b
@@ -2406,17 +2406,281 @@
 ith(
-keyPrefix
+prefix))%7B%0A        results.add(key);%0A      %7D%0A    %7D%0A%0A    Hashtable properties = getProject().getProperties();%0A    for (Iterator ii = properties.keySet().iterator(); ii.hasNext();)%7B%0A      String key = (String)ii.next();%0A      if(key.startsWith(prefix) && !results.contains(key
 ))%7B%0A

allow getValue + getKeysByPrefix to expose ant properties as well