KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > el > PathExpr


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  *
23  * Free Software Foundation, Inc.
24  * 59 Temple Place, Suite 330
25  * Boston, MA 02111-1307 USA
26  *
27  * @author Scott Ferguson
28  */

29
30 package com.caucho.el;
31
32 import com.caucho.vfs.WriteStream;
33
34 import javax.el.ELContext;
35 import javax.el.ELException;
36 import javax.el.MethodInfo;
37 import java.io.IOException JavaDoc;
38
39 /**
40  * Represents a field reference that may also be a dotted property,
41  * e.g. smtp.mail.host.
42  * </pre>
43  */

44 public class PathExpr extends Expr {
45   private Expr _expr;
46
47   private String JavaDoc _path;
48
49   /**
50    * Creates a new path expression.
51    *
52    * @param expr the underlying expression
53    * @param path the property name to use if null
54    */

55   public PathExpr(Expr expr, String JavaDoc path)
56   {
57     _expr = expr;
58     _path = path;
59   }
60
61   /**
62    * Creates a field reference using this expression as the base object.
63    *
64    * @param field the string reference for the field.
65    */

66   @Override JavaDoc
67   public Expr createField(String JavaDoc field)
68   {
69     Expr arrayExpr = _expr.createField(new StringLiteral(field));
70
71     return new PathExpr(arrayExpr, _path + '.' + field);
72   }
73
74   /**
75    * Creates a method call using this as the <code>obj.method</code>
76    * expression
77    *
78    * @param args the arguments for the method
79    */

80   @Override JavaDoc
81   public Expr createMethod(Expr []args)
82   {
83     if (_expr instanceof ArrayExpr) {
84       // jsp/1b71
85

86       ArrayExpr array = (ArrayExpr) _expr;
87
88       Expr index = array.getIndex();
89       
90       if (index instanceof StringLiteral) {
91     StringLiteral string = (StringLiteral) index;
92
93     return new MethodExpr(array.getExpr(), string.getValue(), args);
94       }
95     }
96     else if (_expr instanceof ArrayResolverExpr) {
97       ArrayResolverExpr array = (ArrayResolverExpr) _expr;
98
99       Expr index = array.getIndex();
100       
101       if (index instanceof StringLiteral) {
102     StringLiteral string = (StringLiteral) index;
103
104     return new MethodExpr(array.getExpr(), string.getValue(), args);
105       }
106     }
107       
108     return new FunctionExpr(this, args);
109   }
110   
111   /**
112    * Evaluate the expression as an object.
113    *
114    * @param env the variable environment
115    *
116    * @return the evaluated object
117    */

118   @Override JavaDoc
119   public Object JavaDoc getValue(ELContext env)
120     throws ELException
121   {
122     Object JavaDoc value = _expr.getValue(env);
123
124     if (value != null)
125       return value;
126
127     return env.getELResolver().getValue(env, _path, null);
128   }
129
130   /**
131    * Returns the method info.
132    *
133    * @param env the variable environment
134    *
135    * @return the value of the expression as an object
136    */

137   @Override JavaDoc
138   public MethodInfo getMethodInfo(ELContext env,
139                   Class JavaDoc<?> retType,
140                   Class JavaDoc<?> []argTypes)
141     throws ELException
142   {
143     return _expr.getMethodInfo(env, retType, argTypes);
144   }
145
146   /**
147    * Evaluates the expression, returning an object.
148    *
149    * @param env the variable environment
150    *
151    * @return the value of the expression as an object
152    */

153   @Override JavaDoc
154   public Object JavaDoc invoke(ELContext env, Class JavaDoc<?> []argTypes, Object JavaDoc []args)
155     throws ELException
156   {
157     return _expr.invoke(env, argTypes, args);
158   }
159
160   /**
161    * Prints the code to create an LongLiteral.
162    *
163    * @param os stream to the generated *.java code
164    */

165   @Override JavaDoc
166   public void printCreate(WriteStream os)
167     throws IOException JavaDoc
168   {
169     os.print("new com.caucho.el.PathExpr(");
170     _expr.printCreate(os);
171     os.print(", \"");
172     os.print(_path);
173     os.print("\")");
174   }
175
176   /**
177    * Returns true for equal strings.
178    */

179   public boolean equals(Object JavaDoc o)
180   {
181     if (! (o instanceof PathExpr))
182       return false;
183
184     PathExpr expr = (PathExpr) o;
185
186     return (_expr.equals(expr._expr) && _path.equals(expr._path));
187   }
188
189   /**
190    * Returns a readable representation of the expr.
191    */

192   public String JavaDoc toString()
193   {
194     return String.valueOf(_expr);
195   }
196 }
197
Popular Tags