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: import java.util.logging.Level;
038:
039: /**
040: * Represents a binary boolean expression.
041: */
042: public class BooleanExpr extends AbstractBooleanExpr {
043: private int _op;
044: private Expr _left;
045: private Expr _right;
046:
047: /**
048: * Constructs a new Boolean expression.
049: *
050: * @param op the lexeme code for the boolean operation.
051: * @param left the left expression
052: * @param right the right expression
053: */
054: public BooleanExpr(int op, Expr left, Expr right) {
055: _op = op;
056: _left = left;
057: _right = right;
058: }
059:
060: /**
061: * Returns true if this is a constant expression.
062: */
063: @Override
064: public boolean isConstant() {
065: return _left.isConstant() && _right.isConstant();
066: }
067:
068: /**
069: * Evaluate the expression as a boolean.
070: *
071: * @param env the variable environment
072: *
073: * @return the result as a boolean
074: */
075: @Override
076: public boolean evalBoolean(ELContext env) throws ELException {
077: if (_op == AND)
078: return _left.evalBoolean(env) && _right.evalBoolean(env);
079: else if (_op == OR)
080: return _left.evalBoolean(env) || _right.evalBoolean(env);
081:
082: ELException e = new ELException(L.l("can't compare."));
083: log.log(Level.FINE, e.getMessage(), e);
084:
085: return false;
086: }
087:
088: /**
089: * Prints the Java code to recreate a BooleanExpr
090: */
091: @Override
092: public void printCreate(WriteStream os) throws IOException {
093: os.print("new com.caucho.el.BooleanExpr(");
094: os.print(_op + ", ");
095: _left.printCreate(os);
096: os.print(", ");
097: _right.printCreate(os);
098: os.print(")");
099: }
100:
101: /**
102: * Returns true for equal strings.
103: */
104: public boolean equals(Object o) {
105: if (!(o instanceof BooleanExpr))
106: return false;
107:
108: BooleanExpr expr = (BooleanExpr) o;
109:
110: return (_op == expr._op && _left.equals(expr._left) && _right
111: .equals(expr._right));
112: }
113:
114: /**
115: * Returns a readable representation of the expr.
116: */
117: public String toString() {
118: String op;
119:
120: switch (_op) {
121: case OR:
122: op = " or ";
123: break;
124: case AND:
125: op = " and ";
126: break;
127: default:
128: op = " unknown(" + _op + ") ";
129: break;
130: }
131:
132: return "(" + _left + op + _right + ")";
133: }
134: }
|