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 com.caucho.vfs.WriteStream;
033:
034: import javax.el.ELContext;
035: import javax.el.ELException;
036: import java.io.IOException;
037:
038: /**
039: * Base implementation for a boolean-valued expression.
040: */
041: abstract public class AbstractBooleanExpr extends Expr {
042:
043: /**
044: * Evaluate the expression as a boolean
045: *
046: * @param env the variable environment
047: *
048: * @return the value as a booleanstring
049: */
050: @Override
051: abstract public boolean evalBoolean(ELContext env)
052: throws ELException;
053:
054: /**
055: * Evaluate the expression as an object.
056: *
057: * @param env the variable environment
058: *
059: * @return the value as an object
060: */
061: @Override
062: public Object getValue(ELContext env) throws ELException {
063: return evalBoolean(env) ? Boolean.TRUE : Boolean.FALSE;
064: }
065:
066: /**
067: * Evaluate the expression as a string
068: *
069: * @param env the variable environment
070: *
071: * @return the value as a string
072: */
073: @Override
074: public String evalString(ELContext env) throws ELException {
075: return evalBoolean(env) ? "true" : "false";
076: }
077:
078: /**
079: * Evaluate the expression as a long
080: *
081: * @param env the variable environment
082: *
083: * @return the value as a long
084: */
085: @Override
086: public long evalLong(ELContext env) throws ELException {
087: ELException e = new ELException(
088: L
089: .l(
090: "'{0}': boolean expressions can not be converted to long values.",
091: this ));
092:
093: error(e, env);
094:
095: return 0;
096: }
097:
098: /**
099: * Evaluate the expression as a double
100: */
101: @Override
102: public double evalDouble(ELContext env) throws ELException {
103: ELException e = new ELException(
104: L
105: .l(
106: "`{0}': boolean expressions can not be converted to double values.",
107: this ));
108:
109: error(e, env);
110:
111: return 0;
112: }
113:
114: /**
115: * Evalutes directly to the output.
116: *
117: * @param out the output stream
118: * @param env the variable environment
119: */
120: @Override
121: public boolean print(WriteStream out, ELContext env,
122: boolean isEscaped) throws IOException, ELException {
123: if (evalBoolean(env))
124: out.print("true");
125: else
126: out.print("false");
127:
128: return false;
129: }
130: }
|