1 23 package com.sun.enterprise.tools.jsfext.el; 24 25 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutElement; 26 27 import java.util.ArrayList ; 28 import java.util.EmptyStackException ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.Stack ; 34 35 import javax.faces.component.UIComponent; 36 37 38 73 public class PermissionChecker { 74 75 78 public PermissionChecker(LayoutElement desc, UIComponent component, String infixStr) { 79 setLayoutElement(desc); 80 setUIComponent(component); 81 setInfix(stripWhiteSpace(infixStr)); 82 } 83 84 85 91 protected void setUIComponent(UIComponent component) { 92 _component = component; 93 } 94 95 96 101 public UIComponent getUIComponent() { 102 return _component; 103 } 104 105 106 112 protected void setLayoutElement(LayoutElement desc) { 113 _desc = desc; 114 } 115 116 117 122 public LayoutElement getLayoutElement() { 123 return _desc; 124 } 125 126 127 147 private static int getPrecedence(char op) { 148 switch (op) { 149 case LEFT_PAREN: 150 return 1; 151 case RIGHT_PAREN: 152 return 999; 153 case EQUALS_OPERATOR: 154 return 2; 155 case LESS_THAN_OPERATOR: 156 case MORE_THAN_OPERATOR: 157 return 4; 158 case OR_OPERATOR: 159 return 8; 160 case AND_OPERATOR: 161 return 16; 162 case MODULUS_OPERATOR: 163 return 32; 164 case NOT_OPERATOR: 165 return 64; 166 } 167 return 1; 168 } 169 170 177 protected char[] preProcessString(String source) { 178 char[] arr = source.toCharArray(); 179 int sourceLen = arr.length; 180 int destLen = 0; 181 String str = null; 182 183 for (int idx = 0; idx < sourceLen; idx++) { 185 switch (arr[idx]) { 186 case POST_TRUE: 187 case POST_TRUE_CAP: 188 if (((idx + TRUE.length()) <= sourceLen) 189 && TRUE.equalsIgnoreCase( 190 new String (arr, idx, TRUE.length()))) { 191 arr[destLen++] = POST_TRUE; 192 idx += TRUE.length() - 1; 193 } else { 194 idx = storeFunction(arr, idx); 195 arr[destLen++] = FUNCTION_MARKER; 196 } 197 break; 198 case POST_FALSE: 199 case POST_FALSE_CAP: 200 if (((idx + FALSE.length()) <= sourceLen) 201 && FALSE.equalsIgnoreCase( 202 new String (arr, idx, FALSE.length()))) { 203 arr[destLen++] = POST_FALSE; 204 idx += FALSE.length() - 1; 205 } else { 206 idx = storeFunction(arr, idx); 207 arr[destLen++] = FUNCTION_MARKER; 208 } 209 break; 210 case OR_OPERATOR: 211 case EQUALS_OPERATOR: 212 case LESS_THAN_OPERATOR: 213 case MORE_THAN_OPERATOR: 214 case MODULUS_OPERATOR: 215 case AND_OPERATOR: 216 case NOT_OPERATOR: 217 case LEFT_PAREN: 218 case RIGHT_PAREN: 219 arr[destLen++] = arr[idx]; 220 break; 221 default: 222 idx = storeFunction(arr, idx); 223 arr[destLen++] = FUNCTION_MARKER; 224 } 225 } 226 char[] dest = new char[destLen]; 227 for (int idx = 0; idx < destLen; idx++) { 228 dest[idx] = arr[idx]; 229 } 230 return dest; 231 } 232 233 234 324 325 326 334 protected int storeFunction(char[] arr, int idx) { 335 int start = idx; 337 int len = arr.length; 338 while ((idx < len) && !isOperator(arr[idx])) { 339 idx++; 340 } 341 342 String str = new String (arr, start, idx - start); 344 345 Function function = getFunction(str); 347 if (function != null) { 348 int left = idx; 350 if ((left >= len) || (arr[left] != LEFT_PAREN)) { 351 throw new RuntimeException ("Function '" + str 352 + "' is expected to have a '" + LEFT_PAREN 353 + "' immediately following it. Equation: '" 354 + new String (arr) + "'."); 355 } 356 357 ArrayList arguments = new ArrayList (); 358 359 while ((++idx < len) && (arr[idx] != RIGHT_PAREN)) { 361 if (arr[idx] == ARGUMENT_SEPARATOR) { 362 left++; 363 arguments.add(new String (arr, left, idx - left)); 364 left = idx; 365 } 366 } 367 368 left++; 370 if (idx > left) { 371 arguments.add(new String (arr, left, idx - left)); 372 } 373 374 function.setArguments(arguments); 376 } else { 377 idx--; if ((str.charAt(0) == FUNCTION_MARKER) && (str.length() == 1) 380 && !_tmpFunctionStack.empty()) { 381 function = (Function) _tmpFunctionStack.pop(); 383 } else { 384 function = new StringFunction(str); 386 } 387 } 388 389 _functionList.add(function); 391 392 return idx; 394 } 395 396 397 402 protected static Function getFunction(String functionName) { 403 Class functionClass = (Class ) _functions.get(functionName); 405 if (functionClass == null) { 406 return null; 407 } 408 409 Function function = null; 411 try { 412 function = (Function) functionClass.newInstance(); 413 } catch (Exception ex) { 414 throw new RuntimeException ("Unable to instantiate '" 415 + functionClass.getName() + "' for '" 416 + functionName + "'", ex); 417 } 418 419 return function; 421 } 422 423 424 439 public static void registerFunction(String functionName, Class function) { 440 if (function == null) { 441 _functions.remove(functionName); 442 } 443 if (!Function.class.isAssignableFrom(function)) { 444 throw new RuntimeException ("'" + function.getName() 445 + "' must implement '" + Function.class.getName() + "'"); 446 } 447 _functions.put(functionName, function); 448 } 449 450 451 455 public static boolean isOperator(char ch) { 456 switch (ch) { 457 case LEFT_PAREN: 458 case RIGHT_PAREN: 459 case EQUALS_OPERATOR: 460 case LESS_THAN_OPERATOR: 461 case MORE_THAN_OPERATOR: 462 case MODULUS_OPERATOR: 463 case OR_OPERATOR: 464 case AND_OPERATOR: 465 case NOT_OPERATOR: 466 return true; 474 } 475 return false; 476 } 477 478 479 489 protected char [] generatePostfix(String infixStr) { 490 _functionList = new ArrayList (); 492 493 char[] result = preProcessString(infixStr); 495 int resultLen = result.length; 498 int postIdx = 0; 499 int precedence = 0; 500 Stack opStack = new Stack (); 501 502 for (int idx = 0; idx < resultLen; idx++) { 504 switch(result[idx]) { 505 case FUNCTION_MARKER: 506 case POST_TRUE: 507 case POST_FALSE: 508 result[postIdx++] = result[idx]; 509 break; 510 case LEFT_PAREN: 511 opStack.push(new Character (LEFT_PAREN)); 512 break; 513 case RIGHT_PAREN: 514 while (!opStack.empty() 515 && (((Character ) opStack.peek()).charValue() 516 != LEFT_PAREN)) { 517 result[postIdx++] = 518 ((Character ) opStack.pop()).charValue(); 519 } 520 if (!opStack.empty()) { 521 opStack.pop(); 523 } 524 break; 525 default: 526 precedence = getPrecedence(result[idx]); 528 while (!opStack.empty() 529 && (getPrecedence(((Character ) opStack.peek()). 530 charValue()) >= precedence)) { 531 result[postIdx++] = 532 ((Character ) opStack.pop()).charValue(); 533 } 534 535 536 opStack.push(new Character (result[idx])); 537 break; 538 } 539 } 540 541 while (!opStack.empty()) { 543 result[postIdx++] = ((Character ) opStack.pop()).charValue(); 544 } 545 char[] postfixStr = new char[postIdx]; 547 for (int idx = 0; idx < postIdx; idx++) { 548 postfixStr[idx] = result[idx]; 549 } 550 return postfixStr; 552 } 553 554 555 559 public boolean hasPermission() { 560 char[] postfixArr = getPostfixArr(); 561 int len = postfixArr.length; 562 Stack result = new Stack (); 563 result.push(FALSE_BOOLEAN_FUNCTION); boolean val1, val2; 565 Iterator it = _functionList.iterator(); 566 Function func = null; 567 568 for (int idx = 0; idx < len; idx++) { 570 switch (postfixArr[idx]) { 571 case POST_TRUE: 572 result.push(TRUE_BOOLEAN_FUNCTION); 573 break; 574 case POST_FALSE: 575 result.push(FALSE_BOOLEAN_FUNCTION); 576 break; 577 case FUNCTION_MARKER: 578 if (!it.hasNext()) { 579 throw new RuntimeException ("Unable to evaluate: '" 580 + toString() + "' -- found function marker " 581 + "w/o cooresponding function!"); 582 } 583 result.push(it.next()); 584 break; 585 case EQUALS_OPERATOR: 586 try { 587 String matchStr = result.pop().toString(); 589 val1 = result.pop().toString().matches(matchStr); 590 } catch (EmptyStackException ex) { 591 throw new RuntimeException ("Unable to evaluate: '" 592 + toString() + "'.", ex); 593 } 594 result.push(val1 ? 595 TRUE_BOOLEAN_FUNCTION : FALSE_BOOLEAN_FUNCTION); 596 break; 597 case LESS_THAN_OPERATOR: 598 try { 599 val1 = Integer.parseInt(result.pop().toString()) 601 > Integer.parseInt(result.pop().toString()); 602 } catch (EmptyStackException ex) { 603 throw new RuntimeException ("Unable to evaluate: '" 604 + toString() + "'.", ex); 605 } 606 result.push(val1 ? 607 TRUE_BOOLEAN_FUNCTION : FALSE_BOOLEAN_FUNCTION); 608 break; 609 case MORE_THAN_OPERATOR: 610 try { 611 val1 = Integer.parseInt(result.pop().toString()) 613 < Integer.parseInt(result.pop().toString()); 614 } catch (EmptyStackException ex) { 615 throw new RuntimeException ("Unable to evaluate: '" 616 + toString() + "'.", ex); 617 } 618 result.push(val1 ? 619 TRUE_BOOLEAN_FUNCTION : FALSE_BOOLEAN_FUNCTION); 620 break; 621 case MODULUS_OPERATOR: 622 try { 623 int modNumber = 625 Integer.parseInt(result.pop().toString()); 626 int num = Integer.parseInt(result.pop().toString()); 627 result.push(new StringFunction("" + (num % modNumber))); 628 } catch (EmptyStackException ex) { 629 throw new RuntimeException ("Unable to evaluate: '" 630 + toString() + "'.", ex); 631 } 632 break; 633 case OR_OPERATOR: 634 try { 635 val1 = ((Function) result.pop()).evaluate(); 636 val2 = ((Function) result.pop()).evaluate(); 637 } catch (EmptyStackException ex) { 638 throw new RuntimeException ("Unable to evaluate: '" 639 + toString() + "'.", ex); 640 } 641 result.push((val1 || val2) ? 642 TRUE_BOOLEAN_FUNCTION : FALSE_BOOLEAN_FUNCTION); 643 break; 644 case AND_OPERATOR: 645 try { 646 val1 = ((Function) result.pop()).evaluate(); 647 val2 = ((Function) result.pop()).evaluate(); 648 } catch (EmptyStackException ex) { 649 throw new RuntimeException ("Unable to evaluate: '" 650 + toString() + "'.", ex); 651 } 652 result.push((val1 && val2) ? 653 TRUE_BOOLEAN_FUNCTION : FALSE_BOOLEAN_FUNCTION); 654 break; 655 case NOT_OPERATOR: 656 try { 657 val1 = ((Function) result.pop()).evaluate(); 658 } catch (EmptyStackException ex) { 659 throw new RuntimeException ("Unable to evaluate: '" 660 + toString() + "'.", ex); 661 } 662 result.push((!val1) ? 663 TRUE_BOOLEAN_FUNCTION : FALSE_BOOLEAN_FUNCTION); 664 break; 665 } 666 } 667 668 try { 670 val1 = ((Function) result.pop()).evaluate(); 671 } catch (EmptyStackException ex) { 672 throw new RuntimeException ("Unable to evaluate: '" 673 + toString() + "'.", ex); 674 } 675 if (!result.empty()) { 676 result.pop(); if (!result.empty()) { 678 throw new RuntimeException ("Unable to evaluate: '" 679 + toString() + "' -- values left on the stack."); 680 } 681 } 682 return val1; 683 } 684 685 686 690 public String getInfix() { 691 return _infixStr; 692 } 693 694 695 703 public void setInfix(String equation) { 704 _infixStr = equation; 705 setPostfixArr(generatePostfix(equation)); 706 } 707 708 709 713 protected char [] getPostfixArr() { 714 if (_postfixArr == null) { 715 _postfixArr = new char[] {' '}; 716 } 717 return _postfixArr; 718 } 719 720 721 724 protected void setPostfixArr(char[] postfix) { 725 _postfixArr = postfix; 726 } 727 728 729 733 public String getPostfix() { 734 if (getPostfixArr() == null) { 735 return ""; 736 } 737 return new String (getPostfixArr()); 738 } 739 740 741 744 public String toString() { 745 return _infixStr + " = " + toString(getPostfixArr()); 746 } 747 748 749 758 private String toString(char[] post) { 759 int len = post.length; 760 StringBuffer result = new StringBuffer (""); 761 Iterator it = _functionList.iterator(); 762 763 for (int idx = 0; idx < len; idx++) { 764 switch (post[idx]) { 765 case POST_TRUE: 766 result.append(TRUE); 767 break; 768 case POST_FALSE: 769 result.append(FALSE); 770 break; 771 case FUNCTION_MARKER: 772 result.append(((Function) it.next()).toString()); 773 break; 774 default: 775 result.append(post[idx]); 776 } 777 } 778 779 return result.toString(); 780 } 781 782 783 786 public static String stripWhiteSpace(String input) { 787 char[] arr = input.toCharArray(); 788 int len = arr.length; 789 int destLen = 0; 790 791 for (int idx = 0; idx < len; idx++) { 793 if (Character.isWhitespace(arr[idx])) { 794 continue; 795 } 796 arr[destLen++] = arr[idx]; 797 } 798 799 return new String (arr, 0, destLen); 801 } 802 803 804 805 806 813 public static interface Function { 814 815 818 public List getArguments(); 819 820 824 public void setArguments(List args); 825 826 830 public boolean evaluate(); 831 } 832 833 843 protected class StringFunction implements PermissionChecker.Function { 844 845 850 public StringFunction(String value) { 851 _value = value; 852 } 853 854 857 public List getArguments() { 858 return null; 859 } 860 861 864 public void setArguments(List args) { 865 } 866 867 871 public boolean evaluate() { 872 Object obj = getEvaluatedValue(); 873 if (obj == null) { 874 return false; 875 } 876 obj = obj.toString(); 877 if (obj.equals("")) { 878 return false; 879 } 880 if (((String ) obj).equalsIgnoreCase("false")) { 881 return false; 882 } 883 return true; 884 } 885 886 890 public Object getEvaluatedValue() { 891 return VariableResolver.resolveVariables(getLayoutElement(), 892 getUIComponent(), _value); 893 } 894 895 900 public String toString() { 901 Object obj = getEvaluatedValue(); 902 if (obj == null) { 903 return ""; 904 } 905 return obj.toString(); 906 } 907 908 private String _value; 909 } 910 911 918 protected static class BooleanFunction implements PermissionChecker.Function { 919 920 923 public BooleanFunction() { 924 } 925 926 929 public BooleanFunction(boolean value) { 930 _value = value; 931 } 932 933 936 public List getArguments() { 937 return null; 938 } 939 940 943 public void setArguments(List args) { 944 } 945 946 950 public boolean evaluate() { 951 return _value; 952 } 953 954 958 public String toString() { 959 return _value ? "true" : "false"; 960 } 961 962 private boolean _value = false; 963 } 964 965 966 970 public static void main(String [] args) { 971 PermissionChecker checker; 972 if (args.length > 0) { 973 for (int count = 0; count < args.length; count++) { 974 checker = new PermissionChecker(null, null, args[count]); 975 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 976 } 977 } else { 978 boolean success = true; 979 checker = new PermissionChecker(null, null, "true |false"); 980 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 981 if (!checker.toString().equals("true|false = truefalse|")) { 982 System.out.println("\tFAILED!"); 983 System.out.println("Should have been:\n" + "true|false = truefalse|"); 984 success = false; 985 } 986 if (!checker.hasPermission()) { 987 System.out.println("\tFAILED!"); 988 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 989 success = false; 990 } 991 992 checker = new PermissionChecker(null, null, "true&(false|true)"); 993 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 994 if (!checker.toString().equals("true&(false|true) = truefalsetrue|&")) { 995 System.out.println("\tFAILED!"); 996 System.out.println("Should have been:\n" + "true&(false|true) = truefalsetrue|&"); 997 success = false; 998 } 999 if (!checker.hasPermission()) { 1000 System.out.println("\tFAILED!"); 1001 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1002 success = false; 1003 } 1004 1005 checker = new PermissionChecker(null, null, "true&false|true"); 1006 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1007 if (!checker.toString().equals("true&false|true = truefalse&true|")) { 1008 System.out.println("\tFAILED!"); 1009 System.out.println("Should have been:\n" + "true&false|true = truefalse&true|"); 1010 success = false; 1011 } 1012 if (!checker.hasPermission()) { 1013 System.out.println("\tFAILED!"); 1014 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1015 success = false; 1016 } 1017 1018 checker = new PermissionChecker(null, null, "true&true|false&true"); 1019 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1020 if (!checker.toString().equals("true&true|false&true = truetrue&falsetrue&|")) { 1021 System.out.println("\tFAILED!"); 1022 System.out.println("Should have been:\n" + "true&true|false&true = truetrue&falsetrue&|"); 1023 success = false; 1024 } 1025 if (!checker.hasPermission()) { 1026 System.out.println("\tFAILED!"); 1027 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1028 success = false; 1029 } 1030 1031 checker = new PermissionChecker(null, null, "!true|false&!(false|true)"); 1032 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1033 if (!checker.toString().equals("!true|false&!(false|true) = true!falsefalsetrue|!&|")) { 1034 System.out.println("\tFAILED!"); 1035 System.out.println("Should have been:\n" + "!true|false&!(false|true) = true!falsefalsetrue|!&|"); 1036 success = false; 1037 } 1038 if (checker.hasPermission()) { 1039 System.out.println("\tFAILED!"); 1040 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1041 success = false; 1042 } 1043 1044 checker = new PermissionChecker(null, null, "!(!(true&!true)|!(false|false))|(true|false)&true"); 1045 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1046 if (!checker.toString().equals("!(!(true&!true)|!(false|false))|(true|false)&true = truetrue!&!falsefalse|!|!truefalse|true&|")) { 1047 1048 System.out.println("\tFAILED!"); 1049 System.out.println("Should have been:\n" + "!(!(true&!true)|!(false|false))|(true|false)&true = truetrue!&!falsefalse|!|!truefalse|true&|"); 1050 success = false; 1051 } 1052 if (!checker.hasPermission()) { 1053 System.out.println("\tFAILED!"); 1054 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1055 success = false; 1056 } 1057 1058 checker = new PermissionChecker(null, null, "false =false"); 1060 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1061 if (!checker.toString().equals("false=false = falsefalse=")) { 1062 System.out.println("\tFAILED!"); 1063 System.out.println("Should have been:\n" + "false=false = falsefalse="); 1064 success = false; 1065 } 1066 if (!checker.hasPermission()) { 1067 System.out.println("\tFAILED!"); 1068 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1069 success = false; 1070 } 1071 1072 checker = new PermissionChecker(null, null, " test= me "); 1073 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1074 if (!checker.toString().equals("test=me = testme=")) { 1075 System.out.println("\tFAILED!"); 1076 System.out.println("Should have been:\n" + "test=me = testme="); 1077 success = false; 1078 } 1079 if (checker.hasPermission()) { 1080 System.out.println("\tFAILED!"); 1081 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1082 success = false; 1083 } 1084 1085 checker = new PermissionChecker(null, null, " this should work=thisshouldwork"); 1086 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1087 if (!checker.toString().equals("thisshouldwork=thisshouldwork = thisshouldworkthisshouldwork=")) { 1088 System.out.println("\tFAILED!"); 1089 System.out.println("Should have been:\n" + "thisshouldwork=thisshouldwork = thisshouldworkthisshouldwork="); 1090 success = false; 1091 } 1092 if (!checker.hasPermission()) { 1093 System.out.println("\tFAILED!"); 1094 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1095 success = false; 1096 } 1097 1098 checker = new PermissionChecker(null, null, "false|ab=true"); 1099 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1100 if (!checker.toString().equals("false|ab=true = falseab|true=")) { 1101 System.out.println("\tFAILED!"); 1102 System.out.println("Should have been:\n" + "false|ab=true = falseab|true="); 1103 success = false; 1104 } 1105 if (!checker.hasPermission()) { 1106 System.out.println("\tFAILED!"); 1107 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1108 success = false; 1109 } 1110 1111 checker = new PermissionChecker(null, null, "false|(ab=true)"); 1112 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1113 if (!checker.toString().equals("false|(ab=true) = falseabtrue=|")) { 1114 System.out.println("\tFAILED!"); 1115 System.out.println("Should have been:\n" + "false|ab=true = falseab|true="); 1116 success = false; 1117 } 1118 if (checker.hasPermission()) { 1119 System.out.println("\tFAILED!"); 1120 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1121 success = false; 1122 } 1123 1124 checker = new PermissionChecker(null, null, "false|(ab=ab)"); 1125 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1126 if (!checker.toString().equals("false|(ab=ab) = falseabab=|")) { 1127 System.out.println("\tFAILED!"); 1128 System.out.println("Should have been:\n" + "false|ab=true = falseab|true="); 1129 success = false; 1130 } 1131 if (!checker.hasPermission()) { 1132 System.out.println("\tFAILED!"); 1133 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1134 success = false; 1135 } 1136 1137 checker = new PermissionChecker(null, null, "!"); 1138 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1139 if (!checker.toString().equals("! = !")) { 1140 System.out.println("\tFAILED!"); 1141 System.out.println("Should have been:\n" + "! = !"); 1142 success = false; 1143 } 1144 if (!checker.hasPermission()) { 1145 System.out.println("\tFAILED!"); 1146 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1147 success = false; 1148 } 1149 1150 checker = new PermissionChecker(null, null, ""); 1151 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1152 if (!checker.toString().equals(" = ")) { 1153 System.out.println("\tFAILED!"); 1154 System.out.println("Should have been:\n" + " = "); 1155 success = false; 1156 } 1157 if (checker.hasPermission()) { 1158 System.out.println("\tFAILED!"); 1159 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1160 success = false; 1161 } 1162 1163 checker = new PermissionChecker(null, null, "!$escape{}"); 1164 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1165 if (!checker.toString().equals("!$escape{} = !")) { 1166 System.out.println("\tFAILED!"); 1167 System.out.println("Should have been:\n" + "! = !"); 1168 success = false; 1169 } 1170 if (!checker.hasPermission()) { 1171 System.out.println("\tFAILED!"); 1172 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1173 success = false; 1174 } 1175 1176 checker = new PermissionChecker(null, null, "$escape{}"); 1177 System.out.println("Output:\n" + checker.toString() + " (" + checker.hasPermission() + ")"); 1178 if (!checker.toString().equals("$escape{} = ")) { 1179 System.out.println("\tFAILED!"); 1180 System.out.println("Should have been:\n" + " = "); 1181 success = false; 1182 } 1183 if (checker.hasPermission()) { 1184 System.out.println("\tFAILED!"); 1185 System.out.println("hasPermission(" + checker.toString(checker.getPostfixArr()) + ") returned the wrong result!"); 1186 success = false; 1187 } 1188 1189 if (success) { 1190 System.out.println("\n\tALL TESTS PASSED!"); 1191 } else { 1192 System.out.println("\n\tNOT ALL TESTS PASSED!"); 1193 } 1194 } 1195 } 1196 1197 1198 1201 public static final BooleanFunction FALSE_BOOLEAN_FUNCTION = 1202 new BooleanFunction(false); 1203 1204 1207 public static final BooleanFunction TRUE_BOOLEAN_FUNCTION = 1208 new BooleanFunction(true); 1209 1210 1211 protected static final char POST_TRUE = 't'; 1212 protected static final char POST_FALSE = 'f'; 1213 protected static final char POST_TRUE_CAP = 'T'; 1214 protected static final char POST_FALSE_CAP = 'F'; 1215 1216 public static final String TRUE = "true"; 1217 public static final String FALSE = "false"; 1218 1219 public static final char FUNCTION_MARKER = 'F'; 1221 1222 public static final char LEFT_PAREN = '('; 1224 public static final char RIGHT_PAREN = ')'; 1225 public static final char EQUALS_OPERATOR = '='; 1226 public static final char OR_OPERATOR = '|'; 1227 public static final char AND_OPERATOR = '&'; 1228 public static final char NOT_OPERATOR = '!'; 1229 public static final char LESS_THAN_OPERATOR = '<'; 1230 public static final char MORE_THAN_OPERATOR = '>'; 1231 public static final char MODULUS_OPERATOR = '%'; 1232 1233 public static final char ARGUMENT_SEPARATOR = ','; 1235 1236 1247 1248 1251 private String _infixStr = null; 1252 1253 1256 private char[] _postfixArr = null; 1257 1258 1262 private static Map _functions = new HashMap (); 1263 1264 1268 private List _functionList = null; 1269 1270 1276 private Stack _tmpFunctionStack = null; 1277 private LayoutElement _desc = null; 1278 private UIComponent _component = null; 1279} 1280 | Popular Tags |