KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > el > lang > ELSupport


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  *
21  * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
22  *
23  * Portions Copyright Apache Software Foundation.
24  */

25 package com.sun.el.lang;
26
27 import java.beans.PropertyEditor JavaDoc;
28 import java.beans.PropertyEditorManager JavaDoc;
29 import java.math.BigDecimal JavaDoc;
30 import java.math.BigInteger JavaDoc;
31
32 import javax.el.ELException;
33 import javax.el.PropertyNotFoundException;
34
35 import com.sun.el.util.MessageFactory;
36
37 /**
38  * A helper class that implements the EL Specification
39  *
40  * @author Jacob Hookom [jacob@hookom.net]
41  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: kchung $
42  */

43 public class ELSupport {
44
45     private final static ELSupport REF = new ELSupport();
46
47     private final static Long JavaDoc ZERO = new Long JavaDoc(0L);
48
49     public final static void throwUnhandled(Object JavaDoc base, Object JavaDoc property)
50             throws ELException {
51         if (base == null) {
52             throw new PropertyNotFoundException(MessageFactory.get(
53                     "error.resolver.unhandled.null", property));
54         } else {
55             throw new PropertyNotFoundException(MessageFactory.get(
56                     "error.resolver.unhandled", base.getClass(), property));
57         }
58     }
59
60     /**
61      * @param obj0
62      * @param obj1
63      * @return
64      * @throws EvaluationException
65      */

66     public final static int compare(final Object JavaDoc obj0, final Object JavaDoc obj1)
67             throws ELException {
68         if (obj0 == obj1 || equals(obj0, obj1)) {
69             return 0;
70         }
71         if (isBigDecimalOp(obj0, obj1)) {
72             BigDecimal JavaDoc bd0 = (BigDecimal JavaDoc) coerceToNumber(obj0, BigDecimal JavaDoc.class);
73             BigDecimal JavaDoc bd1 = (BigDecimal JavaDoc) coerceToNumber(obj1, BigDecimal JavaDoc.class);
74             return bd0.compareTo(bd1);
75         }
76         if (isDoubleOp(obj0, obj1)) {
77             Double JavaDoc d0 = (Double JavaDoc) coerceToNumber(obj0, Double JavaDoc.class);
78             Double JavaDoc d1 = (Double JavaDoc) coerceToNumber(obj1, Double JavaDoc.class);
79             return d0.compareTo(d1);
80         }
81         if (isBigIntegerOp(obj0, obj1)) {
82             BigInteger JavaDoc bi0 = (BigInteger JavaDoc) coerceToNumber(obj0, BigInteger JavaDoc.class);
83             BigInteger JavaDoc bi1 = (BigInteger JavaDoc) coerceToNumber(obj1, BigInteger JavaDoc.class);
84             return bi0.compareTo(bi1);
85         }
86         if (isLongOp(obj0, obj1)) {
87             Long JavaDoc l0 = (Long JavaDoc) coerceToNumber(obj0, Long JavaDoc.class);
88             Long JavaDoc l1 = (Long JavaDoc) coerceToNumber(obj1, Long JavaDoc.class);
89             return l0.compareTo(l1);
90         }
91         if (obj0 instanceof String JavaDoc || obj1 instanceof String JavaDoc) {
92             return coerceToString(obj0).compareTo(coerceToString(obj1));
93         }
94         if (obj0 instanceof Comparable JavaDoc) {
95             return (obj1 != null) ? ((Comparable JavaDoc) obj0).compareTo(obj1) : 1;
96         }
97         if (obj1 instanceof Comparable JavaDoc) {
98             return (obj0 != null) ? -((Comparable JavaDoc) obj1).compareTo(obj0) : -1;
99         }
100         throw new ELException(MessageFactory.get("error.compare", obj0, obj1));
101     }
102
103     /**
104      * @param obj0
105      * @param obj1
106      * @return
107      * @throws EvaluationException
108      */

109     public final static boolean equals(final Object JavaDoc obj0, final Object JavaDoc obj1)
110             throws ELException {
111         if (obj0 == obj1) {
112             return true;
113         }
114         if (obj0 == null || obj1 == null) {
115             return false;
116         }
117         if (obj0 instanceof Boolean JavaDoc || obj1 instanceof Boolean JavaDoc) {
118             return coerceToBoolean(obj0).equals(coerceToBoolean(obj1));
119         }
120         if (obj0.getClass().isEnum()) {
121             return obj0.equals(coerceToEnum(obj1, obj0.getClass()));
122         }
123         if (obj1.getClass().isEnum()) {
124             return obj1.equals(coerceToEnum(obj0, obj1.getClass()));
125         }
126         if (obj0 instanceof String JavaDoc || obj1 instanceof String JavaDoc) {
127             return coerceToString(obj0).equals(coerceToString(obj1));
128         }
129         if (isBigDecimalOp(obj0, obj1)) {
130             BigDecimal JavaDoc bd0 = (BigDecimal JavaDoc) coerceToNumber(obj0, BigDecimal JavaDoc.class);
131             BigDecimal JavaDoc bd1 = (BigDecimal JavaDoc) coerceToNumber(obj1, BigDecimal JavaDoc.class);
132             return bd0.equals(bd1);
133         }
134         if (isDoubleOp(obj0, obj1)) {
135             Double JavaDoc d0 = (Double JavaDoc) coerceToNumber(obj0, Double JavaDoc.class);
136             Double JavaDoc d1 = (Double JavaDoc) coerceToNumber(obj1, Double JavaDoc.class);
137             return d0.equals(d1);
138         }
139         if (isBigIntegerOp(obj0, obj1)) {
140             BigInteger JavaDoc bi0 = (BigInteger JavaDoc) coerceToNumber(obj0, BigInteger JavaDoc.class);
141             BigInteger JavaDoc bi1 = (BigInteger JavaDoc) coerceToNumber(obj1, BigInteger JavaDoc.class);
142             return bi0.equals(bi1);
143         }
144         if (isLongOp(obj0, obj1)) {
145             Long JavaDoc l0 = (Long JavaDoc) coerceToNumber(obj0, Long JavaDoc.class);
146             Long JavaDoc l1 = (Long JavaDoc) coerceToNumber(obj1, Long JavaDoc.class);
147             return l0.equals(l1);
148         } else {
149             return obj0.equals(obj1);
150         }
151     }
152
153     /**
154      * @param obj
155      * @return
156      */

157     public final static Boolean JavaDoc coerceToBoolean(final Object JavaDoc obj)
158             throws IllegalArgumentException JavaDoc {
159         if (obj == null || "".equals(obj)) {
160             return Boolean.FALSE;
161         }
162         if (obj instanceof Boolean JavaDoc || obj.getClass() == Boolean.TYPE) {
163             return (Boolean JavaDoc) obj;
164         }
165         if (obj instanceof String JavaDoc) {
166             return Boolean.valueOf((String JavaDoc) obj);
167         }
168
169         throw new IllegalArgumentException JavaDoc(MessageFactory.get("error.convert",
170                 obj, obj.getClass(), Boolean JavaDoc.class));
171     }
172
173     public final static Enum JavaDoc coerceToEnum(final Object JavaDoc obj, Class JavaDoc type)
174             throws IllegalArgumentException JavaDoc {
175         if (obj == null || "".equals(obj)) {
176             return null;
177         }
178         if (type.isInstance(obj)) {
179             return (Enum JavaDoc)obj;
180         }
181         if (obj instanceof String JavaDoc) {
182             return Enum.valueOf(type, (String JavaDoc) obj);
183         }
184
185         throw new IllegalArgumentException JavaDoc(MessageFactory.get("error.convert",
186                 obj, obj.getClass(), type));
187     }
188
189     public final static Character JavaDoc coerceToCharacter(final Object JavaDoc obj)
190             throws IllegalArgumentException JavaDoc {
191         if (obj == null || "".equals(obj)) {
192             return new Character JavaDoc((char) 0);
193         }
194         if (obj instanceof String JavaDoc) {
195             return new Character JavaDoc(((String JavaDoc) obj).charAt(0));
196         }
197         if (ELArithmetic.isNumber(obj)) {
198             return new Character JavaDoc((char) ((Number JavaDoc) obj).shortValue());
199         }
200         Class JavaDoc objType = obj.getClass();
201         if (obj instanceof Character JavaDoc || objType == Character.TYPE) {
202             return (Character JavaDoc) obj;
203         }
204
205         throw new IllegalArgumentException JavaDoc(MessageFactory.get("error.convert",
206                 obj, objType, Character JavaDoc.class));
207     }
208
209     public final static Number JavaDoc coerceToNumber(final Object JavaDoc obj) {
210         if (obj == null) {
211             return ZERO;
212         } else if (obj instanceof Number JavaDoc) {
213             return (Number JavaDoc) obj;
214         } else {
215             String JavaDoc str = coerceToString(obj);
216             if (isStringFloat(str)) {
217                 return toFloat(str);
218             } else {
219                 return toNumber(str);
220             }
221         }
222     }
223
224     protected final static Number JavaDoc coerceToNumber(final Number JavaDoc number,
225             final Class JavaDoc type) throws IllegalArgumentException JavaDoc {
226         if (Long.TYPE == type || Long JavaDoc.class.equals(type)) {
227             return new Long JavaDoc(number.longValue());
228         }
229         if (Double.TYPE == type || Double JavaDoc.class.equals(type)) {
230             return new Double JavaDoc(number.doubleValue());
231         }
232         if (Integer.TYPE == type || Integer JavaDoc.class.equals(type)) {
233             return new Integer JavaDoc(number.intValue());
234         }
235         if (BigInteger JavaDoc.class.equals(type)) {
236             if (number instanceof BigDecimal JavaDoc) {
237                 return ((BigDecimal JavaDoc) number).toBigInteger();
238             }
239             return BigInteger.valueOf(number.longValue());
240         }
241         if (BigDecimal JavaDoc.class.equals(type)) {
242             if (number instanceof BigInteger JavaDoc) {
243                 return new BigDecimal JavaDoc((BigInteger JavaDoc) number);
244             }
245 // return new BigDecimal(number.toString());
246
return new BigDecimal JavaDoc(number.doubleValue());
247         }
248         if (Byte.TYPE == type || Byte JavaDoc.class.equals(type)) {
249             return new Byte JavaDoc(number.byteValue());
250         }
251         if (Short.TYPE == type || Short JavaDoc.class.equals(type)) {
252             return new Short JavaDoc(number.shortValue());
253         }
254         if (Float.TYPE == type || Float JavaDoc.class.equals(type)) {
255             return new Float JavaDoc(number.floatValue());
256         }
257
258         throw new IllegalArgumentException JavaDoc(MessageFactory.get("error.convert",
259                 number, number.getClass(), type));
260     }
261
262     public final static Number JavaDoc coerceToNumber(final Object JavaDoc obj, final Class JavaDoc type)
263             throws IllegalArgumentException JavaDoc {
264         if (obj == null || "".equals(obj)) {
265             return coerceToNumber(ZERO, type);
266         }
267         if (obj instanceof String JavaDoc) {
268             return coerceToNumber((String JavaDoc) obj, type);
269         }
270         if (ELArithmetic.isNumber(obj)) {
271             return coerceToNumber((Number JavaDoc) obj, type);
272         }
273
274         Class JavaDoc objType = obj.getClass();
275         if (Character JavaDoc.class.equals(objType) || Character.TYPE == objType) {
276             return coerceToNumber(new Short JavaDoc((short) ((Character JavaDoc) obj)
277                     .charValue()), type);
278         }
279
280         throw new IllegalArgumentException JavaDoc(MessageFactory.get("error.convert",
281                 obj, objType, type));
282     }
283
284     protected final static Number JavaDoc coerceToNumber(final String JavaDoc val,
285             final Class JavaDoc type) throws IllegalArgumentException JavaDoc {
286         if (Long.TYPE == type || Long JavaDoc.class.equals(type)) {
287             return Long.valueOf(val);
288         }
289         if (Integer.TYPE == type || Integer JavaDoc.class.equals(type)) {
290             return Integer.valueOf(val);
291         }
292         if (Double.TYPE == type || Double JavaDoc.class.equals(type)) {
293             return Double.valueOf(val);
294         }
295         if (BigInteger JavaDoc.class.equals(type)) {
296             return new BigInteger JavaDoc(val);
297         }
298         if (BigDecimal JavaDoc.class.equals(type)) {
299             return new BigDecimal JavaDoc(val);
300         }
301         if (Byte.TYPE == type || Byte JavaDoc.class.equals(type)) {
302             return Byte.valueOf(val);
303         }
304         if (Short.TYPE == type || Short JavaDoc.class.equals(type)) {
305             return Short.valueOf(val);
306         }
307         if (Float.TYPE == type || Float JavaDoc.class.equals(type)) {
308             return Float.valueOf(val);
309         }
310
311         throw new IllegalArgumentException JavaDoc(MessageFactory.get("error.convert",
312                 val, String JavaDoc.class, type));
313     }
314
315     /**
316      * @param obj
317      * @return
318      */

319     public final static String JavaDoc coerceToString(final Object JavaDoc obj) {
320         if (obj == null) {
321             return "";
322         } else if (obj instanceof String JavaDoc) {
323             return (String JavaDoc) obj;
324         } else if (obj instanceof Enum JavaDoc) {
325             return ((Enum JavaDoc) obj).name();
326         } else {
327             return obj.toString();
328         }
329     }
330
331     public final static Object JavaDoc coerceToType(final Object JavaDoc obj, final Class JavaDoc type)
332             throws IllegalArgumentException JavaDoc {
333         if (type == null || Object JavaDoc.class.equals(type)) {
334             return obj;
335         }
336         if (String JavaDoc.class.equals(type)) {
337             return coerceToString(obj);
338         }
339         if (ELArithmetic.isNumberType(type)) {
340             return coerceToNumber(obj, type);
341         }
342         if (Character JavaDoc.class.equals(type) || Character.TYPE == type) {
343             return coerceToCharacter(obj);
344         }
345         if (Boolean JavaDoc.class.equals(type) || Boolean.TYPE == type) {
346             return coerceToBoolean(obj);
347         }
348         if (type.isEnum()) {
349             return coerceToEnum(obj, type);
350         }
351         if (obj != null && type.isAssignableFrom(obj.getClass())) {
352             return obj;
353         }
354
355         // new to spec
356
if (obj == null)
357             return null;
358         if (obj instanceof String JavaDoc) {
359             if ("".equals(obj))
360                 return null;
361             PropertyEditor JavaDoc editor = PropertyEditorManager.findEditor(type);
362             if (editor != null) {
363                 editor.setAsText((String JavaDoc) obj);
364                 return editor.getValue();
365             }
366         }
367         throw new IllegalArgumentException JavaDoc(MessageFactory.get("error.convert",
368                 obj, obj.getClass(), type));
369     }
370
371     /**
372      * @param obj
373      * @return
374      */

375     public final static boolean containsNulls(final Object JavaDoc[] obj) {
376         for (int i = 0; i < obj.length; i++) {
377             if (obj[0] == null) {
378                 return true;
379             }
380         }
381         return false;
382     }
383
384     public final static boolean isBigDecimalOp(final Object JavaDoc obj0,
385             final Object JavaDoc obj1) {
386         return (obj0 instanceof BigDecimal JavaDoc || obj1 instanceof BigDecimal JavaDoc);
387     }
388
389     public final static boolean isBigIntegerOp(final Object JavaDoc obj0,
390             final Object JavaDoc obj1) {
391         return (obj0 instanceof BigInteger JavaDoc || obj1 instanceof BigInteger JavaDoc);
392     }
393
394     public final static boolean isDoubleOp(final Object JavaDoc obj0, final Object JavaDoc obj1) {
395         return (obj0 instanceof Double JavaDoc
396                 || obj1 instanceof Double JavaDoc
397                 || obj0 instanceof Float JavaDoc
398                 || obj1 instanceof Float JavaDoc
399                 || (obj0 != null && (Double.TYPE == obj0.getClass() || Float.TYPE == obj0
400                         .getClass())) || (obj1 != null && (Double.TYPE == obj1
401                 .getClass() || Float.TYPE == obj1.getClass())));
402     }
403
404     public final static boolean isDoubleStringOp(final Object JavaDoc obj0,
405             final Object JavaDoc obj1) {
406         return (isDoubleOp(obj0, obj1)
407                 || (obj0 instanceof String JavaDoc && isStringFloat((String JavaDoc) obj0)) || (obj1 instanceof String JavaDoc && isStringFloat((String JavaDoc) obj1)));
408     }
409
410     public final static boolean isLongOp(final Object JavaDoc obj0, final Object JavaDoc obj1) {
411         return (obj0 instanceof Long JavaDoc
412                 || obj1 instanceof Long JavaDoc
413                 || obj0 instanceof Integer JavaDoc
414                 || obj1 instanceof Integer JavaDoc
415                 || obj0 instanceof Character JavaDoc
416                 || obj1 instanceof Character JavaDoc
417                 || obj0 instanceof Short JavaDoc
418                 || obj1 instanceof Short JavaDoc
419                 || obj0 instanceof Byte JavaDoc
420                 || obj1 instanceof Byte JavaDoc
421                 || (obj0 != null && (Long.TYPE == obj0.getClass()
422                         || Integer.TYPE == obj0.getClass()
423                         || Character.TYPE == obj0.getClass()
424                         || Short.TYPE == obj0.getClass() || Byte.TYPE == obj0
425                         .getClass())) || (obj0 != null && (Long.TYPE == obj0
426                 .getClass()
427                 || Integer.TYPE == obj0.getClass()
428                 || Character.TYPE == obj0.getClass()
429                 || Short.TYPE == obj0.getClass() || Byte.TYPE == obj0
430                 .getClass())));
431     }
432
433     public final static boolean isStringFloat(final String JavaDoc str) {
434         int len = str.length();
435         if (len > 1) {
436             char c = 0;
437             for (int i = 0; i < len; i++) {
438                 switch (c = str.charAt(i)) {
439                 case 'E':
440                     return true;
441                 case 'e':
442                     return true;
443                 case '.':
444                     return true;
445                 }
446             }
447         }
448         return false;
449     }
450
451     public final static Number JavaDoc toFloat(final String JavaDoc value) {
452         try {
453             if (Double.parseDouble(value) > Double.MAX_VALUE) {
454                 return new BigDecimal JavaDoc(value);
455             } else {
456                 return new Double JavaDoc(value);
457             }
458         } catch (NumberFormatException JavaDoc e0) {
459             return new BigDecimal JavaDoc(value);
460         }
461     }
462
463     public final static Number JavaDoc toNumber(final String JavaDoc value) {
464         try {
465             return new Integer JavaDoc(Integer.parseInt(value));
466         } catch (NumberFormatException JavaDoc e0) {
467             try {
468                 return new Long JavaDoc(Long.parseLong(value));
469             } catch (NumberFormatException JavaDoc e1) {
470                 return new BigInteger JavaDoc(value);
471             }
472         }
473     }
474
475     /**
476      *
477      */

478     public ELSupport() {
479         super();
480     }
481
482 }
483
Popular Tags