// 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): 

// Implements the XML-RPC Validation Suite functions as documented by UserLand Software,
// see http://www.xmlrpc.com/validator1Docs

// -- PASSES VALIDATION as of 1/16/2002; 11:31:28 PM

importPackage(java.util);
importPackage(Packages.com.silverwrist.venice.ui.rpc);

xreq = bsf.lookupBean("xmlrpc");

if ("validator1.arrayOfStructsTest"==xreq.method)
{ // arrayOfStructsTest coming right up!
  if (xreq.paramCount!=1)
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter count mismatch"));
  else if ("array"!=xreq.getParamType(0))  
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch"));
  else
  { // The parameter is an array of structs containing at least three elements, named
    // "moe", "larry", and "curly", all of which are integers.  We need to add up all
    // the "curly" elements and return the sum.
    parm = vlib.castList(xreq.getParam(0));
    accum = 0;
    it = parm.iterator();
    while (it.hasNext())
    { // build the return value
      str = vlib.castMap(it.next());
      accum += vlib.toInteger(str.get("curly"));
      
    } // end while

    vlib.output(vlib.createInteger(accum));

  } // end else

  vlib.done();

} // end if

if ("validator1.countTheEntities"==xreq.method)
{ // countTheEntities test coming right up!
  if (xreq.paramCount!=1)
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter count mismatch"));
  else if ("string"!=xreq.getParamType(0))  
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch"));
  else
  { // We get a string in which we're supposed to count the number of left angle brackets,
    // right angle brackets, ampersands, quotes, and apostrophes.  We return that information
    // in a formatted structure.
    str = xreq.getParamString(0) + "";
    nleft = 0;
    nright = 0;
    namper = 0;
    napos = 0;
    nquot = 0;
    for (i=0; i<str.length; i++)
    { // count the entities - note that we use ASCII codes to find the characters because
      // JavaScript doesn't do character constants all that well
      ch = str.charCodeAt(i);
      if (ch==60)
        nleft++;
      else if (ch==62)
        nright++;
      else if (ch==38)
        namper++;
      else if (ch==39)
        napos++;
      else if (ch==34)
        nquot++;

    } // end for

    // create the return value
    rc = vlib.createMap();
    rc.put("ctLeftAngleBrackets",vlib.createInteger(nleft));
    rc.put("ctRightAngleBrackets",vlib.createInteger(nright));
    rc.put("ctAmpersands",vlib.createInteger(namper));
    rc.put("ctApostrophes",vlib.createInteger(napos));
    rc.put("ctQuotes",vlib.createInteger(nquot));
    vlib.output(rc);

  } // end else

  vlib.done();

} // end if

if ("validator1.easyStructTest"==xreq.method)
{ // easyStructTest test coming right up!
  if (xreq.paramCount!=1)
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter count mismatch"));
  else if ("struct"!=xreq.getParamType(0))  
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch"));
  else
  { // We get a struct, which has at least three fields, named "moe", "larry", and "curly",
    // all integers.  We must add these and return the result.
    parm = vlib.castMap(xreq.getParam(0));
    val = vlib.toInteger(parm.get("moe")) + vlib.toInteger(parm.get("larry"))
        + vlib.toInteger(parm.get("curly"));
    vlib.output(vlib.createInteger(val));
    
  } // end else

  vlib.done();
  
} // end if

if ("validator1.echoStructTest"==xreq.method)
{ // echoStructTest test coming right up!
  if (xreq.paramCount!=1)
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter count mismatch"));
  else if ("struct"!=xreq.getParamType(0))  
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch"));
  else  // just echo back the struct we got as a parameter - how easy is that?!?
    vlib.output(xreq.getParam(0));
  vlib.done();

} // end if

if ("validator1.manyTypesTest"==xreq.method)
{ // manyTypesTest coming right up!
  if (xreq.paramCount!=6)
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter count mismatch"));
  else if (   ("int"!=xreq.getParamType(0)) || ("boolean"!=xreq.getParamType(1))
           || ("string"!=xreq.getParamType(2)) || ("double"!=xreq.getParamType(3))
           || ("dateTime"!=xreq.getParamType(4)) || ("base64"!=xreq.getParamType(5)))
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch"));
  else
  { // all six of the passed parameters must be returned in a single array
    rc = vlib.createList();
    for (i=0; i<6; i++)
      rc.add(xreq.getParam(i));
    vlib.output(rc);

  } // end else

  vlib.done();
  
} // end if

if ("validator1.moderateSizeArrayCheck"==xreq.method)
{ // moderateSizeArrayCheck test coming right up!
  if (xreq.paramCount!=1)
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter count mismatch"));
  else if ("array"!=xreq.getParamType(0))  
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch"));
  else
  { // The given array will contain between 100 and 200 strings.  We need to concatenate the
    // first and last string and return them.
    parm = vlib.castList(xreq.getParam(0));
    s1 = parm.get(0) + "";
    s2 = parm.get(parm.size()-1) + "";
    vlib.output(s1 + s2);

  } // end else

  vlib.done();

} // end if

if ("validator1.nestedStructTest"==xreq.method)
{ // nestedStructTest coming right up!
  if (xreq.paramCount!=1)
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter count mismatch"));
  else if ("struct"!=xreq.getParamType(0))  
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch"));
  else
  { // The structure represents a calendar, one struct per year, each containing one struct per
    // month, each containing one struct per day.  (Months and days are identified by 2-digit
    // numbers with leading zeroes, January is month 1.)  We look up the structure for April 1,
    // 2000 ("2000.04.01") and find that it contains at least three members, named "moe", "larry",
    // and "curly", all three integers.  We need to add those three values and return the result.
    map_all = vlib.castMap(xreq.getParam(0));
    map_year = vlib.castMap(map_all.get("2000"));
    map_month = vlib.castMap(map_year.get("04"));
    map_day = vlib.castMap(map_month.get("01"));
    val = vlib.toInteger(map_day.get("moe")) + vlib.toInteger(map_day.get("larry"))
        + vlib.toInteger(map_day.get("curly"));
    vlib.output(vlib.createInteger(val));

  } // end else

  vlib.done();

} // end if

if ("validator1.simpleStructReturnTest"==xreq.method)
{ // simpleStructReturnTest coming right up!
  if (xreq.paramCount!=1)
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter count mismatch"));
  else if ("int"!=xreq.getParamType(0))  
    vlib.output(new XmlRpcFault(XmlRpcFault.INVALID_PARAMS,"parameter type mismatch"));
  else
  { // we get an integer, we need to return a struct which gives that same integer multiplied
    // by 10, 100, and 1000, in elements "times10", "times100", and "times1000", respectively
    base_val = xreq.getParamInt(0);
    rc = vlib.createMap();
    rc.put("times10",vlib.createInteger(base_val * 10));
    rc.put("times100",vlib.createInteger(base_val * 100));
    rc.put("times1000",vlib.createInteger(base_val * 1000));
    vlib.output(rc);

  } // end else

  vlib.done();

} // end if

// just in case there's a method name that wasn't listed
vlib.output(new XmlRpcFault(XmlRpcFault.METHOD_NOT_FOUND,"invalid method name: " + xreq.method));
