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 javax.el.ValueExpression;
037: import java.io.IOException;
038:
039: /**
040: * ValueExpression expression.
041: */
042: public class ValueExpr extends Expr {
043: // The identifier name
044: private final String _name;
045:
046: private final ValueExpression _valExpr;
047:
048: /**
049: * Creates the identifier
050: */
051: public ValueExpr(String name, ValueExpression valExpr) {
052: _name = name;
053: _valExpr = valExpr;
054: }
055:
056: /**
057: * Creates a field reference using this expression as the base object.
058: *
059: * @param field the string reference for the field.
060: */
061: @Override
062: public Expr createField(String field) {
063: if (_valExpr instanceof FieldGenerator) {
064: FieldGenerator gen = (FieldGenerator) _valExpr;
065:
066: ValueExpression fieldExpr = gen.createField(field);
067:
068: if (fieldExpr != null)
069: return new ValueExpr(field, fieldExpr);
070: }
071:
072: Expr arrayExpr = createField(new StringLiteral(field));
073:
074: return new PathExpr(arrayExpr, _name + '.' + field);
075: }
076:
077: /**
078: * Evaluate the expr as an object.
079: *
080: * @param env the variable environment
081: *
082: * @return the value as an object
083: */
084: @Override
085: public Object getValue(ELContext env) throws ELException {
086: return _valExpr.getValue(env);
087: }
088:
089: /**
090: * Sets teh value.
091: *
092: * @param env the variable environment
093: *
094: * @return the value as an object
095: */
096: @Override
097: public void setValue(ELContext env, Object value)
098: throws ELException {
099: _valExpr.setValue(env, value);
100: }
101:
102: /**
103: * Prints the code to create an IdExpr.
104: */
105: @Override
106: public void printCreate(WriteStream os) throws IOException {
107: os.print("new com.caucho.el.ValueExpr(\"");
108: printEscapedString(os, _name);
109: os.print("\")");
110: }
111:
112: public boolean equals(Object o) {
113: if (o == null || !o.getClass().equals(ValueExpr.class))
114: return false;
115:
116: ValueExpr expr = (ValueExpr) o;
117:
118: return _valExpr.equals(expr._valExpr);
119: }
120:
121: public String toString() {
122: return _name;
123: }
124: }
|