1 34 35 36 package com.micronova.util.codec; 37 38 import java.io.*; 39 import java.util.*; 40 import java.util.regex.*; 41 import javax.servlet.*; 42 import javax.servlet.jsp.*; 43 import javax.servlet.http.*; 44 import com.micronova.util.*; 45 import com.micronova.jsp.tag.*; 46 import org.apache.commons.logging.Log; 47 import org.apache.commons.logging.LogFactory; 48 import org.apache.taglibs.standard.tag.common.core.Util; 49 50 51 52 public class CodecJSP extends Codec 53 { 54 55 56 public static Object eval(PageContext pageContext, Object object, Pattern pattern, Object environment, String evalCodec, boolean recursive, boolean allowGroup) throws Exception 57 { 58 if (object != null) 59 { 60 Object valueVarSaved = null; 61 boolean isValueVarChanged = false; 62 63 try 64 { 65 EL el = new EL(pageContext, evalCodec); 66 67 valueVarSaved = pageContext.getAttribute(YuzuTag.VALUEVAR); 68 69 if (environment != null) 70 { 71 EL.setPageAttribute(pageContext, YuzuTag.VALUEVAR, new NestedMap(environment)); 72 } 73 74 isValueVarChanged = true; 75 76 if (object instanceof NestedMap) 77 { 78 NestedMap params = (NestedMap)((NestedMap)object).get("__param"); 79 NestedMap evaluatedMap = new NestedMap(); 80 81 Iterator iterator = params.entrySet().iterator(); 82 83 while (iterator.hasNext()) 84 { 85 Map.Entry entry = (Map.Entry)iterator.next(); 86 87 String key = Template.render(entry.getKey().toString(), pattern, 1, el, null, recursive, allowGroup).toString(); 88 89 Object value = entry.getValue(); 90 91 if (value instanceof String ) 92 { 93 value = Template.render(value.toString(), pattern, 1, el, null, recursive, allowGroup); 94 } 95 96 evaluatedMap.setElement(key, value); 97 } 98 99 object = evaluatedMap; 100 } 101 else 102 { 103 object = Template.render(object.toString(), pattern, 1, el, null, recursive, allowGroup); 104 } 105 } 106 catch (Exception e) 107 { 108 throw e; 109 } 110 finally 111 { 112 if (isValueVarChanged) 113 { 114 EL.setPageAttribute(pageContext, YuzuTag.VALUEVAR, valueVarSaved); 115 } 116 } 117 } 118 119 return object; 120 } 121 122 123 124 public final static String PATTERN = "pattern"; 125 public final static String RECURSIVE = "recursive"; 126 public final static String ALLOWGROUP = "allowGroup"; 127 public final static String ENVIRONMENT = "environment"; 128 public final static String EVALCODEC = "evalCodec"; 129 130 131 132 public static Object eval(Object context, Object object, Object control) throws Exception 133 { 134 if (object != null) 135 { 136 Object valueVarSaved = null; 137 boolean isValueVarChanged = false; 138 139 PageContext pageContext = (PageContext)context; 140 141 NestedMap controlMap = new NestedMap(control); 142 143 Pattern pattern = EL.getPattern(controlMap.get(PATTERN)); 144 145 if (pattern == null) 146 { 147 pattern = EL.defaultPatternEval; 148 } 149 150 boolean recursive = (TypeUtil.isBoolean(controlMap.get(RECURSIVE, Boolean.FALSE))).booleanValue(); 151 boolean allowGroup = (TypeUtil.isBoolean(controlMap.get(ALLOWGROUP, Boolean.FALSE))).booleanValue(); 152 Object environment = controlMap.get(ENVIRONMENT); 153 String evalCodec = controlMap.getString(EVALCODEC); 154 155 object = eval(pageContext, object, pattern, environment, evalCodec, recursive, allowGroup); 156 } 157 158 return object; 159 } 160 161 162 163 public static Object eval(Object context, Object object) throws Exception 164 { 165 return eval(context, object, null); 166 } 167 168 169 170 public static Object encodeURL(Object context, Object object) throws Exception 171 { 172 if (object != null) 173 { 174 object = ((HttpServletResponse)((PageContext)context).getResponse()).encodeURL(object.toString()); 175 } 176 177 return object; 178 } 179 180 181 182 public static Object encodeRedirectURL(Object context, Object object) throws Exception 183 { 184 if (object != null) 185 { 186 object = ((HttpServletResponse)((PageContext)context).getResponse()).encodeRedirectURL(object.toString()); 187 } 188 189 return object; 190 } 191 192 193 194 public static Object getRealPath(Object context, Object object) 195 { 196 if (object != null) 197 { 198 object = (((PageContext)context).getServletContext()).getRealPath(object.toString()); 199 } 200 201 return object; 202 } 203 204 205 206 public static Object getResource(Object context, Object object) throws Exception 207 { 208 if (object != null) 209 { 210 object = (((PageContext)context).getServletContext()).getResource(object.toString()); 211 } 212 213 return object; 214 } 215 216 217 218 public static Object getMimeType(Object context, Object object) 219 { 220 if (object != null) 221 { 222 object = (((PageContext)context).getServletContext()).getMimeType(object.toString()); 223 } 224 225 return object; 226 } 227 228 229 230 public static Object dynamicMap(Object context, Object object) throws Exception 231 { 232 return new ELMap((PageContext)context, object); 233 } 234 235 236 237 public static Object dynamicIterator(Object context, Object object) throws Exception 238 { 239 return new ELIterator((PageContext)context, object); 240 } 241 242 243 244 public static Object applyCodec(Object context, Object object, Object codec) throws Exception 245 { 246 return EL.applyCodec((PageContext)context, EL.replaceEvalEscape(codec.toString()), object); 247 } 248 249 250 251 public static Object applyFilter(Object context, Object object, Object filter) throws Exception 252 { 253 NestedMap filterMap = new NestedMap(filter); 254 255 String include = EL.replaceEvalEscape(filterMap.getString(EL.INCLUDE, "true")); 256 String breakExpression = EL.replaceEvalEscape(filterMap.getString(EL.BREAK)); 257 String apply = EL.replaceEvalEscape(filterMap.getString(EL.APPLY)); 258 String applyCodec = EL.replaceEvalEscape(filterMap.getString(EL.APPLYCODEC)); 259 260 return EL.applyFilter((PageContext)context, object, include, breakExpression, apply, applyCodec); 261 } 262 263 264 265 public static Object getPath(Object context, Object object) throws Exception 266 { 267 String path = null; 268 269 if (object != null) 270 { 271 PageContext pageContext = (PageContext)context; 272 273 path = object.toString(); 274 275 if (!path.startsWith("/")) 276 { 277 path = ((HttpServletRequest)pageContext.getRequest()).getServletPath().replaceFirst("/[^/]*$", "") + "/" + path; 278 } 279 } 280 281 return path; 282 } 283 284 public static final String RETURNPROPERTY = com.micronova.jsp.tag.CallTag.RETURNPROPERTY; 285 public static final String VALUEPROPERTY = com.micronova.jsp.tag.CallTag.VALUEPROPERTY; 286 public static final String BODYPROPERTY = com.micronova.jsp.tag.CallTag.BODYPROPERTY; 287 288 289 290 public static Object call(Object context, Object object, Object control) throws Exception 291 { 292 if (object != null) 293 { 294 PageContext pageContext = (PageContext)context; 295 NestedMap controlMap = TypeUtil.isNestedMap(control); 296 297 String path = (String )getPath(context, controlMap.get("path")); 298 String contextPath = controlMap.getString("contextPath"); 299 String bodyParameter = controlMap.getString("bodyParameter", BODYPROPERTY); 300 301 NestedMap map = TypeUtil.isNestedMap(controlMap.get("parameter")); 302 303 if (object instanceof NestedMap) 304 { 305 map.copyFromSource(object); 306 } 307 else 308 { 309 String body = (String )TypeUtil.isString(object); 310 311 if (!TypeUtil.isEmptyString(body)) 312 { 313 map.put(bodyParameter, body); 314 } 315 } 316 317 Map returnMap = map.getSubMap(RETURNPROPERTY); 318 319 returnMap.put(VALUEPROPERTY, null); 320 321 object = com.micronova.util.servlet.HttpDispatch.include(pageContext, path, contextPath, map); 322 323 Object returnValue = returnMap.get(VALUEPROPERTY); 324 325 if (!TypeUtil.isEmptyString(returnValue)) 326 { 327 object = returnValue; 328 } 329 } 330 331 return object; 332 } 333 334 public static Object _log(PageContext pageContext, Object object, String destination, String category) throws Exception 335 { 336 if (object != null) 337 { 338 if ("log".equals(destination)) 339 { 340 ServletContext context = pageContext.getServletContext(); 341 342 context.log(object.toString()); 343 } 344 else if ("out".equals(destination)) 345 { 346 System.out.println(object.toString()); 347 } 348 else if ("err".equals(destination)) 349 { 350 System.err.println(object.toString()); 351 } 352 else 353 { 354 Log log = LogFactory.getLog(category); 355 356 if ("trace".equals(destination)) 357 { 358 log.trace(object.toString()); 359 } 360 else if ("debug".equals(destination)) 361 { 362 log.debug(object.toString()); 363 } 364 else if ("info".equals(destination)) 365 { 366 log.info(object.toString()); 367 } 368 else if ("warn".equals(destination)) 369 { 370 log.warn(object.toString()); 371 } 372 else if ("error".equals(destination)) 373 { 374 log.error(object.toString()); 375 } 376 else if ("fatal".equals(destination)) 377 { 378 log.fatal(object.toString()); 379 } 380 } 381 } 382 383 return object; 384 } 385 386 387 388 public static Object log(Object context, Object object, Object control) throws Exception 389 { 390 if (object != null) 391 { 392 PageContext pageContext = (PageContext)context; 393 NestedMap controlMap = TypeUtil.isNestedMap(control); 394 395 String destination = controlMap.getString("destination", "log"); 396 String category = controlMap.getString("category", ""); 397 398 object = _log(pageContext, object, destination, category); 399 } 400 401 return object; 402 } 403 404 public static Object log(Object context, Object object) throws Exception 405 { 406 return log(context, object, null); 407 } 408 409 410 411 public static Object sort(Object context, Object object, Object comparatorSpec) throws Exception 412 { 413 if (object != null) 414 { 415 List list = TypeUtil.isList(object); 416 417 if (list != null) 418 { 419 if (comparatorSpec != null) 420 { 421 ELComparator comparator = new ELComparator((PageContext)context, comparatorSpec); 422 423 Collections.sort(list, comparator); 424 } 425 else 426 { 427 Collections.sort(list); 428 } 429 } 430 431 object = list; 432 } 433 434 return object; 435 } 436 437 public static Object sort(Object context, Object object) throws Exception 438 { 439 return sort(context, object, null); 440 } 441 442 443 444 public static Object max(Object context, Object object, Object comparatorSpec) throws Exception 445 { 446 if (object != null) 447 { 448 if (comparatorSpec != null) 449 { 450 ELComparator comparator = new ELComparator((PageContext)context, comparatorSpec); 451 452 object = Collections.max((Collection)object, comparator); 453 } 454 else 455 { 456 object = Collections.max((Collection)object); 457 } 458 } 459 460 return object; 461 } 462 463 public static Object max(Object context, Object object) throws Exception 464 { 465 return max(context, object, null); 466 } 467 468 469 470 public static Object min(Object context, Object object, Object comparatorSpec) throws Exception 471 { 472 if (object != null) 473 { 474 if (comparatorSpec != null) 475 { 476 ELComparator comparator = new ELComparator((PageContext)context, comparatorSpec); 477 478 object = Collections.min((Collection)object, comparator); 479 } 480 else 481 { 482 object = Collections.min((Collection)object); 483 } 484 } 485 486 return object; 487 } 488 489 public static Object min(Object context, Object object) throws Exception 490 { 491 return min(context, object, null); 492 } 493 494 495 496 public static Object binarySearch(Object context, Object object, Object key, Object comparatorSpec) throws Exception 497 { 498 List list = TypeUtil.isList(object); 499 500 int index = -1; 501 502 if (list != null) 503 { 504 if (comparatorSpec != null) 505 { 506 ELComparator comparator = new ELComparator((PageContext)context, comparatorSpec); 507 508 index = Collections.binarySearch(list, key, comparator); 509 } 510 else 511 { 512 index = Collections.binarySearch(list, key); 513 } 514 } 515 516 return new Integer (index); 517 } 518 519 public static Object binarySearch(Object context, Object object, Object key) throws Exception 520 { 521 return binarySearch(context, object, key, null); 522 } 523 524 525 526 public static Object getMessageMap(Object context, Object object) throws Exception 527 { 528 MessageMap messageMap = new MessageMap((PageContext)context); 529 530 if (object != null) 531 { 532 BeanUtil.fill(messageMap, TypeUtil.isNestedMap(object)); 533 } 534 535 return messageMap; 536 } 537 538 public static Object getMessageMap(Object context) throws Exception 539 { 540 return getMessageMap(context, null); 541 } 542 543 544 545 public static Object comparator(Object context, Object object) throws Exception 546 { 547 return new ELComparator((PageContext)context, TypeUtil.isNestedMap(object)); 548 } 549 550 551 552 public static Object flush(Object context, Object object) throws Exception 553 { 554 Writer writer = ((PageContext)context).getOut(); 555 556 if (object instanceof Writer) 557 { 558 writer = (Writer)object; 559 } 560 561 writer.flush(); 562 563 return object; 564 } 565 566 public static Object flush(Object context) throws Exception 567 { 568 return flush(context, null); 569 } 570 571 572 573 public static Object clear(Object context, Object object) throws Exception 574 { 575 JspWriter jspWriter = ((PageContext)context).getOut(); 576 577 if (object instanceof JspWriter) 578 { 579 jspWriter = (JspWriter)object; 580 } 581 582 jspWriter.clear(); 583 584 return object; 585 } 586 587 public static Object clear(Object context) throws Exception 588 { 589 return clear(context, null); 590 } 591 592 593 594 public static Object hasSession(Object context) throws Exception 595 { 596 PageContext pageContext = (PageContext)context; 597 598 HttpSession session = ((HttpServletRequest)pageContext.getRequest()).getSession(false); 599 600 if (session != null) 601 { 602 return new PageContextMap(pageContext, PageContext.SESSION_SCOPE); 603 } 604 else 605 { 606 return null; 607 } 608 } 609 610 611 612 public static Object newSession(Object context) throws Exception 613 { 614 PageContext pageContext = (PageContext)context; 615 616 HttpServletRequest request = ((HttpServletRequest)pageContext.getRequest()); 617 618 HttpSession session = request.getSession(false); 619 620 NestedMap currentSessionMap = null; 621 622 if (session != null) 623 { 624 currentSessionMap = new PageContextMap(pageContext, PageContext.SESSION_SCOPE); 625 session.invalidate(); 626 } 627 628 session = request.getSession(true); 629 630 NestedMap sessionMap = new PageContextMap(pageContext, PageContext.SESSION_SCOPE); 631 632 if (currentSessionMap != null) 633 { 634 sessionMap.copyFromSource(currentSessionMap); 635 } 636 637 return sessionMap; 638 } 639 640 641 642 public static Object invalidateSession(Object context) throws Exception 643 { 644 HttpServletRequest request = ((HttpServletRequest)((PageContext)context).getRequest()); 645 646 HttpSession session = request.getSession(false); 647 648 if (session != null) 649 { 650 session.invalidate(); 651 } 652 653 return null; 654 } 655 656 657 658 public static Object getCallStack(Object context) throws Exception 659 { 660 PageContext pageContext = (PageContext)context; 661 662 List list = new ArrayList(com.micronova.jsp.tag.YuzuTag.getCallStack(pageContext)); 663 664 Collections.reverse(list); 665 666 return list; 667 } 668 669 670 671 public static Object getAttributes(Object context, Object vars, Object scopeName) throws Exception 672 { 673 int scope = PageContext.PAGE_SCOPE; 674 675 if (scopeName != null) 676 { 677 scope = Util.getScope(scopeName.toString()); 678 } 679 680 List list = TypeUtil.isStringList(vars, ',', '\\'); 681 682 return new PageContextMap((PageContext)context, scope, list); 683 } 684 685 public static Object getAttributes(Object context, Object vars) throws Exception 686 { 687 return getAttributes(context, vars, null); 688 } 689 690 public static Object getAttributes(Object context) throws Exception 691 { 692 return getAttributes(context, null); 693 } 694 695 696 697 public static Object putAttributes(Object context, Object source, Object scopeName) throws Exception 698 { 699 PageContext pageContext = (PageContext)context; 700 701 NestedMap sourceMap = TypeUtil.isNestedMap(source); 702 703 Iterator iterator = sourceMap.entrySet().iterator(); 704 705 int scope = PageContext.PAGE_SCOPE; 706 707 if (scopeName != null) 708 { 709 scope = Util.getScope(scopeName.toString()); 710 } 711 712 while (iterator.hasNext()) 713 { 714 Map.Entry entry = (Map.Entry)iterator.next(); 715 716 YuzuTag.setScopedAttribute(pageContext, entry.getKey().toString(), entry.getValue(), scope); 717 } 718 719 return source; 720 } 721 722 public static Object putAttributes(Object context, Object source) throws Exception 723 { 724 return putAttributes(context, source, null); 725 } 726 727 728 729 public static Object setRootPageContext(Object context, Object doesOverwrite) throws Exception 730 { 731 PageContext pageContext = (PageContext)context; 732 733 Object currentRootPageContext = pageContext.getRequest().getAttribute(YuzuTag.ROOTPAGECONTEXT); 734 735 if ((currentRootPageContext == null) || (TypeUtil.isTrue(doesOverwrite))) 736 { 737 pageContext.getRequest().setAttribute(YuzuTag.ROOTPAGECONTEXT, pageContext); 738 } 739 740 return currentRootPageContext; 741 } 742 743 public static Object setRootPageContext(Object context) throws Exception 744 { 745 return setRootPageContext(context, Boolean.FALSE); 746 } 747 748 749 750 public static Object getPageScope(Object context, Object indexSpec) throws Exception 751 { 752 List callStack = (List)getCallStack(context); 753 int index = TypeUtil.isInteger(indexSpec).intValue(); 754 PageContext targetPageContext = (PageContext)TypeUtil.isNestedMap(callStack.get(index)).get(YuzuTag.PAGECONTEXT); 755 return getAttributes(targetPageContext); 756 } 757 758 public static Object getPageScope(Object context) throws Exception 759 { 760 return getPageScope(context, new Integer (0)); 761 } 762 } 763 | Popular Tags |