KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.IOException JavaDoc;
37
38 /**
39  * Identifier expression.
40  */

41 public class IdExpr extends Expr {
42   // The identifier name
43
private String JavaDoc _id;
44
45   /**
46    * Creates the identifier
47    */

48   public IdExpr(String JavaDoc id)
49   {
50     _id = id;
51   }
52
53   /**
54    * Returns true if the expression is read-only.
55    */

56   @Override JavaDoc
57   public boolean isReadOnly(ELContext env)
58   {
59     return false;
60   }
61
62   /**
63    * Creates a field reference using this expression as the base object.
64    *
65    * @param field the string reference for the field.
66    */

67   @Override JavaDoc
68   public Expr createField(String JavaDoc field)
69   {
70     Expr arrayExpr = createField(new StringLiteral(field));
71
72     return new PathExpr(arrayExpr, _id + '.' + field);
73   }
74
75   /**
76    * Evaluate the expr as an object.
77    *
78    * @param env the variable environment
79    *
80    * @return the value as an object
81    */

82   @Override JavaDoc
83   public Object JavaDoc getValue(ELContext env)
84     throws ELException
85   {
86     return env.getELResolver().getValue(env, null, _id);
87   }
88
89   /**
90    * Evaluates the expression, setting an object.
91    *
92    * @param env the variable environment
93    *
94    * @return the value of the expression as an object
95    */

96   @Override JavaDoc
97   public void setValue(ELContext env, Object JavaDoc value)
98     throws ELException
99   {
100     env.getELResolver().setValue(env, null, _id, value);
101   }
102
103   /**
104    * Prints the code to create an IdExpr.
105    */

106   @Override JavaDoc
107   public void printCreate(WriteStream os)
108     throws IOException JavaDoc
109   {
110     os.print("new com.caucho.el.IdExpr(\"");
111     printEscapedString(os, _id);
112     os.print("\")");
113   }
114
115   public boolean equals(Object JavaDoc o)
116   {
117     if (o == null || ! o.getClass().equals(IdExpr.class))
118       return false;
119
120     IdExpr expr = (IdExpr) o;
121
122     return _id.equals(expr._id);
123   }
124
125   public String JavaDoc toString()
126   {
127     return _id;
128   }
129 }
130
Popular Tags