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