1 34 35 package com.micronova.jsp.tag; 36 37 import com.micronova.util.*; 38 import java.util.*; 39 import javax.servlet.jsp.*; 40 import java.lang.reflect.*; 41 import java.util.regex.*; 42 43 46 47 public class EL extends ELRoot implements ObjectSource, ObjectTarget 48 { 49 public static final String DEFAULT = "default"; 50 51 52 53 public static final String DEFAULTPATTERNEVAL = "[@]\\{([^}]*)\\}"; 54 public static final String DEFAULTPATTERNQUERY = "[%]\\{([^\\}]*)\\}"; 55 public static final String DEFAULTPATTERNNAME = "__"; 56 57 public static final Pattern defaultPatternEval = Pattern.compile(DEFAULTPATTERNEVAL); 58 public static final Pattern defaultPatternQuery = Pattern.compile(DEFAULTPATTERNQUERY); 59 60 public static final Pattern defaultPatternName = Pattern.compile(DEFAULTPATTERNNAME); 61 62 public static final Pattern getPattern(Object patternSpec) 63 { 64 if (DEFAULTPATTERNEVAL.equals(patternSpec)) 65 { 66 return defaultPatternEval; 67 } 68 else if (DEFAULTPATTERNQUERY.equals(patternSpec)) 69 { 70 return defaultPatternQuery; 71 } 72 else if (patternSpec instanceof Pattern) 73 { 74 return (Pattern)patternSpec; 75 } 76 else if (patternSpec != null) 77 { 78 String patternString = (String )patternSpec; 79 80 if (patternString.indexOf('(') == -1) 81 { 82 patternString = patternString + "\\{([^}]*)\\}"; 83 } 84 85 return Pattern.compile(patternString); 86 } 87 else 88 { 89 return null; 90 } 91 } 92 93 protected PageContext _pageContext; 94 protected String _codec; 95 protected String _nameCodec; 96 protected String _valueCodec; 97 98 99 100 public static final String CODECPATH = "com.micronova.util.codec.Codec"; 101 102 103 104 public static final String OPERANDVAR = "_operand"; 105 106 107 108 public static void setPageAttribute(PageContext pageContext, String name, Object value) 109 { 110 if (name != null) 111 { 112 if (value == null) 113 { 114 pageContext.removeAttribute(name, PageContext.PAGE_SCOPE); 115 } 116 else 117 { 118 pageContext.setAttribute(name, value, PageContext.PAGE_SCOPE); 119 } 120 } 121 } 122 123 124 125 public static Object getPageAttribute(PageContext pageContext, String name) 126 { 127 return pageContext.getAttribute(name, PageContext.PAGE_SCOPE); 128 } 129 130 131 132 public final static StringBuffer replaceEvalEscape(StringBuffer buffer) 133 { 134 boolean isBeforeBrace = false; 135 136 for (int i = buffer.length(); --i >= 0;) 137 { 138 char c = buffer.charAt(i); 139 140 if (isBeforeBrace) 141 { 142 if (c == '@') 143 { 144 buffer.setCharAt(i, '$'); 145 } 146 147 isBeforeBrace = false; 148 } 149 else 150 { 151 if (c == '{') 152 { 153 isBeforeBrace = true; 154 } 155 } 156 } 157 158 return buffer; 159 } 160 161 162 163 public final static String replaceEvalEscape(String expression) 164 { 165 if (expression != null) 166 { 167 expression = replaceEvalEscape(new StringBuffer (expression)).toString(); 168 } 169 170 return expression; 171 } 172 173 174 175 public static Object applyCodec(PageContext pageContext, String codecs, Object object) throws Exception 176 { 177 if ((codecs != null) && (codecs.length() > 0)) 178 { 179 List codecList = StringUtil.split(codecs, '|'); 180 181 for (int i = 0; i < codecList.size(); i ++) 182 { 183 String codec = (String )codecList.get(i); 184 185 setPageAttribute(pageContext, OPERANDVAR, object); 186 187 List partList = StringUtil.split(codec, ':'); 188 189 int partCount = partList.size(); 190 191 if (partCount < 2) 192 { 193 object = evaluateExpression(pageContext, "codecExpression", codec.replaceAll("\\\\\\{", "{"), Object .class); 194 } 195 else 196 { 197 String codecName = (String )partList.get(0); 198 199 List implicitArg = StringUtil.split((String )partList.get(1), ';'); 200 201 boolean doesUseObject = true; 202 203 if (codecName.startsWith("_")) 204 { 205 codecName = codecName.substring(1); 206 implicitArg.add(1, pageContext); 207 } 208 209 if (codecName.indexOf('.') < 0) 210 { 211 StringBuffer buffer = new StringBuffer (); 212 213 buffer.append(CODECPATH); 214 buffer.append(codecName); 215 216 codecName = buffer.toString(); 217 } 218 219 String codecMethod = (String )implicitArg.get(0); 220 221 if (codecMethod.endsWith("_")) 222 { 223 codecMethod = codecMethod.substring(0, codecMethod.length() - 1); 224 doesUseObject = false; 225 } 226 227 int implicitArgCount = implicitArg.size() - 1; 228 int numArgs = implicitArgCount + partCount - 1; 229 230 if (!doesUseObject) 231 { 232 numArgs --; 233 } 234 235 Class [] types = new Class [numArgs]; 236 Object [] args = new Object [numArgs]; 237 238 int j = 0; 239 240 for (int k = 1; j < implicitArgCount; j ++, k ++) 241 { 242 types[j] = Object .class; 243 244 Object arg = implicitArg.get(k); 245 246 if (arg instanceof String ) 247 { 248 arg = evaluateExpression(pageContext, "codecArgument", (String )arg, Object .class); 249 } 250 251 args[j] = arg; 252 } 253 254 if (doesUseObject) 255 { 256 types[j] = Object .class; 257 args[j] = object; 258 j ++; 259 } 260 261 for (int k = 2; j < numArgs; j ++, k ++) 262 { 263 types[j] = Object .class; 264 args[j] = evaluateExpression(pageContext, "codecArgument", (String )partList.get(k), Object .class); 265 } 266 267 Class c = Class.forName(codecName); 268 Method m = c.getDeclaredMethod(codecMethod, types); 269 270 try 271 { 272 object = m.invoke(null, args); 273 } 274 catch (Exception e) 275 { 276 Throwable cause = e.getCause(); 277 278 if (cause instanceof Exception ) 279 { 280 throw (Exception )cause; 281 } 282 else 283 { 284 throw e; 285 } 286 } 287 } 288 } 289 } 290 291 return object; 292 } 293 294 295 296 public EL(PageContext pageContext, String codec) 297 { 298 _pageContext = pageContext; 299 _codec = codec; 300 } 301 302 303 304 public EL(PageContext pageContext, String nameCodec, String valueCodec) 305 { 306 _pageContext = pageContext; 307 _nameCodec = nameCodec; 308 _valueCodec = valueCodec; 309 } 310 311 312 313 public Object getObject(Object client, Object key) 314 { 315 try 316 { 317 PageContext pageContext = _pageContext; 318 319 return applyCodec(pageContext, _codec, evaluateExpression(pageContext, "eval", "${" + key.toString() + "}", Object .class)); 320 } 321 catch (Exception e) 322 { 323 throw new RuntimeException (e); 324 } 325 } 326 327 328 329 public Object putObject(Object client, Object key, Object value) 330 { 331 try 332 { 333 String nameCodec = _nameCodec; 334 String valueCodec = _valueCodec; 335 336 PageContext pageContext = _pageContext; 337 338 if (nameCodec == DEFAULT) 339 { 340 key = StringUtil.applyPattern(key.toString(), defaultPatternName, null); 341 } 342 else if (!TypeUtil.isEmptyString(nameCodec)) 343 { 344 key = (String )(applyCodec(pageContext, valueCodec, key.toString())); 345 } 346 347 if (value instanceof String ) 348 { 349 if (valueCodec == DEFAULT) 350 { 351 value = XMLUtil.encode(value.toString()); 352 } 353 else if (!TypeUtil.isEmptyString(valueCodec)) 354 { 355 value = (String )applyCodec(pageContext, valueCodec, value.toString()); 356 } 357 } 358 359 return ((Map)client).put(key, value); 360 } 361 catch (Exception e) 362 { 363 throw new RuntimeException (e); 364 } 365 } 366 367 368 369 370 371 public static final String INDEXVAR = "_index"; 372 public static final String ELEMENTVAR = "_element"; 373 public static final String LENGTHVAR = "_length"; 374 public static final String INCLUDE = "include"; 375 public static final String APPLYCODEC = "applyCodec"; 376 public static final String APPLY = "apply"; 377 public static final String BREAK = "break"; 378 379 public static Object applyFilter(PageContext pageContext, Object object, String include, String breakExpression, String apply, String applyCodec) throws Exception 380 { 381 if (object != null) 382 { 383 Object xVar = EL.getPageAttribute(pageContext, ELEMENTVAR); 384 Object iVar = EL.getPageAttribute(pageContext, INDEXVAR); 385 Object lVar = EL.getPageAttribute(pageContext, LENGTHVAR); 386 387 try 388 { 389 if (object instanceof String ) 390 { 391 String string = (String )object; 392 StringBuffer buffer = new StringBuffer (); 393 394 int length = string.length(); 395 396 EL.setPageAttribute(pageContext, LENGTHVAR, new Integer (length)); 397 398 for (int i = 0; i < length; i ++) 399 { 400 char x = string.charAt(i); 401 402 EL.setPageAttribute(pageContext, ELEMENTVAR, "" + x); 403 404 EL.setPageAttribute(pageContext, INDEXVAR, new Integer (i)); 405 406 Boolean b = (Boolean )EL.evaluateExpression(pageContext, "include", include, Boolean .class); 407 408 if (b.booleanValue()) 409 { 410 if (apply == null) 411 { 412 if (applyCodec == null) 413 { 414 buffer.append(x); 415 } 416 else 417 { 418 buffer.append(EL.applyCodec(pageContext, applyCodec, new Character (x))); 419 } 420 } 421 else 422 { 423 Object applyObject = EL.evaluateExpression(pageContext, APPLY, apply, Object .class); 424 425 if (applyCodec != null) 426 { 427 applyObject = EL.applyCodec(pageContext, applyCodec, applyObject); 428 } 429 430 buffer.append(applyObject); 431 } 432 } 433 434 if (breakExpression != null) 435 { 436 Boolean bBreak = (Boolean )EL.evaluateExpression(pageContext, BREAK, breakExpression, Boolean .class); 437 438 if (bBreak.booleanValue()) 439 { 440 break; 441 } 442 } 443 } 444 445 object = buffer.toString(); 446 } 447 else if (object instanceof Map) 448 { 449 Map map = (Map)object; 450 Map newMap = new NestedMap(); 451 452 Iterator iterator = map.entrySet().iterator(); 453 454 int length = map.size(); 455 456 EL.setPageAttribute(pageContext, LENGTHVAR, new Integer (length)); 457 458 for (int i = 0; iterator.hasNext(); i ++) 459 { 460 Map.Entry x = (Map.Entry)iterator.next(); 461 462 EL.setPageAttribute(pageContext, ELEMENTVAR, x); 463 464 EL.setPageAttribute(pageContext, INDEXVAR, new Integer (i)); 465 466 Boolean b = (Boolean )EL.evaluateExpression(pageContext, INCLUDE, include, Boolean .class); 467 468 if (b.booleanValue()) 469 { 470 Object key = x.getKey(); 471 Object keyValue = x.getValue(); 472 473 if (apply != null) 474 { 475 keyValue = EL.evaluateExpression(pageContext, APPLY, apply, Object .class); 476 } 477 478 if (applyCodec != null) 479 { 480 keyValue = EL.applyCodec(pageContext, applyCodec, keyValue); 481 } 482 483 newMap.put(key, keyValue); 484 } 485 486 if (breakExpression != null) 487 { 488 Boolean bBreak = (Boolean )EL.evaluateExpression(pageContext, BREAK, breakExpression, Boolean .class); 489 490 if (bBreak.booleanValue()) 491 { 492 break; 493 } 494 } 495 } 496 497 object = newMap; 498 } 499 else 500 { 501 List list = TypeUtil.isList(object); 502 503 if (list != null) 504 { 505 List newList = new SparseList(); 506 int length = list.size(); 507 508 EL.setPageAttribute(pageContext, LENGTHVAR, new Integer (length)); 509 510 for (int i = 0; i < length; i ++) 511 { 512 Object x = list.get(i); 513 514 EL.setPageAttribute(pageContext, ELEMENTVAR, x); 515 516 EL.setPageAttribute(pageContext, INDEXVAR, new Integer (i)); 517 518 Boolean b = (Boolean )EL.evaluateExpression(pageContext, INCLUDE, include, Boolean .class); 519 520 if (b.booleanValue()) 521 { 522 if (apply != null) 523 { 524 x = EL.evaluateExpression(pageContext, APPLY, apply, Object .class); 525 } 526 527 if (applyCodec != null) 528 { 529 x = EL.applyCodec(pageContext, applyCodec, x); 530 } 531 532 newList.add(x); 533 } 534 535 if (breakExpression != null) 536 { 537 Boolean bBreak = (Boolean )EL.evaluateExpression(pageContext, BREAK, breakExpression, Boolean .class); 538 539 if (bBreak.booleanValue()) 540 { 541 break; 542 } 543 } 544 } 545 546 object = newList; 547 } 548 else 549 { 550 throw new Exception ("unsupported filter object type:" + object.getClass().getName()); 551 } 552 } 553 } 554 finally 555 { 556 EL.setPageAttribute(pageContext, ELEMENTVAR, xVar); 557 EL.setPageAttribute(pageContext, INDEXVAR, iVar); 558 EL.setPageAttribute(pageContext, LENGTHVAR, lVar); 559 } 560 } 561 562 return object; 563 } 564 } 565 | Popular Tags |