KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > el > Expr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.el;
31
32 import com.caucho.config.types.Period;
33 import com.caucho.util.BeanUtil;
34 import com.caucho.util.IntMap;
35 import com.caucho.util.L10N;
36 import com.caucho.vfs.ReadStream;
37 import com.caucho.vfs.WriteStream;
38
39 import javax.el.ELContext;
40 import javax.el.ELException;
41 import javax.el.MethodInfo;
42 import javax.el.PropertyNotFoundException;
43 import javax.el.PropertyNotWritableException;
44 import javax.el.ValueExpression;
45 import javax.servlet.jsp.JspWriter JavaDoc;
46 import java.io.IOException JavaDoc;
47 import java.io.Reader JavaDoc;
48 import java.io.Writer JavaDoc;
49 import java.lang.reflect.InvocationTargetException JavaDoc;
50 import java.lang.reflect.Method JavaDoc;
51 import java.math.BigDecimal JavaDoc;
52 import java.math.BigInteger JavaDoc;
53 import java.util.HashMap JavaDoc;
54 import java.util.Map JavaDoc;
55 import java.util.logging.Level JavaDoc;
56 import java.util.logging.Logger JavaDoc;
57
58 /**
59  * Abstract implementation class for an expression.
60  */

61 public abstract class Expr extends ValueExpression {
62   protected static final Logger JavaDoc log
63     = Logger.getLogger(Expr.class.getName());
64   protected static final L10N L = new L10N(Expr.class);
65
66   private static final Character JavaDoc NULL_CHAR
67     = new Character JavaDoc((char) 0);
68   
69   private static final long DAY = 24L * 3600L * 1000L;
70
71   private static final BigDecimal JavaDoc BIG_DECIMAL_ZERO = new BigDecimal JavaDoc("0");
72
73   private static final BigInteger JavaDoc BIG_INTEGER_ZERO = new BigInteger JavaDoc("0");
74   
75   private static final HashMap JavaDoc<Class JavaDoc,CoerceType> _coerceMap
76     = new HashMap JavaDoc<Class JavaDoc,CoerceType>();
77
78   // lexeme codes
79
final static int ADD = 1;
80   final static int SUB = ADD + 1;
81   final static int MUL = SUB + 1;
82   final static int DIV = MUL + 1;
83   final static int MOD = DIV + 1;
84   final static int EQ = MOD + 1;
85   final static int NE = EQ + 1;
86   final static int LT = NE + 1;
87   final static int LE = LT + 1;
88   final static int GT = LE + 1;
89   final static int GE = GT + 1;
90   final static int AND = GE + 1;
91   final static int OR = AND + 1;
92   
93   final static int NOT = OR + 1;
94   final static int MINUS = NOT + 1;
95   final static int EMPTY = MINUS + 1;
96
97   final static int OBJECT = 0;
98   final static int BOOLEAN = OBJECT + 1;
99   final static int BYTE = BOOLEAN + 1;
100   final static int SHORT = BYTE + 1;
101   final static int INT = SHORT + 1;
102   final static int LONG = INT + 1;
103   final static int FLOAT = LONG + 1;
104   final static int DOUBLE = FLOAT + 1;
105   
106   final static int BOOLEAN_OBJ = DOUBLE + 1;
107   final static int BYTE_OBJ = BOOLEAN_OBJ + 1;
108   final static int SHORT_OBJ = BYTE_OBJ + 1;
109   final static int INT_OBJ = SHORT_OBJ + 1;
110   final static int LONG_OBJ = INT_OBJ + 1;
111   final static int FLOAT_OBJ = LONG_OBJ + 1;
112   final static int DOUBLE_OBJ = FLOAT_OBJ + 1;
113   
114   final static int STRING = DOUBLE_OBJ + 1;
115
116   final static IntMap _typeMap = new IntMap();
117
118   /**
119    * Returns true if the expression is constant.
120    */

121   public boolean isConstant()
122   {
123     return false;
124   }
125
126   /**
127    * Returns true if the expression is read-only.
128    */

129   public boolean isReadOnly(ELContext env)
130   {
131     return true;
132   }
133
134   /**
135    * Returns true if the expression is literal text
136    */

137   public boolean isLiteralText()
138   {
139     return false;
140   }
141
142   /**
143    * Creates a field reference using this expression as the base object.
144    *
145    * @param field the expression for the field.
146    */

147   public Expr createField(Expr field)
148   {
149     return new ArrayResolverExpr(this, field);
150   }
151
152   /**
153    * Creates a field reference using this expression as the base object.
154    *
155    * @param field the string reference for the field.
156    */

157   public Expr createField(String JavaDoc field)
158   {
159     return createField(new StringLiteral(field));
160   }
161
162   /**
163    * Creates a method call using this as the <code>obj.method</code>
164    * expression
165    *
166    * @param args the arguments for the method
167    */

168   public Expr createMethod(Expr []args)
169   {
170     return new FunctionExpr(this, args);
171   }
172
173   /**
174    * Evaluates the expression, returning an object.
175    *
176    * @param env the variable environment
177    *
178    * @return the value of the expression as an object
179    */

180   abstract public Object JavaDoc getValue(ELContext env)
181     throws ELException;
182
183   /**
184    * Evaluates the expression, returning an object.
185    *
186    * @param env the variable environment
187    *
188    * @return the value of the expression as an object
189    */

190   public MethodInfo getMethodInfo(ELContext env,
191                   Class JavaDoc<?> returnType,
192                   Class JavaDoc<?> []argTypes)
193     throws ELException
194   {
195     throw new ELException(L.l("'{0}' is an illegal method expression." + getClass(),
196                   toString()));
197   }
198
199   /**
200    * Evaluates the expression, returning an object.
201    *
202    * @param env the variable environment
203    *
204    * @return the value of the expression as an object
205    */

206   public Object JavaDoc invoke(ELContext env, Class JavaDoc<?> []argTypes, Object JavaDoc []args)
207     throws ELException
208   {
209     throw new ELException(L.l("'{0}' is an illegal method expression." + getClass(),
210                   toString()));
211   }
212
213   /**
214    * Evaluates the expression, returning an object.
215    *
216    * @param env the variable environment
217    *
218    * @return the value of the expression as an object
219    */

220   public final Object JavaDoc evalObject(ELContext env)
221     throws ELException
222   {
223     return getValue(env);
224   }
225
226   /**
227    * Evaluate the expression, knowing the value should be a boolean.
228    *
229    * @param env the variable environment
230    *
231    * @return the value of the expression as a boolean
232    */

233   public boolean evalBoolean(ELContext env)
234     throws ELException
235   {
236     return toBoolean(getValue(env), env);
237   }
238
239   /**
240    * Evaluate the expression, knowing the value should be a double.
241    *
242    * @param env the variable environment
243    *
244    * @return the value of the expression as a double
245    */

246   public double evalDouble(ELContext env)
247     throws ELException
248   {
249     return toDouble(getValue(env), env);
250   }
251
252   /**
253    * Evaluate the expression, knowing the value should be a long
254    *
255    * @param env the variable environment
256    *
257    * @return the value of the expression as a double
258    */

259   public long evalLong(ELContext env)
260     throws ELException
261   {
262     return toLong(getValue(env), env);
263   }
264
265   /**
266    * Evaluate the expression, knowing the value should be a string
267    *
268    * @param env the variable environment
269    *
270    * @return the value of the expression as a string
271    */

272   public String JavaDoc evalString(ELContext env)
273     throws ELException
274   {
275     return toString(getValue(env), env);
276   }
277
278   /**
279    * Evaluate the expression, knowing the value should be a string
280    *
281    * @param env the variable environment
282    *
283    * @return the value of the expression as a string
284    */

285   public String JavaDoc evalStringWithNull(ELContext env)
286     throws ELException
287   {
288     return toStringWithNull(getValue(env), env);
289   }
290
291   /**
292    * Evaluate the expression, knowing the value should be a string
293    *
294    * @param env the variable environment
295    *
296    * @return the value of the expression as a string
297    */

298   public char evalCharacter(ELContext env)
299     throws ELException
300   {
301     return toCharacter(getValue(env), env);
302   }
303
304   /**
305    * Evaluate the expression, knowing the value should be a period
306    *
307    * @param env the variable environment
308    *
309    * @return the value of the expression as a period
310    */

311   public long evalPeriod(ELContext env)
312     throws ELException
313   {
314     try {
315       Object JavaDoc obj = getValue(env);
316
317       if (obj instanceof Number JavaDoc)
318         return 1000L * ((Number JavaDoc) obj).longValue();
319       else
320         return Period.toPeriod(toString(obj, env));
321     } catch (Exception JavaDoc e) {
322       throw new ELException(e.getMessage());
323     }
324                         
325   }
326
327   /**
328    * Evaluate the expression, knowing the value should be a BigInteger.
329    *
330    * @param env the variable environment
331    *
332    * @return the value of the expression as a BigInteger
333    */

334   public BigInteger JavaDoc evalBigInteger(ELContext env)
335     throws ELException
336   {
337     return toBigInteger(getValue(env), env);
338   }
339
340   /**
341    * Evaluate the expression, knowing the value should be a BigDecimal.
342    *
343    * @param env the variable environment
344    *
345    * @return the value of the expression as a BigDecimal
346    */

347   public BigDecimal JavaDoc evalBigDecimal(ELContext env)
348     throws ELException
349   {
350     return toBigDecimal(getValue(env), env);
351   }
352
353   /**
354    * Evaluates the expression, setting an object.
355    *
356    * @param env the variable environment
357    *
358    * @return the value of the expression as an object
359    */

360   @Override JavaDoc
361   public void setValue(ELContext env, Object JavaDoc value)
362     throws PropertyNotFoundException,
363        PropertyNotWritableException,
364        ELException
365   {
366     throw new PropertyNotWritableException(getClass().getName() + ": " + toString());
367   }
368
369   /**
370    * Evaluates directly to the output. The method returns true
371    * if the default value should be printed instead.
372    *
373    * @param out the output writer
374    * @param env the variable environment
375    * @param escapeXml if true, escape reserved XML
376    *
377    * @return true if the object is null, otherwise false
378    */

379   public boolean print(WriteStream out,
380                        ELContext env,
381                        boolean escapeXml)
382     throws IOException JavaDoc, ELException
383   {
384     Object JavaDoc obj = getValue(env);
385
386     if (obj == null)
387       return true;
388     else if (escapeXml) {
389       toStreamEscaped(out, obj);
390       return false;
391     }
392     else {
393       toStream(out, obj);
394       return false;
395     }
396   }
397
398   /**
399    * Evaluates directly to the output. The method returns true
400    * if the default value should be printed instead.
401    *
402    * @param out the output writer
403    * @param env the variable environment
404    * @param escapeXml if true, escape reserved XML
405    *
406    * @return true if the object is null, otherwise false
407    */

408   public boolean print(JspWriter JavaDoc out,
409                        ELContext env,
410                        boolean escapeXml)
411     throws IOException JavaDoc, ELException
412   {
413     Object JavaDoc obj = getValue(env);
414
415     if (obj == null)
416       return true;
417     else if (escapeXml) {
418       toStreamEscaped(out, obj);
419       return false;
420     }
421     else {
422       toStream(out, obj);
423       return false;
424     }
425   }
426
427   /**
428    * Generates the code to regenerate the expression.
429    *
430    * @param os the stream to the *.java page
431    */

432   public void printCreate(WriteStream os)
433     throws IOException JavaDoc
434   {
435     throw new UnsupportedOperationException JavaDoc(getClass().getName());
436   }
437
438   //
439
// EL methods
440
//
441

442   @Override JavaDoc
443   public String JavaDoc getExpressionString()
444   {
445     return toString();
446   }
447
448   @Override JavaDoc
449   public Class JavaDoc<?> getExpectedType()
450   {
451     return Object JavaDoc.class;
452   }
453
454   @Override JavaDoc
455   public Class JavaDoc<?> getType(ELContext context)
456     throws PropertyNotFoundException,
457        ELException
458   {
459     Object JavaDoc value = getValue(context);
460
461     if (value == null)
462       return null;
463     else
464       return value.getClass();
465   }
466
467   //
468
// Static convenience methods
469
//
470

471   /**
472    * Returns true for a double or double-equivalent.
473    */

474   public static boolean isDouble(Object JavaDoc o)
475   {
476     if (o instanceof Double JavaDoc)
477       return true;
478     else if (o instanceof Float JavaDoc)
479       return true;
480     else if (! (o instanceof String JavaDoc))
481       return false;
482     else {
483       String JavaDoc s = (String JavaDoc) o;
484       int len = s.length();
485
486       for (int i = 0; i < len; i++) {
487     char ch = s.charAt(i);
488
489     if (ch == '.' || ch == 'e' || ch == 'E')
490       return true;
491       }
492
493       return false;
494     }
495   }
496
497   /**
498    * Converts some unknown value to a string.
499    *
500    * @param value the value to be converted.
501    *
502    * @return the string-converted value.
503    */

504   public static String JavaDoc toStringWithNull(Object JavaDoc value, ELContext env)
505   {
506     if (value == null || value instanceof String JavaDoc)
507       return (String JavaDoc) value;
508     else
509       return value.toString();
510   }
511
512   /**
513    * Converts some unknown value to a string.
514    *
515    * @param value the value to be converted.
516    *
517    * @return the string-converted value.
518    */

519   public static String JavaDoc toString(Object JavaDoc value, ELContext env)
520   {
521     if (value == null)
522       return "";
523     else if (value instanceof String JavaDoc)
524       return (String JavaDoc) value;
525     else
526       return value.toString();
527   }
528
529   /**
530    * Converts some unknown value to a string.
531    *
532    * @param value the value to be converted.
533    *
534    * @return the string-converted value.
535    */

536   public static String JavaDoc toString(long value, ELContext env)
537   {
538     return String.valueOf(value);
539   }
540
541   /**
542    * Converts some unknown value to a string.
543    *
544    * @param value the value to be converted.
545    *
546    * @return the string-converted value.
547    */

548   public static String JavaDoc toString(double value, ELContext env)
549   {
550     return String.valueOf(value);
551   }
552
553   /**
554    * Converts some unknown value to a string.
555    *
556    * @param value the value to be converted.
557    *
558    * @return the string-converted value.
559    */

560   public static String JavaDoc toString(boolean value, ELContext env)
561   {
562     return String.valueOf(value);
563   }
564
565   /**
566    * Converts some unknown value to a string.
567    *
568    * @param value the value to be converted.
569    *
570    * @return the string-converted value.
571    */

572   public static String JavaDoc toString(char value, ELContext env)
573   {
574     return String.valueOf(value);
575   }
576
577   /**
578    * Converts some unknown value to a string.
579    *
580    * @param value the value to be converted.
581    *
582    * @return the string-converted value.
583    */

584   public static char toCharacter(Object JavaDoc value, ELContext env)
585     throws ELException
586   {
587     if (value == null)
588       return (char) 0;
589     else if (value instanceof Character JavaDoc) {
590       return ((Character JavaDoc) value).charValue();
591     }
592     else if (value instanceof String JavaDoc) {
593       String JavaDoc s = (String JavaDoc) value;
594
595       if (s == null || s.length() == 0)
596     return (char) 0;
597       else
598     return s.charAt(0);
599     }
600     else if (value instanceof Number JavaDoc) {
601       Number JavaDoc number = (Number JavaDoc) value;
602
603       return (char) number.intValue();
604     }
605     else if (value instanceof Boolean JavaDoc) {
606       ELException e = new ELException(L.l("can't convert {0} to character.",
607                                           value.getClass().getName()));
608
609       throw e;
610       /*
611       error(e, env);
612
613       return (char) 0;
614       */

615     }
616     else
617       return (char) toLong(value, env);
618   }
619
620   /**
621    * Converts some unknown value to a boolean.
622    *
623    * @param value the value to be converted.
624    *
625    * @return the boolean-converted value.
626    */

627   public static boolean toBoolean(Object JavaDoc value, ELContext env)
628     throws ELException
629   {
630     if (value == null || value.equals(""))
631       return false;
632     else if (value instanceof Boolean JavaDoc)
633       return ((Boolean JavaDoc) value).booleanValue();
634     else if (value instanceof String JavaDoc)
635       return value.equals("true") || value.equals("yes");
636     else {
637       ELException e = new ELException(L.l("can't convert {0} to boolean.",
638                                           value.getClass().getName()));
639
640       // jsp/18s1
641
throw e;
642       /*
643       error(e, env);
644
645       return false;
646       */

647     }
648   }
649
650   /**
651    * Converts some unknown value to a double.
652    *
653    * @param value the value to be converted.
654    *
655    * @return the double-converted value.
656    */

657   public static double toDouble(Object JavaDoc value, ELContext env)
658     throws ELException
659   {
660     if (value == null)
661       return 0;
662     else if (value instanceof Number JavaDoc) {
663       double dValue = ((Number JavaDoc) value).doubleValue();
664
665       if (Double.isNaN(dValue))
666         return 0;
667       else
668         return dValue;
669     }
670     else if (value.equals(""))
671       return 0;
672     else if (value instanceof String JavaDoc) {
673       double dValue = Double.parseDouble((String JavaDoc) value);
674
675       if (Double.isNaN(dValue))
676         return 0;
677       else
678         return dValue;
679     }
680     else if (value instanceof Character JavaDoc) {
681       // jsp/18s7
682
return ((Character JavaDoc) value).charValue();
683     }
684     else {
685       ELException e = new ELException(L.l("can't convert {0} to double.",
686                                           value.getClass().getName()));
687       
688       // error(e, env);
689

690       // return 0;
691

692       throw e;
693     }
694   }
695
696   /**
697    * Converts some unknown value to a big decimal
698    *
699    * @param value the value to be converted.
700    *
701    * @return the BigDecimal-converted value.
702    */

703   public static BigDecimal JavaDoc toBigDecimal(Object JavaDoc value, ELContext env)
704     throws ELException
705   {
706     if (value == null)
707       return BIG_DECIMAL_ZERO;
708     else if (value instanceof BigDecimal JavaDoc)
709       return (BigDecimal JavaDoc) value;
710     else if (value instanceof Number JavaDoc) {
711       double dValue = ((Number JavaDoc) value).doubleValue();
712
713       return new BigDecimal JavaDoc(dValue);
714     }
715     else if (value.equals(""))
716       return BIG_DECIMAL_ZERO;
717     else if (value instanceof String JavaDoc) {
718       return new BigDecimal JavaDoc((String JavaDoc) value);
719     }
720     else if (value instanceof Character JavaDoc) {
721       return new BigDecimal JavaDoc(((Character JavaDoc) value).charValue());
722     }
723     else {
724       ELException e = new ELException(L.l("can't convert {0} to BigDecimal.",
725                                           value.getClass().getName()));
726       
727       error(e, env);
728
729       return BIG_DECIMAL_ZERO;
730     }
731   }
732
733   /**
734    * Converts some unknown value to a big integer
735    *
736    * @param value the value to be converted.
737    *
738    * @return the BigInteger-converted value.
739    */

740   public static BigInteger JavaDoc toBigInteger(Object JavaDoc value, ELContext env)
741     throws ELException
742   {
743     if (value == null)
744       return BIG_INTEGER_ZERO;
745     else if (value instanceof BigInteger JavaDoc)
746       return (BigInteger JavaDoc) value;
747     else if (value instanceof Number JavaDoc) {
748       // return new BigInteger(value.toString());
749
return new BigDecimal JavaDoc(value.toString()).toBigInteger();
750     }
751     else if (value.equals(""))
752       return BIG_INTEGER_ZERO;
753     else if (value instanceof String JavaDoc) {
754       return new BigInteger JavaDoc((String JavaDoc) value);
755     }
756     else if (value instanceof Character JavaDoc) {
757       return new BigInteger JavaDoc(String.valueOf((int) ((Character JavaDoc) value).charValue()));
758     }
759     else {
760       ELException e = new ELException(L.l("can't convert {0} to BigInteger.",
761                                           value.getClass().getName()));
762       
763       error(e, env);
764
765       return BIG_INTEGER_ZERO;
766     }
767   }
768
769   /**
770    * Converts some unknown value to a long.
771    *
772    * @param value the value to be converted.
773    *
774    * @return the long-converted value.
775    */

776   public static long toLong(Object JavaDoc value, ELContext env)
777     throws ELException
778   {
779     if (value == null)
780       return 0;
781     else if (value instanceof Number JavaDoc)
782       return ((Number JavaDoc) value).longValue();
783     else if (value.equals(""))
784       return 0;
785     else if (value instanceof String JavaDoc) {
786       try {
787     return (long) Double.parseDouble((String JavaDoc) value);
788       } catch (Exception JavaDoc e) {
789     throw new ELException(e);
790       }
791     }
792     else if (value instanceof Character JavaDoc) {
793       // jsp/18s6
794
return ((Character JavaDoc) value).charValue();
795     }
796     else {
797       ELException e = new ELException(L.l("can't convert {0} to long.",
798                                           value.getClass().getName()));
799       
800       // error(e, env);
801

802       // return 0;
803

804       throw e;
805     }
806   }
807
808   /**
809    * Write to the stream.
810    *
811    * @param out the output stream
812    * @param value the value to be written.
813    *
814    * @return true for null
815    */

816   public static boolean toStream(JspWriter JavaDoc out,
817                  Object JavaDoc value,
818                  boolean isEscaped)
819     throws IOException JavaDoc
820   {
821     if (value == null)
822       return true;
823     
824     if (isEscaped)
825       toStreamEscaped(out, value);
826     else
827       toStream(out, value);
828
829     return false;
830   }
831
832   /**
833    * Write to the stream.
834    *
835    * @param out the output stream
836    * @param value the value to be written.
837    */

838   public static void toStream(WriteStream out, Object JavaDoc value)
839     throws IOException JavaDoc
840   {
841     if (value == null)
842       return;
843     else if (value instanceof String JavaDoc)
844       out.print((String JavaDoc) value);
845     else if (value instanceof Reader JavaDoc) {
846       out.writeStream((Reader JavaDoc) value);
847     }
848     else
849       out.print(value.toString());
850   }
851
852   /**
853    * Write to the stream.
854    *
855    * @param out the output stream
856    * @param value the value to be written.
857    */

858   public static void toStream(JspWriter JavaDoc out, Object JavaDoc value)
859     throws IOException JavaDoc
860   {
861     if (value == null)
862       return;
863     else if (value instanceof String JavaDoc)
864       out.print((String JavaDoc) value);
865     else if (value instanceof Reader JavaDoc) {
866       Reader JavaDoc reader = (Reader JavaDoc) value;
867       int ch;
868
869       while ((ch = reader.read()) > 0) {
870     out.print((char) ch);
871       }
872     }
873     else
874       out.print(value.toString());
875   }
876
877   /**
878    * Write to the *.java stream escaping Java reserved characters.
879    *
880    * @param out the output stream to the *.java code.
881    *
882    * @param value the value to be converted.
883    */

884   public static void printEscapedString(WriteStream os, String JavaDoc string)
885     throws IOException JavaDoc
886   {
887     int length = string.length();
888     for (int i = 0; i < length; i++) {
889       char ch = string.charAt(i);
890
891       switch (ch) {
892       case '\\':
893         os.print("\\\\");
894         break;
895       case '\n':
896         os.print("\\n");
897         break;
898       case '\r':
899         os.print("\\r");
900         break;
901       case '\"':
902         os.print("\\\"");
903         break;
904       default:
905         os.print((char) ch);
906         break;
907       }
908     }
909   }
910
911   /**
912    * Write to the stream.
913    *
914    * @param out the output stream
915    * @param value the value to be written.
916    */

917   public static void toStreamEscaped(Writer JavaDoc out, Object JavaDoc value)
918     throws IOException JavaDoc
919   {
920     if (value == null)
921       return;
922     else if (value instanceof Reader JavaDoc) {
923       toStreamEscaped(out, (Reader JavaDoc) value);
924       return;
925     }
926
927     String JavaDoc string = value.toString();
928     int length = string.length();
929     for (int i = 0; i < length; i++) {
930       int ch = string.charAt(i);
931
932       switch (ch) {
933       case '<':
934         out.write("&lt;");
935         break;
936       case '>':
937         out.write("&gt;");
938         break;
939       case '&':
940         out.write("&amp;");
941         break;
942       case '\'':
943         out.write("&#039;");
944         break;
945       case '"':
946         out.write("&#034;");
947         break;
948       default:
949         out.write((char) ch);
950         break;
951       }
952     }
953   }
954
955   /**
956    * Write to the stream escaping XML reserved characters.
957    *
958    * @param out the output stream.
959    * @param value the value to be converted.
960    */

961   public static void toStreamEscaped(WriteStream out, Object JavaDoc value)
962     throws IOException JavaDoc
963   {
964     if (value == null)
965       return;
966
967     String JavaDoc string = value.toString();
968     int length = string.length();
969     for (int i = 0; i < length; i++) {
970       int ch = string.charAt(i);
971
972       switch (ch) {
973       case '<':
974         out.print("&lt;");
975         break;
976       case '>':
977         out.print("&gt;");
978         break;
979       case '&':
980         out.print("&amp;");
981         break;
982       case '\'':
983         out.print("&#039;");
984         break;
985       case '"':
986         out.print("&#034;");
987         break;
988       default:
989         out.print((char) ch);
990         break;
991       }
992     }
993   }
994
995   /**
996    * Write to the stream escaping XML reserved characters.
997    *
998    * @param out the output stream.
999    * @param value the value to be converted.
1000   */

1001  public static void toStreamEscaped(Writer JavaDoc out, Reader JavaDoc in)
1002    throws IOException JavaDoc
1003  {
1004    if (in == null)
1005      return;
1006    
1007    int ch;
1008    while ((ch = in.read()) >= 0) {
1009      switch (ch) {
1010      case '<':
1011        out.write("&lt;");
1012        break;
1013      case '>':
1014        out.write("&gt;");
1015        break;
1016      case '&':
1017        out.write("&amp;");
1018        break;
1019      case '\'':
1020        out.write("&#039;");
1021        break;
1022      case '"':
1023        out.write("&#034;");
1024        break;
1025      default:
1026        out.write((char) ch);
1027        break;
1028      }
1029    }
1030  }
1031  
1032  /**
1033   * Write to the *.java stream escaping Java reserved characters.
1034   *
1035   * @param out the output stream to the *.java code.
1036   *
1037   * @param value the value to be converted.
1038   */

1039  public static void printEscaped(WriteStream os, ReadStream is)
1040    throws IOException JavaDoc
1041  {
1042    int ch;
1043    
1044    while ((ch = is.readChar()) >= 0) {
1045      switch (ch) {
1046      case '\\':
1047        os.print("\\\\");
1048        break;
1049      case '\n':
1050        os.print("\\n");
1051        break;
1052      case '\r':
1053        os.print("\\r");
1054        break;
1055      case '\"':
1056        os.print("\\\"");
1057        break;
1058      default:
1059        os.print((char) ch);
1060        break;
1061      }
1062    }
1063  }
1064
1065  public static void setProperty(Object JavaDoc target, String JavaDoc property, Object JavaDoc value)
1066    throws ELException
1067  {
1068    if (target instanceof Map) {
1069      Map<String JavaDoc,Object JavaDoc> map = (Map) target;
1070      
1071      if (value != null)
1072        map.put(property, value);
1073      else
1074        map.remove(property);
1075    }
1076    else if (target != null) {
1077      Method JavaDoc method = null;
1078
1079      try {
1080        method = BeanUtil.getSetMethod(target.getClass(), property);
1081      } catch (Exception JavaDoc e) {
1082        throw new ELException(e);
1083      }
1084
1085      if (method == null)
1086        throw new ELException(L.l("can't find property `{0}' in `{1}'",
1087                                  property, target.getClass()));
1088
1089      Class JavaDoc type = method.getParameterTypes()[0];
1090
1091      try {
1092    int code = _typeMap.get(type);
1093
1094    switch (code) {
1095    case BOOLEAN:
1096      value = toBoolean(value, null) ? Boolean.TRUE : Boolean.FALSE;
1097      break;
1098      
1099    case BYTE:
1100      value = new Byte JavaDoc((byte) toLong(value, null));
1101      break;
1102      
1103    case SHORT:
1104      value = new Short JavaDoc((short) toLong(value, null));
1105      break;
1106      
1107    case INT:
1108      value = new Integer JavaDoc((int) toLong(value, null));
1109      break;
1110      
1111    case LONG:
1112      value = new Long JavaDoc((long) toLong(value, null));
1113      break;
1114      
1115    case FLOAT:
1116      value = new Float JavaDoc((float) toDouble(value, null));
1117      break;
1118      
1119    case DOUBLE:
1120      value = new Double JavaDoc((double) toDouble(value, null));
1121      break;
1122      
1123    case BOOLEAN_OBJ:
1124      if (value != null)
1125        value = toBoolean(value, null) ? Boolean.TRUE : Boolean.FALSE;
1126      break;
1127      
1128    case BYTE_OBJ:
1129      if (value != null)
1130        value = new Byte JavaDoc((byte) toLong(value, null));
1131      break;
1132      
1133    case SHORT_OBJ:
1134      if (value != null)
1135        value = new Short JavaDoc((short) toLong(value, null));
1136      break;
1137      
1138    case INT_OBJ:
1139      if (value != null)
1140        value = new Integer JavaDoc((int) toLong(value, null));
1141      break;
1142      
1143    case LONG_OBJ:
1144      if (value != null)
1145        value = new Long JavaDoc((long) toLong(value, null));
1146      break;
1147      
1148    case FLOAT_OBJ:
1149      if (value != null)
1150        value = new Float JavaDoc((float) toDouble(value, null));
1151      break;
1152      
1153    case DOUBLE_OBJ:
1154      if (value != null)
1155        value = new Double JavaDoc((double) toDouble(value, null));
1156      break;
1157
1158    case STRING:
1159      if (value != null)
1160        value = String.valueOf(value);
1161      break;
1162      
1163    default:
1164      break;
1165    }
1166    
1167        method.invoke(target, new Object JavaDoc[] { value });
1168      } catch (Exception JavaDoc e) {
1169        throw new ELException(e);
1170      }
1171    }
1172  }
1173
1174  protected static boolean isDoubleString(Object JavaDoc obj)
1175  {
1176    if (! (obj instanceof String JavaDoc))
1177      return false;
1178
1179    String JavaDoc s = (String JavaDoc) obj;
1180
1181    int len = s.length();
1182    for (int i = 0; i < len; i++) {
1183      char ch = s.charAt(i);
1184
1185      if (ch == '.' || ch == 'e' || ch == 'E')
1186        return true;
1187    }
1188
1189    return false;
1190  }
1191  
1192  public static Object JavaDoc coerceToType(Object JavaDoc obj, Class JavaDoc<?> targetType)
1193    throws ELException
1194  {
1195    CoerceType type = _coerceMap.get(targetType);
1196
1197    if (type == null)
1198      return obj;
1199
1200    switch (type) {
1201    case BOOLEAN:
1202      return Expr.toBoolean(obj, null) ? Boolean.FALSE : Boolean.TRUE;
1203    case CHARACTER:
1204      return Expr.toCharacter(obj, null);
1205    case BYTE:
1206      return new Byte JavaDoc((byte) Expr.toLong(obj, null));
1207    case SHORT:
1208      return new Short JavaDoc((short) Expr.toLong(obj, null));
1209    case INTEGER:
1210      return new Integer JavaDoc((int) Expr.toLong(obj, null));
1211    case LONG:
1212      return new Long JavaDoc(Expr.toLong(obj, null));
1213    case FLOAT:
1214      return new Float JavaDoc((float) Expr.toDouble(obj, null));
1215    case DOUBLE:
1216      return new Double JavaDoc(Expr.toDouble(obj, null));
1217    case STRING:
1218      if (obj == null)
1219    return "";
1220      else
1221    return obj.toString();
1222    case BIG_DECIMAL:
1223      return Expr.toBigDecimal(obj, null);
1224    case BIG_INTEGER:
1225      return Expr.toBigInteger(obj, null);
1226    }
1227
1228    return null;
1229  }
1230
1231  /**
1232   * Returns an error object
1233   */

1234  public static Object JavaDoc error(Throwable JavaDoc e, ELContext env)
1235    throws ELException
1236  {
1237    if (env == null) {
1238      // jsp/1b56
1239
throw new ELException(e);
1240    }
1241    else if (env instanceof ExprEnv && ((ExprEnv) env).isIgnoreException()) {
1242      log.log(Level.FINE, e.toString(), e);
1243
1244      return null;
1245    }
1246    else if (e instanceof RuntimeException JavaDoc)
1247      throw (RuntimeException JavaDoc) e;
1248    else
1249      throw new ELException(e);
1250  }
1251
1252  public int hashCode()
1253  {
1254    return toString().hashCode();
1255  }
1256  
1257  public boolean equals(Object JavaDoc o)
1258  {
1259    if (this == o)
1260      return true;
1261    else if (! (o instanceof Expr))
1262      return false;
1263
1264    return toString().equals(o.toString());
1265  }
1266
1267  abstract public String JavaDoc toString();
1268
1269  /**
1270   * Returns an error object
1271   */

1272  public static Object JavaDoc invocationError(Throwable JavaDoc e)
1273    throws ELException
1274  {
1275    if (e instanceof InvocationTargetException JavaDoc && e.getCause() != null)
1276      e = e.getCause();
1277
1278    if (e instanceof RuntimeException JavaDoc)
1279      throw (RuntimeException JavaDoc) e;
1280    else if (e instanceof Error JavaDoc)
1281      throw (Error JavaDoc) e;
1282    else
1283      throw new ELException(e);
1284  }
1285
1286  private enum CoerceType {
1287    BOOLEAN,
1288    CHARACTER,
1289    STRING,
1290    INTEGER,
1291    DOUBLE,
1292    LONG,
1293    FLOAT,
1294    SHORT,
1295    BYTE,
1296    BIG_INTEGER,
1297    BIG_DECIMAL,
1298    VOID
1299  };
1300
1301  static {
1302    _coerceMap.put(boolean.class, CoerceType.BOOLEAN);
1303    _coerceMap.put(Boolean JavaDoc.class, CoerceType.BOOLEAN);
1304    
1305    _coerceMap.put(byte.class, CoerceType.BYTE);
1306    _coerceMap.put(Byte JavaDoc.class, CoerceType.BYTE);
1307    
1308    _coerceMap.put(short.class, CoerceType.SHORT);
1309    _coerceMap.put(Short JavaDoc.class, CoerceType.SHORT);
1310    
1311    _coerceMap.put(int.class, CoerceType.INTEGER);
1312    _coerceMap.put(Integer JavaDoc.class, CoerceType.INTEGER);
1313    
1314    _coerceMap.put(long.class, CoerceType.LONG);
1315    _coerceMap.put(Long JavaDoc.class, CoerceType.LONG);
1316    
1317    _coerceMap.put(float.class, CoerceType.FLOAT);
1318    _coerceMap.put(Float JavaDoc.class, CoerceType.FLOAT);
1319    
1320    _coerceMap.put(double.class, CoerceType.DOUBLE);
1321    _coerceMap.put(Double JavaDoc.class, CoerceType.DOUBLE);
1322    
1323    _coerceMap.put(char.class, CoerceType.CHARACTER);
1324    _coerceMap.put(Character JavaDoc.class, CoerceType.CHARACTER);
1325    
1326    _coerceMap.put(String JavaDoc.class, CoerceType.STRING);
1327    
1328    _coerceMap.put(BigDecimal JavaDoc.class, CoerceType.BIG_DECIMAL);
1329    _coerceMap.put(BigInteger JavaDoc.class, CoerceType.BIG_INTEGER);
1330    
1331    _coerceMap.put(void.class, CoerceType.VOID);
1332  }
1333
1334  static {
1335    _typeMap.put(boolean.class, BOOLEAN);
1336    _typeMap.put(byte.class, BYTE);
1337    _typeMap.put(short.class, SHORT);
1338    _typeMap.put(int.class, INT);
1339    _typeMap.put(long.class, LONG);
1340    _typeMap.put(float.class, FLOAT);
1341    _typeMap.put(double.class, DOUBLE);
1342    
1343    _typeMap.put(Boolean JavaDoc.class, BOOLEAN_OBJ);
1344    _typeMap.put(Byte JavaDoc.class, BYTE_OBJ);
1345    _typeMap.put(Short JavaDoc.class, SHORT_OBJ);
1346    _typeMap.put(Integer JavaDoc.class, INT_OBJ);
1347    _typeMap.put(Long JavaDoc.class, LONG_OBJ);
1348    _typeMap.put(Float JavaDoc.class, FLOAT_OBJ);
1349    _typeMap.put(Double JavaDoc.class, DOUBLE_OBJ);
1350    
1351    _typeMap.put(String JavaDoc.class, STRING);
1352  }
1353}
1354
Popular Tags