KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.el.ELContext;
33 import javax.el.ELException;
34 import java.util.HashMap JavaDoc;
35
36 /**
37  * Marshalls an expression.
38  */

39 abstract public class Marshall {
40   private static final HashMap JavaDoc<Class JavaDoc,Marshall> ARG_MAP
41     = new HashMap JavaDoc<Class JavaDoc,Marshall>();
42
43   public static Marshall create(Class JavaDoc arg)
44   {
45     Marshall marshall = ARG_MAP.get(arg);
46
47     if (marshall != null)
48       return marshall;
49     else
50       return OBJECT;
51   }
52   
53   abstract public Object JavaDoc marshall(Expr expr, ELContext env)
54     throws ELException;
55
56   public static final Marshall BOOLEAN = new Marshall() {
57       public Object JavaDoc marshall(Expr expr, ELContext env)
58     throws ELException
59       {
60     return new Boolean JavaDoc((boolean) expr.evalBoolean(env));
61       }
62     };
63
64   public static final Marshall BYTE = new Marshall() {
65       public Object JavaDoc marshall(Expr expr, ELContext env)
66     throws ELException
67       {
68     return new Byte JavaDoc((byte) expr.evalLong(env));
69       }
70     };
71
72   public static final Marshall SHORT = new Marshall() {
73       public Object JavaDoc marshall(Expr expr, ELContext env)
74     throws ELException
75       {
76     return new Short JavaDoc((short) expr.evalLong(env));
77       }
78     };
79
80   public static final Marshall INTEGER = new Marshall() {
81       public Object JavaDoc marshall(Expr expr, ELContext env)
82     throws ELException
83       {
84     return new Integer JavaDoc((int) expr.evalLong(env));
85       }
86     };
87
88   public static final Marshall LONG = new Marshall() {
89       public Object JavaDoc marshall(Expr expr, ELContext env)
90     throws ELException
91       {
92     return new Long JavaDoc(expr.evalLong(env));
93       }
94     };
95
96   public static final Marshall FLOAT = new Marshall() {
97       public Object JavaDoc marshall(Expr expr, ELContext env)
98     throws ELException
99       {
100     return new Float JavaDoc((float) expr.evalDouble(env));
101       }
102     };
103
104   public static final Marshall DOUBLE = new Marshall() {
105       public Object JavaDoc marshall(Expr expr, ELContext env)
106     throws ELException
107       {
108     return new Double JavaDoc(expr.evalDouble(env));
109       }
110     };
111
112   public static final Marshall STRING = new Marshall() {
113       public Object JavaDoc marshall(Expr expr, ELContext env)
114     throws ELException
115       {
116     return expr.evalString(env);
117       }
118     };
119
120   public static final Marshall CHARACTER = new Marshall() {
121       public Object JavaDoc marshall(Expr expr, ELContext env)
122     throws ELException
123       {
124     String JavaDoc s = expr.evalString(env);
125
126     if (s == null || s.length() == 0)
127       return null;
128     else
129       return new Character JavaDoc(s.charAt(0));
130       }
131     };
132
133   public static final Marshall OBJECT = new Marshall() {
134       public Object JavaDoc marshall(Expr expr, ELContext env)
135     throws ELException
136       {
137     return expr.getValue(env);
138       }
139     };
140
141   static {
142     ARG_MAP.put(boolean.class, BOOLEAN);
143     ARG_MAP.put(Boolean JavaDoc.class, BOOLEAN);
144     
145     ARG_MAP.put(byte.class, BYTE);
146     ARG_MAP.put(Byte JavaDoc.class, BYTE);
147     
148     ARG_MAP.put(short.class, SHORT);
149     ARG_MAP.put(Short JavaDoc.class, SHORT);
150     
151     ARG_MAP.put(int.class, INTEGER);
152     ARG_MAP.put(Integer JavaDoc.class, INTEGER);
153     
154     ARG_MAP.put(long.class, LONG);
155     ARG_MAP.put(Long JavaDoc.class, LONG);
156     
157     ARG_MAP.put(float.class, FLOAT);
158     ARG_MAP.put(Float JavaDoc.class, FLOAT);
159     
160     ARG_MAP.put(double.class, DOUBLE);
161     ARG_MAP.put(Double JavaDoc.class, DOUBLE);
162     
163     ARG_MAP.put(char.class, CHARACTER);
164     ARG_MAP.put(Character JavaDoc.class, CHARACTER);
165     
166     ARG_MAP.put(String JavaDoc.class, STRING);
167   }
168 }
169
Popular Tags