KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > jayasoft > ivy > event > IvyEventFilter


1 package fr.jayasoft.ivy.event;
2
3 import fr.jayasoft.ivy.filter.AndFilter;
4 import fr.jayasoft.ivy.filter.Filter;
5 import fr.jayasoft.ivy.filter.NoFilter;
6 import fr.jayasoft.ivy.filter.NotFilter;
7 import fr.jayasoft.ivy.filter.OrFilter;
8 import fr.jayasoft.ivy.matcher.ExactPatternMatcher;
9 import fr.jayasoft.ivy.matcher.Matcher;
10 import fr.jayasoft.ivy.matcher.PatternMatcher;
11
12 /**
13  * A filter implementation filtering {@link IvyEvent} based upon
14  * an event name and a filter expression.
15  *
16  * The name will be matched against the event name using the
17  * {@link PatternMatcher} used to construct this object.
18  *
19  * The filter expression is a string describing how the event
20  * should be filtered according to its attributes values.
21  * The matching between the filter values and the event attribute values
22  * is done using the {@link PatternMatcher} used to construct this object.
23  *
24  * Here are some examples:
25  * <table>
26  * <tr><td>expression</td><td>effect</td></tr>
27  * <tr><td>type=zip</td><td>accepts event with a type attribute matching zip</td></tr>
28  * <tr><td>type=zip,jar</td><td>accepts event with a type attribute matching zip or jar</td></tr>
29  * <tr><td>type=src AND ext=zip</td><td>accepts event with a type attribute matching src AND an ext attribute matching zip</td></tr>
30  * <tr><td>type=src OR ext=zip</td><td>accepts event with a type attribute matching src OR an ext attribute matching zip</td></tr>
31  * <tr><td>NOT type=src</td><td>accepts event with a type attribute NOT matching src</td></tr>
32  * </table>
33  *
34  * Combination of these can be used, but no parentheses are supported right now, so only the default priority can be used.
35  * The priority order is this one: AND OR NOT =
36  *
37  * This means that artifact=foo AND ext=zip OR type=src will match event with artifact matching foo AND (ext matching zip OR type matching src)
38  *
39  * @since 1.4
40  * @author Xavier Hanin
41  *
42  */

43 public class IvyEventFilter implements Filter {
44     private static final String JavaDoc NOT = "NOT ";
45     private static final String JavaDoc OR = " OR ";
46     private static final String JavaDoc AND = " AND ";
47     private PatternMatcher _matcher;
48     private Filter _nameFilter;
49     private Filter _attFilter;
50
51     public IvyEventFilter(String JavaDoc event, String JavaDoc filterExpression, PatternMatcher matcher) {
52         _matcher = matcher == null ? ExactPatternMatcher.getInstance() : matcher;
53         if (event == null) {
54             _nameFilter = NoFilter.INSTANCE;
55         } else {
56             final Matcher eventNameMatcher = _matcher.getMatcher(event);
57             _nameFilter = new Filter() {
58                 public boolean accept(Object JavaDoc o) {
59                     IvyEvent e = (IvyEvent) o;
60                     return eventNameMatcher.matches(e.getName());
61                 }
62             };
63         }
64         _attFilter = filterExpression == null || filterExpression.trim().length() == 0 ?
65                 NoFilter.INSTANCE
66                 : parseExpression(filterExpression);
67     }
68
69     private Filter parseExpression(String JavaDoc filterExpression) {
70         // expressions handled for the moment: (informal grammar)
71
// EXP := SIMPLE_EXP | AND_EXP | OR_EXP | NOT_EXP
72
// AND_EXP := EXP && EXP
73
// OR_EXP := EXP || EXP
74
// NOT_EXP := ! EXP
75
// SIMPLE_EXP := attname = comma, separated, list, of, accepted, values
76
// example: organisation = foo && module = bar, baz
77
filterExpression = filterExpression.trim();
78         int index = filterExpression.indexOf(AND);
79         if (index == -1) {
80             index = filterExpression.indexOf(OR);
81             if (index == -1) {
82                 if (filterExpression.startsWith(NOT)) {
83                     return new NotFilter(parseExpression(filterExpression.substring(NOT.length())));
84                 } else {
85                     index = filterExpression.indexOf("=");
86                     if (index == -1) {
87                         throw new IllegalArgumentException JavaDoc("bad filter expression: "+filterExpression+": no equal sign found");
88                     }
89                     final String JavaDoc attname = filterExpression.substring(0, index).trim();
90                     String JavaDoc[] values = filterExpression.substring(index+1).trim().split(",");
91                     final Matcher[] matchers = new Matcher[values.length];
92                     for (int i = 0; i < values.length; i++) {
93                         matchers[i] = _matcher.getMatcher(values[i].trim());
94                     }
95                     return new Filter() {
96                         public boolean accept(Object JavaDoc o) {
97                             IvyEvent e = (IvyEvent) o;
98                             String JavaDoc val = (String JavaDoc) e.getAttributes().get(attname);
99                             if (val == null) {
100                                 return false;
101                             }
102                             for (int i = 0; i < matchers.length; i++) {
103                                 if (matchers[i].matches(val)) {
104                                     return true;
105                                 }
106                             }
107                             return false;
108                         }
109                     };
110                 }
111             } else {
112                 return new OrFilter(parseExpression(filterExpression.substring(0, index)), parseExpression(filterExpression.substring(index+OR.length())));
113             }
114         } else {
115             return new AndFilter(parseExpression(filterExpression.substring(0, index)), parseExpression(filterExpression.substring(index+AND.length())));
116         }
117     }
118
119     public boolean accept(Object JavaDoc o) {
120         if (! (o instanceof IvyEvent)) {
121             return false;
122         }
123         return _nameFilter.accept(o) && _attFilter.accept(o);
124     }
125
126 }
127
Popular Tags