001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.el;
031:
032: import javax.el.ELContext;
033: import javax.el.ELException;
034: import java.util.HashMap;
035:
036: /**
037: * Marshalls an expression.
038: */
039: abstract public class Marshall {
040: private static final HashMap<Class, Marshall> ARG_MAP = new HashMap<Class, Marshall>();
041:
042: public static Marshall create(Class arg) {
043: Marshall marshall = ARG_MAP.get(arg);
044:
045: if (marshall != null)
046: return marshall;
047: else
048: return OBJECT;
049: }
050:
051: abstract public Object marshall(Expr expr, ELContext env)
052: throws ELException;
053:
054: public static final Marshall BOOLEAN = new Marshall() {
055: public Object marshall(Expr expr, ELContext env)
056: throws ELException {
057: return new Boolean((boolean) expr.evalBoolean(env));
058: }
059: };
060:
061: public static final Marshall BYTE = new Marshall() {
062: public Object marshall(Expr expr, ELContext env)
063: throws ELException {
064: return new Byte((byte) expr.evalLong(env));
065: }
066: };
067:
068: public static final Marshall SHORT = new Marshall() {
069: public Object marshall(Expr expr, ELContext env)
070: throws ELException {
071: return new Short((short) expr.evalLong(env));
072: }
073: };
074:
075: public static final Marshall INTEGER = new Marshall() {
076: public Object marshall(Expr expr, ELContext env)
077: throws ELException {
078: return new Integer((int) expr.evalLong(env));
079: }
080: };
081:
082: public static final Marshall LONG = new Marshall() {
083: public Object marshall(Expr expr, ELContext env)
084: throws ELException {
085: return new Long(expr.evalLong(env));
086: }
087: };
088:
089: public static final Marshall FLOAT = new Marshall() {
090: public Object marshall(Expr expr, ELContext env)
091: throws ELException {
092: return new Float((float) expr.evalDouble(env));
093: }
094: };
095:
096: public static final Marshall DOUBLE = new Marshall() {
097: public Object marshall(Expr expr, ELContext env)
098: throws ELException {
099: return new Double(expr.evalDouble(env));
100: }
101: };
102:
103: public static final Marshall STRING = new Marshall() {
104: public Object marshall(Expr expr, ELContext env)
105: throws ELException {
106: return expr.evalString(env);
107: }
108: };
109:
110: public static final Marshall CHARACTER = new Marshall() {
111: public Object marshall(Expr expr, ELContext env)
112: throws ELException {
113: String s = expr.evalString(env);
114:
115: if (s == null || s.length() == 0)
116: return null;
117: else
118: return new Character(s.charAt(0));
119: }
120: };
121:
122: public static final Marshall OBJECT = new Marshall() {
123: public Object marshall(Expr expr, ELContext env)
124: throws ELException {
125: return expr.getValue(env);
126: }
127: };
128:
129: static {
130: ARG_MAP.put(boolean.class, BOOLEAN);
131: ARG_MAP.put(Boolean.class, BOOLEAN);
132:
133: ARG_MAP.put(byte.class, BYTE);
134: ARG_MAP.put(Byte.class, BYTE);
135:
136: ARG_MAP.put(short.class, SHORT);
137: ARG_MAP.put(Short.class, SHORT);
138:
139: ARG_MAP.put(int.class, INTEGER);
140: ARG_MAP.put(Integer.class, INTEGER);
141:
142: ARG_MAP.put(long.class, LONG);
143: ARG_MAP.put(Long.class, LONG);
144:
145: ARG_MAP.put(float.class, FLOAT);
146: ARG_MAP.put(Float.class, FLOAT);
147:
148: ARG_MAP.put(double.class, DOUBLE);
149: ARG_MAP.put(Double.class, DOUBLE);
150:
151: ARG_MAP.put(char.class, CHARACTER);
152: ARG_MAP.put(Character.class, CHARACTER);
153:
154: ARG_MAP.put(String.class, STRING);
155: }
156: }
|