1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.module.rssaggregator.importhandler;
35
36 import java.util.Set;
37
38 import org.apache.commons.beanutils.MethodUtils;
39 import org.apache.commons.lang3.StringUtils;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42
43 import com.rometools.rome.feed.synd.SyndEntry;
44
45
46
47
48 public class PlanetFilter {
49
50 private static final Logger log = LoggerFactory.getLogger(PlanetFilter.class);
51 private static final String METHOD_PREFIX = "get";
52 private final Set<FilterPredicate> filters;
53
54 public PlanetFilter(Set<FilterPredicate> filters) {
55 if (filters == null) {
56 throw new IllegalArgumentException("'filters' must not be null");
57 }
58 this.filters = filters;
59 }
60
61 public boolean include(SyndEntry entry) {
62 boolean includeAnd = includeByCondition(entry, FilterPredicate.Condition.AND);
63 boolean includeOr = includeByCondition(entry, FilterPredicate.Condition.OR);
64 boolean includeNot = includeByCondition(entry, FilterPredicate.Condition.NOT);
65
66 return includeAnd && includeOr && includeNot;
67 }
68
69 boolean includeByCondition(SyndEntry entry, FilterPredicate.Condition matchingCondition) {
70 boolean include = false;
71 boolean conditionFound = false;
72
73 for (FilterPredicate filter : filters) {
74 Object propertyValue = getPropertyValue(entry, filter.getProperty());
75 FilterPredicate.Condition condition = filter.getCondition();
76
77 if (propertyValue != null && condition.equals(matchingCondition)) {
78 boolean hit;
79
80 hit = matchSingleString(propertyValue.toString(), filter.getRegularExpression());
81
82 switch (condition) {
83 case AND:
84 if (hit && !conditionFound) {
85 include = true;
86 } else {
87 include &= hit;
88 }
89 conditionFound = true;
90 break;
91 case OR:
92 if (hit) {
93 include = true;
94 }
95 conditionFound = true;
96 break;
97 case NOT:
98 if (hit) {
99 include = false;
100 } else {
101 if (!conditionFound) {
102 include = true;
103 }
104 }
105 conditionFound = true;
106 break;
107 }
108 }
109 }
110 if (!conditionFound) {
111 include = true;
112 }
113 return include;
114 }
115
116 protected boolean matchSingleString(String propertyValue, String regularExpression) {
117
118 propertyValue = StringUtils.remove(propertyValue, "\n");
119
120 return propertyValue.matches(regularExpression);
121 }
122
123 Object getPropertyValue(SyndEntry entry, String property) {
124 Object propertyValue = StringUtils.EMPTY;
125
126 String getterName = getCamelCaseProperty(property);
127
128 try {
129 propertyValue = MethodUtils.invokeExactMethod(entry, getterName, null);
130 } catch (Exception e) {
131 if (log.isWarnEnabled()) {
132 log.warn("Property {} can't be retrieved from the feed and will be treated as if it was empty during filtering", property);
133 }
134 }
135
136 return propertyValue;
137 }
138
139 protected String getCamelCaseProperty(String prop) {
140 if (StringUtils.isBlank(prop)) {
141 return "";
142 }
143 return METHOD_PREFIX + StringUtils.upperCase(StringUtils.substring(prop, 0, 1)) +
144 StringUtils.lowerCase((StringUtils.substring(prop, 1, prop.length())));
145 }
146 }