|
1 | 1 | <%@ page contentType="text/plain;charset=utf-8"
|
2 | 2 | import="java.io.*,
|
| 3 | + java.text.*, |
3 | 4 | java.util.*,
|
4 | 5 | java.util.regex.*,
|
5 | 6 | org.json.simple.*,
|
|
30 | 31 | options.addAll(Arrays.asList(ArrOption));
|
31 | 32 | }
|
32 | 33 | String[] ArrInput = request.getParameterValues("input");
|
| 34 | + int timeoutMillis = 10 * 1000; |
33 | 35 |
|
34 | 36 | if (Strings.isNullOrEmpty(regex))
|
35 | 37 | {
|
|
164 | 166 | pw.print(StringEscapeUtils.escapeHtml4(test));
|
165 | 167 | pw.println("</td>");
|
166 | 168 |
|
167 |
| - Matcher m = p.matcher(test); |
| 169 | + Matcher m = null; |
| 170 | + boolean matches, lookingAt; |
| 171 | + String replaceFirst, replaceAll; |
168 | 172 |
|
169 |
| - pw.print("\t\t\t<td>"); |
170 |
| - pw.print(StringEscapeUtils.escapeHtml4(booleanToString(m.matches(), loc))); |
171 |
| - pw.println("</td>"); |
172 |
| -
|
173 |
| - m.reset(); |
174 |
| -
|
175 |
| - pw.print("\t\t\t<td>"); |
176 | 173 | try
|
177 | 174 | {
|
178 |
| - pw.print(StringEscapeUtils.escapeHtml4(m.replaceFirst(replacement))); |
| 175 | + m = p.matcher(new TimeoutCharSequence(test, timeoutMillis)); |
| 176 | + matches = m.matches(); |
| 177 | + m.reset(); |
| 178 | + replaceFirst = m.replaceFirst(replacement); |
| 179 | + m.reset(); |
| 180 | + replaceAll = m.replaceAll(replacement); |
| 181 | + m.reset(); |
| 182 | + lookingAt = m.lookingAt(); |
179 | 183 | }
|
180 | 184 | catch (Exception e)
|
181 | 185 | {
|
182 |
| - pw.print("<i>(ERROR: "); |
| 186 | + pw.print("\t\t\t<td class=\"text-error\" colspan=\"6\">"); |
| 187 | + pw.print("ERROR: "); |
183 | 188 | pw.print(StringEscapeUtils.escapeHtml4(e.getMessage()));
|
184 |
| - pw.print(")</i>"); |
| 189 | + pw.println("</td>"); |
| 190 | + pw.println("\t\t</tr>"); |
| 191 | + continue; |
185 | 192 | }
|
186 | 193 |
|
| 194 | + pw.print("\t\t\t<td>"); |
| 195 | + pw.print(StringEscapeUtils.escapeHtml4(booleanToString(matches, loc))); |
187 | 196 | pw.println("</td>");
|
188 | 197 |
|
189 |
| - m.reset(); |
190 | 198 |
|
191 | 199 | pw.print("\t\t\t<td>");
|
192 | 200 | try
|
193 | 201 | {
|
194 |
| - pw.print(StringEscapeUtils.escapeHtml4(m.replaceAll(replacement))); |
| 202 | + pw.print(StringEscapeUtils.escapeHtml4(replaceFirst)); |
195 | 203 | }
|
196 | 204 | catch (Exception e)
|
197 | 205 | {
|
198 | 206 | pw.print("<i>(ERROR: ");
|
199 | 207 | pw.print(StringEscapeUtils.escapeHtml4(e.getMessage()));
|
200 | 208 | pw.print(")</i>");
|
201 | 209 | }
|
| 210 | +
|
202 | 211 | pw.println("</td>");
|
203 | 212 |
|
204 | 213 | m.reset();
|
205 | 214 |
|
206 | 215 | pw.print("\t\t\t<td>");
|
207 |
| - pw.print(StringEscapeUtils.escapeHtml4(booleanToString(m.lookingAt(), loc))); |
| 216 | + pw.print(StringEscapeUtils.escapeHtml4(replaceAll)); |
| 217 | + pw.println("</td>"); |
| 218 | +
|
| 219 | + pw.print("\t\t\t<td>"); |
| 220 | + pw.print(StringEscapeUtils.escapeHtml4(booleanToString(lookingAt, loc))); |
208 | 221 | pw.println("</td>");
|
209 | 222 |
|
210 | 223 | m.reset();
|
|
300 | 313 |
|
301 | 314 | return false;
|
302 | 315 | }
|
| 316 | +
|
| 317 | + class TimeoutCharSequence |
| 318 | + implements CharSequence |
| 319 | + { |
| 320 | + private final CharSequence inner; |
| 321 | + private final int timeoutMillis; |
| 322 | + private final long timeoutTime; |
| 323 | +
|
| 324 | + public TimeoutCharSequence(CharSequence inner, int timeoutMillis) |
| 325 | + { |
| 326 | + super(); |
| 327 | + this.inner = inner; |
| 328 | + this.timeoutMillis = timeoutMillis; |
| 329 | + timeoutTime = System.currentTimeMillis() + timeoutMillis; |
| 330 | + } |
| 331 | +
|
| 332 | + public char charAt(int index) |
| 333 | + { |
| 334 | + if (System.currentTimeMillis() > timeoutTime) |
| 335 | + { |
| 336 | + throw new RuntimeException(MessageFormat.format("Interrupted after {0}ms", timeoutMillis)); |
| 337 | + } |
| 338 | + return inner.charAt(index); |
| 339 | + } |
| 340 | +
|
| 341 | + public int length() |
| 342 | + { |
| 343 | + return inner.length(); |
| 344 | + } |
| 345 | +
|
| 346 | + public CharSequence subSequence(int start, int end) |
| 347 | + { |
| 348 | + return new TimeoutCharSequence(inner.subSequence(start, end), timeoutMillis); |
| 349 | + } |
| 350 | +
|
| 351 | + @Override |
| 352 | + public String toString() |
| 353 | + { |
| 354 | + return inner.toString(); |
| 355 | + } |
| 356 | + } |
303 | 357 | %>
|
304 | 358 |
|
305 | 359 |
|
|
0 commit comments