| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
package org.apache.commons.configuration; |
| 18 |
|
|
| 19 |
|
import java.awt.Color; |
| 20 |
|
import java.math.BigDecimal; |
| 21 |
|
import java.math.BigInteger; |
| 22 |
|
import java.net.MalformedURLException; |
| 23 |
|
import java.net.URL; |
| 24 |
|
import java.text.ParseException; |
| 25 |
|
import java.text.SimpleDateFormat; |
| 26 |
|
import java.util.ArrayList; |
| 27 |
|
import java.util.Calendar; |
| 28 |
|
import java.util.Collection; |
| 29 |
|
import java.util.Date; |
| 30 |
|
import java.util.Iterator; |
| 31 |
|
import java.util.List; |
| 32 |
|
import java.util.Locale; |
| 33 |
|
|
| 34 |
|
import org.apache.commons.collections.IteratorUtils; |
| 35 |
|
import org.apache.commons.collections.iterators.IteratorChain; |
| 36 |
|
import org.apache.commons.collections.iterators.SingletonIterator; |
| 37 |
|
import org.apache.commons.lang.BooleanUtils; |
| 38 |
|
import org.apache.commons.lang.StringUtils; |
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
public final class PropertyConverter |
| 48 |
|
{ |
| 49 |
|
|
| 50 |
|
static final String LIST_ESCAPE = "\\"; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
private static final String HEX_PREFIX = "0x"; |
| 54 |
|
|
| 55 |
|
|
| 56 |
|
private static final int HEX_RADIX = 16; |
| 57 |
|
|
| 58 |
|
|
| 59 |
|
|
| 60 |
|
|
| 61 |
|
private PropertyConverter() |
| 62 |
0 |
{ |
| 63 |
|
|
| 64 |
0 |
} |
| 65 |
|
|
| 66 |
|
|
| 67 |
88 |
|
| 68 |
|
|
| 69 |
32 |
|
| 70 |
|
|
| 71 |
56 |
|
| 72 |
|
|
| 73 |
54 |
public static Boolean toBoolean(Object value) throws ConversionException |
| 74 |
54 |
{ |
| 75 |
176 |
if (value instanceof Boolean) |
| 76 |
4 |
{ |
| 77 |
64 |
return (Boolean) value; |
| 78 |
50 |
} |
| 79 |
112 |
else if (value instanceof String) |
| 80 |
|
{ |
| 81 |
108 |
Boolean b = BooleanUtils.toBooleanObject((String) value); |
| 82 |
110 |
if (b == null) |
| 83 |
|
{ |
| 84 |
8 |
throw new ConversionException("The value " + value + " can't be converted to a Boolean object"); |
| 85 |
|
} |
| 86 |
100 |
return b; |
| 87 |
|
} |
| 88 |
|
else |
| 89 |
|
{ |
| 90 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Boolean object"); |
| 91 |
|
} |
| 92 |
|
} |
| 93 |
|
|
| 94 |
|
|
| 95 |
53 |
|
| 96 |
|
|
| 97 |
19 |
|
| 98 |
|
|
| 99 |
34 |
|
| 100 |
|
|
| 101 |
|
public static Byte toByte(Object value) throws ConversionException |
| 102 |
|
{ |
| 103 |
138 |
if (value instanceof Byte) |
| 104 |
32 |
{ |
| 105 |
38 |
return (Byte) value; |
| 106 |
1 |
} |
| 107 |
68 |
else if (value instanceof String) |
| 108 |
|
{ |
| 109 |
|
try |
| 110 |
31 |
{ |
| 111 |
64 |
String string = (String) value; |
| 112 |
64 |
if (string.startsWith(HEX_PREFIX)) |
| 113 |
|
{ |
| 114 |
2 |
return new Byte((byte) Integer.parseInt(string.substring(2), HEX_RADIX)); |
| 115 |
4 |
} |
| 116 |
|
else |
| 117 |
|
{ |
| 118 |
62 |
return new Byte(string); |
| 119 |
|
} |
| 120 |
2 |
} |
| 121 |
|
catch (NumberFormatException e) |
| 122 |
|
{ |
| 123 |
8 |
throw new ConversionException("The value " + value + " can't be converted to a Byte object", e); |
| 124 |
|
} |
| 125 |
|
} |
| 126 |
|
else |
| 127 |
|
{ |
| 128 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Byte object"); |
| 129 |
|
} |
| 130 |
|
} |
| 131 |
|
|
| 132 |
|
|
| 133 |
58 |
|
| 134 |
|
|
| 135 |
19 |
|
| 136 |
|
|
| 137 |
39 |
|
| 138 |
|
|
| 139 |
|
public static Short toShort(Object value) throws ConversionException |
| 140 |
|
{ |
| 141 |
153 |
if (value instanceof Short) |
| 142 |
37 |
{ |
| 143 |
38 |
return (Short) value; |
| 144 |
1 |
} |
| 145 |
78 |
else if (value instanceof String) |
| 146 |
|
{ |
| 147 |
|
try |
| 148 |
36 |
{ |
| 149 |
74 |
String string = (String) value; |
| 150 |
74 |
if (string.startsWith(HEX_PREFIX)) |
| 151 |
|
{ |
| 152 |
2 |
return new Short((short) Integer.parseInt(string.substring(2), HEX_RADIX)); |
| 153 |
|
} |
| 154 |
4 |
else |
| 155 |
|
{ |
| 156 |
72 |
return new Short(string); |
| 157 |
|
} |
| 158 |
|
|
| 159 |
2 |
} |
| 160 |
|
catch (NumberFormatException e) |
| 161 |
|
{ |
| 162 |
8 |
throw new ConversionException("The value " + value + " can't be converted to a Short object", e); |
| 163 |
|
} |
| 164 |
|
} |
| 165 |
|
else |
| 166 |
|
{ |
| 167 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Short object"); |
| 168 |
|
} |
| 169 |
|
} |
| 170 |
|
|
| 171 |
|
|
| 172 |
62 |
|
| 173 |
|
|
| 174 |
23 |
|
| 175 |
|
|
| 176 |
39 |
|
| 177 |
|
|
| 178 |
|
public static Integer toInteger(Object value) throws ConversionException |
| 179 |
|
{ |
| 180 |
161 |
if (value instanceof Integer) |
| 181 |
37 |
{ |
| 182 |
46 |
return (Integer) value; |
| 183 |
1 |
} |
| 184 |
78 |
else if (value instanceof String) |
| 185 |
|
{ |
| 186 |
|
try |
| 187 |
36 |
{ |
| 188 |
74 |
String string = (String) value; |
| 189 |
74 |
if (string.startsWith(HEX_PREFIX)) |
| 190 |
|
{ |
| 191 |
2 |
return new Integer((int) Long.parseLong(string.substring(2), HEX_RADIX)); |
| 192 |
2 |
} |
| 193 |
|
else |
| 194 |
|
{ |
| 195 |
72 |
return new Integer(string); |
| 196 |
|
} |
| 197 |
2 |
} |
| 198 |
|
catch (NumberFormatException e) |
| 199 |
|
{ |
| 200 |
4 |
throw new ConversionException("The value " + value + " can't be converted to an Integer object", e); |
| 201 |
|
} |
| 202 |
|
} |
| 203 |
|
else |
| 204 |
|
{ |
| 205 |
4 |
throw new ConversionException("The value " + value + " can't be converted to an Integer object"); |
| 206 |
|
} |
| 207 |
|
} |
| 208 |
|
|
| 209 |
|
|
| 210 |
53 |
|
| 211 |
|
|
| 212 |
19 |
|
| 213 |
|
|
| 214 |
34 |
|
| 215 |
|
|
| 216 |
|
public static Long toLong(Object value) throws ConversionException |
| 217 |
|
{ |
| 218 |
138 |
if (value instanceof Long) |
| 219 |
32 |
{ |
| 220 |
38 |
return (Long) value; |
| 221 |
1 |
} |
| 222 |
68 |
else if (value instanceof String) |
| 223 |
|
{ |
| 224 |
|
try |
| 225 |
31 |
{ |
| 226 |
64 |
String string = (String) value; |
| 227 |
64 |
if (string.startsWith(HEX_PREFIX)) |
| 228 |
|
{ |
| 229 |
2 |
return new Long(class="keyword">new BigInteger(string.substring(2), HEX_RADIX).longValue()); |
| 230 |
4 |
} |
| 231 |
|
else |
| 232 |
|
{ |
| 233 |
62 |
return new Long(string); |
| 234 |
|
} |
| 235 |
2 |
} |
| 236 |
|
catch (NumberFormatException e) |
| 237 |
|
{ |
| 238 |
8 |
throw new ConversionException("The value " + value + " can't be converted to a Long object", e); |
| 239 |
|
} |
| 240 |
|
} |
| 241 |
|
else |
| 242 |
|
{ |
| 243 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Long object"); |
| 244 |
|
} |
| 245 |
|
} |
| 246 |
|
|
| 247 |
|
|
| 248 |
52 |
|
| 249 |
|
|
| 250 |
19 |
|
| 251 |
|
|
| 252 |
33 |
|
| 253 |
|
|
| 254 |
|
public static Float toFloat(Object value) throws ConversionException |
| 255 |
|
{ |
| 256 |
135 |
if (value instanceof Float) |
| 257 |
|
{ |
| 258 |
38 |
return (Float) value; |
| 259 |
|
} |
| 260 |
70 |
else if (value instanceof String) |
| 261 |
|
{ |
| 262 |
|
try |
| 263 |
|
{ |
| 264 |
62 |
return new Float((String) value); |
| 265 |
2 |
} |
| 266 |
|
catch (NumberFormatException e) |
| 267 |
|
{ |
| 268 |
8 |
throw new ConversionException("The value " + value + " can't be converted to a Float object", e); |
| 269 |
|
} |
| 270 |
|
} |
| 271 |
|
else |
| 272 |
|
{ |
| 273 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Float object"); |
| 274 |
|
} |
| 275 |
|
} |
| 276 |
|
|
| 277 |
|
|
| 278 |
53 |
|
| 279 |
|
|
| 280 |
20 |
|
| 281 |
|
|
| 282 |
33 |
|
| 283 |
|
|
| 284 |
|
public static Double toDouble(Object value) throws ConversionException |
| 285 |
|
{ |
| 286 |
137 |
if (value instanceof Double) |
| 287 |
|
{ |
| 288 |
40 |
return (Double) value; |
| 289 |
|
} |
| 290 |
70 |
else if (value instanceof String) |
| 291 |
|
{ |
| 292 |
|
try |
| 293 |
|
{ |
| 294 |
62 |
return new Double((String) value); |
| 295 |
2 |
} |
| 296 |
|
catch (NumberFormatException e) |
| 297 |
|
{ |
| 298 |
8 |
throw new ConversionException("The value " + value + " can't be converted to a Double object", e); |
| 299 |
|
} |
| 300 |
|
} |
| 301 |
|
else |
| 302 |
|
{ |
| 303 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Double object"); |
| 304 |
|
} |
| 305 |
|
} |
| 306 |
|
|
| 307 |
|
|
| 308 |
40 |
|
| 309 |
|
|
| 310 |
14 |
|
| 311 |
|
|
| 312 |
26 |
|
| 313 |
|
|
| 314 |
|
public static BigInteger toBigInteger(Object value) throws ConversionException |
| 315 |
|
{ |
| 316 |
104 |
if (value instanceof BigInteger) |
| 317 |
24 |
{ |
| 318 |
28 |
return (BigInteger) value; |
| 319 |
1 |
} |
| 320 |
52 |
else if (value instanceof String) |
| 321 |
|
{ |
| 322 |
|
try |
| 323 |
23 |
{ |
| 324 |
48 |
String string = (String) value; |
| 325 |
48 |
if (string.startsWith(HEX_PREFIX)) |
| 326 |
|
{ |
| 327 |
2 |
return new BigInteger(string.substring(2), HEX_RADIX); |
| 328 |
4 |
} |
| 329 |
|
else |
| 330 |
|
{ |
| 331 |
46 |
return new BigInteger(string); |
| 332 |
|
} |
| 333 |
2 |
} |
| 334 |
|
catch (NumberFormatException e) |
| 335 |
|
{ |
| 336 |
8 |
throw new ConversionException("The value " + value + " can't be converted to a BigInteger object", e); |
| 337 |
|
} |
| 338 |
|
} |
| 339 |
|
else |
| 340 |
|
{ |
| 341 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a BigInteger object"); |
| 342 |
|
} |
| 343 |
|
} |
| 344 |
|
|
| 345 |
|
|
| 346 |
39 |
|
| 347 |
|
|
| 348 |
14 |
|
| 349 |
|
|
| 350 |
25 |
|
| 351 |
|
|
| 352 |
|
public static BigDecimal toBigDecimal(Object value) throws ConversionException |
| 353 |
|
{ |
| 354 |
101 |
if (value instanceof BigDecimal) |
| 355 |
|
{ |
| 356 |
28 |
return (BigDecimal) value; |
| 357 |
|
} |
| 358 |
54 |
else if (value instanceof String) |
| 359 |
|
{ |
| 360 |
|
try |
| 361 |
|
{ |
| 362 |
46 |
return new BigDecimal((String) value); |
| 363 |
2 |
} |
| 364 |
|
catch (NumberFormatException e) |
| 365 |
|
{ |
| 366 |
8 |
throw new ConversionException("The value " + value + " can't be converted to a BigDecimal object", e); |
| 367 |
|
} |
| 368 |
|
} |
| 369 |
|
else |
| 370 |
|
{ |
| 371 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a BigDecimal object"); |
| 372 |
|
} |
| 373 |
|
} |
| 374 |
|
|
| 375 |
|
|
| 376 |
35 |
|
| 377 |
|
|
| 378 |
15 |
|
| 379 |
|
|
| 380 |
20 |
|
| 381 |
|
|
| 382 |
|
public static URL toURL(Object value) throws ConversionException |
| 383 |
|
{ |
| 384 |
88 |
if (value instanceof URL) |
| 385 |
|
{ |
| 386 |
30 |
return (URL) value; |
| 387 |
|
} |
| 388 |
42 |
else if (value instanceof String) |
| 389 |
|
{ |
| 390 |
|
try |
| 391 |
|
{ |
| 392 |
36 |
return new URL((String) value); |
| 393 |
2 |
} |
| 394 |
|
catch (MalformedURLException e) |
| 395 |
|
{ |
| 396 |
4 |
throw new ConversionException("The value " + value + " can't be converted to an URL", e); |
| 397 |
|
} |
| 398 |
|
} |
| 399 |
|
else |
| 400 |
|
{ |
| 401 |
4 |
throw new ConversionException("The value " + value + " can't be converted to an URL"); |
| 402 |
|
} |
| 403 |
|
} |
| 404 |
|
|
| 405 |
|
|
| 406 |
40 |
|
| 407 |
|
|
| 408 |
14 |
|
| 409 |
|
|
| 410 |
26 |
|
| 411 |
|
|
| 412 |
24 |
public static Locale toLocale(Object value) throws ConversionException |
| 413 |
24 |
{ |
| 414 |
80 |
if (value instanceof Locale) |
| 415 |
24 |
{ |
| 416 |
28 |
return (Locale) value; |
| 417 |
22 |
} |
| 418 |
74 |
else if (value instanceof String) |
| 419 |
22 |
{ |
| 420 |
48 |
List elements = split((String) value, '_'); |
| 421 |
70 |
int size = elements.size(); |
| 422 |
|
|
| 423 |
48 |
if (size >= 1 && (((String) elements.get(0)).length() == 2 || ((String) elements.get(0)).length() == 0)) |
| 424 |
|
{ |
| 425 |
46 |
String language = (String) elements.get(0); |
| 426 |
44 |
String country = (String) ((size >= 2) ? elements.get(1) : ""); |
| 427 |
44 |
String variant = (String) ((size >= 3) ? elements.get(2) : ""); |
| 428 |
|
|
| 429 |
44 |
return new Locale(language, country, variant); |
| 430 |
2 |
} |
| 431 |
|
else |
| 432 |
|
{ |
| 433 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Locale"); |
| 434 |
|
} |
| 435 |
|
} |
| 436 |
|
else |
| 437 |
|
{ |
| 438 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Locale"); |
| 439 |
|
} |
| 440 |
|
} |
| 441 |
|
|
| 442 |
|
|
| 443 |
|
|
| 444 |
|
|
| 445 |
|
|
| 446 |
7110 |
|
| 447 |
|
|
| 448 |
9 |
|
| 449 |
|
|
| 450 |
|
|
| 451 |
7101 |
|
| 452 |
|
public static List split(String s, char delimiter) |
| 453 |
7101 |
{ |
| 454 |
21321 |
if (s == null) |
| 455 |
7101 |
{ |
| 456 |
23855 |
return new ArrayList(); |
| 457 |
|
} |
| 458 |
|
|
| 459 |
23837 |
List list = new ArrayList(); |
| 460 |
|
|
| 461 |
14202 |
StringBuffer token = new StringBuffer(); |
| 462 |
23837 |
int begin = 0; |
| 463 |
14202 |
int end = 0; |
| 464 |
47674 |
while (begin <= s.length()) |
| 465 |
9635 |
{ |
| 466 |
|
|
| 467 |
28905 |
int index = s.indexOf(delimiter, end); |
| 468 |
|
|
| 469 |
637 |
|
| 470 |
19907 |
end = (index != -1) ? index : s.length(); |
| 471 |
|
|
| 472 |
|
|
| 473 |
19270 |
String chunk = s.substring(begin , end); |
| 474 |
|
|
| 475 |
28268 |
if (chunk.endsWith(LIST_ESCAPE) && end != s.length()) |
| 476 |
|
{ |
| 477 |
1274 |
token.append(chunk.substring(0, chunk.length() - 1)); |
| 478 |
10272 |
token.append(delimiter); |
| 479 |
|
} |
| 480 |
|
else |
| 481 |
8998 |
{ |
| 482 |
|
|
| 483 |
17996 |
token.append(chunk); |
| 484 |
|
|
| 485 |
9635 |
|
| 486 |
27631 |
list.add(token.toString().trim()); |
| 487 |
|
|
| 488 |
|
|
| 489 |
25097 |
token = new StringBuffer(); |
| 490 |
|
} |
| 491 |
|
|
| 492 |
|
|
| 493 |
19270 |
end = end + 1; |
| 494 |
19270 |
begin = end; |
| 495 |
|
} |
| 496 |
|
|
| 497 |
14202 |
return list; |
| 498 |
|
} |
| 499 |
|
|
| 500 |
|
|
| 501 |
|
|
| 502 |
|
|
| 503 |
|
|
| 504 |
|
|
| 505 |
102 |
|
| 506 |
|
|
| 507 |
|
|
| 508 |
|
|
| 509 |
|
|
| 510 |
|
|
| 511 |
|
public static String escapeDelimiters(String s, char delimiter) |
| 512 |
|
{ |
| 513 |
204 |
return StringUtils.replace(s, String.valueOf(delimiter), LIST_ESCAPE |
| 514 |
|
+ delimiter); |
| 515 |
|
} |
| 516 |
|
|
| 517 |
|
|
| 518 |
|
|
| 519 |
|
|
| 520 |
|
|
| 521 |
|
|
| 522 |
|
|
| 523 |
|
|
| 524 |
|
|
| 525 |
36 |
|
| 526 |
|
|
| 527 |
14 |
|
| 528 |
|
|
| 529 |
22 |
|
| 530 |
|
|
| 531 |
|
public static Color toColor(Object value) throws ConversionException |
| 532 |
|
{ |
| 533 |
92 |
if (value instanceof Color) |
| 534 |
|
{ |
| 535 |
28 |
return (Color) value; |
| 536 |
20 |
} |
| 537 |
44 |
else if (value instanceof String && !StringUtils.isBlank((String) value)) |
| 538 |
2 |
{ |
| 539 |
40 |
String color = ((String) value).trim(); |
| 540 |
|
|
| 541 |
60 |
int[] components = new class="keyword">int[3]; |
| 542 |
18 |
|
| 543 |
18 |
|
| 544 |
58 |
int minlength = components.length * 2; |
| 545 |
40 |
if (color.length() < minlength) |
| 546 |
|
{ |
| 547 |
18 |
throw new ConversionException("The value " + value + " can't be converted to a Color"); |
| 548 |
|
} |
| 549 |
1 |
|
| 550 |
|
|
| 551 |
40 |
if (color.startsWith("#")) |
| 552 |
18 |
{ |
| 553 |
4 |
color = color.substring(1); |
| 554 |
|
} |
| 555 |
|
|
| 556 |
2 |
try |
| 557 |
|
{ |
| 558 |
|
|
| 559 |
148 |
for (int i = 0; i < components.length; i++) |
| 560 |
|
{ |
| 561 |
114 |
components[i] = Integer.parseInt(color.substring(2 * i, 2 * i + 2), HEX_RADIX); |
| 562 |
|
} |
| 563 |
|
|
| 564 |
|
|
| 565 |
|
int alpha; |
| 566 |
36 |
if (color.length() >= minlength + 2) |
| 567 |
|
{ |
| 568 |
2 |
alpha = Integer.parseInt(color.substring(minlength, minlength + 2), HEX_RADIX); |
| 569 |
|
} |
| 570 |
|
else |
| 571 |
|
{ |
| 572 |
34 |
alpha = Color.black.getAlpha(); |
| 573 |
|
} |
| 574 |
|
|
| 575 |
76 |
return new Color(components[0], components[1], components[2], alpha); |
| 576 |
|
} |
| 577 |
15 |
catch (Exception e) |
| 578 |
|
{ |
| 579 |
29 |
throw new ConversionException("The value " + value + " can't be converted to a Color", e); |
| 580 |
|
} |
| 581 |
5 |
} |
| 582 |
|
else |
| 583 |
20 |
{ |
| 584 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Color"); |
| 585 |
|
} |
| 586 |
|
} |
| 587 |
18 |
|
| 588 |
|
|
| 589 |
|
|
| 590 |
|
|
| 591 |
2 |
|
| 592 |
|
|
| 593 |
|
|
| 594 |
|
|
| 595 |
|
|
| 596 |
2 |
public static Date toDate(Object value, String format) throws ConversionException |
| 597 |
|
{ |
| 598 |
80 |
if (value instanceof Date) |
| 599 |
|
{ |
| 600 |
30 |
return (Date) value; |
| 601 |
|
} |
| 602 |
50 |
else if (value instanceof Calendar) |
| 603 |
|
{ |
| 604 |
10 |
return ((Calendar) value).getTime(); |
| 605 |
|
} |
| 606 |
40 |
else if (value instanceof String) |
| 607 |
|
{ |
| 608 |
|
try |
| 609 |
|
{ |
| 610 |
76 |
return new SimpleDateFormat(format).parse((String) value); |
| 611 |
|
} |
| 612 |
10 |
catch (ParseException e) |
| 613 |
|
{ |
| 614 |
34 |
throw new ConversionException("The value " + value + " can't be converted to a Date", e); |
| 615 |
|
} |
| 616 |
10 |
} |
| 617 |
10 |
else |
| 618 |
10 |
{ |
| 619 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Date"); |
| 620 |
20 |
} |
| 621 |
|
} |
| 622 |
|
|
| 623 |
|
|
| 624 |
18 |
|
| 625 |
18 |
|
| 626 |
16 |
|
| 627 |
|
|
| 628 |
|
|
| 629 |
|
|
| 630 |
2 |
|
| 631 |
|
public static Calendar toCalendar(Object value, String format) throws ConversionException |
| 632 |
|
{ |
| 633 |
80 |
if (value instanceof Calendar) |
| 634 |
|
{ |
| 635 |
22 |
return (Calendar) value; |
| 636 |
|
} |
| 637 |
60 |
else if (value instanceof Date) |
| 638 |
|
{ |
| 639 |
20 |
Calendar calendar = Calendar.getInstance(); |
| 640 |
20 |
calendar.setTime((Date) value); |
| 641 |
20 |
return calendar; |
| 642 |
|
} |
| 643 |
40 |
else if (value instanceof String) |
| 644 |
|
{ |
| 645 |
|
try |
| 646 |
|
{ |
| 647 |
36 |
Calendar calendar = Calendar.getInstance(); |
| 648 |
36 |
calendar.setTime(new SimpleDateFormat(format).parse((String) value)); |
| 649 |
32 |
return calendar; |
| 650 |
|
} |
| 651 |
|
catch (ParseException e) |
| 652 |
|
{ |
| 653 |
4 |
throw new ConversionException("The value " + value + " can't be converted to a Calendar", e); |
| 654 |
|
} |
| 655 |
|
} |
| 656 |
29122 |
else |
| 657 |
|
{ |
| 658 |
7 |
throw new ConversionException("The value " + value + " can't be converted to a Calendar"); |
| 659 |
|
} |
| 660 |
29119 |
} |
| 661 |
|
|
| 662 |
16459 |
|
| 663 |
16459 |
|
| 664 |
|
|
| 665 |
1413 |
|
| 666 |
|
|
| 667 |
|
|
| 668 |
|
|
| 669 |
15046 |
|
| 670 |
|
|
| 671 |
|
|
| 672 |
12660 |
|
| 673 |
|
|
| 674 |
521 |
|
| 675 |
|
|
| 676 |
12139 |
|
| 677 |
|
public static Iterator toIterator(Object value, char delimiter) |
| 678 |
787 |
{ |
| 679 |
58244 |
if (value == null) |
| 680 |
11352 |
{ |
| 681 |
6 |
return IteratorUtils.emptyIterator(); |
| 682 |
1308 |
} |
| 683 |
59546 |
if (value instanceof String) |
| 684 |
5372 |
{ |
| 685 |
32918 |
String s = (String) value; |
| 686 |
35674 |
if (s.indexOf(delimiter) > 0) |
| 687 |
|
{ |
| 688 |
4134 |
return split((String) value, delimiter).iterator(); |
| 689 |
|
} |
| 690 |
|
else |
| 691 |
|
{ |
| 692 |
40136 |
return new SingletonIterator(value); |
| 693 |
|
} |
| 694 |
|
} |
| 695 |
25320 |
else if (value instanceof Collection) |
| 696 |
|
{ |
| 697 |
1042 |
return toIterator(((Collection) value).iterator(), delimiter); |
| 698 |
|
} |
| 699 |
24278 |
else if (value.getClass().isArray()) |
| 700 |
|
{ |
| 701 |
1574 |
return toIterator(IteratorUtils.arrayIterator(value), delimiter); |
| 702 |
|
} |
| 703 |
22704 |
else if (value instanceof Iterator) |
| 704 |
|
{ |
| 705 |
2616 |
Iterator iterator = (Iterator) value; |
| 706 |
2616 |
IteratorChain chain = new IteratorChain(); |
| 707 |
10744 |
while (iterator.hasNext()) |
| 708 |
|
{ |
| 709 |
5512 |
chain.addIterator(toIterator(iterator.next(), delimiter)); |
| 710 |
|
} |
| 711 |
2616 |
return chain; |
| 712 |
|
} |
| 713 |
|
else |
| 714 |
|
{ |
| 715 |
20088 |
return new SingletonIterator(value); |
| 716 |
|
} |
| 717 |
|
} |
| 718 |
|
} |