1 package nmp.util;
2
3 /***
4 * Implements facilities to compare a string with the '*' wildcard and common strings.
5 * Wildcard string should have the form
6 * [ * | ** | other char]+
7 *
8 * @author Nuno Preguica
9 */
10 public class WildcardString
11 {
12 private String nameFilter;
13
14 public WildcardString( String s) {
15 nameFilter = s;
16 }
17
18 private static final char wildChar = '*';
19
20 /***
21 * Returns true if str1.substring( from1, len1) could be a compacted
22 * form of str2.substring( from2, len2) (using wildcards)
23 */
24 private static boolean match( String str1, int from1, int len1,
25 String str2, int from2, int len2) {
26 char ch, nextch;
27 for( ; ; ) {
28 if( from1 == len1 && from2 == len2)
29 return true;
30 if( from1 == len1)
31 return false;
32 if( from2 == len2)
33 return len1 - from1 == 1 && str1.charAt( from1) == wildChar;
34
35 ch = str1.charAt( from1);
36 nextch = ' ';
37 if( from1 + 1 < len1)
38 nextch = str1.charAt( from1 + 1);
39 condition:
40 {
41 if( ch == wildChar && nextch != wildChar) {
42 from1++;
43 if( len1 == from1) //wild char e o ultimo caracter
44 return true;
45 ch = str1.charAt( from1);
46 if( ch == wildChar) {
47 break condition;
48 }
49 for( ; from2 < len2 ;from2++ )
50 if( ch == str2.charAt( from2))
51 if( match( str1, from1 + 1, len1, str2, from2 + 1, len2))
52 return true;
53 return false;
54 }
55 }
56 if( ch == wildChar && nextch == wildChar)
57 from1++;
58 if( ch != str2.charAt( from2))
59 return false;
60 from1++;
61 from2++;
62 }
63 }
64
65 /***
66 * Returns true if 1.st string could be a compacted form of
67 * the second (using wildcards)
68 */
69 public static boolean match( String str1, String str2) {
70 return match( str1, 0, str1.length(), str2, 0, str2.length());
71 }
72
73 public boolean match( String name) {
74 return match( nameFilter, name);
75 }
76 }
77
This page was automatically generated by Maven