KickJava   Java API By Example, From Geeks To Geeks.

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


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 string literal expression
41  */

42 public class StringLiteral extends Expr {
43   private String JavaDoc _value;
44
45   public StringLiteral(String JavaDoc value)
46   {
47     _value = value;
48   }
49
50   /**
51    * Returns true if the expression is constant.
52    */

53   @Override JavaDoc
54   public boolean isConstant()
55   {
56     return true;
57   }
58
59   /**
60    * Returns true if the expression is literal text
61    */

62   @Override JavaDoc
63   public boolean isLiteralText()
64   {
65     return true;
66   }
67
68   /**
69    * Evaluates the expression, returning an object.
70    *
71    * @param env the variable environment
72    *
73    * @return the value of the expression as an object
74    */

75   @Override JavaDoc
76   public MethodInfo getMethodInfo(ELContext env,
77                   Class JavaDoc<?> returnType,
78                   Class JavaDoc<?> []argTypes)
79     throws ELException
80   {
81     return new MethodInfo(_value, returnType, argTypes);
82   }
83
84   /**
85    * Evaluates the expression, returning an object.
86    *
87    * @param env the variable environment
88    *
89    * @return the value of the expression as an object
90    */

91   @Override JavaDoc
92   public Object JavaDoc invoke(ELContext env, Class JavaDoc<?> []argTypes, Object JavaDoc []args)
93     throws ELException
94   {
95     return _value;
96   }
97   
98   /**
99    * Returns the value of the literal.
100    */

101   public String JavaDoc getValue()
102   {
103     return _value;
104   }
105   
106   /**
107    * Evaluate the expr as an object.
108    *
109    * @param env the variable environment
110    */

111   @Override JavaDoc
112   public Object JavaDoc getValue(ELContext env)
113     throws ELException
114   {
115     return _value;
116   }
117   
118   /**
119    * Evaluate the expr as a string
120    *
121    * @param env the variable environment
122    */

123   @Override JavaDoc
124   public String JavaDoc evalString(ELContext env)
125     throws ELException
126   {
127     return _value;
128   }
129
130   /**
131    * Evalutes directly to the output.
132    */

133   @Override JavaDoc
134   public boolean print(WriteStream out,
135                ELContext env,
136                boolean isEscape)
137     throws IOException JavaDoc, ELException
138   {
139     if (isEscape)
140       toStreamEscaped(out, _value);
141     else
142       out.print(_value);
143     
144     return false;
145   }
146
147   /**
148    * Prints the code to create an LongLiteral.
149    */

150   @Override JavaDoc
151   public void printCreate(WriteStream os)
152     throws IOException JavaDoc
153   {
154     os.print("new com.caucho.el.StringLiteral(\"");
155     printEscapedString(os, _value);
156     os.print("\")");
157   }
158
159   /**
160    * Returns true for equal strings.
161    */

162   public boolean equals(Object JavaDoc o)
163   {
164     if (! (o instanceof StringLiteral))
165       return false;
166
167     StringLiteral literal = (StringLiteral) o;
168
169     return _value.equals(literal._value);
170   }
171
172   /**
173    * Returns a printable version.
174    */

175   public String JavaDoc toString()
176   {
177     return "\"" + _value + "\"";
178   }
179 }
180
Popular Tags