1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package net.sf.jour.filter;
22
23 import java.util.regex.Pattern;
24
25 import net.sf.jour.log.Logger;
26
27
28
29
30 public abstract class BasicFilter extends MatchFilter {
31
32 protected static final Logger log = Logger.getLogger();
33
34 public static Pattern getGlobPattern(String globPattern) {
35 char[] gPat = globPattern.toCharArray();
36 char[] rPat = new char[gPat.length * 2 + 2];
37 boolean inBrackets = false;
38
39 int j = 0;
40
41
42
43 rPat[j++] = '^';
44
45 for (int i = 0; i < gPat.length; i++) {
46 switch (gPat[i]) {
47 case '*':
48
49 if (!inBrackets) {
50 rPat[j++] = '.';
51 }
52
53 rPat[j++] = '*';
54
55 break;
56
57 case '?':
58 rPat[j++] = inBrackets ? '?' : '.';
59
60 break;
61
62 case '[':
63 inBrackets = true;
64 rPat[j++] = gPat[i];
65
66 if (i < (gPat.length - 1)) {
67 switch (gPat[i + 1]) {
68 case '!':
69 case '^':
70 rPat[j++] = '^';
71 i++;
72
73 break;
74
75 case ']':
76
77 rPat[j - 1] = '\\';
78 rPat[j++] = '[';
79 rPat[j++] = '\\';
80
81 rPat[j++] = gPat[++i];
82
83 break;
84 }
85 }
86
87 break;
88
89 case ']':
90 rPat[j++] = gPat[i];
91 inBrackets = false;
92
93 break;
94
95 case '\\':
96 rPat[j++] = '\\';
97
98 if ((i < (gPat.length - 1)) && ("*?[]".indexOf(gPat[i + 1]) >= 0)) {
99 rPat[j++] = gPat[++i];
100 } else {
101 rPat[j++] = '\\';
102 }
103
104 break;
105
106 default:
107
108 if (!Character.isLetterOrDigit(gPat[i])) {
109 rPat[j++] = '\\';
110 }
111
112 rPat[j++] = gPat[i];
113
114 break;
115 }
116 }
117
118
119
120 rPat[j++] = '$';
121
122
123
124 return Pattern.compile(new String(rPat, 0, j), Pattern.CASE_INSENSITIVE);
125 }
126
127 protected static boolean accept(Pattern pattern, String str) {
128 if (str == null) {
129 return false;
130 }
131 return pattern.matcher(str).matches();
132 }
133
134 }