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: XmlRpcTypes.java,v 1.1 2002/11/11 22:19:48 mikehenderson Exp $
26   package com.mjh.switchrmi.protocol.xmlrpc;
27   
28   import java.util.HashMap;
29   
30   import org.apache.log4j.Logger;
31   
32   public class XmlRpcTypes
33   {
34   	
35   	private static Logger log = Logger.getLogger(XmlRpcTypes.class.getName());
36   	
37       public final static String INT_TYPE = "int";
38       public final static String I4_TYPE = "i4";
39       public final static String DOUBLE_TYPE = "double";
40       public final static String STRING_TYPE = "string";
41       public final static String BOOLEAN_TYPE = "boolean";
42       public final static String DATETIME_TYPE = "dateTime.iso8601";
43       public final static String STRUCT_TYPE = "struct";
44       public final static String ARRAY_TYPE = "array";
45       public final static String BASE64_TYPE = "base64";
46       private static HashMap javaTypes = new HashMap();
47       private static HashMap xmlrpcTypes = new HashMap();
48       private static HashMap primitives = new HashMap();
49   
50       static
51       {
52           javaTypes.put("boolean", BOOLEAN_TYPE);
53           javaTypes.put("java.lang.Boolean", BOOLEAN_TYPE);
54           javaTypes.put("int", INT_TYPE);
55           javaTypes.put("java.lang.Integer", INT_TYPE);
56   
57           javaTypes.put("double", DOUBLE_TYPE);
58           javaTypes.put("java.lang.Double", DOUBLE_TYPE);
59           javaTypes.put("java.lang.String", STRING_TYPE);
60           javaTypes.put("java.util.Date", DATETIME_TYPE);
61           javaTypes.put("java.util.Map", STRUCT_TYPE);
62           javaTypes.put("java.util.List", ARRAY_TYPE);
63           javaTypes.put("java.util.List", ARRAY_TYPE);
64           javaTypes.put("[B", BASE64_TYPE);
65   
66           xmlrpcTypes.put(INT_TYPE, 
67                           new Class[] { int.class, java.lang.Integer.class });
68           xmlrpcTypes.put(I4_TYPE, 
69                           new Class[] { int.class, java.lang.Integer.class });
70           xmlrpcTypes.put(BOOLEAN_TYPE, 
71                           new Class[] { boolean.class, java.lang.Boolean.class });
72           xmlrpcTypes.put(DOUBLE_TYPE, 
73                           new Class[] { double.class, java.lang.Double.class });
74           xmlrpcTypes.put(STRING_TYPE, new Class[] { java.lang.String.class });
75           xmlrpcTypes.put(DATETIME_TYPE, new Class[] { java.util.Date.class });
76           xmlrpcTypes.put(STRUCT_TYPE, new Class[] { java.util.Map.class });
77           xmlrpcTypes.put(ARRAY_TYPE, new Class[] { java.util.List.class });
78           xmlrpcTypes.put(BASE64_TYPE, new Class[] { byte[].class });
79   
80           primitives.put("char", char.class);
81           primitives.put("byte", byte.class);
82           primitives.put("short", short.class);
83           primitives.put("int", int.class);
84           primitives.put("long", long.class);
85           primitives.put("float", float.class);
86           primitives.put("double", double.class);
87       }
88   
89       public static Class[] getJavaTypesForXmlRpcType(String type)
90       {
91           //type = getInterfaceType(type);
92           return (Class[]) xmlrpcTypes.get(type);
93       }
94   
95       public static boolean javaTypeMatchesXmlRpcType(String javaType, 
96                                                       String xmlRmiType)
97       {
98           boolean result = false;
99           Class[] possibleTypes = getJavaTypesForXmlRpcType(xmlRmiType);
100          Class javaClazz = convertJavaTypeToClass(javaType);
101  
102          if (possibleTypes != null)
103          {
104              for (int i = 0; i < possibleTypes.length; i++)
105              {
106                  Class possibleType = possibleTypes[i];
107  
108                  result = possibleType.isAssignableFrom(javaClazz);
109  
110                  if (result)
111                  {
112                      break;
113                  }
114              }
115          }
116  
117          return result;
118      }
119  
120      private static Class convertJavaTypeToClass(String javaType)
121      {
122          Class clazz = (Class) primitives.get(javaType);
123  
124          if (clazz == null)
125          {
126              try
127              {
128                  clazz = Class.forName(javaType);
129              }
130              catch (ClassNotFoundException ex)
131              {
132                  // will this occur review!!!
133              }
134          }
135  
136          return clazz;
137      }
138  
139      public static String getXmlRpcTypeForJavaType(String type)
140      {
141  	boolean debug = log.isDebugEnabled();
142  	
143  	if (debug) log.debug("type = " + type);
144          String result = (String) javaTypes.get(type);
145  
146          if (result == null)
147          {
148              result = getXmlRpcTypeByInterface(type);
149          }
150  
151          return result;
152      }
153  
154      public static String getXmlRpcTypeByInterface(String type)
155      {
156          String result = null;
157  
158          Class clazz = null;
159  
160          try
161          {
162              clazz = Class.forName(type);
163          }
164          catch (ClassNotFoundException ex)
165          {
166              // will this occur review!!!
167          }
168  
169          if (java.util.Map.class.isAssignableFrom(clazz))
170          {
171              result = STRUCT_TYPE;
172          }
173          else if (java.util.List.class.isAssignableFrom(clazz))
174          {
175              result = ARRAY_TYPE;
176          }
177  
178          return result;
179      }
180  }