KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.CharBuffer;
33 import com.caucho.vfs.WriteStream;
34
35 import javax.el.ELContext;
36 import javax.el.ELException;
37 import java.io.IOException JavaDoc;
38
39 /**
40  * Representing a string interpolation expression.
41  */

42 public class InterpolateExpr extends Expr {
43   private Expr _left;
44   private Expr _right;
45
46   /**
47    * Create a new interpolation expression.
48    *
49    * @param left the left subexpression
50    * @param right the right subexpression
51    */

52   public InterpolateExpr(Expr left, Expr right)
53   {
54     _left = left;
55     _right = right;
56   }
57
58   /**
59    * Returns true if this is a constant expression.
60    */

61   @Override JavaDoc
62   public boolean isConstant()
63   {
64     return _left.isConstant() && _right.isConstant();
65   }
66   
67   /**
68    * Evaluate the expression as an object.
69    *
70    * @param env the variable environment
71    *
72    * @return the string value
73    */

74   @Override JavaDoc
75   public Object JavaDoc getValue(ELContext env)
76     throws ELException
77   {
78     return evalString(env);
79   }
80   
81   /**
82    * Evaluate the expression as an object.
83    */

84   @Override JavaDoc
85   public String JavaDoc evalString(ELContext env)
86     throws ELException
87   {
88     CharBuffer cb = new CharBuffer();
89
90     Expr expr = this;
91
92     for (;
93          expr instanceof InterpolateExpr;
94          expr = ((InterpolateExpr) expr)._right) {
95       InterpolateExpr subExpr = (InterpolateExpr) expr;
96
97       String JavaDoc value = subExpr._left.evalString(env);
98
99       if (value != null)
100         cb.append(value);
101     }
102
103     String JavaDoc value = expr.evalString(env);
104     if (value != null)
105       cb.append(value);
106     
107     return cb.close();
108   }
109
110   /**
111    * Prints the interpolated value directly to the output.
112    *
113    * @param out the output stream
114    * @param env the variable environment
115    * @param escapeXml if true, then escape the output
116    *
117    * @return false, since the interpolated value is never null
118    */

119   @Override JavaDoc
120   public boolean print(WriteStream out,
121                        ELContext env,
122                        boolean escapeXml)
123     throws IOException JavaDoc, ELException
124   {
125     _left.print(out, env, escapeXml);
126     _right.print(out, env, escapeXml);
127
128     return false;
129   }
130
131   public String JavaDoc toString()
132   {
133     StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
134
135     if (_left instanceof StringLiteral)
136       sb.append(_left);
137     else
138       sb.append("${" + _left + "}");
139
140     if (_right instanceof StringLiteral)
141       sb.append(_right);
142     else
143       sb.append("${" + _right + "}");
144
145     return sb.toString();
146   }
147
148   /**
149    * Prints the code to create an LongLiteral.
150    */

151   @Override JavaDoc
152   public void printCreate(WriteStream os)
153     throws IOException JavaDoc
154   {
155     os.print("new com.caucho.el.InterpolateExpr(");
156     _left.printCreate(os);
157     os.print(", ");
158     _right.printCreate(os);
159     os.print(")");
160   }
161
162   /**
163    * Returns true for equal strings.
164    */

165   public boolean equals(Object JavaDoc o)
166   {
167     if (! (o instanceof InterpolateExpr))
168       return false;
169
170     InterpolateExpr expr = (InterpolateExpr) o;
171
172     return _left.equals(expr._left) && _right.equals(expr._right);
173   }
174 }
175
Popular Tags