/*
 * 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) 2001-02 Eric J. Bowersox/Silverwrist Design Studios.  All Rights Reserved.
 * 
 * Contributor(s): 
 */
package com.silverwrist.venice.ui.script;

import java.net.URLEncoder;
import java.util.*;
import org.w3c.dom.*;
import com.silverwrist.util.StringUtil;
import com.silverwrist.venice.core.*;
import com.silverwrist.venice.ui.helpers.CookieControl;
import com.silverwrist.venice.ui.helpers.HTMLRendering;
import com.silverwrist.venice.ui.helpers.SessionControl;
import com.silverwrist.venice.util.*;

public class ScriptLibrary
{
  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final Boolean booleanObject(boolean b)
  {
    return (b ? Boolean.TRUE : Boolean.FALSE);

  } // end booleanObject

  public final byte[] castByteArray(Object o)
  {
    if (o instanceof byte[])
      return (byte[])o;
    throw new ClassCastException("ScriptLibrary.castByteArray: invalid cast");

  } // end castByteArray

  public final ConferenceContext castConferenceContext(Object o)
  {
    if (o instanceof ConferenceContext)
      return (ConferenceContext)o;
    throw new ClassCastException("ScriptLibrary.castConferenceContext: invalid cast");

  } // end castConferenceContext

  public final ConferenceHotlistEntry castConferenceHotlistEntry(Object o)
  {
    if (o instanceof ConferenceHotlistEntry)
      return (ConferenceHotlistEntry)o;
    throw new ClassCastException("ScriptLibrary.castConferenceHotlistEntry: invalid cast");

  } // end castConferenceHotlistEntry

  public final Date castDate(Object o)
  {
    if (o instanceof Date)
      return (Date)o;
    throw new ClassCastException("ScriptLibrary.castDate: invalid cast");

  } // end castDate

  public final List castList(Object o)
  {
    if (o instanceof List)
      return (List)o;
    throw new ClassCastException("ScriptLibrary.castList: invalid cast");

  } // end castList

  public final Map castMap(Object o)
  {
    if (o instanceof Map)
      return (Map)o;
    throw new ClassCastException("ScriptLibrary.castMap: invalid cast");

  } // end castMap

  public final SideBoxDescriptor castSideBoxDescriptor(Object o)
  {
    if (o instanceof SideBoxDescriptor)
      return (SideBoxDescriptor)o;
    throw new ClassCastException("ScriptLibrary.castSideBoxDescriptor: invalid cast");

  } // end castSideBoxDescriptor

  public final TopicContext castTopicContext(Object o)
  {
    if (o instanceof TopicContext)
      return (TopicContext)o;
    throw new ClassCastException("ScriptLibrary.castTopicContext: invalid cast");

  } // end castTopicContext

  public final TopicMessageContext castTopicMessageContext(Object o)
  {
    if (o instanceof TopicMessageContext)
      return (TopicMessageContext)o;
    throw new ClassCastException("ScriptLibrary.castTopicMessageContext: invalid cast");

  } // end castTopicMessageContext

  public final UserFound castUserFound(Object o)
  {
    if (o instanceof UserFound)
      return (UserFound)o;
    throw new ClassCastException("ScriptLibrary.castUserFound: invalid cast");

  } // end castUserSideBoxDescriptor

  public final UserSideBoxDescriptor castUserSideBoxDescriptor(Object o)
  {
    if (o instanceof UserSideBoxDescriptor)
      return (UserSideBoxDescriptor)o;
    throw new ClassCastException("ScriptLibrary.castUserSideBoxDescriptor: invalid cast");

  } // end castUserSideBoxDescriptor

  public final boolean[] createBooleanArray(int len)
  {
    return new boolean[len];

  } // end createBooleanArray

  public final int[] createIntArray(int len)
  {
    return new int[len];

  } // end createIntArray

  public final Integer createInteger(int value)
  {
    return new Integer(value);

  } // end createInteger

  public final List createList()
  {
    return new ArrayList();

  } // end createList

  public final Map createMap()
  {
    return new HashMap();

  } // end createMap

  public final void done() throws ScriptExit
  {
    throw new ScriptExit();

  } // end done

  public final boolean emptyString(String s)
  {
    return StringUtil.isStringEmpty(s);

  } // end emptyString

  public final String encodeHTML(String s)
  {
    return StringUtil.encodeHTML(s);

  } // end encodeHTML

  public final String encodeURL(String s)
  {
    return URLEncoder.encode(s);

  } // end encodeURL

  public final String exceptionType(Object o) throws ScriptExit
  {
    if (o instanceof ScriptExit)
      throw (ScriptExit)o;   // rethrow ScriptExit exceptions
    if (o instanceof Throwable)
      return o.getClass().getName();
    else
      return null;

  } // end exceptionType

  public final String join(List l, String separator)
  {
    return StringUtil.join(l,separator);

  } // end join

  public final CookieControl queryCookieControl(ServiceProvider sp)
  {
    return (CookieControl)(sp.queryService(CookieControl.class));

  } // end queryCookieControl

  public final HTMLRendering queryHTMLRendering(ServiceProvider sp)
  {
    return (HTMLRendering)(sp.queryService(HTMLRendering.class));

  } // end queryHTMLRendering

  public final SessionControl querySessionControl(ServiceProvider sp)
  {
    return (SessionControl)(sp.queryService(SessionControl.class));

  } // end querySessionControl

  public final List splitList(String data, String delims)
  {
    return StringUtil.splitList(data,delims);

  } // end splitList

  public final double toDouble(Object o) throws NumberFormatException
  {
    if (   (o instanceof Byte) || (o instanceof Short) || (o instanceof Integer) || (o instanceof Long)
        || (o instanceof Float) || (o instanceof Double))
      return ((Number)o).doubleValue();
    else
      return Double.parseDouble(o.toString());

  } // end toDouble

  public final int toInteger(Object o) throws NumberFormatException
  {
    if ((o instanceof Byte) || (o instanceof Short) || (o instanceof Integer))
      return ((Number)o).intValue();
    else if (o instanceof Boolean)
      return ((Boolean)o).booleanValue() ? 1 : 0;
    else
      return Integer.parseInt(o.toString());

  } // end toInteger

  public final boolean validVeniceID(String s)
  {
    return IDUtils.isValidVeniceID(s);

  } // end validVeniceID

} // end class ScriptLibrary
