1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.config.expression; |
8 | |
|
9 | |
import org.mule.api.DefaultMuleException; |
10 | |
import org.mule.api.routing.filter.Filter; |
11 | |
import org.mule.routing.filters.logic.AndFilter; |
12 | |
import org.mule.routing.filters.logic.OrFilter; |
13 | |
|
14 | |
import java.util.ArrayList; |
15 | |
import java.util.List; |
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | 0 | public class ExpressionFilterParser |
28 | |
{ |
29 | |
public Filter parseFilterString(String filterString) throws DefaultMuleException |
30 | |
{ |
31 | 0 | List<String> strings = split(filterString); |
32 | 0 | Filter filter = null; |
33 | |
|
34 | 0 | for (String s : strings) |
35 | |
{ |
36 | 0 | s = s.trim(); |
37 | 0 | if (s.equals("AND")) |
38 | |
{ |
39 | 0 | filter = new AndFilter(filter); |
40 | |
} |
41 | 0 | else if (s.equals("OR")) |
42 | |
{ |
43 | 0 | filter = new OrFilter(filter); |
44 | |
} |
45 | 0 | else if (filter instanceof AndFilter) |
46 | |
{ |
47 | 0 | ((AndFilter) filter).getFilters().add(new org.mule.routing.filters.ExpressionFilter(s)); |
48 | |
} |
49 | 0 | else if (filter instanceof OrFilter) |
50 | |
{ |
51 | 0 | ((OrFilter) filter).getFilters().add(new org.mule.routing.filters.ExpressionFilter(s)); |
52 | |
} |
53 | 0 | else if (filter == null) |
54 | |
{ |
55 | 0 | filter = new org.mule.routing.filters.ExpressionFilter(s); |
56 | |
} |
57 | |
else |
58 | |
{ |
59 | 0 | throw new DefaultMuleException("Expression Filter is malformed. IF this is a nested filter make sure each expression is separated by either 'AND' or 'OR'"); |
60 | |
} |
61 | |
} |
62 | 0 | return filter; |
63 | |
} |
64 | |
|
65 | |
protected List<String> split(String string) |
66 | |
{ |
67 | 0 | List<String> strings = new ArrayList<String>(); |
68 | 0 | int i = 0; |
69 | |
|
70 | 0 | while(i> -1) |
71 | |
{ |
72 | 0 | int a = string.indexOf("AND", i); |
73 | 0 | int o = string.indexOf("OR", i); |
74 | 0 | if(a > 1) |
75 | |
{ |
76 | 0 | strings.add(string.substring(0, a)); |
77 | 0 | strings.add("AND"); |
78 | 0 | string = string.substring(a + 4).trim(); |
79 | |
|
80 | |
} |
81 | 0 | else if(o > 1) |
82 | |
{ |
83 | 0 | strings.add(string.substring(0, o)); |
84 | 0 | strings.add("OR"); |
85 | 0 | string = string.substring(o + 3).trim(); |
86 | |
} |
87 | |
else |
88 | |
{ |
89 | 0 | strings.add(string); |
90 | 0 | i = -1; |
91 | |
} |
92 | 0 | } |
93 | 0 | return strings; |
94 | |
} |
95 | |
} |