KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jzonic > jlo > filter > RegExFilter


1 /*
2  * RegExFilter.java
3  *
4  * Created on 28. August 2003, 22:09
5  */

6
7 package org.jzonic.jlo.filter;
8
9 import gnu.regexp.RE;
10 import gnu.regexp.REMatch;
11
12 import java.util.Map JavaDoc;
13 /**
14  * This is a filter that uses a regular expression to find out
15  * if the given message and the expression matches.
16  *
17  * @author Andreas Mecky andreasmecky@yahoo.de
18  */

19 public class RegExFilter implements LogFilter {
20     
21     private String JavaDoc expression;
22     
23     public RegExFilter() {
24     }
25     
26     public boolean match(String JavaDoc message) {
27         if ( message != null && expression != null ) {
28             try {
29                 RE re = new RE(expression,RE.REG_ICASE);
30                 REMatch[] matches = re.getAllMatches(message);
31                 if ( matches.length > 0 ) {
32                     return true;
33                 }
34             }
35             catch (Exception JavaDoc e) {
36                 // TODO: do some error reporting
37
}
38         }
39         return false;
40     }
41     
42     public void setParameters(Map JavaDoc parameter) {
43         if ( parameter.containsKey("expression") ) {
44             expression = (String JavaDoc)parameter.get("expression");
45         }
46     }
47     
48 }
49
Popular Tags