1 18 19 package org.apache.struts.taglib.html; 20 21 import java.lang.reflect.InvocationTargetException ; 22 import java.lang.reflect.Method ; 23 import java.util.Locale ; 24 25 import javax.servlet.jsp.PageContext ; 26 import javax.servlet.jsp.JspException ; 27 import javax.servlet.jsp.tagext.BodyTagSupport ; 28 29 import org.apache.commons.beanutils.BeanUtils; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.apache.struts.Globals; 33 import org.apache.struts.action.ActionMessages; 34 import org.apache.struts.taglib.TagUtils; 35 import org.apache.struts.taglib.logic.IterateTag; 36 import org.apache.struts.util.MessageResources; 37 import org.apache.struts.util.RequestUtils; 38 39 47 public abstract class BaseHandlerTag extends BodyTagSupport { 48 49 52 private static Log log = LogFactory.getLog(BaseHandlerTag.class); 53 54 56 60 protected static final Locale defaultLocale = Locale.getDefault(); 61 62 65 protected static MessageResources messages = 66 MessageResources.getMessageResources(Constants.Package + ".LocalStrings"); 67 68 70 71 protected String accesskey = null; 72 73 74 protected String tabindex = null; 75 76 78 81 protected boolean indexed = false; 82 83 85 86 private String onclick = null; 87 88 89 private String ondblclick = null; 90 91 92 private String onmouseover = null; 93 94 95 private String onmouseout = null; 96 97 98 private String onmousemove = null; 99 100 101 private String onmousedown = null; 102 103 104 private String onmouseup = null; 105 106 108 109 private String onkeydown = null; 110 111 112 private String onkeyup = null; 113 114 115 private String onkeypress = null; 116 117 119 120 private String onselect = null; 121 122 123 private String onchange = null; 124 125 127 128 private String onblur = null; 129 130 131 private String onfocus = null; 132 133 134 private boolean disabled = false; 135 136 137 protected boolean doDisabled = true; 138 139 140 private boolean readonly = false; 141 142 150 protected boolean doReadonly = false; 151 152 154 155 private String style = null; 156 157 158 private String styleClass = null; 159 160 161 private String styleId = null; 162 163 164 private String errorKey = Globals.ERROR_KEY; 165 166 167 private String errorStyle = null; 168 169 170 private String errorStyleClass = null; 171 172 173 private String errorStyleId = null; 174 175 177 178 private String alt = null; 179 180 181 private String altKey = null; 182 183 184 private String bundle = null; 185 186 187 private String locale = Globals.LOCALE_KEY; 188 189 190 private String title = null; 191 192 193 private String titleKey = null; 194 195 197 199 200 public void setAccesskey(String accessKey) { 201 this.accesskey = accessKey; 202 } 203 204 205 public String getAccesskey() { 206 return (this.accesskey); 207 } 208 209 210 public void setTabindex(String tabIndex) { 211 this.tabindex = tabIndex; 212 } 213 214 215 public String getTabindex() { 216 return (this.tabindex); 217 } 218 219 221 224 public void setIndexed(boolean indexed) { 225 this.indexed = indexed; 226 } 227 228 231 public boolean getIndexed() { 232 return (this.indexed); 233 } 234 235 237 238 public void setOnclick(String onClick) { 239 this.onclick = onClick; 240 } 241 242 243 public String getOnclick() { 244 return onclick; 245 } 246 247 248 public void setOndblclick(String onDblClick) { 249 this.ondblclick = onDblClick; 250 } 251 252 253 public String getOndblclick() { 254 return ondblclick; 255 } 256 257 258 public void setOnmousedown(String onMouseDown) { 259 this.onmousedown = onMouseDown; 260 } 261 262 263 public String getOnmousedown() { 264 return onmousedown; 265 } 266 267 268 public void setOnmouseup(String onMouseUp) { 269 this.onmouseup = onMouseUp; 270 } 271 272 273 public String getOnmouseup() { 274 return onmouseup; 275 } 276 277 278 public void setOnmousemove(String onMouseMove) { 279 this.onmousemove = onMouseMove; 280 } 281 282 283 public String getOnmousemove() { 284 return onmousemove; 285 } 286 287 288 public void setOnmouseover(String onMouseOver) { 289 this.onmouseover = onMouseOver; 290 } 291 292 293 public String getOnmouseover() { 294 return onmouseover; 295 } 296 297 298 public void setOnmouseout(String onMouseOut) { 299 this.onmouseout = onMouseOut; 300 } 301 302 303 public String getOnmouseout() { 304 return onmouseout; 305 } 306 307 309 310 public void setOnkeydown(String onKeyDown) { 311 this.onkeydown = onKeyDown; 312 } 313 314 315 public String getOnkeydown() { 316 return onkeydown; 317 } 318 319 320 public void setOnkeyup(String onKeyUp) { 321 this.onkeyup = onKeyUp; 322 } 323 324 325 public String getOnkeyup() { 326 return onkeyup; 327 } 328 329 330 public void setOnkeypress(String onKeyPress) { 331 this.onkeypress = onKeyPress; 332 } 333 334 335 public String getOnkeypress() { 336 return onkeypress; 337 } 338 339 341 342 public void setOnchange(String onChange) { 343 this.onchange = onChange; 344 } 345 346 347 public String getOnchange() { 348 return onchange; 349 } 350 351 352 public void setOnselect(String onSelect) { 353 this.onselect = onSelect; 354 } 355 356 357 public String getOnselect() { 358 return onselect; 359 } 360 361 363 364 public void setOnblur(String onBlur) { 365 this.onblur = onBlur; 366 } 367 368 369 public String getOnblur() { 370 return onblur; 371 } 372 373 374 public void setOnfocus(String onFocus) { 375 this.onfocus = onFocus; 376 } 377 378 379 public String getOnfocus() { 380 return onfocus; 381 } 382 383 384 public void setDisabled(boolean disabled) { 385 this.disabled = disabled; 386 } 387 388 389 public boolean getDisabled() { 390 return disabled; 391 } 392 393 394 public void setReadonly(boolean readonly) { 395 this.readonly = readonly; 396 } 397 398 399 public boolean getReadonly() { 400 return readonly; 401 } 402 403 405 406 public void setStyle(String style) { 407 this.style = style; 408 } 409 410 411 public String getStyle() { 412 return style; 413 } 414 415 416 public void setStyleClass(String styleClass) { 417 this.styleClass = styleClass; 418 } 419 420 421 public String getStyleClass() { 422 return styleClass; 423 } 424 425 426 public void setStyleId(String styleId) { 427 this.styleId = styleId; 428 } 429 430 431 public String getStyleId() { 432 return styleId; 433 } 434 435 436 public String getErrorKey() { 437 return errorKey; 438 } 439 440 441 public void setErrorKey(String errorKey) { 442 this.errorKey = errorKey; 443 } 444 445 446 public String getErrorStyle() { 447 return errorStyle; 448 } 449 450 451 public void setErrorStyle(String errorStyle) { 452 this.errorStyle = errorStyle; 453 } 454 455 456 public String getErrorStyleClass() { 457 return errorStyleClass; 458 } 459 460 461 public void setErrorStyleClass(String errorStyleClass) { 462 this.errorStyleClass = errorStyleClass; 463 } 464 465 466 public String getErrorStyleId() { 467 return errorStyleId; 468 } 469 470 471 public void setErrorStyleId(String errorStyleId) { 472 this.errorStyleId = errorStyleId; 473 } 474 475 477 478 public String getAlt() { 479 return alt; 480 } 481 482 483 public void setAlt(String alt) { 484 this.alt = alt; 485 } 486 487 488 public String getAltKey() { 489 return altKey; 490 } 491 492 493 public void setAltKey(String altKey) { 494 this.altKey = altKey; 495 } 496 497 498 public String getBundle() { 499 return bundle; 500 } 501 502 503 public void setBundle(String bundle) { 504 this.bundle = bundle; 505 } 506 507 508 public String getLocale() { 509 return locale; 510 } 511 512 513 public void setLocale(String locale) { 514 this.locale = locale; 515 } 516 517 518 public String getTitle() { 519 return title; 520 } 521 522 523 public void setTitle(String title) { 524 this.title = title; 525 } 526 527 528 public String getTitleKey() { 529 return titleKey; 530 } 531 532 533 public void setTitleKey(String titleKey) { 534 this.titleKey = titleKey; 535 } 536 537 539 542 public void release() { 543 544 super.release(); 545 accesskey = null; 546 alt = null; 547 altKey = null; 548 bundle = null; 549 errorKey = Globals.ERROR_KEY; 550 errorStyle = null; 551 errorStyleClass = null; 552 errorStyleId = null; 553 indexed = false; 554 locale = Globals.LOCALE_KEY; 555 onclick = null; 556 ondblclick = null; 557 onmouseover = null; 558 onmouseout = null; 559 onmousemove = null; 560 onmousedown = null; 561 onmouseup = null; 562 onkeydown = null; 563 onkeyup = null; 564 onkeypress = null; 565 onselect = null; 566 onchange = null; 567 onblur = null; 568 onfocus = null; 569 disabled = false; 570 readonly = false; 571 style = null; 572 styleClass = null; 573 styleId = null; 574 tabindex = null; 575 title = null; 576 titleKey = null; 577 578 } 579 580 582 591 protected String message(String literal, String key) throws JspException { 592 593 if (literal != null) { 594 if (key != null) { 595 JspException e = 596 new JspException (messages.getMessage("common.both")); 597 TagUtils.getInstance().saveException(pageContext, e); 598 throw e; 599 } else { 600 return (literal); 601 } 602 } else { 603 if (key != null) { 604 return TagUtils.getInstance().message( 605 pageContext, 606 getBundle(), 607 getLocale(), 608 key); 609 } else { 610 return null; 611 } 612 } 613 614 } 615 616 private Class loopTagSupportClass = null; 617 private Method loopTagSupportGetStatus = null; 618 private Class loopTagStatusClass = null; 619 private Method loopTagStatusGetIndex = null; 620 private boolean triedJstlInit = false; 621 private boolean triedJstlSuccess = false; 622 623 private Integer getJstlLoopIndex() { 624 if (!triedJstlInit) { 625 triedJstlInit = true; 626 try { 627 loopTagSupportClass = 628 RequestUtils.applicationClass( 629 "javax.servlet.jsp.jstl.core.LoopTagSupport"); 630 631 loopTagSupportGetStatus = 632 loopTagSupportClass.getDeclaredMethod("getLoopStatus", null); 633 634 loopTagStatusClass = 635 RequestUtils.applicationClass( 636 "javax.servlet.jsp.jstl.core.LoopTagStatus"); 637 638 loopTagStatusGetIndex = 639 loopTagStatusClass.getDeclaredMethod("getIndex", null); 640 641 triedJstlSuccess = true; 642 643 } catch (ClassNotFoundException ex) { 644 } catch (NoSuchMethodException ex) { 646 } 647 } 648 649 if (triedJstlSuccess) { 650 try { 651 Object loopTag = findAncestorWithClass(this, loopTagSupportClass); 652 if (loopTag == null) { 653 return null; 654 } 655 656 Object status = loopTagSupportGetStatus.invoke(loopTag, null); 657 return (Integer ) loopTagStatusGetIndex.invoke(status, null); 658 659 } catch (IllegalAccessException ex) { 660 log.error(ex.getMessage(), ex); 661 662 } catch (IllegalArgumentException ex) { 663 log.error(ex.getMessage(), ex); 664 665 } catch (InvocationTargetException ex) { 666 log.error(ex.getMessage(), ex); 667 668 } catch (NullPointerException ex) { 669 log.error(ex.getMessage(), ex); 670 671 } catch (ExceptionInInitializerError ex) { 672 log.error(ex.getMessage(), ex); 673 } 674 } 675 return null; 676 } 677 678 684 protected void prepareIndex(StringBuffer handlers, String name) 685 throws JspException { 686 687 if (name != null) { 688 handlers.append(name); 689 } 690 691 handlers.append("["); 692 handlers.append(getIndexValue()); 693 handlers.append("]"); 694 695 if (name != null) { 696 handlers.append("."); 697 } 698 } 699 700 705 protected int getIndexValue() throws JspException { 706 707 IterateTag iterateTag = 709 (IterateTag) findAncestorWithClass(this, IterateTag.class); 710 if (iterateTag != null) { 711 return iterateTag.getIndex(); 712 } 713 714 Integer i = getJstlLoopIndex(); 716 if (i != null) { 717 return i.intValue(); 718 } 719 720 JspException e = 722 new JspException (messages.getMessage("indexed.noEnclosingIterate")); 723 TagUtils.getInstance().saveException(pageContext, e); 724 throw e; 725 726 } 727 728 733 protected String prepareStyles() throws JspException { 734 735 StringBuffer styles = new StringBuffer (); 736 737 boolean errorsExist = doErrorsExist(); 738 739 if (errorsExist && getErrorStyleId() != null) { 740 prepareAttribute(styles , "id", getErrorStyleId()); 741 } else { 742 prepareAttribute(styles , "id", getStyleId()); 743 } 744 745 if (errorsExist && getErrorStyle() != null) { 746 prepareAttribute(styles , "style", getErrorStyle()); 747 } else { 748 prepareAttribute(styles , "style", getStyle()); 749 } 750 751 if (errorsExist && getErrorStyleClass() != null) { 752 prepareAttribute(styles , "class", getErrorStyleClass()); 753 } else { 754 prepareAttribute(styles , "class", getStyleClass()); 755 } 756 757 prepareAttribute(styles , "title", message(getTitle(), getTitleKey())); 758 prepareAttribute(styles , "alt", message(getAlt(), getAltKey())); 759 760 return styles.toString(); 761 762 } 763 764 768 protected boolean doErrorsExist() throws JspException { 769 770 boolean errorsExist = false; 771 772 if (getErrorStyleId() != null || 773 getErrorStyle() != null || 774 getErrorStyleClass() != null) { 775 String actualName = prepareName(); 776 if (actualName != null) { 777 ActionMessages errors = TagUtils.getInstance() 778 .getActionMessages(pageContext, 779 errorKey); 780 errorsExist = (errors != null && errors.size(actualName) > 0); 781 } 782 } 783 return errorsExist; 784 785 } 786 787 791 protected String prepareName() throws JspException { 792 return null; 793 } 794 795 799 protected String prepareEventHandlers() { 800 StringBuffer handlers = new StringBuffer (); 801 prepareMouseEvents(handlers); 802 prepareKeyEvents(handlers); 803 prepareTextEvents(handlers); 804 prepareFocusEvents(handlers); 805 return handlers.toString(); 806 } 807 808 813 protected void prepareMouseEvents(StringBuffer handlers) { 814 815 prepareAttribute(handlers, "onclick", getOnclick()); 816 prepareAttribute(handlers, "ondblclick", getOndblclick()); 817 prepareAttribute(handlers, "onmouseover", getOnmouseover()); 818 prepareAttribute(handlers, "onmouseout", getOnmouseout()); 819 prepareAttribute(handlers, "onmousemove", getOnmousemove()); 820 prepareAttribute(handlers, "onmousedown", getOnmousedown()); 821 prepareAttribute(handlers, "onmouseup", getOnmouseup()); 822 823 } 824 825 830 protected void prepareKeyEvents(StringBuffer handlers) { 831 832 prepareAttribute(handlers, "onkeydown", getOnkeydown()); 833 prepareAttribute(handlers, "onkeyup", getOnkeyup()); 834 prepareAttribute(handlers, "onkeypress", getOnkeypress()); 835 836 } 837 838 843 protected void prepareTextEvents(StringBuffer handlers) { 844 845 prepareAttribute(handlers, "onselect", getOnselect()); 846 prepareAttribute(handlers, "onchange", getOnchange()); 847 848 } 849 850 855 protected void prepareFocusEvents(StringBuffer handlers) { 856 857 prepareAttribute(handlers, "onblur", getOnblur()); 858 prepareAttribute(handlers, "onfocus", getOnfocus()); 859 860 FormTag formTag = null; 862 if ((doDisabled && !getDisabled()) || 863 (doReadonly && !getReadonly())) { 864 formTag = (FormTag)pageContext.getAttribute(Constants.FORM_KEY, 865 PageContext.REQUEST_SCOPE); 866 } 867 868 if (doDisabled) { 870 boolean formDisabled = formTag == null ? false : formTag.isDisabled(); 871 if (formDisabled || getDisabled()) { 872 handlers.append(" disabled=\"disabled\""); 873 } 874 } 875 876 if (doReadonly) { 878 boolean formReadOnly = formTag == null ? false : formTag.isReadonly(); 879 if (formReadOnly || getReadonly()) { 880 handlers.append(" readonly=\"readonly\""); 881 } 882 } 883 884 } 885 886 891 protected void prepareOtherAttributes(StringBuffer handlers) { 892 } 893 894 899 protected void prepareAttribute(StringBuffer handlers, String name, Object value) { 900 if (value != null) { 901 handlers.append(" "); 902 handlers.append(name); 903 handlers.append("=\""); 904 handlers.append(value); 905 handlers.append("\""); 906 } 907 } 908 909 916 protected boolean isXhtml() { 917 return TagUtils.getInstance().isXhtml(this.pageContext); 918 } 919 920 926 protected String getElementClose() { 927 return this.isXhtml() ? " />" : ">"; 928 } 929 930 940 protected String lookupProperty(String beanName, String property) 941 throws JspException { 942 943 Object bean = TagUtils.getInstance().lookup(this.pageContext, beanName, null); 944 if (bean == null) { 945 throw new JspException (messages.getMessage("getter.bean", beanName)); 946 } 947 948 try { 949 return BeanUtils.getProperty(bean, property); 950 951 } catch (IllegalAccessException e) { 952 throw new JspException ( 953 messages.getMessage("getter.access", property, beanName)); 954 955 } catch (InvocationTargetException e) { 956 Throwable t = e.getTargetException(); 957 throw new JspException ( 958 messages.getMessage("getter.result", property, t.toString())); 959 960 } catch (NoSuchMethodException e) { 961 throw new JspException ( 962 messages.getMessage("getter.method", property, beanName)); 963 } 964 } 965 966 } 967 | Popular Tags |