KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > quercus > expr > VarExpr


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.quercus.expr;
31
32 import com.caucho.quercus.Location;
33 import com.caucho.quercus.env.ArrayValueImpl;
34 import com.caucho.quercus.env.Env;
35 import com.caucho.quercus.env.NullValue;
36 import com.caucho.quercus.env.Value;
37 import com.caucho.quercus.parser.QuercusParser;
38
39 /**
40  * Represents a PHP variable expression.
41  */

42 public class VarExpr
43   extends AbstractVarExpr
44 {
45   private static final NullValue NULL = NullValue.create();
46
47   private final VarInfo _var;
48   protected final String JavaDoc _name;
49
50   private VarState _varState = VarState.INIT;
51
52   protected VarExpr(Location location, VarInfo var)
53   {
54     super(location);
55     _var = var;
56     _name = var.getName();
57   }
58
59   protected VarExpr(VarInfo var)
60   {
61     _var = var;
62     _name = var.getName();
63   }
64
65   /**
66    * Returns the variable info.
67    */

68   public VarInfo getVarInfo()
69   {
70     return _var;
71   }
72
73   /**
74    * Returns the variable name.
75    */

76   public String JavaDoc getName()
77   {
78     return _name;
79   }
80
81   /**
82    * Returns the java variable name.
83    */

84   public String JavaDoc getJavaVar()
85   {
86     return "v_" + _name;
87   }
88
89   /**
90    * Copy for things like $a .= "test";
91    * @param location
92    */

93   public Expr copy(Location location)
94   {
95     return new VarExpr(location, _var);
96   }
97
98   /**
99    * Creates the assignment.
100    */

101   public Expr createAssign(QuercusParser parser, Expr value)
102   {
103     _var.setAssigned();
104
105     return super.createAssign(parser, value);
106   }
107
108   /**
109    * Creates the assignment.
110    */

111   public void assign(QuercusParser parser)
112   {
113     _var.setAssigned();
114   }
115
116   /**
117    * Creates the assignment.
118    */

119   public Expr createAssignRef(QuercusParser parser,
120                               Expr value
121   )
122   {
123     _var.setAssigned();
124
125     return super.createAssignRef(parser, value);
126   }
127
128   /**
129    * Evaluates the expression.
130    *
131    * @param env the calling environment.
132    * @return the expression value.
133    */

134   public Value eval(Env env)
135   {
136     Value value;
137
138     if (_var.isGlobal())
139       value = env.getGlobalValue(_name);
140     else
141       value = env.getValue(_name);
142
143     if (value != null)
144       return value;
145     else
146       return NULL;
147   }
148
149   /**
150    * Evaluates the expression.
151    *
152    * @param env the calling environment.
153    * @return the expression value.
154    */

155   public Value evalCopy(Env env)
156   {
157     Value v = eval(env);
158     
159     return v.copy();
160   }
161
162   /**
163    * Evaluates the expression, converting to an array if unset.
164    *
165    * @param env the calling environment.
166    * @return the expression value.
167    */

168   public Value evalArray(Env env)
169   {
170     Value value;
171
172     if (_var.isGlobal()) {
173       value = env.getGlobalValue(_name);
174
175       if (value == null) {
176         value = new ArrayValueImpl();
177
178         env.setGlobalValue(_name, value);
179       }
180       else {
181         Value array = value.toAutoArray();
182
183     if (array != value) {
184       env.setGlobalValue(_name, array);
185
186       value = array;
187     }
188       }
189     } else {
190       value = env.getVar(_name);
191
192       if (value == null) {
193         value = new ArrayValueImpl();
194
195         env.setValue(_name, value);
196       }
197       else {
198     value = value.toAutoArray();
199       }
200     }
201
202     return value;
203   }
204
205   /**
206    * Evaluates the expression, converting to an object if unset.
207    *
208    * @param env the calling environment.
209    * @return the expression value.
210    */

211   public Value evalObject(Env env)
212   {
213     Value value;
214
215     if (_var.isGlobal()) {
216       value = env.getGlobalValue(_name);
217
218       if (value == null || ! value.isset()) {
219         value = env.createObject();
220
221         env.setGlobalValue(_name, value);
222       }
223     } else {
224       value = env.getValue(_name);
225
226       if (value == null || ! value.isset()) {
227         value = env.createObject();
228
229         env.setValue(_name, value);
230       }
231     }
232
233     return value;
234   }
235
236   /**
237    * Evaluates the expression.
238    *
239    * @param env the calling environment.
240    * @return the expression value.
241    */

242   public Value evalRef(Env env)
243   {
244     if (getVarInfo().isGlobal())
245       return env.getGlobalVar(_name);
246     else
247       return env.getVar(_name);
248   }
249
250   /**
251    * Evaluates the expression.
252    *
253    * @param env the calling environment.
254    * @return the expression value.
255    */

256   public Value evalArg(Env env)
257   {
258     // quercus/043k
259
// quercus/0443
260

261     if (getVarInfo().isGlobal())
262       return env.getGlobalVar(_name);
263     else
264       return env.getVar(_name);
265   }
266
267   /**
268    * Evaluates the expression.
269    *
270    * @param env the calling environment.
271    */

272   public void evalAssign(Env env, Value value)
273   {
274     if (getVarInfo().isGlobal())
275       env.setGlobalValue(_name, value);
276     else
277       env.setValue(_name, value);
278   }
279
280   /**
281    * Evaluates the expression.
282    *
283    * @param env the calling environment.
284    */

285   public void evalUnset(Env env)
286   {
287     if (getVarInfo().isGlobal())
288       env.unsetGlobalVar(_name);
289     else
290       env.unsetLocalVar(_name);
291   }
292
293   public int hashCode()
294   {
295     return _name.hashCode();
296   }
297
298   public boolean equals(Object JavaDoc o)
299   {
300     if (this == o)
301       return true;
302     else if (getClass() != o.getClass())
303       return false;
304
305     VarExpr var = (VarExpr) o;
306
307     return _var == var._var;
308   }
309
310   public String JavaDoc toString()
311   {
312     return "$" + _name;
313   }
314 }
315
316
Popular Tags