1 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 ; 38 39 42 public class InterpolateExpr extends Expr { 43 private Expr _left; 44 private Expr _right; 45 46 52 public InterpolateExpr(Expr left, Expr right) 53 { 54 _left = left; 55 _right = right; 56 } 57 58 61 @Override 62 public boolean isConstant() 63 { 64 return _left.isConstant() && _right.isConstant(); 65 } 66 67 74 @Override 75 public Object getValue(ELContext env) 76 throws ELException 77 { 78 return evalString(env); 79 } 80 81 84 @Override 85 public String 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 value = subExpr._left.evalString(env); 98 99 if (value != null) 100 cb.append(value); 101 } 102 103 String value = expr.evalString(env); 104 if (value != null) 105 cb.append(value); 106 107 return cb.close(); 108 } 109 110 119 @Override 120 public boolean print(WriteStream out, 121 ELContext env, 122 boolean escapeXml) 123 throws IOException , ELException 124 { 125 _left.print(out, env, escapeXml); 126 _right.print(out, env, escapeXml); 127 128 return false; 129 } 130 131 public String toString() 132 { 133 StringBuilder sb = new StringBuilder (); 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 151 @Override 152 public void printCreate(WriteStream os) 153 throws IOException 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 165 public boolean equals(Object 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 |