/*
 * 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@silcom.com>,
 * for Silverwrist Design Studios.  Portions created by Eric J. Bowersox are
 * Copyright (C) 2002 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.dynamo.servlet;

import java.util.*;
import com.silverwrist.util.*;
import com.silverwrist.dynamo.BrowserFlag;
import com.silverwrist.dynamo.iface.*;

class ServletBrowserData implements BrowserData
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static final Map KEY_TO_NAME;
  private static final Map FLAG_TO_NAME;

  /*--------------------------------------------------------------------------------
   * Attributes
   *--------------------------------------------------------------------------------
   */

  private String m_agent;
  private Map m_attributes;
  private Set m_flags;

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

  ServletBrowserData(String agent, BrowserDatabaseEntry dbentry)
  {
    m_agent = agent;

    HashMap tmp = new HashMap();
    Iterator it = KEY_TO_NAME.keySet().iterator();
    while (it.hasNext())
    { // copy key values from BrowserDatabaseEntry to here
      String k = (String)(it.next());
      tmp.put(k,dbentry.getValue((String)(KEY_TO_NAME.get(k))));

    } // end while

    m_attributes = Collections.unmodifiableMap(tmp);

    HashSet tmp2 = new HashSet();
    it = FLAG_TO_NAME.keySet().iterator();
    while (it.hasNext())
    { // construct the flag set
      BrowserFlag flag = (BrowserFlag)(it.next());
      String bv = (String)(FLAG_TO_NAME.get(flag));
      if (StringUtils.isBooleanTrue(bv))
	tmp2.add(flag);

    } // end while

    m_flags = Collections.unmodifiableSet(tmp2);

  } // end constructor

  /*--------------------------------------------------------------------------------
   * Implementations from interface BrowserData
   *--------------------------------------------------------------------------------
   */

  public String getUserAgent()
  {
    return m_agent;

  } // end getUserAgent

  public String getBrowserAttribute(String key)
  {
    return (String)(m_attributes.get(key));

  } // end getBrowserAttribute

  public Set getBrowserFlags()
  {
    return m_flags;

  } // end getBrowserFlags

  /*--------------------------------------------------------------------------------
   * Static initializer
   *--------------------------------------------------------------------------------
   */

  static
  { // create the attribute key to name map
    HashMap tmp = new HashMap();
    tmp.put(KEY_BROWSER_NAME,"browser");
    tmp.put(KEY_VERSION,"version");
    tmp.put(KEY_MAJOR_VERSION,"majorver");
    tmp.put(KEY_MINOR_VERSION,"minorver");
    tmp.put(KEY_PLATFORM,"platform");
    tmp.put(KEY_CLR_VERSION,"netclr");
    KEY_TO_NAME = Collections.unmodifiableMap(tmp);

    // create the BrowserFlag to name map
    tmp = new HashMap();
    tmp.put(BrowserFlag.FRAMES,"frames");
    tmp.put(BrowserFlag.IFRAMES,"iframes");
    tmp.put(BrowserFlag.TABLES,"tables");
    tmp.put(BrowserFlag.COOKIES,"cookies");
    tmp.put(BrowserFlag.BACKGROUND_SOUNDS,"backgroundsounds");
    tmp.put(BrowserFlag.VBSCRIPT,"vbscript");
    tmp.put(BrowserFlag.JAVASCRIPT,"javascript");
    tmp.put(BrowserFlag.JAVA_APPLETS,"javaapplets");
    tmp.put(BrowserFlag.ACTIVEX_CONTROLS,"activexcontrols");
    tmp.put(BrowserFlag.CDF,"cdf");
    tmp.put(BrowserFlag.AOL,"aol");
    tmp.put(BrowserFlag.BETA,"beta");
    tmp.put(BrowserFlag.WIN16,"win16");
    tmp.put(BrowserFlag.CRAWLER,"crawler");
    tmp.put(BrowserFlag.STRIPPER,"stripper");
    tmp.put(BrowserFlag.WAP,"wap");
    FLAG_TO_NAME = Collections.unmodifiableMap(tmp);

  } // end static initializer

} // end class ServletBrowserData
