1 16 17 package org.apache.velocity.tools.struts; 18 19 import java.util.ArrayList ; 20 import java.util.Comparator ; 21 import java.util.Collections ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Locale ; 25 import java.util.Map ; 26 27 import javax.servlet.ServletContext ; 28 import javax.servlet.http.HttpServletRequest ; 29 import javax.servlet.http.HttpSession ; 30 31 import org.apache.commons.validator.Field; 32 import org.apache.commons.validator.Form; 33 import org.apache.commons.validator.ValidatorAction; 34 import org.apache.commons.validator.ValidatorResources; 35 import org.apache.commons.validator.ValidatorUtil; 36 import org.apache.commons.validator.Var; 37 38 import org.apache.struts.Globals; 39 import org.apache.struts.config.ActionConfig; 40 import org.apache.struts.config.ModuleConfig; 41 import org.apache.struts.util.MessageResources; 42 import org.apache.struts.util.RequestUtils; 43 import org.apache.struts.validator.Resources; 44 import org.apache.struts.validator.ValidatorPlugIn; 45 46 import org.apache.velocity.tools.view.context.ViewContext; 47 import org.apache.velocity.tools.view.tools.ViewTool; 48 49 76 public class ValidatorTool implements ViewTool { 77 78 79 protected ViewContext context; 80 81 82 protected ServletContext app; 83 84 85 protected HttpServletRequest request; 86 87 88 protected HttpSession session; 89 90 91 protected ValidatorResources resources; 92 93 94 private static final String HTML_BEGIN_COMMENT = "\n<!-- Begin \n"; 95 private static final String HTML_END_COMMENT = "//End --> \n"; 96 97 private boolean xhtml = false; 98 99 private boolean htmlComment = true; 100 private boolean cdata = true; 101 private String formName = null; 102 private String methodName = null; 103 private String src = null; 104 private int page = 0; 105 106 107 110 public ValidatorTool() {} 111 112 113 119 public void init(Object obj) 120 { 121 if (!(obj instanceof ViewContext)) 122 { 123 throw new IllegalArgumentException ( 124 "Tool can only be initialized with a ViewContext"); 125 } 126 127 this.context = (ViewContext)obj; 128 this.request = context.getRequest(); 129 this.session = request.getSession(false); 130 this.app = context.getServletContext(); 131 132 Boolean b = (Boolean )context.getAttribute(ViewContext.XHTML); 133 if (b != null) 134 { 135 this.xhtml = b.booleanValue(); 136 } 137 138 139 ActionConfig config = 140 (ActionConfig)request.getAttribute(Globals.MAPPING_KEY); 141 if (config != null) 142 { 143 144 this.formName = config.getAttribute(); 145 } 146 147 ModuleConfig mconfig = RequestUtils.getModuleConfig(request, app); 148 this.resources = (ValidatorResources)app.getAttribute(ValidatorPlugIn. 149 VALIDATOR_KEY + 150 mconfig.getPrefix()); 151 152 } 153 154 155 156 157 165 public int getPage() 166 { 167 return page; 168 } 169 170 177 public void setPage(int page) 178 { 179 this.page = page; 180 } 181 182 190 public String getMethod() 191 { 192 return methodName; 193 } 194 195 203 public void setMethod(String methodName) 204 { 205 this.methodName = methodName; 206 } 207 208 215 public boolean getHtmlComment() 216 { 217 return this.htmlComment; 218 } 219 220 227 public void setHtmlComment(boolean htmlComment) 228 { 229 this.htmlComment = htmlComment; 230 } 231 232 238 public String getSrc() 239 { 240 return src; 241 } 242 243 251 public void setSrc(String src) 252 { 253 this.src = src; 254 } 255 256 261 public boolean getCdata() 262 { 263 return cdata; 264 } 265 266 270 public void setCdata(boolean cdata) 271 { 272 this.cdata = cdata; 273 } 274 275 276 277 278 286 public String getJavascript() throws Exception 287 { 288 return getJavascript(this.formName); 289 } 290 291 299 public String getJavascript(String formName) throws Exception 300 { 301 this.formName = formName; 302 return getJavascript(formName, true); 303 } 304 305 314 public String getDynamicJavascript() throws Exception 315 { 316 return getDynamicJavascript(this.formName); 317 } 318 319 320 327 public String getStaticJavascript() throws Exception 328 { 329 StringBuffer results = new StringBuffer (); 330 331 results.append(getStartElement()); 332 if (this.htmlComment) 333 { 334 results.append(HTML_BEGIN_COMMENT); 335 } 336 results.append(getJavascriptStaticMethods(resources)); 337 results.append(getJavascriptEnd()); 338 339 return results.toString(); 340 } 341 342 343 352 public String getDynamicJavascript(String formName) throws Exception 353 { 354 this.formName = formName; 355 return getJavascript(formName, false); 356 } 357 358 367 protected String getJavascript(String formName, boolean getStatic) throws Exception 368 { 369 StringBuffer results = new StringBuffer (); 370 371 Locale locale = StrutsUtils.getLocale(request, session); 372 373 Form form = resources.get(locale, formName); 374 if (form != null) 375 { 376 results.append(getDynamicJavascript(resources, locale, form)); 377 } 378 379 if(getStatic) 380 { 381 results.append(getJavascriptStaticMethods(resources)); 382 } 383 384 if (form != null) 385 { 386 results.append(getJavascriptEnd()); 387 } 388 389 return results.toString(); 390 } 391 392 393 394 402 protected String getDynamicJavascript(ValidatorResources resources, 403 Locale locale, 404 Form form) 405 { 406 StringBuffer results = new StringBuffer (); 407 408 MessageResources messages = 409 StrutsUtils.getMessageResources(request, app); 410 411 List actions = createActionList(resources, form); 412 413 String methods = createMethods(actions); 414 results.append(getJavascriptBegin(methods)); 415 416 for (Iterator i = actions.iterator(); i.hasNext();) 417 { 418 ValidatorAction va = (ValidatorAction)i.next(); 419 String jscriptVar = null; 420 String functionName = null; 421 422 if (va.getJsFunctionName() != null && va.getJsFunctionName().length() > 0) 423 { 424 functionName = va.getJsFunctionName(); 425 } 426 else 427 { 428 functionName = va.getName(); 429 } 430 431 results.append(" function "); 432 results.append(functionName); 433 results.append(" () { \n"); 434 435 for (Iterator x = form.getFields().iterator(); x.hasNext();) 436 { 437 Field field = (Field)x.next(); 438 439 if (field.isIndexed() 443 || field.getPage() != page 444 || !field.isDependency(va.getName())) 445 { 446 continue; 447 } 448 449 String message = 450 Resources.getMessage(messages, locale, va, field); 451 452 if (message == null) 453 { 454 message = ""; 455 } 456 457 jscriptVar = this.getNextVar(jscriptVar); 458 459 results.append(" this."); 460 results.append(jscriptVar); 461 results.append(" = new Array(\""); 462 results.append(field.getKey()); 463 results.append("\", \""); 464 results.append(message); 465 results.append("\", "); 466 results.append("new Function (\"varName\", \""); 467 468 Map vars = field.getVars(); 469 Iterator varsIterator = vars.keySet().iterator(); 471 while (varsIterator.hasNext()) 472 { 473 String varName = (String )varsIterator.next(); 474 Var var = (Var)vars.get(varName); 475 String varValue = var.getValue(); 476 String jsType = var.getJsType(); 477 478 if (varName.startsWith("field")) 480 { 481 continue; 482 } 483 484 results.append("this."); 486 results.append(varName); 487 488 String escapedVarValue = 489 ValidatorUtil.replace(varValue, "\\", "\\\\"); 490 491 if (Var.JSTYPE_INT.equalsIgnoreCase(jsType)) 492 { 493 results.append("="); 494 results.append(escapedVarValue); 495 results.append("; "); 496 } 497 else if (Var.JSTYPE_REGEXP.equalsIgnoreCase(jsType)) 498 { 499 results.append("=/"); 500 results.append(escapedVarValue); 501 results.append("/; "); 502 } 503 else if (Var.JSTYPE_STRING.equalsIgnoreCase(jsType)) 504 { 505 results.append("='"); 506 results.append(escapedVarValue); 507 results.append("'; "); 508 } 509 else if ("mask".equalsIgnoreCase(varName)) 512 { 513 results.append("=/"); 514 results.append(escapedVarValue); 515 results.append("/; "); 516 } 517 else 518 { 519 results.append("='"); 520 results.append(escapedVarValue); 521 results.append("'; "); 522 } 523 } 524 results.append(" return this[varName];\"));\n"); 525 } 526 results.append(" } \n\n"); 527 } 528 return results.toString(); 529 } 530 531 532 537 protected String createMethods(List actions) 538 { 539 String methodOperator = " && "; 540 541 StringBuffer methods = null; 542 for (Iterator i = actions.iterator(); i.hasNext();) 543 { 544 ValidatorAction va = (ValidatorAction)i.next(); 545 if (methods == null) 546 { 547 methods = new StringBuffer (va.getMethod()); 548 } 549 else 550 { 551 methods.append(methodOperator); 552 methods.append(va.getMethod()); 553 } 554 methods.append("(form)"); 555 } 556 return methods.toString(); 557 } 558 559 560 567 protected List createActionList(ValidatorResources resources, Form form) 568 { 569 List actionMethods = new ArrayList (); 570 for (Iterator i = form.getFields().iterator(); i.hasNext();) 572 { 573 Field field = (Field)i.next(); 574 for (Iterator x = field.getDependencies().iterator(); x.hasNext();) 575 { 576 Object o = x.next(); 577 if (o != null && !actionMethods.contains(o)) 578 { 579 actionMethods.add(o); 580 } 581 } 582 } 583 584 List actions = new ArrayList (); 585 586 for (Iterator i = actionMethods.iterator(); i.hasNext();) 588 { 589 String depends = (String ) i.next(); 590 ValidatorAction va = resources.getValidatorAction(depends); 591 592 if (va == null) 594 { 595 throw new NullPointerException ( 596 "Depends string \"" + depends + 597 "\" was not found in validator-rules.xml."); 598 } 599 600 String javascript = va.getJavascript(); 601 if (javascript != null && javascript.length() > 0) 602 { 603 actions.add(va); 604 } 605 else 606 { 607 i.remove(); 608 } 609 } 610 611 Comparator comp = new ValidatorActionComparator(); 613 Collections.sort(actions, comp); 614 return actions; 615 } 616 617 618 624 protected String getJavascriptBegin(String methods) 625 { 626 StringBuffer sb = new StringBuffer (); 627 String name = formName.replace('/', '_'); name = name.substring(0, 1).toUpperCase() + 629 name.substring(1, name.length()); 630 631 sb.append(getStartElement()); 632 633 if (this.xhtml && this.cdata) 634 { 635 sb.append("<![CDATA[\r\n"); 636 } 637 638 if (!this.xhtml && this.htmlComment) 639 { 640 sb.append(HTML_BEGIN_COMMENT); 641 } 642 sb.append("\n var bCancel = false; \n\n"); 643 644 if (methodName == null || methodName.length() == 0) 645 { 646 sb.append(" function validate"); 647 sb.append(name); 648 } 649 else 650 { 651 sb.append(" function "); 652 sb.append(methodName); 653 } 654 sb.append("(form) {"); 655 sb.append(" \n"); 657 sb.append(" if (bCancel) \n"); 658 sb.append(" return true; \n"); 659 sb.append(" else \n"); 660 661 if (methods == null || methods.length() == 0) 663 { 664 sb.append(" return true; \n"); 665 } 666 else 667 { 668 sb.append(" return "); 669 sb.append(methods); 670 sb.append("; \n"); 671 } 672 sb.append(" } \n\n"); 673 674 return sb.toString(); 675 } 676 677 682 protected String getJavascriptStaticMethods(ValidatorResources resources) 683 { 684 StringBuffer sb = new StringBuffer ("\n\n"); 685 686 Iterator actions = resources.getValidatorActions().values().iterator(); 687 while (actions.hasNext()) 688 { 689 ValidatorAction va = (ValidatorAction) actions.next(); 690 if (va != null) 691 { 692 String javascript = va.getJavascript(); 693 if (javascript != null && javascript.length() > 0) 694 { 695 sb.append(javascript + "\n"); 696 } 697 } 698 } 699 return sb.toString(); 700 } 701 702 703 708 protected String getJavascriptEnd() 709 { 710 StringBuffer sb = new StringBuffer (); 711 sb.append("\n"); 712 713 if (!this.xhtml && this.htmlComment) 714 { 715 sb.append(HTML_END_COMMENT); 716 } 717 718 if (this.xhtml && this.cdata) 719 { 720 sb.append("]]>\r\n"); 721 } 722 sb.append("</script>\n\n"); 723 724 return sb.toString(); 725 } 726 727 728 735 private String getNextVar(String input) 736 { 737 if (input == null) 738 { 739 return "aa"; 740 } 741 742 input = input.toLowerCase(); 743 744 for (int i = input.length(); i > 0; i--) 745 { 746 int pos = i - 1; 747 748 char c = input.charAt(pos); 749 c++; 750 751 if (c <= 'z') 752 { 753 if (i == 0) 754 { 755 return c + input.substring(pos, input.length()); 756 } 757 else if (i == input.length()) 758 { 759 return input.substring(0, pos) + c; 760 } 761 else 762 { 763 return input.substring(0, pos) + c + 764 input.substring(pos, input.length() - 1); 765 } 766 } 767 else 768 { 769 input = replaceChar(input, pos, 'a'); 770 } 771 } 772 return null; 773 } 774 775 776 784 private String replaceChar(String input, int pos, char c) 785 { 786 if (pos == 0) 787 { 788 return c + input.substring(pos, input.length()); 789 } 790 else if (pos == input.length()) 791 { 792 return input.substring(0, pos) + c; 793 } 794 else 795 { 796 return input.substring(0, pos) + c + 797 input.substring(pos, input.length() - 1); 798 } 799 } 800 801 802 807 private String getStartElement() 808 { 809 StringBuffer start = new StringBuffer ("<script type=\"text/javascript\""); 810 811 if (!this.xhtml) 813 { 814 start.append(" language=\"Javascript1.1\""); 815 } 816 817 if (this.src != null) 818 { 819 start.append(" SRC=\"" + src + "\""); 820 } 821 822 start.append("> \n"); 823 return start.toString(); 824 } 825 826 827 830 protected class ValidatorActionComparator implements Comparator 831 { 832 838 public int compare(Object o1, Object o2) 839 { 840 ValidatorAction va1 = (ValidatorAction)o1; 841 ValidatorAction va2 = (ValidatorAction)o2; 842 843 String vad1 = va1.getDepends(); 844 String vad2 = va2.getDepends(); 845 846 if ((vad1 == null || vad1.length() == 0) 847 && (vad2 == null || vad2.length() == 0)) 848 { 849 return 0; 850 } 851 else if ((vad1 != null && vad1.length() > 0) 852 && (vad2 == null || vad2.length() == 0)) 853 { 854 return 1; 855 } 856 else if ((vad1 == null || vad1.length() == 0) 857 && (vad2 != null && vad2.length() > 0)) 858 { 859 return -1; 860 } 861 else 862 { 863 return va1.getDependencies().size() - va2.getDependencies().size(); 864 } 865 } 866 } 867 868 } 869 | Popular Tags |