1 14 15 package com.sun.facelets.el; 16 17 import java.io.IOException ; 18 import java.io.StringWriter ; 19 import java.io.Writer ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import javax.el.ELContext; 24 import javax.el.ELException; 25 import javax.el.ExpressionFactory; 26 import javax.el.ValueExpression; 27 import javax.faces.context.ResponseWriter; 28 29 import com.sun.facelets.util.FastWriter; 30 31 38 public class ELText { 39 40 private static final class LiteralValueExpression extends ValueExpression { 41 42 45 private static final long serialVersionUID = 1L; 46 47 private final String text; 48 49 public LiteralValueExpression(String text) { 50 this.text = text; 51 } 52 53 public boolean isLiteralText() { 54 return false; 55 } 56 57 public int hashCode() { 58 return 0; 59 } 60 61 public String getExpressionString() { 62 return this.text; 63 } 64 65 public boolean equals(Object obj) { 66 return false; 67 } 68 69 public void setValue(ELContext context, Object value) { 70 } 71 72 public boolean isReadOnly(ELContext context) { 73 return false; 74 } 75 76 public Object getValue(ELContext context) { 77 return null; 78 } 79 80 public Class getType(ELContext context) { 81 return null; 82 } 83 84 public Class getExpectedType() { 85 return null; 86 } 87 88 } 89 90 private static final class ELTextComposite extends ELText { 91 private final ELText[] txt; 92 93 public ELTextComposite(ELText[] txt) { 94 super(null); 95 this.txt = txt; 96 } 97 98 public void write(Writer out, ELContext ctx) throws ELException, 99 IOException { 100 for (int i = 0; i < this.txt.length; i++) { 101 this.txt[i].write(out, ctx); 102 } 103 } 104 105 public void writeText(ResponseWriter out, ELContext ctx) 106 throws ELException, IOException { 107 for (int i = 0; i < this.txt.length; i++) { 108 this.txt[i].writeText(out, ctx); 109 } 110 } 111 112 public String toString(ELContext ctx) { 113 StringBuffer sb = new StringBuffer (); 114 for (int i = 0; i < this.txt.length; i++) { 115 sb.append(this.txt[i].toString(ctx)); 116 } 117 return sb.toString(); 118 } 119 120 125 126 public String toString() { 127 StringBuffer sb = new StringBuffer (); 128 for (int i = 0; i < this.txt.length; i++) { 129 sb.append(this.txt[i].toString()); 130 } 131 return sb.toString(); 132 } 133 134 public boolean isLiteral() { 135 return false; 136 } 137 138 public ELText apply(ExpressionFactory factory, ELContext ctx) { 139 int len = this.txt.length; 140 ELText[] nt = new ELText[len]; 141 for (int i = 0; i < len; i++) { 142 nt[i] = this.txt[i].apply(factory, ctx); 143 } 144 return new ELTextComposite(nt); 145 } 146 } 147 148 private static final class ELTextVariable extends ELText { 149 private final ValueExpression ve; 150 151 public ELTextVariable(ValueExpression ve) { 152 super(ve.getExpressionString()); 153 this.ve = ve; 154 } 155 156 public boolean isLiteral() { 157 return false; 158 } 159 160 public ELText apply(ExpressionFactory factory, ELContext ctx) { 161 return new ELTextVariable(factory.createValueExpression(ctx, 162 this.ve.getExpressionString(), String .class)); 163 } 164 165 public void write(Writer out, ELContext ctx) throws ELException, 166 IOException { 167 Object v = this.ve.getValue(ctx); 168 if (v != null) { 169 out.write((String ) v); 170 } 171 } 172 173 public String toString(ELContext ctx) throws ELException { 174 Object v = this.ve.getValue(ctx); 175 if (v != null) { 176 return v.toString(); 177 } 178 179 return null; 180 } 181 182 public void writeText(ResponseWriter out, ELContext ctx) 183 throws ELException, IOException { 184 Object v = this.ve.getValue(ctx); 185 if (v != null) { 186 out.writeText((String ) v, null); 187 } 188 } 189 } 190 191 protected final String literal; 192 193 public ELText(String literal) { 194 this.literal = literal; 195 } 196 197 203 public boolean isLiteral() { 204 return true; 205 } 206 207 217 public ELText apply(ExpressionFactory factory, ELContext ctx) { 218 return this; 219 } 220 221 232 public void write(Writer out, ELContext ctx) throws ELException, 233 IOException { 234 out.write(this.literal); 235 } 236 237 public void writeText(ResponseWriter out, ELContext ctx) 238 throws ELException, IOException { 239 out.writeText(this.literal, null); 240 } 241 242 250 public String toString(ELContext ctx) throws ELException { 251 return this.literal; 252 } 253 254 public String toString() { 255 return this.literal; 256 } 257 258 266 public static boolean isLiteral(String in) { 267 ELText txt = parse(in); 268 return txt == null || txt.isLiteral(); 269 } 270 271 281 public static ELText parse(String in) throws ELException { 282 return parse(null, null, in); 283 } 284 285 300 public static ELText parse(ExpressionFactory fact, ELContext ctx, String in) 301 throws ELException { 302 char[] ca = in.toCharArray(); 303 int i = 0; 304 char c = 0; 305 int len = ca.length; 306 int end = len - 1; 307 boolean esc = false; 308 int vlen = 0; 309 310 StringBuffer buff = new StringBuffer (128); 311 List text = new ArrayList (); 312 ELText t = null; 313 ValueExpression ve = null; 314 315 while (i < len) { 316 c = ca[i]; 317 if ('\\' == c) { 318 esc = !esc; 319 if (esc && i < end && ca[i + 1] == '$' || ca[i + 1] == '#') { 320 i++; 321 continue; 322 } 323 } else if (!esc && ('$' == c || '#' == c)) { 324 if (i < end) { 325 if ('{' == ca[i + 1]) { 326 if (buff.length() > 0) { 327 text.add(new ELText(buff.toString())); 328 buff.setLength(0); 329 } 330 vlen = findVarLength(ca, i); 331 if (ctx != null && fact != null) { 332 ve = fact.createValueExpression(ctx, new String (ca, 333 i, vlen), String .class); 334 t = new ELTextVariable(ve); 335 } else { 336 t = new ELTextVariable(new LiteralValueExpression( 337 new String (ca, i, vlen))); 338 } 339 text.add(t); 340 i += vlen; 341 continue; 342 } 343 } 344 } 345 esc = false; 346 buff.append(c); 347 i++; 348 } 349 350 if (buff.length() > 0) { 351 text.add(new ELText(new String (buff.toString()))); 352 buff.setLength(0); 353 } 354 355 if (text.size() == 0) { 356 return null; 357 } else if (text.size() == 1) { 358 return (ELText) text.get(0); 359 } else { 360 ELText[] ta = (ELText[]) text.toArray(new ELText[text.size()]); 361 return new ELTextComposite(ta); 362 } 363 } 364 365 private static int findVarLength(char[] ca, int s) throws ELException { 366 int i = s; 367 int len = ca.length; 368 char c = 0; 369 int str = 0; 370 boolean esc = false; 371 while (i < len) { 372 c = ca[i]; 373 if ('\\' == c) { 374 esc = true; 375 } else if (!esc && ('\'' == c || '"' == c)) { 376 if (str == c) { 377 str = 0; 378 } else { 379 str = c; 380 } 381 } else if (!esc && str == 0 && ('}' == c)) { 382 return i - s + 1; 383 } 384 i++; 385 } 386 throw new ELException("EL Expression Unbalanced: ... " 387 + new String (ca, s, i - s)); 388 } 389 390 } 391 | Popular Tags |