1 57 58 package org.apache.wsif.providers; 59 60 import java.lang.reflect.Array ; 61 62 import org.apache.wsif.WSIFException; 63 64 69 public class ProviderUtils { 70 71 78 public static Object stringArrayToCharacterArray(Object obj) throws WSIFException { 79 return stringArrayToCharacterArray(obj, null); 80 } 81 82 90 protected static Object stringArrayToCharacterArray(Object obj, Object ret) throws WSIFException { 91 if (obj.getClass().isArray()) { 92 Object [] objs = (Object []) obj; 93 ret = new Object [objs.length]; 94 for (int i=0; i<objs.length; i++) { 95 Object temp = stringArrayToCharacterArray(objs[i], ((Object []) ret)[i]); 96 if (i == 0) { 97 Class tempc = temp.getClass(); 98 ret = Array.newInstance(tempc, objs.length); 99 } 100 ((Object []) ret)[i] = temp; 101 } 102 return ret; 103 } else if (obj instanceof String ) { 104 String s = (String ) obj; 105 if (s.length() == 1) { 106 ret = new Character (s.charAt(0)); 107 return ret; 108 } else { 109 throw new WSIFException("String is longer than 1 character"); 110 } 111 } else { 112 throw new WSIFException("Array entry is not a String or another array"); 113 } 114 } 115 116 123 public static Object stringArrayToCharArray(Object obj) throws WSIFException { 124 return stringArrayToCharArray(obj, null); 125 } 126 127 135 protected static Object stringArrayToCharArray(Object obj, Object ret) throws WSIFException { 136 if (obj.getClass().isArray()) { 137 Object [] objs = (Object []) obj; 138 ret = new Object [objs.length]; 139 for (int i=0; i<objs.length; i++) { 140 Object temp = stringArrayToCharArray(objs[i], ((Object []) ret)[i]); 141 if (i == 0) { 142 Class tempc = temp.getClass(); 143 ret = Array.newInstance(tempc, objs.length); 144 } 145 ((Object []) ret)[i] = temp; 146 if (temp instanceof Character && (i == objs.length - 1)) { 147 char[] tempca = new char[objs.length]; 148 for (int j=0; j<objs.length; j++) { 149 tempca[j] = ((Character []) ret)[j].charValue(); 150 } 151 ret = tempca; 152 } 153 } 154 return ret; 155 } else if (obj instanceof String ) { 156 String s = (String ) obj; 157 if (s.length() == 1) { 158 ret = new Character (s.charAt(0)); 159 return ret; 160 } else { 161 throw new WSIFException("String is longer than 1 character"); 162 } 163 } else { 164 throw new WSIFException("Array entry is not a String or another array"); 165 } 166 } 167 168 175 public static Object characterArrayToStringArray(Object obj) throws WSIFException { 176 return characterArrayToStringArray(obj, null); 177 } 178 179 187 protected static Object characterArrayToStringArray(Object obj, Object ret) throws WSIFException { 188 if (obj.getClass().isArray()) { 189 Object [] objs = (Object []) obj; 190 ret = new Object [objs.length]; 191 for (int i=0; i<objs.length; i++) { 192 Object temp = characterArrayToStringArray(objs[i], ((Object []) ret)[i]); 193 if (i == 0) { 194 Class tempc = temp.getClass(); 195 ret = Array.newInstance(tempc, objs.length); 196 } 197 ((Object []) ret)[i] = temp; 198 } 199 return ret; 200 } else if (obj instanceof Character ) { 201 String s = obj.toString(); 202 return s; 203 } else { 204 throw new WSIFException("Array entry is not a Character or another array"); 205 } 206 } 207 208 215 public static Object charArrayToStringArray(Object obj) throws WSIFException { 216 return charArrayToStringArray(obj, null); 217 } 218 219 227 protected static Object charArrayToStringArray(Object obj, Object ret) throws WSIFException { 228 if (obj.getClass().isArray()) { 229 if (obj instanceof char[]) { 230 char[] ca = (char[]) obj; 231 Character [] chra = new Character [ca.length]; 232 for (int j=0; j<ca.length; j++) { 233 chra[j] = new Character (ca[j]); 234 } 235 obj = chra; 236 } 237 Object [] objs = (Object []) obj; 238 ret = new Object [objs.length]; 239 for (int i=0; i<objs.length; i++) { 240 Object temp = charArrayToStringArray(objs[i], ((Object []) ret)[i]); 241 if (i == 0) { 242 Class tempc = temp.getClass(); 243 ret = Array.newInstance(tempc, objs.length); 244 } 245 ((Object []) ret)[i] = temp; 246 } 247 return ret; 248 } else if (obj instanceof Character ) { 249 String s = obj.toString(); 250 return s; 251 } else { 252 throw new WSIFException("Array entry is not a char or another array"); 253 } 254 } 255 256 262 public static Character stringToCharacter(String str) { 263 if (str.length() != 1) 264 return null; 265 return new Character (str.charAt(0)); 266 } 267 268 277 public static Object getDefaultObject(Class cls) { 278 if (cls == null) { 279 return null; 280 } else if (cls.isPrimitive()) { 281 if (cls.getName().equals("int")) { 282 return new Integer (0); 283 } else if (cls.getName().equals("char")) { 284 return new Character ('0'); 285 } else if (cls.getName().equals("long")) { 286 return new Long (0); 287 } else if (cls.getName().equals("short")) { 288 short s = 0; 289 return new Short (s); 290 } else if (cls.getName().equals("double")) { 291 return new Double (0); 292 } else if (cls.getName().equals("boolean")) { 293 return new Boolean (false); 294 } else if (cls.getName().equals("byte")) { 295 byte b = 0; 296 return new Byte (b); 297 } else { 298 return new Float (0); 299 } 300 } else { 301 return null; 302 } 303 } 304 } 305 | Popular Tags |