1    //
2    // SwitchRMI  Framework
3    // Copyright (c) 2000-2002 by Michael J. Henderson & Associates.
4    //
5    // Michael Henderson
6    // http://switchrmi.sf.net
7    // mailto:mikehenderson@dunelm.org.uk
8    //
9    // This library is free software.
10   //
11   // You may redistribute it and/or modify it under the terms of the GNU
12   // Lesser General Public License as published by the Free Software Foundation.
13   //
14   // Version 2.1 of the license should be included with this distribution in
15   // the file LICENSE, as well as License.html. If the license is not
16   // included with this distribution, you may find a copy at the FSF web
17   // site at 'www.gnu.org' or 'www.fsf.org', or you may write to the
18   // Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.
19   //
20   // This library is distributed in the hope that it will be useful,
21   // but WITHOUT ANY WARRANTY; without even the implied waranty of
22   // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23   // Lesser General Public License for more details.
24   //
25   // $Id: IValidatorImpl.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   package com.mjh.switchrmi.protocol.xmlrpc.validator;
27   
28   import java.util.*;
29   
30   public class IValidatorImpl implements IValidator
31   {
32       public IValidatorImpl()
33       {
34       }
35   
36       public int arrayOfStructsTest(List list)
37       {
38           int result = 0;
39           Iterator elements = list.iterator();
40   
41           while (elements.hasNext())
42           {
43               Map map = (Map) elements.next();
44               Number number = (Number) map.get("curly");
45   
46               if (number != null)
47               {
48                   result += number.intValue();
49               }
50           }
51   
52           return result;
53       }
54   
55       public Map countTheEntities(String string)
56       {
57           int ctLeftAngle = 0;
58           int ctRightAngle = 0;
59           int ctAmpersands = 0;
60           int ctApostrophes = 0;
61           int ctQuotes = 0;
62   
63           char[] array = string.toCharArray();
64   
65           for (int i = 0; i < array.length; i++)
66           {
67               char c = array[i];
68   
69               if (c == '<')
70               {
71                   ctLeftAngle++;
72               }
73   
74               if (c == '>')
75               {
76                   ctRightAngle++;
77               }
78   
79               if (c == '&')
80               {
81                   ctAmpersands++;
82               }
83   
84               if (c == '\'')
85               {
86                   ctApostrophes++;
87               }
88   
89               if (c == '"')
90               {
91                   ctQuotes++;
92               }
93           }
94   
95           HashMap map = new HashMap();
96   
97           map.put("ctLeftAngleBrackets", new Integer(ctLeftAngle));
98           map.put("ctRightAngleBrackets", new Integer(ctRightAngle));
99           map.put("ctAmpersands", new Integer(ctAmpersands));
100          map.put("ctApostrophes", new Integer(ctApostrophes));
101          map.put("ctQuotes", new Integer(ctQuotes));
102  
103          /*
104          HashMap map = new HashMap();
105          map.put("ctLeftAngleBrackets",  new Integer(countSubStringInString("<", string)));
106          map.put("ctRightAngleBrackets", new Integer(countSubStringInString(">", string)));
107          map.put("ctAmpersands",         new Integer(countSubStringInString("&", string)));
108          map.put("ctApostrophes",        new Integer(countSubStringInString("'", string)));
109          map.put("ctQuotes",             new Integer(countSubStringInString(""", string)));
110          */
111          return map;
112      }
113  
114      public int easyStructTest(Map map)
115      {
116          int result = 0;
117          Set keySet = map.keySet();
118          Iterator keys = keySet.iterator();
119  
120          while (keys.hasNext())
121          {
122              String key = (String) keys.next();
123  
124              if (key.equals("moe") || key.equals("larry") || key.equals("curly"))
125              {
126                  Number number = (Number) map.get(key);
127  
128                  result += number.intValue();
129              }
130          }
131  
132          return result;
133      }
134  
135      public Map echoStructTest(Map map)
136      {
137          return map;
138      }
139  
140      public List manyTypesTest(int number, boolean bool, String string, 
141                                double dbl, Date date, byte[] base64)
142      {
143          ArrayList list = new ArrayList();
144  
145          list.add(new Integer(number));
146          list.add(new Boolean(bool));
147          list.add(string);
148          list.add(new Double(dbl));
149          list.add(date);
150          list.add(base64);
151  
152          return list;
153      }
154  
155      public String moderateSizeArrayCheck(List list)
156      {
157          String first = (String) list.get(0);
158          String last = (String) list.get(list.size() - 1);
159  
160          return first + last;
161      }
162  
163      public int nestedStructTest(Map map)
164      {
165          map = (Map) map.get("2000");
166          map = (Map) map.get("04");
167          map = (Map) map.get("01");
168  
169          Number moe = (Number) map.get("moe");
170  
171          Number larry = (Number) map.get("larry");
172          Number curly = (Number) map.get("curly");
173  
174          return larry.intValue() + moe.intValue() + curly.intValue();
175      }
176  
177      public Map simpleStructReturnTest(int number)
178      {
179          HashMap map = new HashMap();
180  
181          map.put("times10", new Integer(number * 10));
182          map.put("times100", new Integer(number * 100));
183          map.put("times1000", new Integer(number * 1000));
184  
185          return map;
186      }
187  }