1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
package org.apache.commons.el; |
18 | |
|
19 | |
import java.beans.PropertyEditor; |
20 | |
import java.beans.PropertyEditorManager; |
21 | |
import java.math.BigDecimal; |
22 | |
import java.math.BigInteger; |
23 | |
|
24 | |
import javax.servlet.jsp.el.ELException; |
25 | |
|
26 | |
import org.apache.commons.logging.Log; |
27 | |
import org.apache.commons.logging.LogFactory; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | |
|
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
|
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
|
165 | |
|
166 | |
|
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
|
186 | |
|
187 | |
|
188 | |
|
189 | |
|
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
|
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
|
204 | |
|
205 | |
|
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
|
219 | |
|
220 | |
|
221 | |
|
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
|
229 | |
|
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
|
244 | |
|
245 | |
|
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
|
251 | |
|
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
|
267 | |
|
268 | |
|
269 | |
|
270 | |
|
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
|
277 | |
|
278 | |
|
279 | |
|
280 | 0 | public class Coercions |
281 | |
{ |
282 | |
|
283 | |
|
284 | |
|
285 | 1 | private static final Number ZERO = new Integer(0); |
286 | 6 | private static Log log = LogFactory.getLog(Coercions.class); |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
|
293 | |
public static Object coerce (Object pValue, |
294 | |
Class pClass) |
295 | |
throws ELException |
296 | |
{ |
297 | 9 | if (pClass == String.class) { |
298 | 9 | return coerceToString (pValue); |
299 | |
} |
300 | 0 | else if (isNumberClass (pClass)) { |
301 | 0 | return coerceToPrimitiveNumber (pValue, pClass); |
302 | |
} |
303 | 0 | else if (pClass == Character.class || |
304 | |
pClass == Character.TYPE) { |
305 | 0 | return coerceToCharacter (pValue); |
306 | |
} |
307 | 0 | else if (pClass == Boolean.class || |
308 | |
pClass == Boolean.TYPE) { |
309 | 0 | return coerceToBoolean (pValue); |
310 | |
} |
311 | |
else { |
312 | 0 | return coerceToObject (pValue, pClass); |
313 | |
} |
314 | |
} |
315 | |
|
316 | |
|
317 | |
|
318 | |
|
319 | |
|
320 | |
|
321 | |
|
322 | |
static boolean isNumberClass (Class pClass) |
323 | |
{ |
324 | 0 | return |
325 | |
pClass == Byte.class || |
326 | |
pClass == Byte.TYPE || |
327 | |
pClass == Short.class || |
328 | |
pClass == Short.TYPE || |
329 | |
pClass == Integer.class || |
330 | |
pClass == Integer.TYPE || |
331 | |
pClass == Long.class || |
332 | |
pClass == Long.TYPE || |
333 | |
pClass == Float.class || |
334 | |
pClass == Float.TYPE || |
335 | |
pClass == Double.class || |
336 | |
pClass == Double.TYPE || |
337 | |
pClass == BigInteger.class || |
338 | |
pClass == BigDecimal.class; |
339 | |
} |
340 | |
|
341 | |
|
342 | |
|
343 | |
|
344 | |
|
345 | |
|
346 | |
public static String coerceToString (Object pValue) |
347 | |
throws ELException |
348 | |
{ |
349 | 9 | if (pValue == null) { |
350 | 0 | return ""; |
351 | |
} |
352 | 9 | else if (pValue instanceof String) { |
353 | 9 | return (String) pValue; |
354 | |
} |
355 | |
else { |
356 | |
try { |
357 | 0 | return pValue.toString (); |
358 | |
} |
359 | 0 | catch (Exception exc) { |
360 | 0 | if (log.isErrorEnabled()) { |
361 | 0 | String message = MessageUtil.getMessageWithArgs( |
362 | |
Constants.TOSTRING_EXCEPTION, |
363 | |
pValue.getClass().getName()); |
364 | 0 | log.error(message, exc); |
365 | 0 | throw new ELException(exc); |
366 | |
} |
367 | 0 | return ""; |
368 | |
} |
369 | |
} |
370 | |
} |
371 | |
|
372 | |
|
373 | |
|
374 | |
|
375 | |
|
376 | |
|
377 | |
public static Number coerceToPrimitiveNumber (Object pValue, |
378 | |
Class pClass) |
379 | |
throws ELException |
380 | |
{ |
381 | 4 | if (pValue == null || |
382 | |
"".equals (pValue)) { |
383 | 0 | return coerceToPrimitiveNumber (ZERO, pClass); |
384 | |
} |
385 | 4 | else if (pValue instanceof Character) { |
386 | 0 | char val = ((Character) pValue).charValue (); |
387 | 0 | return coerceToPrimitiveNumber (new Short((short) val), pClass); |
388 | |
} |
389 | 4 | else if (pValue instanceof Boolean) { |
390 | 0 | if (log.isErrorEnabled()) { |
391 | 0 | String message = MessageUtil.getMessageWithArgs( |
392 | |
Constants.BOOLEAN_TO_NUMBER, pValue, pClass.getName()); |
393 | 0 | log.error(message); |
394 | 0 | throw new ELException(message); |
395 | |
} |
396 | 0 | return coerceToPrimitiveNumber(ZERO, pClass); |
397 | |
} |
398 | 4 | else if (pValue.getClass () == pClass) { |
399 | 4 | return (Number) pValue; |
400 | |
} |
401 | 0 | else if (pValue instanceof Number) { |
402 | 0 | return coerceToPrimitiveNumber ((Number) pValue, pClass); |
403 | |
} |
404 | 0 | else if (pValue instanceof String) { |
405 | |
try { |
406 | 0 | return coerceToPrimitiveNumber ((String) pValue, pClass); |
407 | |
} |
408 | 0 | catch (Exception exc) { |
409 | 0 | if (log.isErrorEnabled()) { |
410 | 0 | String message = MessageUtil.getMessageWithArgs( |
411 | |
Constants.STRING_TO_NUMBER_EXCEPTION, |
412 | |
(String) pValue, pClass.getName()); |
413 | 0 | log.error(message); |
414 | 0 | throw new ELException(message); |
415 | |
} |
416 | 0 | return coerceToPrimitiveNumber (ZERO, pClass); |
417 | |
} |
418 | |
} |
419 | |
else { |
420 | 0 | if (log.isErrorEnabled()) { |
421 | 0 | String message = MessageUtil.getMessageWithArgs( |
422 | |
Constants.COERCE_TO_NUMBER, |
423 | |
pValue.getClass().getName(), |
424 | |
pClass.getName()); |
425 | 0 | log.error(message); |
426 | 0 | throw new ELException(message); |
427 | |
} |
428 | 0 | return coerceToPrimitiveNumber (0, pClass); |
429 | |
} |
430 | |
} |
431 | |
|
432 | |
|
433 | |
|
434 | |
|
435 | |
|
436 | |
|
437 | |
|
438 | |
public static Integer coerceToInteger (Object pValue) |
439 | |
throws ELException |
440 | |
{ |
441 | 0 | if (pValue == null) { |
442 | 0 | return null; |
443 | |
} |
444 | 0 | else if (pValue instanceof Character) { |
445 | 0 | return PrimitiveObjects.getInteger |
446 | |
((int) (((Character) pValue).charValue ())); |
447 | |
} |
448 | 0 | else if (pValue instanceof Boolean) { |
449 | 0 | if (log.isWarnEnabled()) { |
450 | 0 | log.warn( |
451 | |
MessageUtil.getMessageWithArgs( |
452 | |
Constants.BOOLEAN_TO_NUMBER, pValue, Integer.class.getName())); |
453 | |
} |
454 | 0 | return PrimitiveObjects.getInteger |
455 | |
(((Boolean) pValue).booleanValue () ? 1 : 0); |
456 | |
} |
457 | 0 | else if (pValue instanceof Integer) { |
458 | 0 | return (Integer) pValue; |
459 | |
} |
460 | 0 | else if (pValue instanceof Number) { |
461 | 0 | return PrimitiveObjects.getInteger (((Number) pValue).intValue ()); |
462 | |
} |
463 | 0 | else if (pValue instanceof String) { |
464 | |
try { |
465 | 0 | return Integer.valueOf ((String) pValue); |
466 | |
} |
467 | 0 | catch (Exception exc) { |
468 | 0 | if (log.isWarnEnabled()) { |
469 | 0 | log.warn( |
470 | |
MessageUtil.getMessageWithArgs( |
471 | |
Constants.STRING_TO_NUMBER_EXCEPTION, |
472 | |
(String) pValue, |
473 | |
Integer.class.getName())); |
474 | |
} |
475 | 0 | return null; |
476 | |
} |
477 | |
} |
478 | |
else { |
479 | 0 | if (log.isWarnEnabled()) { |
480 | 0 | log.warn( |
481 | |
MessageUtil.getMessageWithArgs( |
482 | |
Constants.COERCE_TO_NUMBER, |
483 | |
pValue.getClass().getName(), |
484 | |
Integer.class.getName())); |
485 | |
} |
486 | 0 | return null; |
487 | |
} |
488 | |
} |
489 | |
|
490 | |
|
491 | |
|
492 | |
|
493 | |
|
494 | |
|
495 | |
static Number coerceToPrimitiveNumber (long pValue, |
496 | |
Class pClass) |
497 | |
throws ELException |
498 | |
{ |
499 | 0 | if (pClass == Byte.class || pClass == Byte.TYPE) { |
500 | 0 | return PrimitiveObjects.getByte ((byte) pValue); |
501 | |
} |
502 | 0 | else if (pClass == Short.class || pClass == Short.TYPE) { |
503 | 0 | return PrimitiveObjects.getShort ((short) pValue); |
504 | |
} |
505 | 0 | else if (pClass == Integer.class || pClass == Integer.TYPE) { |
506 | 0 | return PrimitiveObjects.getInteger ((int) pValue); |
507 | |
} |
508 | 0 | else if (pClass == Long.class || pClass == Long.TYPE) { |
509 | 0 | return PrimitiveObjects.getLong (pValue); |
510 | |
} |
511 | 0 | else if (pClass == Float.class || pClass == Float.TYPE) { |
512 | 0 | return PrimitiveObjects.getFloat ((float) pValue); |
513 | |
} |
514 | 0 | else if (pClass == Double.class || pClass == Double.TYPE) { |
515 | 0 | return PrimitiveObjects.getDouble ((double) pValue); |
516 | |
} |
517 | |
else { |
518 | 0 | return PrimitiveObjects.getInteger (0); |
519 | |
} |
520 | |
} |
521 | |
|
522 | |
|
523 | |
|
524 | |
|
525 | |
|
526 | |
|
527 | |
static Number coerceToPrimitiveNumber (double pValue, |
528 | |
Class pClass) |
529 | |
throws ELException |
530 | |
{ |
531 | 0 | if (pClass == Byte.class || pClass == Byte.TYPE) { |
532 | 0 | return PrimitiveObjects.getByte ((byte) pValue); |
533 | |
} |
534 | 0 | else if (pClass == Short.class || pClass == Short.TYPE) { |
535 | 0 | return PrimitiveObjects.getShort ((short) pValue); |
536 | |
} |
537 | 0 | else if (pClass == Integer.class || pClass == Integer.TYPE) { |
538 | 0 | return PrimitiveObjects.getInteger ((int) pValue); |
539 | |
} |
540 | 0 | else if (pClass == Long.class || pClass == Long.TYPE) { |
541 | 0 | return PrimitiveObjects.getLong ((long) pValue); |
542 | |
} |
543 | 0 | else if (pClass == Float.class || pClass == Float.TYPE) { |
544 | 0 | return PrimitiveObjects.getFloat ((float) pValue); |
545 | |
} |
546 | 0 | else if (pClass == Double.class || pClass == Double.TYPE) { |
547 | 0 | return PrimitiveObjects.getDouble (pValue); |
548 | |
} |
549 | |
else { |
550 | 0 | return PrimitiveObjects.getInteger (0); |
551 | |
} |
552 | |
} |
553 | |
|
554 | |
|
555 | |
|
556 | |
|
557 | |
|
558 | |
|
559 | |
static Number coerceToPrimitiveNumber (Number pValue, Class pClass) |
560 | |
throws ELException |
561 | |
{ |
562 | 0 | if (pClass == Byte.class || pClass == Byte.TYPE) { |
563 | 0 | return PrimitiveObjects.getByte (pValue.byteValue ()); |
564 | |
} |
565 | 0 | else if (pClass == Short.class || pClass == Short.TYPE) { |
566 | 0 | return PrimitiveObjects.getShort (pValue.shortValue ()); |
567 | |
} |
568 | 0 | else if (pClass == Integer.class || pClass == Integer.TYPE) { |
569 | 0 | return PrimitiveObjects.getInteger (pValue.intValue ()); |
570 | |
} |
571 | 0 | else if (pClass == Long.class || pClass == Long.TYPE) { |
572 | 0 | return PrimitiveObjects.getLong (pValue.longValue ()); |
573 | |
} |
574 | 0 | else if (pClass == Float.class || pClass == Float.TYPE) { |
575 | 0 | return PrimitiveObjects.getFloat (pValue.floatValue ()); |
576 | |
} |
577 | 0 | else if (pClass == Double.class || pClass == Double.TYPE) { |
578 | 0 | return PrimitiveObjects.getDouble (pValue.doubleValue ()); |
579 | |
} |
580 | 0 | else if (pClass == BigInteger.class) { |
581 | 0 | if (pValue instanceof BigDecimal) |
582 | 0 | return ((BigDecimal) pValue).toBigInteger(); |
583 | |
else |
584 | 0 | return BigInteger.valueOf(pValue.longValue()); |
585 | |
} |
586 | 0 | else if (pClass == BigDecimal.class) { |
587 | 0 | if (pValue instanceof BigInteger) |
588 | 0 | return new BigDecimal((BigInteger) pValue); |
589 | |
else |
590 | 0 | return new BigDecimal(pValue.doubleValue()); |
591 | |
} |
592 | |
else { |
593 | 0 | return PrimitiveObjects.getInteger (0); |
594 | |
} |
595 | |
} |
596 | |
|
597 | |
|
598 | |
|
599 | |
|
600 | |
|
601 | |
|
602 | |
static Number coerceToPrimitiveNumber (String pValue, Class pClass) |
603 | |
throws ELException |
604 | |
{ |
605 | 0 | if (pClass == Byte.class || pClass == Byte.TYPE) { |
606 | 0 | return Byte.valueOf (pValue); |
607 | |
} |
608 | 0 | else if (pClass == Short.class || pClass == Short.TYPE) { |
609 | 0 | return Short.valueOf (pValue); |
610 | |
} |
611 | 0 | else if (pClass == Integer.class || pClass == Integer.TYPE) { |
612 | 0 | return Integer.valueOf (pValue); |
613 | |
} |
614 | 0 | else if (pClass == Long.class || pClass == Long.TYPE) { |
615 | 0 | return Long.valueOf (pValue); |
616 | |
} |
617 | 0 | else if (pClass == Float.class || pClass == Float.TYPE) { |
618 | 0 | return Float.valueOf (pValue); |
619 | |
} |
620 | 0 | else if (pClass == Double.class || pClass == Double.TYPE) { |
621 | 0 | return Double.valueOf (pValue); |
622 | |
} |
623 | 0 | else if (pClass == BigInteger.class) { |
624 | 0 | return new BigInteger(pValue); |
625 | |
} |
626 | 0 | else if (pClass == BigDecimal.class) { |
627 | 0 | return new BigDecimal(pValue); |
628 | |
} |
629 | |
else { |
630 | 0 | return PrimitiveObjects.getInteger (0); |
631 | |
} |
632 | |
} |
633 | |
|
634 | |
|
635 | |
|
636 | |
|
637 | |
|
638 | |
|
639 | |
public static Character coerceToCharacter (Object pValue) |
640 | |
throws ELException |
641 | |
{ |
642 | 0 | if (pValue == null || |
643 | |
"".equals (pValue)) { |
644 | 0 | return PrimitiveObjects.getCharacter ((char) 0); |
645 | |
} |
646 | 0 | else if (pValue instanceof Character) { |
647 | 0 | return (Character) pValue; |
648 | |
} |
649 | 0 | else if (pValue instanceof Boolean) { |
650 | 0 | if (log.isErrorEnabled()) { |
651 | 0 | String message = MessageUtil.getMessageWithArgs( |
652 | |
Constants.BOOLEAN_TO_CHARACTER, pValue); |
653 | 0 | log.error(message); |
654 | 0 | throw new ELException(message); |
655 | |
} |
656 | 0 | return PrimitiveObjects.getCharacter ((char) 0); |
657 | |
} |
658 | 0 | else if (pValue instanceof Number) { |
659 | 0 | return PrimitiveObjects.getCharacter |
660 | |
((char) ((Number) pValue).shortValue ()); |
661 | |
} |
662 | 0 | else if (pValue instanceof String) { |
663 | 0 | String str = (String) pValue; |
664 | 0 | return PrimitiveObjects.getCharacter (str.charAt (0)); |
665 | |
} |
666 | |
else { |
667 | 0 | if (log.isErrorEnabled()) { |
668 | 0 | String message = MessageUtil.getMessageWithArgs( |
669 | |
Constants.COERCE_TO_CHARACTER, |
670 | |
pValue.getClass().getName()); |
671 | 0 | log.error(message); |
672 | 0 | throw new ELException(message); |
673 | |
} |
674 | 0 | return PrimitiveObjects.getCharacter ((char) 0); |
675 | |
} |
676 | |
} |
677 | |
|
678 | |
|
679 | |
|
680 | |
|
681 | |
|
682 | |
|
683 | |
public static Boolean coerceToBoolean (Object pValue) |
684 | |
throws ELException |
685 | |
{ |
686 | 0 | if (pValue == null || |
687 | |
"".equals (pValue)) { |
688 | 0 | return Boolean.FALSE; |
689 | |
} |
690 | 0 | else if (pValue instanceof Boolean) { |
691 | 0 | return (Boolean) pValue; |
692 | |
} |
693 | 0 | else if (pValue instanceof String) { |
694 | 0 | String str = (String) pValue; |
695 | |
try { |
696 | 0 | return Boolean.valueOf (str); |
697 | |
} |
698 | 0 | catch (Exception exc) { |
699 | 0 | if (log.isErrorEnabled()) { |
700 | 0 | String message = MessageUtil.getMessageWithArgs( |
701 | |
Constants.STRING_TO_BOOLEAN, (String) pValue); |
702 | 0 | log.error(message, exc); |
703 | 0 | throw new ELException(message, exc); |
704 | |
} |
705 | 0 | return Boolean.FALSE; |
706 | |
} |
707 | |
} |
708 | |
else { |
709 | 0 | if (log.isErrorEnabled()) { |
710 | 0 | String message = MessageUtil.getMessageWithArgs( |
711 | |
Constants.COERCE_TO_BOOLEAN, |
712 | |
pValue.getClass().getName()); |
713 | 0 | log.error(message); |
714 | 0 | throw new ELException(message); |
715 | |
} |
716 | 0 | return Boolean.TRUE; |
717 | |
} |
718 | |
} |
719 | |
|
720 | |
|
721 | |
|
722 | |
|
723 | |
|
724 | |
|
725 | |
|
726 | |
public static Object coerceToObject (Object pValue, Class pClass) |
727 | |
throws ELException |
728 | |
{ |
729 | 0 | if (pValue == null) { |
730 | 0 | return null; |
731 | |
} |
732 | 0 | else if (pClass.isAssignableFrom (pValue.getClass ())) { |
733 | 0 | return pValue; |
734 | |
} |
735 | 0 | else if (pValue instanceof String) { |
736 | 0 | String str = (String) pValue; |
737 | 0 | PropertyEditor pe = PropertyEditorManager.findEditor (pClass); |
738 | 0 | if (pe == null) { |
739 | 0 | if ("".equals (str)) { |
740 | 0 | return null; |
741 | |
} |
742 | |
else { |
743 | 0 | if (log.isErrorEnabled()) { |
744 | 0 | String message = MessageUtil.getMessageWithArgs( |
745 | |
Constants.NO_PROPERTY_EDITOR, |
746 | |
str, pClass.getName()); |
747 | 0 | log.error(message); |
748 | 0 | throw new ELException(message); |
749 | |
} |
750 | 0 | return null; |
751 | |
} |
752 | |
} |
753 | |
try { |
754 | 0 | pe.setAsText (str); |
755 | 0 | return pe.getValue (); |
756 | |
} |
757 | 0 | catch (IllegalArgumentException exc) { |
758 | 0 | if ("".equals (str)) { |
759 | 0 | return null; |
760 | |
} |
761 | |
else { |
762 | 0 | if (log.isErrorEnabled()) { |
763 | 0 | String message = MessageUtil.getMessageWithArgs( |
764 | |
Constants.PROPERTY_EDITOR_ERROR, |
765 | |
pValue, |
766 | |
pClass.getName()); |
767 | 0 | log.error(message, exc); |
768 | 0 | throw new ELException(message, exc); |
769 | |
} |
770 | 0 | return null; |
771 | |
} |
772 | |
} |
773 | |
} |
774 | |
else { |
775 | 0 | if (log.isErrorEnabled()) { |
776 | 0 | String message = MessageUtil.getMessageWithArgs( |
777 | |
Constants.COERCE_TO_OBJECT, |
778 | |
pValue.getClass().getName(), |
779 | |
pClass.getName()); |
780 | 0 | log.error(message); |
781 | 0 | throw new ELException(message); |
782 | |
} |
783 | 0 | return null; |
784 | |
} |
785 | |
} |
786 | |
|
787 | |
|
788 | |
|
789 | |
|
790 | |
|
791 | |
|
792 | |
|
793 | |
|
794 | |
|
795 | |
public static Object applyArithmeticOperator |
796 | |
(Object pLeft, |
797 | |
Object pRight, |
798 | |
ArithmeticOperator pOperator) |
799 | |
throws ELException |
800 | |
{ |
801 | 2 | if (pLeft == null && |
802 | |
pRight == null) { |
803 | 0 | if (log.isWarnEnabled()) { |
804 | 0 | log.warn( |
805 | |
MessageUtil.getMessageWithArgs( |
806 | |
Constants.ARITH_OP_NULL, |
807 | |
pOperator.getOperatorSymbol())); |
808 | |
} |
809 | 0 | return PrimitiveObjects.getInteger (0); |
810 | |
} |
811 | |
|
812 | 2 | else if (isBigDecimal(pLeft) || isBigDecimal(pRight)) { |
813 | 0 | BigDecimal left = (BigDecimal) |
814 | |
coerceToPrimitiveNumber(pLeft, BigDecimal.class); |
815 | 0 | BigDecimal right = (BigDecimal) |
816 | |
coerceToPrimitiveNumber(pRight, BigDecimal.class); |
817 | 0 | return pOperator.apply(left, right); |
818 | |
} |
819 | |
|
820 | 2 | else if (isFloatingPointType(pLeft) || |
821 | |
isFloatingPointType(pRight) || |
822 | |
isFloatingPointString(pLeft) || |
823 | |
isFloatingPointString(pRight)) { |
824 | 0 | if (isBigInteger(pLeft) || isBigInteger(pRight)) { |
825 | 0 | BigDecimal left = (BigDecimal) |
826 | |
coerceToPrimitiveNumber(pLeft, BigDecimal.class); |
827 | 0 | BigDecimal right = (BigDecimal) |
828 | |
coerceToPrimitiveNumber(pRight, BigDecimal.class); |
829 | 0 | return pOperator.apply(left, right); |
830 | |
} else { |
831 | 0 | double left = |
832 | |
coerceToPrimitiveNumber(pLeft, Double.class). |
833 | |
doubleValue(); |
834 | 0 | double right = |
835 | |
coerceToPrimitiveNumber(pRight, Double.class). |
836 | |
doubleValue(); |
837 | 0 | return |
838 | |
PrimitiveObjects.getDouble(pOperator.apply(left, right)); |
839 | |
} |
840 | |
} |
841 | |
|
842 | 2 | else if (isBigInteger(pLeft) || isBigInteger(pRight)) { |
843 | 0 | BigInteger left = (BigInteger) |
844 | |
coerceToPrimitiveNumber(pLeft, BigInteger.class); |
845 | 0 | BigInteger right = (BigInteger) |
846 | |
coerceToPrimitiveNumber(pRight, BigInteger.class); |
847 | 0 | return pOperator.apply(left, right); |
848 | |
} |
849 | |
|
850 | |
else { |
851 | 2 | long left = |
852 | |
coerceToPrimitiveNumber (pLeft, Long.class). |
853 | |
longValue (); |
854 | 2 | long right = |
855 | |
coerceToPrimitiveNumber (pRight, Long.class). |
856 | |
longValue (); |
857 | 2 | return |
858 | |
PrimitiveObjects.getLong (pOperator.apply (left, right)); |
859 | |
} |
860 | |
} |
861 | |
|
862 | |
|
863 | |
|
864 | |
|
865 | |
|
866 | |
|
867 | |
|
868 | |
public static Object applyRelationalOperator |
869 | |
(Object pLeft, |
870 | |
Object pRight, |
871 | |
RelationalOperator pOperator) |
872 | |
throws ELException |
873 | |
{ |
874 | 0 | if (isBigDecimal(pLeft) || isBigDecimal(pRight)) { |
875 | 0 | BigDecimal left = (BigDecimal) |
876 | |
coerceToPrimitiveNumber(pLeft, BigDecimal.class); |
877 | 0 | BigDecimal right = (BigDecimal) |
878 | |
coerceToPrimitiveNumber(pRight, BigDecimal.class); |
879 | 0 | return PrimitiveObjects.getBoolean(pOperator.apply(left, right)); |
880 | |
} |
881 | |
|
882 | 0 | else if (isFloatingPointType (pLeft) || |
883 | |
isFloatingPointType (pRight)) { |
884 | 0 | double left = |
885 | |
coerceToPrimitiveNumber (pLeft, Double.class). |
886 | |
doubleValue (); |
887 | 0 | double right = |
888 | |
coerceToPrimitiveNumber (pRight, Double.class). |
889 | |
doubleValue (); |
890 | 0 | return |
891 | |
PrimitiveObjects.getBoolean (pOperator.apply (left, right)); |
892 | |
} |
893 | |
|
894 | 0 | else if (isBigInteger(pLeft) || isBigInteger(pRight)) { |
895 | 0 | BigInteger left = (BigInteger) |
896 | |
coerceToPrimitiveNumber(pLeft, BigInteger.class); |
897 | 0 | BigInteger right = (BigInteger) |
898 | |
coerceToPrimitiveNumber(pRight, BigInteger.class); |
899 | 0 | return PrimitiveObjects.getBoolean(pOperator.apply(left, right)); |
900 | |
} |
901 | |
|
902 | 0 | else if (isIntegerType (pLeft) || |
903 | |
isIntegerType (pRight)) { |
904 | 0 | long left = |
905 | |
coerceToPrimitiveNumber (pLeft, Long.class). |
906 | |
longValue (); |
907 | 0 | long right = |
908 | |
coerceToPrimitiveNumber (pRight, Long.class). |
909 | |
longValue (); |
910 | 0 | return |
911 | |
PrimitiveObjects.getBoolean (pOperator.apply (left, right)); |
912 | |
} |
913 | |
|
914 | 0 | else if (pLeft instanceof String || |
915 | |
pRight instanceof String) { |
916 | 0 | String left = coerceToString (pLeft); |
917 | 0 | String right = coerceToString (pRight); |
918 | 0 | return |
919 | |
PrimitiveObjects.getBoolean (pOperator.apply (left, right)); |
920 | |
} |
921 | |
|
922 | 0 | else if (pLeft instanceof Comparable) { |
923 | |
try { |
924 | 0 | int result = ((Comparable) pLeft).compareTo (pRight); |
925 | 0 | return |
926 | |
PrimitiveObjects.getBoolean |
927 | |
(pOperator.apply (result, -result)); |
928 | |
} |
929 | 0 | catch (Exception exc) { |
930 | 0 | if (log.isErrorEnabled()) { |
931 | 0 | String message = MessageUtil.getMessageWithArgs( |
932 | |
Constants.COMPARABLE_ERROR, |
933 | |
pLeft.getClass().getName(), |
934 | |
(pRight == null) ? "null" : pRight.getClass().getName(), |
935 | |
pOperator.getOperatorSymbol()); |
936 | 0 | log.error(message, exc); |
937 | 0 | throw new ELException(message, exc); |
938 | |
} |
939 | 0 | return Boolean.FALSE; |
940 | |
} |
941 | |
} |
942 | |
|
943 | 0 | else if (pRight instanceof Comparable) { |
944 | |
try { |
945 | 0 | int result = ((Comparable) pRight).compareTo (pLeft); |
946 | 0 | return |
947 | |
PrimitiveObjects.getBoolean |
948 | |
(pOperator.apply (-result, result)); |
949 | |
} |
950 | 0 | catch (Exception exc) { |
951 | 0 | if (log.isErrorEnabled()) { |
952 | 0 | String message = MessageUtil.getMessageWithArgs( |
953 | |
Constants.COMPARABLE_ERROR, |
954 | |
pRight.getClass().getName(), |
955 | |
(pLeft == null) ? "null" : pLeft.getClass().getName(), |
956 | |
pOperator.getOperatorSymbol()); |
957 | 0 | log.error(message, exc); |
958 | 0 | throw new ELException(message, exc); |
959 | |
} |
960 | 0 | return Boolean.FALSE; |
961 | |
} |
962 | |
} |
963 | |
|
964 | |
else { |
965 | 0 | if (log.isErrorEnabled()) { |
966 | 0 | String message = MessageUtil.getMessageWithArgs( |
967 | |
Constants.ARITH_OP_BAD_TYPE, |
968 | |
pOperator.getOperatorSymbol(), |
969 | |
pLeft.getClass().getName(), |
970 | |
pRight.getClass().getName()); |
971 | 0 | log.error(message); |
972 | 0 | throw new ELException(message); |
973 | |
} |
974 | 0 | return Boolean.FALSE; |
975 | |
} |
976 | |
} |
977 | |
|
978 | |
|
979 | |
|
980 | |
|
981 | |
|
982 | |
|
983 | |
|
984 | |
public static Object applyEqualityOperator |
985 | |
(Object pLeft, |
986 | |
Object pRight, |
987 | |
EqualityOperator pOperator) |
988 | |
throws ELException |
989 | |
{ |
990 | 0 | if (pLeft == pRight) { |
991 | 0 | return PrimitiveObjects.getBoolean (pOperator.apply (true)); |
992 | |
} |
993 | |
|
994 | 0 | else if (pLeft == null || |
995 | |
pRight == null) { |
996 | 0 | return PrimitiveObjects.getBoolean (pOperator.apply (false)); |
997 | |
} |
998 | |
|
999 | 0 | else if (isBigDecimal(pLeft) || isBigDecimal(pRight)) { |
1000 | 0 | BigDecimal left = (BigDecimal) |
1001 | |
coerceToPrimitiveNumber(pLeft, BigDecimal.class); |
1002 | 0 | BigDecimal right = (BigDecimal) |
1003 | |
coerceToPrimitiveNumber(pRight, BigDecimal.class); |
1004 | 0 | return PrimitiveObjects.getBoolean(pOperator.apply(left.equals(right))); |
1005 | |
} |
1006 | |
|
1007 | 0 | else if (isFloatingPointType (pLeft) || |
1008 | |
isFloatingPointType (pRight)) { |
1009 | 0 | double left = |
1010 | |
coerceToPrimitiveNumber (pLeft, Double.class). |
1011 | |
doubleValue (); |
1012 | 0 | double right = |
1013 | |
coerceToPrimitiveNumber (pRight, Double.class). |
1014 | |
doubleValue (); |
1015 | 0 | return |
1016 | |
PrimitiveObjects.getBoolean |
1017 | |
(pOperator.apply (left == right)); |
1018 | |
} |
1019 | |
|
1020 | 0 | else if (isBigInteger(pLeft) || isBigInteger(pRight)) { |
1021 | 0 | BigInteger left = (BigInteger) |
1022 | |
coerceToPrimitiveNumber(pLeft, BigInteger.class); |
1023 | 0 | BigInteger right = (BigInteger) |
1024 | |
coerceToPrimitiveNumber(pRight, BigInteger.class); |
1025 | 0 | return PrimitiveObjects.getBoolean(pOperator.apply(left.equals(right))); |
1026 | |
} |
1027 | |
|
1028 | 0 | else if (isIntegerType (pLeft) || |
1029 | |
isIntegerType (pRight)) { |
1030 | 0 | long left = |
1031 | |
coerceToPrimitiveNumber (pLeft, Long.class). |
1032 | |
longValue (); |
1033 | 0 | long right = |
1034 | |
coerceToPrimitiveNumber (pRight, Long.class). |
1035 | |
longValue (); |
1036 | 0 | return |
1037 | |
PrimitiveObjects.getBoolean |
1038 | |
(pOperator.apply (left == right)); |
1039 | |
} |
1040 | |
|
1041 | 0 | else if (pLeft instanceof Boolean || |
1042 | |
pRight instanceof Boolean) { |
1043 | 0 | boolean left = coerceToBoolean (pLeft).booleanValue (); |
1044 | 0 | boolean right = coerceToBoolean (pRight).booleanValue (); |
1045 | 0 | return |
1046 | |
PrimitiveObjects.getBoolean |
1047 | |
(pOperator.apply (left == right)); |
1048 | |
} |
1049 | |
|
1050 | 0 | else if (pLeft instanceof String || |
1051 | |
pRight instanceof String) { |
1052 | 0 | String left = coerceToString (pLeft); |
1053 | 0 | String right = coerceToString (pRight); |
1054 | 0 | return |
1055 | |
PrimitiveObjects.getBoolean |
1056 | |
(pOperator.apply (left.equals (right))); |
1057 | |
} |
1058 | |
|
1059 | |
else { |
1060 | |
try { |
1061 | 0 | return |
1062 | |
PrimitiveObjects.getBoolean |
1063 | |
(pOperator.apply (pLeft.equals (pRight))); |
1064 | |
} |
1065 | 0 | catch (Exception exc) { |
1066 | 0 | if (log.isErrorEnabled()) { |
1067 | 0 | String message = MessageUtil.getMessageWithArgs( |
1068 | |
Constants.ERROR_IN_EQUALS, |
1069 | |
pLeft.getClass().getName(), |
1070 | |
pRight.getClass().getName(), |
1071 | |
pOperator.getOperatorSymbol()); |
1072 | 0 | log.error(message, exc); |
1073 | 0 | throw new ELException(message, exc); |
1074 | |
} |
1075 | 0 | return Boolean.FALSE; |
1076 | |
} |
1077 | |
} |
1078 | |
} |
1079 | |
|
1080 | |
|
1081 | |
|
1082 | |
|
1083 | |
|
1084 | |
|
1085 | |
public static boolean isFloatingPointType (Object pObject) |
1086 | |
{ |
1087 | 4 | return |
1088 | |
pObject != null && |
1089 | |
isFloatingPointType (pObject.getClass ()); |
1090 | |
} |
1091 | |
|
1092 | |
|
1093 | |
|
1094 | |
|
1095 | |
|
1096 | |
|
1097 | |
public static boolean isFloatingPointType (Class pClass) |
1098 | |
{ |
1099 | 4 | return |
1100 | |
pClass == Float.class || |
1101 | |
pClass == Float.TYPE || |
1102 | |
pClass == Double.class || |
1103 | |
pClass == Double.TYPE; |
1104 | |
} |
1105 | |
|
1106 | |
|
1107 | |
|
1108 | |
|
1109 | |
|
1110 | |
|
1111 | |
|
1112 | |
public static boolean isFloatingPointString (Object pObject) |
1113 | |
{ |
1114 | 4 | if (pObject instanceof String) { |
1115 | 0 | String str = (String) pObject; |
1116 | 0 | int len = str.length (); |
1117 | 0 | for (int i = 0; i < len; i++) { |
1118 | 0 | char ch = str.charAt (i); |
1119 | 0 | if (ch == '.' || |
1120 | |
ch == 'e' || |
1121 | |
ch == 'E') { |
1122 | 0 | return true; |
1123 | |
} |
1124 | |
} |
1125 | 0 | return false; |
1126 | |
} |
1127 | |
else { |
1128 | 4 | return false; |
1129 | |
} |
1130 | |
} |
1131 | |
|
1132 | |
|
1133 | |
|
1134 | |
|
1135 | |
|
1136 | |
|
1137 | |
public static boolean isIntegerType (Object pObject) |
1138 | |
{ |
1139 | 0 | return |
1140 | |
pObject != null && |
1141 | |
isIntegerType (pObject.getClass ()); |
1142 | |
} |
1143 | |
|
1144 | |
|
1145 | |
|
1146 | |
|
1147 | |
|
1148 | |
|
1149 | |
public static boolean isIntegerType (Class pClass) |
1150 | |
{ |
1151 | 0 | return |
1152 | |
pClass == Byte.class || |
1153 | |
pClass == Byte.TYPE || |
1154 | |
pClass == Short.class || |
1155 | |
pClass == Short.TYPE || |
1156 | |
pClass == Character.class || |
1157 | |
pClass == Character.TYPE || |
1158 | |
pClass == Integer.class || |
1159 | |
pClass == Integer.TYPE || |
1160 | |
pClass == Long.class || |
1161 | |
pClass == Long.TYPE; |
1162 | |
} |
1163 | |
|
1164 | |
|
1165 | |
|
1166 | |
|
1167 | |
|
1168 | |
|
1169 | |
|
1170 | |
|
1171 | |
public static boolean isBigInteger(Object pObject) { |
1172 | 4 | return |
1173 | |
pObject != null && pObject instanceof BigInteger; |
1174 | |
} |
1175 | |
|
1176 | |
|
1177 | |
|
1178 | |
|
1179 | |
|
1180 | |
|
1181 | |
public static boolean isBigDecimal(Object pObject) { |
1182 | 4 | return |
1183 | |
pObject != null && pObject instanceof BigDecimal; |
1184 | |
} |
1185 | |
} |