KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > el > lang > ELSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.el.lang;
19
20 import java.beans.PropertyEditor JavaDoc;
21 import java.beans.PropertyEditorManager JavaDoc;
22 import java.math.BigDecimal JavaDoc;
23 import java.math.BigInteger JavaDoc;
24
25 import javax.el.ELException;
26 import javax.el.PropertyNotFoundException;
27
28 import org.apache.el.util.MessageFactory;
29
30
31 /**
32  * A helper class that implements the EL Specification
33  *
34  * @author Jacob Hookom [jacob@hookom.net]
35  * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: markt $
36  */

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

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

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

148     public final static Enum JavaDoc coerceToEnum(final Object JavaDoc obj, Class JavaDoc type) {
149         if (obj == null || "".equals(obj)) {
150             return null;
151         }
152         if (obj.getClass().isEnum()) {
153             return (Enum JavaDoc) obj;
154         }
155         return Enum.valueOf(type, obj.toString());
156     }
157
158     /**
159      * @param obj
160      * @return
161      */

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

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

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

466     public ELSupport() {
467         super();
468     }
469
470 }
471
Popular Tags