1 7 package com.inversoft.util.typeconverter; 8 9 10 import java.lang.reflect.Array ; 11 import java.util.StringTokenizer ; 12 13 import com.inversoft.util.ReflectionTools; 14 import com.inversoft.util.StringTools; 15 16 17 31 public class BaseTypeConverter implements TypeConverter { 32 33 68 public Object convert(Object value, Class convertTo) throws TypeConversionException { 69 70 if (value == null) { 72 return convertString((String ) value, convertTo); 73 } 74 75 if (convertTo.isInstance(value)) { 77 return value; 78 } 79 80 if (convertTo.isArray()) { 81 return convertToArray(value, convertTo.getComponentType()); 82 } 83 84 if (value instanceof Object []) { 87 return convertArray( (Object []) value, convertTo); 88 } 89 90 return convertString(value.toString(), convertTo); 91 } 92 93 123 public Object convertString(String value, Class convertTo) throws TypeConversionException { 124 125 if (StringTools.isTrimmedEmpty(value)) { 127 return null; 128 } 129 130 if (convertTo.isArray()) { 131 return convertStringToArray(value, null, convertTo.getComponentType()); 132 } 133 134 return value; 135 } 136 137 163 public Object convertArray(Object [] values, Class convertTo) 164 throws TypeConversionException { 165 166 if (values == null) { 168 return null; 169 } 170 171 if (convertTo.isArray()) { 172 return convertArrayToArray(values, convertTo); 173 } 174 175 if (values.length == 0) { 177 return null; 178 } 179 180 if (values.length == 1) { 181 if (convertTo.isInstance(values[0])) { 182 return values[0]; 183 } 184 185 return convert(values[0], convertTo); 186 } 187 188 throw new TypeConversionException("BaseTypeConverter does not handle" + 189 " converting an array with size not equal to one to an object"); 190 } 191 192 211 public Object [] convertArrayToArray(Object [] values, Class convertTo) 212 throws TypeConversionException { 213 214 if (values == null || values.length == 0) { 216 return null; 217 } 218 219 if (convertTo.isArray()) { 220 convertTo = convertTo.getComponentType(); 221 } 222 223 Object [] array; 224 if (convertTo.isPrimitive()) { 225 Class wrapper = ReflectionTools.convertToWrapper(convertTo); 226 array = (Object []) Array.newInstance(wrapper, values.length); 227 } else { 228 array = (Object []) Array.newInstance(convertTo, values.length); 229 } 230 231 for (int i = 0; i < array.length; i++) { 232 if (values[i] != null && 233 convertTo.isAssignableFrom(values[i].getClass())) { 234 array[i] = values[i]; 235 } else { 236 array[i] = convert(values[i], convertTo); 237 } 238 } 239 240 return array; 241 } 242 243 260 public Object [] convertToArray(Object value, Class convertTo) throws TypeConversionException { 261 262 if (value == null) { 264 return null; 265 } 266 267 if (value instanceof Object []) { 268 return convertArrayToArray((Object []) value, convertTo); 269 } 270 271 return convertStringToArray(value.toString(), null, convertTo); 272 } 273 274 292 public Object [] convertStringToArray(String value, String delimiter, 293 Class convertTo) throws TypeConversionException { 294 295 if (value == null) { 297 return null; 298 } 299 300 if (convertTo.isArray()) { 301 convertTo = convertTo.getComponentType(); 302 } 303 304 boolean startsWithDelim = false; 308 StringTokenizer st; 309 if (StringTools.isEmpty(delimiter)) { 310 st = new StringTokenizer (value, ",;\t\n"); 311 startsWithDelim = (value.length() > 0) ? 312 (",;\t\n".indexOf(value.charAt(0)) != -1) : false; 313 } else { 314 st = new StringTokenizer (value, delimiter); 315 startsWithDelim = (value.length() > 0) ? 316 (delimiter.indexOf(value.charAt(0)) != -1) : false; 317 } 318 319 int count = st.countTokens(); 321 if (startsWithDelim) { 322 count++; 323 } 324 325 Object [] array; 327 if (convertTo.isPrimitive()) { 328 Class wrapper = ReflectionTools.convertToWrapper(convertTo); 329 array = (Object []) Array.newInstance(wrapper, count); 330 } else { 331 array = (Object []) Array.newInstance(convertTo, count); 332 } 333 334 int i = 0; 337 if (startsWithDelim) { 338 array[0] = convertString("", convertTo); 339 i++; 340 } 341 342 for (; i < array.length; i++) { 343 array[i] = convertString(st.nextToken(), convertTo); 344 } 345 346 return array; 347 } 348 } | Popular Tags |