001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: *
023: * Free Software Foundation, Inc.
024: * 59 Temple Place, Suite 330
025: * Boston, MA 02111-1307 USA
026: *
027: * @author Scott Ferguson
028: */
029:
030: package com.caucho.el;
031:
032: import com.caucho.util.CharBuffer;
033: import com.caucho.vfs.WriteStream;
034:
035: import javax.el.ELContext;
036: import javax.el.ELException;
037: import java.io.IOException;
038:
039: /**
040: * Representing a string interpolation expression.
041: */
042: public class InterpolateExpr extends Expr {
043: private Expr _left;
044: private Expr _right;
045:
046: /**
047: * Create a new interpolation expression.
048: *
049: * @param left the left subexpression
050: * @param right the right subexpression
051: */
052: public InterpolateExpr(Expr left, Expr right) {
053: _left = left;
054: _right = right;
055: }
056:
057: /**
058: * Returns true if this is a constant expression.
059: */
060: @Override
061: public boolean isConstant() {
062: return _left.isConstant() && _right.isConstant();
063: }
064:
065: /**
066: * Evaluate the expression as an object.
067: *
068: * @param env the variable environment
069: *
070: * @return the string value
071: */
072: @Override
073: public Object getValue(ELContext env) throws ELException {
074: return evalString(env);
075: }
076:
077: /**
078: * Evaluate the expression as an object.
079: */
080: @Override
081: public String evalString(ELContext env) throws ELException {
082: CharBuffer cb = new CharBuffer();
083:
084: Expr expr = this ;
085:
086: for (; expr instanceof InterpolateExpr; expr = ((InterpolateExpr) expr)._right) {
087: InterpolateExpr subExpr = (InterpolateExpr) expr;
088:
089: String value = subExpr._left.evalString(env);
090:
091: if (value != null)
092: cb.append(value);
093: }
094:
095: String value = expr.evalString(env);
096: if (value != null)
097: cb.append(value);
098:
099: return cb.close();
100: }
101:
102: /**
103: * Prints the interpolated value directly to the output.
104: *
105: * @param out the output stream
106: * @param env the variable environment
107: * @param escapeXml if true, then escape the output
108: *
109: * @return false, since the interpolated value is never null
110: */
111: @Override
112: public boolean print(WriteStream out, ELContext env,
113: boolean escapeXml) throws IOException, ELException {
114: _left.print(out, env, escapeXml);
115: _right.print(out, env, escapeXml);
116:
117: return false;
118: }
119:
120: public String toString() {
121: StringBuilder sb = new StringBuilder();
122:
123: if (_left instanceof StringLiteral)
124: sb.append(_left);
125: else
126: sb.append("${" + _left + "}");
127:
128: if (_right instanceof StringLiteral)
129: sb.append(_right);
130: else
131: sb.append("${" + _right + "}");
132:
133: return sb.toString();
134: }
135:
136: /**
137: * Prints the code to create an LongLiteral.
138: */
139: @Override
140: public void printCreate(WriteStream os) throws IOException {
141: os.print("new com.caucho.el.InterpolateExpr(");
142: _left.printCreate(os);
143: os.print(", ");
144: _right.printCreate(os);
145: os.print(")");
146: }
147:
148: /**
149: * Returns true for equal strings.
150: */
151: public boolean equals(Object o) {
152: if (!(o instanceof InterpolateExpr))
153: return false;
154:
155: InterpolateExpr expr = (InterpolateExpr) o;
156:
157: return _left.equals(expr._left) && _right.equals(expr._right);
158: }
159: }
|