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