View Javadoc

1   /**
2    * This file Copyright (c) 2008-2012 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.rssaggregator.importhandler;
35  
36  import static java.lang.String.format;
37  
38  import info.magnolia.module.rssaggregator.util.Assert;
39  
40  import java.util.Collection;
41  import java.util.Set;
42  
43  import org.apache.commons.beanutils.MethodUtils;
44  import org.apache.commons.lang.StringUtils;
45  import org.slf4j.Logger;
46  import org.slf4j.LoggerFactory;
47  
48  import com.sun.syndication.feed.synd.SyndEntry;
49  
50  /**
51   * A set of {@link FilterPredicate filter predicates} defined for an {@link AggregateFeed}. Entries in a {@link
52   * FeedChannel} must be {@link #include(SyndEntry) included} by this filter before they will actually be imported.
53   *
54   * @author had
55   * @author Rob van der Linden Vooren
56   */
57  public class AggregateFilter {
58  
59      private Logger log = LoggerFactory.getLogger(AggregateFilter.class);
60  
61      private final Set<FilterPredicate> filters;
62  
63      public AggregateFilter(Set<FilterPredicate> filters) {
64          Assert.notNull(filters, "'filters' must not be null");
65          this.filters = filters;
66      }
67  
68      public boolean include(SyndEntry entry) {
69          boolean include = true;
70          for (FilterPredicate filter : filters) {
71              String property = filter.getProperty();
72              String getterName = format("get%s", property);
73              Object propertyValue = StringUtils.EMPTY;
74              try {
75                  propertyValue = MethodUtils.invokeExactMethod(entry, getterName, null);
76              } catch (Exception e) {
77                  if (log.isWarnEnabled()) {
78                      log.warn("Property {} can't be retrieved from the feed and will be treated as if it was empty during filtering", property);
79                  }
80              }
81              String regularExpression = filter.getRegularExpression();
82              FilterPredicate.Condition condition = filter.getCondition();
83              if (propertyValue instanceof Collection) {
84                  Collection<Object> propertyValues = (Collection<Object>) propertyValue;
85                  boolean hit = getHit(propertyValues, regularExpression);
86                  switch (condition) {
87                      case AND:
88                          include &= hit;
89                          break;
90                      case OR:
91                          include |= hit;
92                          break;
93                      case NOT:
94                          include &= !hit;
95                          break;
96                  }
97              } else {
98                  if (propertyValue != null) {
99                      include = matchSingleString(include, propertyValue.toString(), regularExpression, condition);
100                 } else {
101                     include = false;
102                 }
103             }
104         }
105         return include;
106     }
107 
108     //  Helper methods
109 
110     protected boolean getHit(Collection<Object> propertyValues, String regularExpression) {
111         boolean hit = false;
112         for (Object valueWrapper : propertyValues) {
113             String single;
114             try {
115                 single = (String) MethodUtils.invokeExactMethod(valueWrapper, "getName", null);
116             } catch (Exception e) {
117                 single = valueWrapper.toString();
118             }
119             if (single.matches(regularExpression)) {
120                 hit = true;
121                 break;
122             }
123         }
124         return hit;
125     }
126 
127     protected boolean matchSingleString(boolean passed, String propValue, String regularExpression, FilterPredicate.Condition condition) {
128         switch (condition) {
129             case AND:
130                 passed &= propValue.matches(regularExpression);
131                 break;
132             case OR:
133                 passed |= propValue.matches(regularExpression);
134                 break;
135             case NOT:
136                 passed &= !propValue.matches(regularExpression);
137                 break;
138         }
139         return passed;
140     }
141 
142     protected boolean contains(FilterPredicate filterPredicate) {
143         return filters.contains(filterPredicate);
144     }
145 }