/*
 * 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.rpc;

import java.util.*;
import com.silverwrist.util.*;

public class XmlRpcLibrary
{
  /*--------------------------------------------------------------------------------
   * Static data members
   *--------------------------------------------------------------------------------
   */

  private static XmlRpcLibrary s_self = null;

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

  private XmlRpcLibrary()
  { // no external access
  } // end constructor

  /*--------------------------------------------------------------------------------
   * External operations
   *--------------------------------------------------------------------------------
   */

  public final void setIfNotNull(Map struct, String name, Object value)
  {
    if (value!=null)
      struct.put(name,value);

  } // end setIfNotNull

  public final int structMemberAsInt(Map struct, String name) throws XmlRpcFault
  {
    Object foo = struct.get(name);
    if (foo instanceof Integer)
      return ((Integer)foo).intValue();
    else if (foo instanceof Boolean)
      return ((Boolean)foo).booleanValue() ? 1 : 0;
    else
      throw new XmlRpcFault(10000,"type mismatch in structure member \"" + name + "\"");
    
  } // end structMemberAsInt

  public final String structMemberAsString(Map struct, String name) throws XmlRpcFault
  {
    Object foo = struct.get(name);
    if ((foo instanceof byte[]) || (foo instanceof List) || (foo instanceof Map))
      throw new XmlRpcFault(10000,"type mismatch in structure member \"" + name + "\"");
    return foo.toString();

  } // end structMemberAsString

  public final boolean structMemberAsBoolean(Map struct, String name) throws XmlRpcFault
  {
    Object foo = struct.get(name);
    if (foo instanceof Boolean)
      return ((Boolean)foo).booleanValue();
    else if (foo instanceof Integer)
      return (((Integer)foo).intValue()!=0);
    else if (foo instanceof String)
      return !StringUtil.isStringEmpty((String)foo);
    else
      throw new XmlRpcFault(10000,"type mismatch in structure member \"" + name + "\"");
    
  } // end structMemberAsBoolean

  public final java.util.Date structMemberAsDate(Map struct, String name) throws XmlRpcFault
  {
    Object foo = struct.get(name);
    if (!(foo instanceof java.util.Date))
      throw new XmlRpcFault(10000,"type mismatch in structure member \"" + name + "\"");
    return (java.util.Date)foo;

  } // end structMemberAsDate

  /*--------------------------------------------------------------------------------
   * External static operations
   *--------------------------------------------------------------------------------
   */

  public static final XmlRpcLibrary get()
  {
    if (s_self==null)
      s_self = new XmlRpcLibrary();
    return s_self;

  } // end get

} // end class XmlRpcLibrary
