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.*;
035: import java.io.IOException;
036:
037: /**
038: * Represents a field reference that may also be a dotted property,
039: * e.g. smtp.mail.host.
040: * </pre>
041: */
042: public class PathExpr extends Expr {
043: private Expr _expr;
044:
045: private String _path;
046:
047: /**
048: * Creates a new path expression.
049: *
050: * @param expr the underlying expression
051: * @param path the property name to use if null
052: */
053: public PathExpr(Expr expr, String path) {
054: _expr = expr;
055: _path = path;
056: }
057:
058: /**
059: * Creates a field reference using this expression as the base object.
060: *
061: * @param field the string reference for the field.
062: */
063: @Override
064: public Expr createField(String field) {
065: Expr arrayExpr = _expr.createField(new StringLiteral(field));
066:
067: return new PathExpr(arrayExpr, _path + '.' + field);
068: }
069:
070: /**
071: * Creates a method call using this as the <code>obj.method</code>
072: * expression
073: *
074: * @param args the arguments for the method
075: */
076: @Override
077: public Expr createMethod(Expr[] args) {
078: if (_expr instanceof ArrayExpr) {
079: // jsp/1b71
080:
081: ArrayExpr array = (ArrayExpr) _expr;
082:
083: Expr index = array.getIndex();
084:
085: if (index instanceof StringLiteral) {
086: StringLiteral string = (StringLiteral) index;
087:
088: return new MethodExpr(array.getExpr(), string
089: .getValue(), args);
090: }
091: } else if (_expr instanceof ArrayResolverExpr) {
092: ArrayResolverExpr array = (ArrayResolverExpr) _expr;
093:
094: Expr index = array.getIndex();
095:
096: if (index instanceof StringLiteral) {
097: StringLiteral string = (StringLiteral) index;
098:
099: return new MethodExpr(array.getExpr(), string
100: .getValue(), args);
101: }
102: }
103:
104: return new FunctionExpr(this , args);
105: }
106:
107: /**
108: * Evaluates the expression as applicable to the provided context, and returns the
109: * most general type that can be accepted by the
110: * setValue(javax.el.ELContext, java.lang.Object) method.
111: * @param env
112: * @return
113: * @throws PropertyNotFoundException
114: * @throws ELException
115: */
116: @Override
117: public Class<?> getType(ELContext env)
118: throws PropertyNotFoundException, ELException {
119: Class value = _expr.getType(env);
120: if (env.isPropertyResolved()) {
121: return value;
122: }
123:
124: return env.getELResolver().getType(env, _path, null);
125: }
126:
127: /**
128: * Evaluate the expression as an object.
129: *
130: * @param env the variable environment
131: *
132: * @return the evaluated object
133: */
134: @Override
135: public Object getValue(ELContext env) throws ELException {
136: Object value = _expr.getValue(env);
137:
138: if (value != null)
139: return value;
140:
141: env.setPropertyResolved(false);
142: return env.getELResolver().getValue(env, null, _path);
143: }
144:
145: /**
146: * Evaluate the expression as an object.
147: *
148: * @param env the variable environment
149: *
150: * @return the evaluated object
151: */
152: @Override
153: public boolean isReadOnly(ELContext env) throws ELException {
154: return _expr.isReadOnly(env);
155: }
156:
157: /**
158: * Evaluate the expression as an object.
159: *
160: * @param env the variable environment
161: *
162: * @return the evaluated object
163: */
164: @Override
165: public void setValue(ELContext env, Object value)
166: throws ELException {
167: _expr.setValue(env, value);
168: }
169:
170: /**
171: * Returns the method info.
172: *
173: * @param env the variable environment
174: *
175: * @return the value of the expression as an object
176: */
177: @Override
178: public MethodInfo getMethodInfo(ELContext env, Class<?> retType,
179: Class<?>[] argTypes) throws ELException {
180: return _expr.getMethodInfo(env, retType, argTypes);
181: }
182:
183: /**
184: * Evaluates the expression, returning an object.
185: *
186: * @param env the variable environment
187: *
188: * @return the value of the expression as an object
189: */
190: @Override
191: public Object invoke(ELContext env, Class<?>[] argTypes,
192: Object[] args) throws ELException {
193: return _expr.invoke(env, argTypes, args);
194: }
195:
196: /**
197: * Prints the code to create an LongLiteral.
198: *
199: * @param os stream to the generated *.java code
200: */
201: @Override
202: public void printCreate(WriteStream os) throws IOException {
203: os.print("new com.caucho.el.PathExpr(");
204: _expr.printCreate(os);
205: os.print(", \"");
206: os.print(_path);
207: os.print("\")");
208: }
209:
210: /**
211: * Returns true for equal strings.
212: */
213: public boolean equals(Object o) {
214: if (!(o instanceof PathExpr))
215: return false;
216:
217: PathExpr expr = (PathExpr) o;
218:
219: return (_expr.equals(expr._expr) && _path.equals(expr._path));
220: }
221:
222: /**
223: * Returns a readable representation of the expr.
224: */
225: public String toString() {
226: return String.valueOf(_expr);
227: }
228: }
|