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 | 0 | private final KeyedPoolableObjectFactory _factory = new RegexpPoolObjectFactory(); |
42 | |
|
43 | |
private final GenericKeyedObjectPool _pool; |
44 | |
|
45 | 0 | private Map _escapedPatternStrings = new HashMap(); |
46 | |
|
47 | |
public RegexpMatcher() |
48 | 0 | { |
49 | 0 | _pool = new GenericKeyedObjectPool(_factory); |
50 | |
|
51 | 0 | _pool.setMinEvictableIdleTimeMillis(EVICT_IDLE_TIME); |
52 | 0 | _pool.setTimeBetweenEvictionRunsMillis(SLEEP_TIME); |
53 | 0 | } |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
public void clear() |
59 | |
{ |
60 | 0 | _pool.clear(); |
61 | 0 | } |
62 | |
|
63 | |
public boolean matches(String pattern, String input) |
64 | |
{ |
65 | 0 | Pattern compiled = null; |
66 | |
|
67 | |
try { |
68 | |
|
69 | 0 | compiled = (Pattern)_pool.borrowObject(pattern); |
70 | |
|
71 | 0 | return compiled.matcher(input).matches(); |
72 | |
|
73 | 0 | } catch (Exception e) { |
74 | |
|
75 | 0 | throw new ApplicationRuntimeException(e); |
76 | |
} finally { |
77 | |
|
78 | 0 | try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { } |
79 | |
} |
80 | |
} |
81 | |
|
82 | |
public boolean contains(String pattern, String input) |
83 | |
{ |
84 | 0 | Pattern compiled = null; |
85 | |
|
86 | |
try { |
87 | |
|
88 | 0 | compiled = (Pattern)_pool.borrowObject(pattern); |
89 | |
|
90 | 0 | return compiled.matcher(input).find(); |
91 | |
|
92 | 0 | } catch (Exception e) { |
93 | |
|
94 | 0 | throw new ApplicationRuntimeException(e); |
95 | |
} finally { |
96 | |
|
97 | 0 | try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { } |
98 | |
} |
99 | |
} |
100 | |
|
101 | |
public String getEscapedPatternString(String pattern) |
102 | |
{ |
103 | 0 | String result = (String) _escapedPatternStrings.get(pattern); |
104 | |
|
105 | 0 | if (result == null) |
106 | |
{ |
107 | 0 | result = Perl5Compiler.quotemeta(pattern); |
108 | |
|
109 | 0 | _escapedPatternStrings.put(pattern, result); |
110 | |
} |
111 | |
|
112 | 0 | return result; |
113 | |
} |
114 | |
|
115 | |
|
116 | |
|
117 | |
|
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
public RegexpMatch[] getMatches(String pattern, String input) |
126 | |
{ |
127 | 0 | Pattern compiled = null; |
128 | |
|
129 | |
try { |
130 | |
|
131 | 0 | compiled = (Pattern)_pool.borrowObject(pattern); |
132 | |
|
133 | 0 | Matcher matcher = compiled.matcher(input); |
134 | 0 | List matches = new ArrayList(); |
135 | |
|
136 | 0 | while (matcher.find()) |
137 | |
{ |
138 | 0 | int length = matcher.groupCount(); |
139 | 0 | String[] groups = new String[length + 1]; |
140 | 0 | groups[0] = matcher.group(); |
141 | |
|
142 | 0 | for (int i=1; i <= length; i++) { |
143 | 0 | groups[i] = matcher.group(i); |
144 | |
} |
145 | |
|
146 | 0 | matches.add(new RegexpMatch(length, groups)); |
147 | 0 | } |
148 | |
|
149 | 0 | 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 | 0 | 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 | 0 | Pattern compiled = null; |
174 | |
|
175 | |
try { |
176 | |
|
177 | 0 | compiled = (Pattern)_pool.borrowObject(pattern); |
178 | |
|
179 | 0 | Matcher matcher = compiled.matcher(input); |
180 | 0 | List matches = new ArrayList(); |
181 | |
|
182 | 0 | while (matcher.find()) |
183 | |
{ |
184 | 0 | String matchedInput = matcher.group(subgroup); |
185 | |
|
186 | 0 | matches.add(matchedInput); |
187 | 0 | } |
188 | |
|
189 | 0 | 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 | 0 | try { _pool.returnObject(pattern, compiled); } catch (Throwable t) { } |
197 | |
} |
198 | |
} |
199 | |
|
200 | |
} |