1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.util; |
16 |
|
|
17 |
|
import org.apache.commons.pool.KeyedPoolableObjectFactory; |
18 |
|
import org.apache.commons.pool.impl.GenericKeyedObjectPool; |
19 |
|
import org.apache.hivemind.ApplicationRuntimeException; |
20 |
|
import org.apache.oro.text.regex.Perl5Compiler; |
21 |
|
|
22 |
|
import java.util.ArrayList; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
import java.util.regex.Matcher; |
27 |
|
import java.util.regex.Pattern; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
public class RegexpMatcher |
36 |
|
{ |
37 |
|
private static final long SLEEP_TIME = 1000 * 60 * 4; |
38 |
|
|
39 |
|
private static final long EVICT_IDLE_TIME = 1000 * 60 * 60; |
40 |
|
|
41 |
123 |
private final KeyedPoolableObjectFactory _factory = new RegexpPoolObjectFactory(); |
42 |
|
|
43 |
|
private final GenericKeyedObjectPool _pool; |
44 |
|
|
45 |
123 |
private Map _escapedPatternStrings = new HashMap(); |
46 |
|
|
47 |
|
public RegexpMatcher() |
48 |
123 |
{ |
49 |
123 |
_pool = new GenericKeyedObjectPool(_factory); |
50 |
|
|
51 |
123 |
_pool.setMinEvictableIdleTimeMillis(EVICT_IDLE_TIME); |
52 |
123 |
_pool.setTimeBetweenEvictionRunsMillis(SLEEP_TIME); |
53 |
123 |
} |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
public void clear() |
59 |
|
{ |
60 |
1 |
_pool.clear(); |
61 |
1 |
} |
62 |
|
|
63 |
|
public boolean matches(String pattern, String input) |
64 |
|
{ |
65 |
190 |
Pattern compiled = null; |
66 |
|
|
67 |
|
try { |
68 |
|
|
69 |
190 |
compiled = (Pattern)_pool.borrowObject(pattern); |
70 |
|
|
71 |
189 |
return compiled.matcher(input).matches(); |
72 |
|
|
73 |
1 |
} catch (Exception e) { |
74 |
|
|
75 |
1 |
throw new ApplicationRuntimeException(e); |
76 |
|
} finally { |
77 |
|
|
78 |
190 |
try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { } |
79 |
|
} |
80 |
|
} |
81 |
|
|
82 |
|
public boolean contains(String pattern, String input) |
83 |
|
{ |
84 |
99 |
Pattern compiled = null; |
85 |
|
|
86 |
|
try { |
87 |
|
|
88 |
99 |
compiled = (Pattern)_pool.borrowObject(pattern); |
89 |
|
|
90 |
98 |
return compiled.matcher(input).find(); |
91 |
|
|
92 |
1 |
} catch (Exception e) { |
93 |
|
|
94 |
1 |
throw new ApplicationRuntimeException(e); |
95 |
|
} finally { |
96 |
|
|
97 |
99 |
try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { } |
98 |
|
} |
99 |
|
} |
100 |
|
|
101 |
|
public String getEscapedPatternString(String pattern) |
102 |
|
{ |
103 |
6 |
String result = (String) _escapedPatternStrings.get(pattern); |
104 |
|
|
105 |
6 |
if (result == null) |
106 |
|
{ |
107 |
6 |
result = Perl5Compiler.quotemeta(pattern); |
108 |
|
|
109 |
6 |
_escapedPatternStrings.put(pattern, result); |
110 |
|
} |
111 |
|
|
112 |
6 |
return result; |
113 |
|
} |
114 |
|
|
115 |
|
|
116 |
|
|
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
|
|
125 |
|
public RegexpMatch[] getMatches(String pattern, String input) |
126 |
|
{ |
127 |
22 |
Pattern compiled = null; |
128 |
|
|
129 |
|
try { |
130 |
|
|
131 |
22 |
compiled = (Pattern)_pool.borrowObject(pattern); |
132 |
|
|
133 |
22 |
Matcher matcher = compiled.matcher(input); |
134 |
22 |
List matches = new ArrayList(); |
135 |
|
|
136 |
45 |
while (matcher.find()) |
137 |
|
{ |
138 |
23 |
int length = matcher.groupCount(); |
139 |
23 |
String[] groups = new String[length + 1]; |
140 |
23 |
groups[0] = matcher.group(); |
141 |
|
|
142 |
109 |
for (int i=1; i <= length; i++) { |
143 |
86 |
groups[i] = matcher.group(i); |
144 |
|
} |
145 |
|
|
146 |
23 |
matches.add(new RegexpMatch(length, groups)); |
147 |
23 |
} |
148 |
|
|
149 |
22 |
return (RegexpMatch[]) matches.toArray(new RegexpMatch[matches.size()]); |
150 |
|
|
151 |
0 |
} catch (Exception e) { |
152 |
|
|
153 |
0 |
throw new ApplicationRuntimeException(e); |
154 |
|
} finally { |
155 |
|
|
156 |
22 |
try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { } |
157 |
|
} |
158 |
|
} |
159 |
|
|
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
public String[] getMatches(String pattern, String input, int subgroup) |
172 |
|
{ |
173 |
3 |
Pattern compiled = null; |
174 |
|
|
175 |
|
try { |
176 |
|
|
177 |
3 |
compiled = (Pattern)_pool.borrowObject(pattern); |
178 |
|
|
179 |
3 |
Matcher matcher = compiled.matcher(input); |
180 |
3 |
List matches = new ArrayList(); |
181 |
|
|
182 |
11 |
while (matcher.find()) |
183 |
|
{ |
184 |
8 |
String matchedInput = matcher.group(subgroup); |
185 |
|
|
186 |
8 |
matches.add(matchedInput); |
187 |
8 |
} |
188 |
|
|
189 |
3 |
return (String[]) matches.toArray(new String[matches.size()]); |
190 |
|
|
191 |
0 |
} catch (Exception e) { |
192 |
|
|
193 |
0 |
throw new ApplicationRuntimeException(e); |
194 |
|
} finally { |
195 |
|
|
196 |
3 |
try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { } |
197 |
|
} |
198 |
|
} |
199 |
|
|
200 |
|
} |