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: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.el;
030:
031: import com.caucho.vfs.WriteStream;
032:
033: import javax.el.ELContext;
034: import javax.el.ELException;
035: import java.io.IOException;
036: import java.util.Collection;
037: import java.util.Map;
038:
039: /**
040: * Represents a unary expression.
041: */
042: public class UnaryExpr extends Expr {
043: private int _op;
044: private Expr _expr;
045:
046: /**
047: * Create a new unary expression.
048: *
049: * @param op the lexical code for the operation
050: * @param expr the base expression
051: */
052: private UnaryExpr(int op, Expr expr) {
053: _op = op;
054: _expr = expr;
055: }
056:
057: public static Expr create(int op, Expr expr) {
058: switch (op) {
059: case MINUS:
060: return new MinusExpr(expr);
061: }
062:
063: return new UnaryExpr(op, expr);
064: }
065:
066: /**
067: * Returns true if this is a constant expression.
068: */
069: @Override
070: public boolean isConstant() {
071: return _expr.isConstant();
072: }
073:
074: /**
075: * Evaluate the expression as an object.
076: *
077: * @param env the variable resolver
078: *
079: * @return the value as an object
080: */
081: @Override
082: public Object getValue(ELContext env) throws ELException {
083: switch (_op) {
084: case NOT:
085: return new Boolean(!_expr.evalBoolean(env));
086:
087: case MINUS: {
088: Object obj = _expr.getValue(env);
089:
090: if (obj == null)
091: return new Long(0);
092: else if (obj instanceof Double || obj instanceof Float)
093: return new Double(-((Number) obj).doubleValue());
094: else if (obj instanceof Number)
095: return new Long(-((Number) obj).longValue());
096: else if (obj instanceof String) {
097: String s = (String) obj;
098:
099: if (s.indexOf('.') < 0)
100: return new Long(-toLong(obj, env));
101: else
102: return new Double(-toDouble(obj, env));
103: } else
104: return new Double(-toDouble(obj, env));
105: }
106:
107: case EMPTY:
108: if (evalBoolean(env))
109: return Boolean.TRUE;
110: else
111: return Boolean.FALSE;
112: }
113:
114: throw new UnsupportedOperationException();
115: }
116:
117: /**
118: * Evaluate the expression as a boolean.
119: *
120: * @param env the variable resolver
121: *
122: * @return the value as a boolean
123: */
124: @Override
125: public boolean evalBoolean(ELContext env) throws ELException {
126: switch (_op) {
127: case NOT:
128: return !_expr.evalBoolean(env);
129:
130: case EMPTY: {
131: Object obj = _expr.getValue(env);
132:
133: if (obj == null)
134: return true;
135: else if (obj instanceof String)
136: return "".equals(obj);
137: else if (obj instanceof Collection)
138: return ((Collection) obj).isEmpty();
139: else if (obj instanceof Map)
140: return ((Map) obj).isEmpty();
141: else if (obj.getClass().isArray())
142: return java.lang.reflect.Array.getLength(obj) == 0;
143: else
144: return false;
145: }
146: }
147:
148: ELException e = new ELException(L.l("can't compare."));
149:
150: error(e, env);
151:
152: return false;
153: }
154:
155: /**
156: * Evaluate the expression as a long
157: *
158: * @param env the variable resolver
159: *
160: * @return the value as a long
161: */
162: @Override
163: public long evalLong(ELContext env) throws ELException {
164: if (_op != MINUS) {
165: ELException e = new ELException(
166: L
167: .l("'not' and 'empty' operations can not be converted to long values."));
168: error(e, env);
169:
170: return 0;
171: }
172:
173: // _op == MINUS
174: return -_expr.evalLong(env);
175: }
176:
177: /**
178: * Evaluate the expression as a double
179: */
180: @Override
181: public double evalDouble(ELContext env) throws ELException {
182: if (_op != MINUS) {
183: ELException e = new ELException(
184: L
185: .l("'not' and 'empty' operations can not be converted to double values."));
186:
187: error(e, env);
188:
189: return 0;
190: }
191:
192: // _op == MINUS
193: return -_expr.evalDouble(env);
194: }
195:
196: /**
197: * Prints the Java code to recreate the UnaryExpr.
198: */
199: public void printCreate(WriteStream os) throws IOException {
200: os.print("com.caucho.el.UnaryExpr.create(");
201: os.print(_op + ", ");
202: _expr.printCreate(os);
203: os.print(")");
204: }
205:
206: /**
207: * Returns true for equal strings.
208: */
209: public boolean equals(Object o) {
210: if (!(o instanceof UnaryExpr))
211: return false;
212:
213: UnaryExpr uexpr = (UnaryExpr) o;
214:
215: return (_op == uexpr._op && _expr.equals(uexpr._expr));
216: }
217:
218: /**
219: * Returns a readable representation of the expr.
220: */
221: public String toString() {
222: String op;
223:
224: switch (_op) {
225: case MINUS:
226: op = " -";
227: break;
228: case NOT:
229: op = " not ";
230: break;
231: case EMPTY:
232: op = " empty ";
233: break;
234: default:
235: op = " unknown(" + _op + ") ";
236: break;
237: }
238:
239: return op + _expr;
240: }
241: }
|