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
24 import org.apache.log4j.Logger;
25 import org.apache.regexp.RE;
26 import org.apache.regexp.RESyntaxException;
27
28 /***
29 * @author michaellif TODO To change the template for this generated type
30 * comment go to Window - Preferences - Java - Code Style - Code
31 * Templates
32 */
33 public abstract class BasicFilter extends MatchFilter {
34
35 protected static final Logger log = Logger.getLogger(BasicFilter.class);
36
37 /***
38 * DOCUMENT ME!
39 *
40 * @param globPattern DOCUMENT ME!
41 */
42 public static RE getGlobPattern(String globPattern) {
43 char[] gPat = globPattern.toCharArray();
44 char[] rPat = new char[gPat.length * 2 + 2];
45 boolean inBrackets = false;
46 StringBuffer buf = new StringBuffer();
47 int j = 0;
48
49
50
51 rPat[j++] = '^';
52
53 for (int i = 0; i < gPat.length; i++) {
54 switch (gPat[i]) {
55 case '*':
56
57 if (!inBrackets) {
58 rPat[j++] = '.';
59 }
60
61 rPat[j++] = '*';
62
63 break;
64
65 case '?':
66 rPat[j++] = inBrackets ? '?' : '.';
67
68 break;
69
70 case '[':
71 inBrackets = true;
72 rPat[j++] = gPat[i];
73
74 if (i < (gPat.length - 1)) {
75 switch (gPat[i + 1]) {
76 case '!':
77 case '^':
78 rPat[j++] = '^';
79 i++;
80
81 break;
82
83 case ']':
84
85 rPat[j-1] = '//';
86 rPat[j++] = '[';
87 rPat[j++] = '//';
88
89 rPat[j++] = gPat[++i];
90
91 break;
92 }
93 }
94
95 break;
96
97 case ']':
98 rPat[j++] = gPat[i];
99 inBrackets = false;
100
101 break;
102
103 case '//':
104 rPat[j++] = '//';
105
106 if ((i < (gPat.length - 1)) &&
107 ("*?[]".indexOf(gPat[i + 1]) >= 0)) {
108 rPat[j++] = gPat[++i];
109 } else {
110 rPat[j++] = '//';
111 }
112
113 break;
114
115 default:
116
117 if (!Character.isLetterOrDigit(gPat[i])) {
118 rPat[j++] = '//';
119 }
120
121 rPat[j++] = gPat[i];
122
123 break;
124 }
125 }
126
127
128
129 rPat[j++] = '$';
130
131
132
133 try {
134
135
136
137 return new RE(new String(rPat, 0, j),
138 RE.MATCH_CASEINDEPENDENT);
139 } catch (RESyntaxException e) {
140
141 log.error("Regexpr Error in ["
142 + new String(rPat, 0, j)
143 + "] for " + globPattern, e);
144 return new RE("!!!TODO");
145 }
146 }
147
148 /***
149 * DOCUMENT ME!
150 *
151 * @param str DOCUMENT ME!
152 *
153 * @return DOCUMENT ME!
154 */
155 protected static boolean accept(RE pattern, String str) {
156 if (str == null) {
157 return false;
158 }
159
160
161 return pattern.match(str);
162 }
163
164 }