1 11 12 package org.eclipse.osgi.framework.internal.core; 13 14 import java.lang.reflect.Constructor ; 15 import java.lang.reflect.InvocationTargetException ; 16 import java.security.AccessController ; 17 import java.security.PrivilegedAction ; 18 import java.util.ArrayList ; 19 import java.util.Collection ; 20 import java.util.Dictionary ; 21 import java.util.Iterator ; 22 23 import org.eclipse.osgi.framework.debug.Debug; 24 import org.eclipse.osgi.framework.util.Headers; 25 import org.eclipse.osgi.util.NLS; 26 import org.osgi.framework.Filter; 27 import org.osgi.framework.InvalidSyntaxException; 28 29 130 131 public class FilterImpl implements Filter { 132 133 134 146 public FilterImpl(String filter) throws InvalidSyntaxException { 147 topLevel = true; 148 new Parser(filter).parse(this); 149 } 150 151 161 public boolean match(org.osgi.framework.ServiceReference reference) { 162 return match0(((ServiceReferenceImpl) reference).registration.properties); 163 } 164 165 173 public boolean match(Dictionary dictionary) { 174 if (dictionary != null) { 175 dictionary = new Headers(dictionary); 176 } 177 178 return match0(dictionary); 179 } 180 181 194 public boolean matchCase(Dictionary dictionary) { 195 return match0(dictionary); 196 } 197 198 205 public String toString() { 206 if (this.filter == null) { 207 StringBuffer filter = new StringBuffer (); 208 filter.append('('); 209 210 switch (operation) { 211 case AND : 212 { 213 filter.append('&'); 214 215 FilterImpl[] filters = (FilterImpl[]) value; 216 int size = filters.length; 217 218 for (int i = 0; i < size; i++) { 219 filter.append(filters[i].toString()); 220 } 221 222 break; 223 } 224 225 case OR : 226 { 227 filter.append('|'); 228 229 FilterImpl[] filters = (FilterImpl[]) value; 230 int size = filters.length; 231 232 for (int i = 0; i < size; i++) { 233 filter.append(filters[i].toString()); 234 } 235 236 break; 237 } 238 239 case NOT : 240 { 241 filter.append('!'); 242 filter.append(value.toString()); 243 244 break; 245 } 246 247 case SUBSTRING : 248 { 249 filter.append(attr); 250 filter.append('='); 251 252 String [] substrings = (String []) value; 253 254 int size = substrings.length; 255 256 for (int i = 0; i < size; i++) { 257 String substr = substrings[i]; 258 259 if (substr == null) { 260 filter.append('*'); 261 } else { 262 filter.append(encodeValue(substr)); 263 } 264 } 265 266 break; 267 } 268 case EQUAL : 269 { 270 filter.append(attr); 271 filter.append('='); 272 filter.append(encodeValue(value.toString())); 273 274 break; 275 } 276 case GREATER : 277 { 278 filter.append(attr); 279 filter.append(">="); filter.append(encodeValue(value.toString())); 281 282 break; 283 } 284 case LESS : 285 { 286 filter.append(attr); 287 filter.append("<="); filter.append(encodeValue(value.toString())); 289 290 break; 291 } 292 case APPROX : 293 { 294 filter.append(attr); 295 filter.append("~="); filter.append(encodeValue(approxString(value.toString()))); 297 298 break; 299 } 300 301 case PRESENT : 302 { 303 filter.append(attr); 304 filter.append("=*"); 306 break; 307 } 308 } 309 310 filter.append(')'); 311 312 if (topLevel) { 313 this.filter = filter.toString(); 314 } else { 315 return filter.toString(); 316 } 317 } 318 319 return this.filter; 320 } 321 322 330 public boolean equals(Object obj) { 331 if (obj == this) { 332 return true; 333 } 334 335 if (!(obj instanceof FilterImpl)) { 336 return false; 337 } 338 339 return this.toString().equals(obj.toString()); 340 } 341 342 348 public int hashCode() { 349 return this.toString().hashCode(); 350 } 351 352 353 354 355 protected int operation; 356 protected static final int EQUAL = 1; 357 protected static final int APPROX = 2; 358 protected static final int GREATER = 3; 359 protected static final int LESS = 4; 360 protected static final int PRESENT = 5; 361 protected static final int SUBSTRING = 6; 362 protected static final int AND = 7; 363 protected static final int OR = 8; 364 protected static final int NOT = 9; 365 366 367 protected String attr; 368 369 protected Object value; 370 371 372 protected String filter; 373 374 375 protected boolean topLevel; 376 377 protected FilterImpl() { 378 topLevel = false; 379 } 380 381 protected void setFilter(int operation, String attr, Object value) { 382 this.operation = operation; 383 this.attr = attr; 384 this.value = value; 385 } 386 387 397 protected boolean match(ServiceReferenceImpl reference) { 398 return match0(reference.registration.properties); 399 } 400 401 410 protected boolean match0(Dictionary properties) { 411 switch (operation) { 412 case AND : 413 { 414 FilterImpl[] filters = (FilterImpl[]) value; 415 int size = filters.length; 416 417 for (int i = 0; i < size; i++) { 418 if (!filters[i].match0(properties)) { 419 return false; 420 } 421 } 422 423 return true; 424 } 425 426 case OR : 427 { 428 FilterImpl[] filters = (FilterImpl[]) value; 429 int size = filters.length; 430 431 for (int i = 0; i < size; i++) { 432 if (filters[i].match0(properties)) { 433 return true; 434 } 435 } 436 437 return false; 438 } 439 440 case NOT : 441 { 442 FilterImpl filter = (FilterImpl) value; 443 444 return !filter.match0(properties); 445 } 446 447 case SUBSTRING : 448 case EQUAL : 449 case GREATER : 450 case LESS : 451 case APPROX : 452 { 453 Object prop = (properties == null) ? null : properties.get(attr); 454 455 return compare(operation, prop, value); 456 } 457 458 case PRESENT : 459 { 460 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 461 Debug.println("PRESENT(" + attr + ")"); } 463 464 Object prop = (properties == null) ? null : properties.get(attr); 465 466 return prop != null; 467 } 468 } 469 470 return false; 471 } 472 473 480 protected static String encodeValue(String value) { 481 boolean encoded = false; 482 int inlen = value.length(); 483 int outlen = inlen << 1; 484 485 char[] output = new char[outlen]; 486 value.getChars(0, inlen, output, inlen); 487 488 int cursor = 0; 489 for (int i = inlen; i < outlen; i++) { 490 char c = output[i]; 491 492 switch (c) { 493 case '(' : 494 case '*' : 495 case ')' : 496 case '\\' : 497 { 498 output[cursor] = '\\'; 499 cursor++; 500 encoded = true; 501 502 break; 503 } 504 } 505 506 output[cursor] = c; 507 cursor++; 508 } 509 510 return encoded ? new String (output, 0, cursor) : value; 511 } 512 513 protected boolean compare(int operation, Object value1, Object value2) { 514 if (value1 == null) { 515 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 516 Debug.println("compare(" + value1 + "," + value2 + ")"); } 518 519 return false; 520 } 521 522 if (value1 instanceof String ) { 523 return compare_String(operation, (String ) value1, value2); 524 } 525 526 Class clazz = value1.getClass(); 527 528 if (clazz.isArray()) { 529 Class type = clazz.getComponentType(); 530 531 if (type.isPrimitive()) { 532 return compare_PrimitiveArray(operation, type, value1, value2); 533 } else { 534 return compare_ObjectArray(operation, (Object []) value1, value2); 535 } 536 } 537 538 if (value1 instanceof Collection ) { 539 return compare_Collection(operation, (Collection ) value1, value2); 540 } 541 542 if (value1 instanceof Integer ) { 543 return compare_Integer(operation, ((Integer ) value1).intValue(), value2); 544 } 545 546 if (value1 instanceof Long ) { 547 return compare_Long(operation, ((Long ) value1).longValue(), value2); 548 } 549 550 if (value1 instanceof Byte ) { 551 return compare_Byte(operation, ((Byte ) value1).byteValue(), value2); 552 } 553 554 if (value1 instanceof Short ) { 555 return compare_Short(operation, ((Short ) value1).shortValue(), value2); 556 } 557 558 if (value1 instanceof Character ) { 559 return compare_Character(operation, ((Character ) value1).charValue(), value2); 560 } 561 562 if (value1 instanceof Float ) { 563 return compare_Float(operation, ((Float ) value1).floatValue(), value2); 564 } 565 566 if (value1 instanceof Double ) { 567 return compare_Double(operation, ((Double ) value1).doubleValue(), value2); 568 } 569 570 if (value1 instanceof Boolean ) { 571 return compare_Boolean(operation, ((Boolean ) value1).booleanValue(), value2); 572 } 573 574 if (value1 instanceof Comparable ) { 575 return compare_Comparable(operation, (Comparable ) value1, value2); 576 } 577 578 return compare_Unknown(operation, value1, value2); } 580 581 protected boolean compare_Collection(int operation, Collection collection, Object value2) { 582 Iterator iterator = collection.iterator(); 583 584 while (iterator.hasNext()) { 585 if (compare(operation, iterator.next(), value2)) { 586 return true; 587 } 588 } 589 590 return false; 591 } 592 593 protected boolean compare_ObjectArray(int operation, Object [] array, Object value2) { 594 int size = array.length; 595 596 for (int i = 0; i < size; i++) { 597 if (compare(operation, array[i], value2)) { 598 return true; 599 } 600 } 601 602 return false; 603 } 604 605 protected boolean compare_PrimitiveArray(int operation, Class type, Object primarray, Object value2) { 606 if (Integer.TYPE.isAssignableFrom(type)) { 607 int[] array = (int[]) primarray; 608 609 int size = array.length; 610 611 for (int i = 0; i < size; i++) { 612 if (compare_Integer(operation, array[i], value2)) { 613 return true; 614 } 615 } 616 617 return false; 618 } 619 620 if (Long.TYPE.isAssignableFrom(type)) { 621 long[] array = (long[]) primarray; 622 623 int size = array.length; 624 625 for (int i = 0; i < size; i++) { 626 if (compare_Long(operation, array[i], value2)) { 627 return true; 628 } 629 } 630 631 return false; 632 } 633 634 if (Byte.TYPE.isAssignableFrom(type)) { 635 byte[] array = (byte[]) primarray; 636 637 int size = array.length; 638 639 for (int i = 0; i < size; i++) { 640 if (compare_Byte(operation, array[i], value2)) { 641 return true; 642 } 643 } 644 645 return false; 646 } 647 648 if (Short.TYPE.isAssignableFrom(type)) { 649 short[] array = (short[]) primarray; 650 651 int size = array.length; 652 653 for (int i = 0; i < size; i++) { 654 if (compare_Short(operation, array[i], value2)) { 655 return true; 656 } 657 } 658 659 return false; 660 } 661 662 if (Character.TYPE.isAssignableFrom(type)) { 663 char[] array = (char[]) primarray; 664 665 int size = array.length; 666 667 for (int i = 0; i < size; i++) { 668 if (compare_Character(operation, array[i], value2)) { 669 return true; 670 } 671 } 672 673 return false; 674 } 675 676 if (Float.TYPE.isAssignableFrom(type)) { 677 float[] array = (float[]) primarray; 678 679 int size = array.length; 680 681 for (int i = 0; i < size; i++) { 682 if (compare_Float(operation, array[i], value2)) { 683 return true; 684 } 685 } 686 687 return false; 688 } 689 690 if (Double.TYPE.isAssignableFrom(type)) { 691 double[] array = (double[]) primarray; 692 693 int size = array.length; 694 695 for (int i = 0; i < size; i++) { 696 if (compare_Double(operation, array[i], value2)) { 697 return true; 698 } 699 } 700 701 return false; 702 } 703 704 if (Boolean.TYPE.isAssignableFrom(type)) { 705 boolean[] array = (boolean[]) primarray; 706 707 int size = array.length; 708 709 for (int i = 0; i < size; i++) { 710 if (compare_Boolean(operation, array[i], value2)) { 711 return true; 712 } 713 } 714 715 return false; 716 } 717 718 return false; 719 } 720 721 protected boolean compare_String(int operation, String string, Object value2) { 722 switch (operation) { 723 case SUBSTRING : 724 { 725 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 726 Debug.println("SUBSTRING(" + string + "," + value2 + ")"); } 728 729 String [] substrings = (String []) value2; 730 int pos = 0; 731 732 int size = substrings.length; 733 734 for (int i = 0; i < size; i++) { 735 String substr = substrings[i]; 736 737 if (i + 1 < size) { 738 if (substr == null) { 739 String substr2 = substrings[i + 1]; 740 741 if (substr2 == null) 742 continue; 743 744 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 745 Debug.println("indexOf(\"" + substr2 + "\"," + pos + ")"); } 747 int index = string.indexOf(substr2, pos); 748 if (index == -1) { 749 return false; 750 } 751 752 pos = index + substr2.length(); 753 if (i + 2 < size) i++; 755 } else { 756 int len = substr.length(); 757 758 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 759 Debug.println("regionMatches(" + pos + ",\"" + substr + "\")"); } 761 if (string.regionMatches(pos, substr, 0, len)) { 762 pos += len; 763 } else { 764 return false; 765 } 766 } 767 } else { 768 if (substr == null) { 769 return true; 770 } else { 771 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 772 Debug.println("regionMatches(" + pos + "," + substr + ")"); } 774 return string.endsWith(substr); 775 } 776 } 777 } 778 779 return true; 780 } 781 case EQUAL : 782 { 783 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 784 Debug.println("EQUAL(" + string + "," + value2 + ")"); } 786 return string.equals(value2); 787 } 788 case APPROX : 789 { 790 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 791 Debug.println("APPROX(" + string + "," + value2 + ")"); } 793 794 string = approxString(string); 795 String string2 = approxString((String ) value2); 796 797 return string.equalsIgnoreCase(string2); 798 } 799 case GREATER : 800 { 801 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 802 Debug.println("GREATER(" + string + "," + value2 + ")"); } 804 return string.compareTo((String ) value2) >= 0; 805 } 806 case LESS : 807 { 808 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 809 Debug.println("LESS(" + string + "," + value2 + ")"); } 811 return string.compareTo((String ) value2) <= 0; 812 } 813 } 814 815 return false; 816 } 817 818 protected boolean compare_Integer(int operation, int intval, Object value2) { 819 int intval2 = Integer.parseInt(((String ) value2).trim()); 820 821 switch (operation) { 822 case SUBSTRING : 823 { 824 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 825 Debug.println("SUBSTRING(" + intval + "," + value2 + ")"); } 827 return false; 828 } 829 case EQUAL : 830 { 831 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 832 Debug.println("EQUAL(" + intval + "," + value2 + ")"); } 834 return intval == intval2; 835 } 836 case APPROX : 837 { 838 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 839 Debug.println("APPROX(" + intval + "," + value2 + ")"); } 841 return intval == intval2; 842 } 843 case GREATER : 844 { 845 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 846 Debug.println("GREATER(" + intval + "," + value2 + ")"); } 848 return intval >= intval2; 849 } 850 case LESS : 851 { 852 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 853 Debug.println("LESS(" + intval + "," + value2 + ")"); } 855 return intval <= intval2; 856 } 857 } 858 859 return false; 860 } 861 862 protected boolean compare_Long(int operation, long longval, Object value2) { 863 long longval2 = Long.parseLong(((String ) value2).trim()); 864 865 switch (operation) { 866 case SUBSTRING : 867 { 868 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 869 Debug.println("SUBSTRING(" + longval + "," + value2 + ")"); } 871 return false; 872 } 873 case EQUAL : 874 { 875 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 876 Debug.println("EQUAL(" + longval + "," + value2 + ")"); } 878 return longval == longval2; 879 } 880 case APPROX : 881 { 882 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 883 Debug.println("APPROX(" + longval + "," + value2 + ")"); } 885 return longval == longval2; 886 } 887 case GREATER : 888 { 889 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 890 Debug.println("GREATER(" + longval + "," + value2 + ")"); } 892 return longval >= longval2; 893 } 894 case LESS : 895 { 896 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 897 Debug.println("LESS(" + longval + "," + value2 + ")"); } 899 return longval <= longval2; 900 } 901 } 902 903 return false; 904 } 905 906 protected boolean compare_Byte(int operation, byte byteval, Object value2) { 907 byte byteval2 = Byte.parseByte(((String ) value2).trim()); 908 909 switch (operation) { 910 case SUBSTRING : 911 { 912 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 913 Debug.println("SUBSTRING(" + byteval + "," + value2 + ")"); } 915 return false; 916 } 917 case EQUAL : 918 { 919 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 920 Debug.println("EQUAL(" + byteval + "," + value2 + ")"); } 922 return byteval == byteval2; 923 } 924 case APPROX : 925 { 926 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 927 Debug.println("APPROX(" + byteval + "," + value2 + ")"); } 929 return byteval == byteval2; 930 } 931 case GREATER : 932 { 933 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 934 Debug.println("GREATER(" + byteval + "," + value2 + ")"); } 936 return byteval >= byteval2; 937 } 938 case LESS : 939 { 940 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 941 Debug.println("LESS(" + byteval + "," + value2 + ")"); } 943 return byteval <= byteval2; 944 } 945 } 946 947 return false; 948 } 949 950 protected boolean compare_Short(int operation, short shortval, Object value2) { 951 short shortval2 = Short.parseShort(((String ) value2).trim()); 952 953 switch (operation) { 954 case SUBSTRING : 955 { 956 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 957 Debug.println("SUBSTRING(" + shortval + "," + value2 + ")"); } 959 return false; 960 } 961 case EQUAL : 962 { 963 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 964 Debug.println("EQUAL(" + shortval + "," + value2 + ")"); } 966 return shortval == shortval2; 967 } 968 case APPROX : 969 { 970 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 971 Debug.println("APPROX(" + shortval + "," + value2 + ")"); } 973 return shortval == shortval2; 974 } 975 case GREATER : 976 { 977 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 978 Debug.println("GREATER(" + shortval + "," + value2 + ")"); } 980 return shortval >= shortval2; 981 } 982 case LESS : 983 { 984 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 985 Debug.println("LESS(" + shortval + "," + value2 + ")"); } 987 return shortval <= shortval2; 988 } 989 } 990 991 return false; 992 } 993 994 protected boolean compare_Character(int operation, char charval, Object value2) { 995 char charval2 = (((String ) value2).trim()).charAt(0); 996 997 switch (operation) { 998 case SUBSTRING : 999 { 1000 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1001 Debug.println("SUBSTRING(" + charval + "," + value2 + ")"); } 1003 return false; 1004 } 1005 case EQUAL : 1006 { 1007 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1008 Debug.println("EQUAL(" + charval + "," + value2 + ")"); } 1010 return charval == charval2; 1011 } 1012 case APPROX : 1013 { 1014 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1015 Debug.println("APPROX(" + charval + "," + value2 + ")"); } 1017 return Character.toLowerCase(charval) == Character.toLowerCase(charval2); 1018 } 1019 case GREATER : 1020 { 1021 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1022 Debug.println("GREATER(" + charval + "," + value2 + ")"); } 1024 return charval >= charval2; 1025 } 1026 case LESS : 1027 { 1028 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1029 Debug.println("LESS(" + charval + "," + value2 + ")"); } 1031 return charval <= charval2; 1032 } 1033 } 1034 1035 return false; 1036 } 1037 1038 protected boolean compare_Boolean(int operation, boolean boolval, Object value2) { 1039 boolean boolval2 = new Boolean (((String ) value2).trim()).booleanValue(); 1040 1041 switch (operation) { 1042 case SUBSTRING : 1043 { 1044 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1045 Debug.println("SUBSTRING(" + boolval + "," + value2 + ")"); } 1047 return false; 1048 } 1049 case EQUAL : 1050 { 1051 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1052 Debug.println("EQUAL(" + boolval + "," + value2 + ")"); } 1054 return boolval == boolval2; 1055 } 1056 case APPROX : 1057 { 1058 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1059 Debug.println("APPROX(" + boolval + "," + value2 + ")"); } 1061 return boolval == boolval2; 1062 } 1063 case GREATER : 1064 { 1065 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1066 Debug.println("GREATER(" + boolval + "," + value2 + ")"); } 1068 return boolval == boolval2; 1069 } 1070 case LESS : 1071 { 1072 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1073 Debug.println("LESS(" + boolval + "," + value2 + ")"); } 1075 return boolval == boolval2; 1076 } 1077 } 1078 1079 return false; 1080 } 1081 1082 protected boolean compare_Float(int operation, float floatval, Object value2) { 1083 float floatval2 = Float.parseFloat(((String ) value2).trim()); 1084 1085 switch (operation) { 1086 case SUBSTRING : 1087 { 1088 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1089 Debug.println("SUBSTRING(" + floatval + "," + value2 + ")"); } 1091 return false; 1092 } 1093 case EQUAL : 1094 { 1095 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1096 Debug.println("EQUAL(" + floatval + "," + value2 + ")"); } 1098 return floatval == floatval2; 1099 } 1100 case APPROX : 1101 { 1102 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1103 Debug.println("APPROX(" + floatval + "," + value2 + ")"); } 1105 return floatval == floatval2; 1106 } 1107 case GREATER : 1108 { 1109 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1110 Debug.println("GREATER(" + floatval + "," + value2 + ")"); } 1112 return floatval >= floatval2; 1113 } 1114 case LESS : 1115 { 1116 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1117 Debug.println("LESS(" + floatval + "," + value2 + ")"); } 1119 return floatval <= floatval2; 1120 } 1121 } 1122 1123 return false; 1124 } 1125 1126 protected boolean compare_Double(int operation, double doubleval, Object value2) { 1127 double doubleval2 = Double.parseDouble(((String ) value2).trim()); 1128 1129 switch (operation) { 1130 case SUBSTRING : 1131 { 1132 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1133 Debug.println("SUBSTRING(" + doubleval + "," + value2 + ")"); } 1135 return false; 1136 } 1137 case EQUAL : 1138 { 1139 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1140 Debug.println("EQUAL(" + doubleval + "," + value2 + ")"); } 1142 return doubleval == doubleval2; 1143 } 1144 case APPROX : 1145 { 1146 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1147 Debug.println("APPROX(" + doubleval + "," + value2 + ")"); } 1149 return doubleval == doubleval2; 1150 } 1151 case GREATER : 1152 { 1153 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1154 Debug.println("GREATER(" + doubleval + "," + value2 + ")"); } 1156 return doubleval >= doubleval2; 1157 } 1158 case LESS : 1159 { 1160 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1161 Debug.println("LESS(" + doubleval + "," + value2 + ")"); } 1163 return doubleval <= doubleval2; 1164 } 1165 } 1166 1167 return false; 1168 } 1169 1170 protected static final Class [] constructorType = new Class [] {String .class}; 1171 1172 protected boolean compare_Comparable(int operation, Comparable value1, Object value2) { 1173 Constructor constructor; 1174 1175 try { 1176 constructor = value1.getClass().getConstructor(constructorType); 1177 } 1178 catch (NoSuchMethodException e) { 1179 return false; 1180 } 1181 try { 1182 if (!constructor.isAccessible()) 1183 AccessController.doPrivileged(new SetAccessibleAction(constructor)); 1184 value2 = constructor.newInstance(new Object [] {((String ) value2).trim()}); 1185 } 1186 catch (IllegalAccessException e) { 1187 return false; 1188 } 1189 catch (InvocationTargetException e) { 1190 return false; 1191 } 1192 catch (InstantiationException e) { 1193 return false; 1194 } 1195 1196 switch (operation) { 1197 case SUBSTRING : 1198 { 1199 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1200 Debug.println("SUBSTRING(" + value1 + "," + value2 + ")"); } 1202 return false; 1203 } 1204 case EQUAL : 1205 { 1206 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1207 Debug.println("EQUAL(" + value1 + "," + value2 + ")"); } 1209 return value1.compareTo(value2) == 0; 1210 } 1211 case APPROX : 1212 { 1213 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1214 Debug.println("APPROX(" + value1 + "," + value2 + ")"); } 1216 return value1.compareTo(value2) == 0; 1217 } 1218 case GREATER : 1219 { 1220 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1221 Debug.println("GREATER(" + value1 + "," + value2 + ")"); } 1223 return value1.compareTo(value2) >= 0; 1224 } 1225 case LESS : 1226 { 1227 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1228 Debug.println("LESS(" + value1 + "," + value2 + ")"); } 1230 return value1.compareTo(value2) <= 0; 1231 } 1232 } 1233 1234 return false; 1235 } 1236 1237 protected boolean compare_Unknown(int operation, Object value1, Object value2) { Constructor constructor; 1239 try { 1240 constructor = value1.getClass().getConstructor(constructorType); 1241 } 1242 catch (NoSuchMethodException e) { 1243 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1244 Debug.println("Type not supported"); } 1246 return false; 1247 } 1248 try { 1249 if (!constructor.isAccessible()) 1250 AccessController.doPrivileged(new SetAccessibleAction(constructor)); 1251 value2 = constructor.newInstance(new Object [] {((String ) value2).trim()}); 1252 } 1253 catch (IllegalAccessException e) { 1254 return false; 1255 } 1256 catch (InvocationTargetException e) { 1257 return false; 1258 } 1259 catch (InstantiationException e) { 1260 return false; 1261 } 1262 1263 switch (operation) { 1264 case SUBSTRING : 1265 { 1266 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1267 Debug.println("SUBSTRING(" + value1 + "," + value2 + ")"); } 1269 return false; 1270 } 1271 case EQUAL : 1272 { 1273 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1274 Debug.println("EQUAL(" + value1 + "," + value2 + ")"); } 1276 return value1.equals(value2); 1277 } 1278 case APPROX : 1279 { 1280 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1281 Debug.println("APPROX(" + value1 + "," + value2 + ")"); } 1283 return value1.equals(value2); 1284 } 1285 case GREATER : 1286 { 1287 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1288 Debug.println("GREATER(" + value1 + "," + value2 + ")"); } 1290 return value1.equals(value2); 1291 } 1292 case LESS : 1293 { 1294 if (Debug.DEBUG && Debug.DEBUG_FILTER) { 1295 Debug.println("LESS(" + value1 + "," + value2 + ")"); } 1297 return value1.equals(value2); 1298 } 1299 } 1300 1301 return false; 1302 } 1303 1304 1314 protected static String approxString(String input) { 1315 boolean changed = false; 1316 char[] output = input.toCharArray(); 1317 1318 int length = output.length; 1319 1320 int cursor = 0; 1321 for (int i = 0; i < length; i++) { 1322 char c = output[i]; 1323 1324 if (Character.isWhitespace(c)) { 1325 changed = true; 1326 continue; 1327 } 1328 1329 output[cursor] = c; 1330 cursor++; 1331 } 1332 1333 return changed ? new String (output, 0, cursor) : input; 1334 } 1335 1336 1341 public String getRequiredObjectClass() { 1342 switch (operation) { 1349 case EQUAL : 1350 if (attr.equalsIgnoreCase(org.osgi.framework.Constants.OBJECTCLASS) && (value instanceof String )) 1351 return (String ) value; 1352 break; 1353 case AND : 1354 FilterImpl[] clauses = (FilterImpl[]) value; 1355 for (int i = 0; i < clauses.length; i++) 1356 if (clauses[i].operation == EQUAL) { 1357 String result = clauses[i].getRequiredObjectClass(); 1358 if (result != null) 1359 return result; 1360 } 1361 break; 1362 } 1363 return null; 1364 } 1365 1366 1370 public static String getObjectClassFilterString(String objectClass) { 1371 if (objectClass == null) 1372 return null; 1373 return "(" + org.osgi.framework.Constants.OBJECTCLASS + "=" + objectClass + ")"; } 1375 1376 1380 public String [] getAttributes() { 1381 ArrayList results = new ArrayList (); 1382 getAttributesInternal(results); 1383 return (String []) results.toArray(new String [results.size()]); 1384 } 1385 1386 private void getAttributesInternal(ArrayList results) { 1387 if (value instanceof FilterImpl[]) { 1388 FilterImpl[] children = (FilterImpl[]) value; 1389 for (int i = 0; i < children.length; i++) 1390 children[i].getAttributesInternal(results); 1391 return; 1392 } else if (value instanceof FilterImpl) { 1393 ((FilterImpl) value).getAttributesInternal(results); 1395 return; 1396 } 1397 if (attr != null) 1398 results.add(attr); 1399 } 1400 1401 1406 static class Parser { 1407 protected String filterstring; 1408 protected char[] filter; 1409 protected int pos; 1410 1411 protected Parser(String filterstring) { 1412 this.filterstring = filterstring; 1413 filter = filterstring.toCharArray(); 1414 pos = 0; 1415 } 1416 1417 protected void parse(FilterImpl parent) throws InvalidSyntaxException { 1418 try { 1419 parse_filter(parent); 1420 } catch (ArrayIndexOutOfBoundsException e) { 1421 throw new InvalidSyntaxException(Msg.FILTER_TERMINATED_ABRUBTLY, filterstring); 1422 } 1423 1424 if (pos != filter.length) { 1425 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_TRAILING_CHARACTERS, String.valueOf(pos)), filterstring); 1426 } 1427 } 1428 1429 protected void parse_filter(FilterImpl parent) throws InvalidSyntaxException { 1430 skipWhiteSpace(); 1431 1432 if (filter[pos] != '(') { 1433 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_LEFTPAREN, String.valueOf(pos)), filterstring); 1434 } 1435 1436 pos++; 1437 1438 parse_filtercomp(parent); 1439 1440 skipWhiteSpace(); 1441 1442 if (filter[pos] != ')') { 1443 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_RIGHTPAREN, String.valueOf(pos)), filterstring); 1444 } 1445 1446 pos++; 1447 1448 skipWhiteSpace(); 1449 } 1450 1451 protected void parse_filtercomp(FilterImpl parent) throws InvalidSyntaxException { 1452 skipWhiteSpace(); 1453 1454 char c = filter[pos]; 1455 1456 switch (c) { 1457 case '&' : 1458 { 1459 pos++; 1460 parse_and(parent); 1461 break; 1462 } 1463 case '|' : 1464 { 1465 pos++; 1466 parse_or(parent); 1467 break; 1468 } 1469 case '!' : 1470 { 1471 pos++; 1472 parse_not(parent); 1473 break; 1474 } 1475 default : 1476 { 1477 parse_item(parent); 1478 break; 1479 } 1480 } 1481 } 1482 1483 protected void parse_and(FilterImpl parent) throws InvalidSyntaxException { 1484 skipWhiteSpace(); 1485 1486 if (filter[pos] != '(') { 1487 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_LEFTPAREN, String.valueOf(pos)), filterstring); 1488 } 1489 1490 ArrayList operands = new ArrayList (10); 1491 1492 while (filter[pos] == '(') { 1493 FilterImpl child = new FilterImpl(); 1494 parse_filter(child); 1495 operands.add(child); 1496 } 1497 1498 int size = operands.size(); 1499 1500 FilterImpl[] children = new FilterImpl[size]; 1501 1502 operands.toArray(children); 1503 1504 parent.setFilter(FilterImpl.AND, null, children); 1505 } 1506 1507 protected void parse_or(FilterImpl parent) throws InvalidSyntaxException { 1508 skipWhiteSpace(); 1509 1510 if (filter[pos] != '(') { 1511 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_LEFTPAREN, String.valueOf(pos)), filterstring); 1512 } 1513 1514 ArrayList operands = new ArrayList (10); 1515 1516 while (filter[pos] == '(') { 1517 FilterImpl child = new FilterImpl(); 1518 parse_filter(child); 1519 operands.add(child); 1520 } 1521 1522 int size = operands.size(); 1523 1524 FilterImpl[] children = new FilterImpl[size]; 1525 1526 operands.toArray(children); 1527 1528 parent.setFilter(FilterImpl.OR, null, children); 1529 } 1530 1531 protected void parse_not(FilterImpl parent) throws InvalidSyntaxException { 1532 skipWhiteSpace(); 1533 1534 if (filter[pos] != '(') { 1535 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_LEFTPAREN, String.valueOf(pos)), filterstring); 1536 } 1537 1538 FilterImpl child = new FilterImpl(); 1539 parse_filter(child); 1540 1541 parent.setFilter(FilterImpl.NOT, null, child); 1542 } 1543 1544 protected void parse_item(FilterImpl parent) throws InvalidSyntaxException { 1545 String attr = parse_attr(); 1546 1547 skipWhiteSpace(); 1548 1549 switch (filter[pos]) { 1550 case '~' : 1551 { 1552 if (filter[pos + 1] == '=') { 1553 pos += 2; 1554 parent.setFilter(FilterImpl.APPROX, attr, parse_value()); 1555 return; 1556 } 1557 break; 1558 } 1559 case '>' : 1560 { 1561 if (filter[pos + 1] == '=') { 1562 pos += 2; 1563 parent.setFilter(FilterImpl.GREATER, attr, parse_value()); 1564 return; 1565 } 1566 break; 1567 } 1568 case '<' : 1569 { 1570 if (filter[pos + 1] == '=') { 1571 pos += 2; 1572 parent.setFilter(FilterImpl.LESS, attr, parse_value()); 1573 return; 1574 } 1575 break; 1576 } 1577 case '=' : 1578 { 1579 if (filter[pos + 1] == '*') { 1580 int oldpos = pos; 1581 pos += 2; 1582 skipWhiteSpace(); 1583 if (filter[pos] == ')') { 1584 parent.setFilter(FilterImpl.PRESENT, attr, null); 1585 return; 1586 } 1587 pos = oldpos; 1588 } 1589 1590 pos++; 1591 Object string = parse_substring(); 1592 1593 if (string instanceof String ) { 1594 parent.setFilter(FilterImpl.EQUAL, attr, string); 1595 } else { 1596 parent.setFilter(FilterImpl.SUBSTRING, attr, string); 1597 } 1598 1599 return; 1600 } 1601 } 1602 1603 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_INVALID_OPERATOR, String.valueOf(pos)), filterstring); 1604 } 1605 1606 protected String parse_attr() throws InvalidSyntaxException { 1607 skipWhiteSpace(); 1608 1609 int begin = pos; 1610 int end = pos; 1611 1612 char c = filter[pos]; 1613 1614 while ("~<>=()".indexOf(c) == -1) { pos++; 1616 1617 if (!Character.isWhitespace(c)) { 1618 end = pos; 1619 } 1620 1621 c = filter[pos]; 1622 } 1623 1624 int length = end - begin; 1625 1626 if (length == 0) { 1627 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_ATTR, String.valueOf(pos)), filterstring); 1628 } 1629 1630 return new String (filter, begin, length); 1631 } 1632 1633 protected String parse_value() throws InvalidSyntaxException { 1634 StringBuffer sb = new StringBuffer (filter.length - pos); 1635 1636 parseloop: while (true) { 1637 char c = filter[pos]; 1638 1639 switch (c) { 1640 case ')' : 1641 { 1642 break parseloop; 1643 } 1644 1645 case '(' : 1646 { 1647 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_INVALID_VALUE, String.valueOf(pos)), filterstring); 1648 } 1649 1650 case '\\' : 1651 { 1652 pos++; 1653 c = filter[pos]; 1654 1655 } 1656 1657 default : 1658 { 1659 sb.append(c); 1660 pos++; 1661 break; 1662 } 1663 } 1664 } 1665 1666 if (sb.length() == 0) { 1667 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_VALUE, String.valueOf(pos)), filterstring); 1668 } 1669 1670 return sb.toString(); 1671 } 1672 1673 protected Object parse_substring() throws InvalidSyntaxException { 1674 StringBuffer sb = new StringBuffer (filter.length - pos); 1675 1676 ArrayList operands = new ArrayList (10); 1677 1678 parseloop: while (true) { 1679 char c = filter[pos]; 1680 1681 switch (c) { 1682 case ')' : 1683 { 1684 if (sb.length() > 0) { 1685 operands.add(sb.toString()); 1686 } 1687 1688 break parseloop; 1689 } 1690 1691 case '(' : 1692 { 1693 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_INVALID_VALUE, String.valueOf(pos)), filterstring); 1694 } 1695 1696 case '*' : 1697 { 1698 if (sb.length() > 0) { 1699 operands.add(sb.toString()); 1700 } 1701 1702 sb.setLength(0); 1703 1704 operands.add(null); 1705 pos++; 1706 1707 break; 1708 } 1709 1710 case '\\' : 1711 { 1712 pos++; 1713 c = filter[pos]; 1714 1715 } 1716 1717 default : 1718 { 1719 sb.append(c); 1720 pos++; 1721 break; 1722 } 1723 } 1724 } 1725 1726 int size = operands.size(); 1727 1728 if (size == 0) { 1729 throw new InvalidSyntaxException(NLS.bind(Msg.FILTER_MISSING_VALUE, String.valueOf(pos)), filterstring); 1730 } 1731 1732 if (size == 1) { 1733 Object single = operands.get(0); 1734 1735 if (single != null) { 1736 return single; 1737 } 1738 } 1739 1740 String [] strings = new String [size]; 1741 1742 operands.toArray(strings); 1743 1744 return strings; 1745 } 1746 1747 protected void skipWhiteSpace() { 1748 int length = filter.length; 1749 1750 while ((pos < length) && Character.isWhitespace(filter[pos])) { 1751 pos++; 1752 } 1753 } 1754 } 1755 1756 static class SetAccessibleAction implements PrivilegedAction { 1757 private Constructor constructor; 1758 public SetAccessibleAction(Constructor constructor) { 1759 this.constructor = constructor; 1760 } 1761 public Object run() { 1762 constructor.setAccessible(true); 1763 return null; 1764 } 1765 } 1766} 1767 | Popular Tags |