1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.archive.crawler.filter;
25
26 import java.util.Iterator;
27 import java.util.logging.Logger;
28
29 import javax.management.AttributeNotFoundException;
30 import javax.management.InvalidAttributeValueException;
31
32 import org.archive.crawler.datamodel.CrawlURI;
33 import org.archive.crawler.deciderules.DecideRule;
34 import org.archive.crawler.deciderules.DecidingFilter;
35 import org.archive.crawler.framework.Filter;
36 import org.archive.crawler.settings.CrawlerSettings;
37 import org.archive.crawler.settings.MapType;
38 import org.archive.crawler.settings.SimpleType;
39
40 /***
41 * OrFilter allows any number of other filters to be set up
42 * inside it, as <filter> child elements. If any of those
43 * children accept a presented object, the OrFilter will
44 * also accept it.
45 *
46 * @author gojomo
47 * @deprecated As of release 1.10.0. Replaced by {@link DecidingFilter} and
48 * {@link DecideRule}.
49 */
50 public class OrFilter extends Filter {
51
52 private static final long serialVersionUID = -6835737313105835112L;
53
54 private static final Logger logger =
55 Logger.getLogger(OrFilter.class.getName());
56 public static final String ATTR_MATCH_RETURN_VALUE = "if-matches-return";
57 public static final String ATTR_FILTERS = "filters";
58
59 public OrFilter(String name, String description) {
60 this(name);
61 setDescription(description);
62 }
63
64 /***
65 * @param name
66 */
67 public OrFilter(String name) {
68 super(
69 name,
70 "OR Filter *Deprecated* Use DecidingFilter instead. " +
71 "A filter that serves as a placeholder for other" +
72 " filters whose functionality should be logically OR'ed together.");
73
74 addElementToDefinition(
75 new SimpleType(
76 ATTR_MATCH_RETURN_VALUE,
77 "What to return when one of the filters matches. \nIf true, "
78 + "this filter will return true if one of the subfilters "
79 + "return true, false otherwise. If false, this filter "
80 + "will return false if one of the subfilters"
81 + " returns true, false otherwise.",
82 new Boolean(true)));
83
84 addElementToDefinition(new MapType(ATTR_FILTERS,
85 "List of filters whose functionality should be" +
86 " logically or'ed together by the OrFilter.", Filter.class));
87 }
88
89 private MapType getFilters (Object o) {
90 MapType filters = null;
91 try {
92 filters = (MapType)getAttribute(o, ATTR_FILTERS);
93 } catch (AttributeNotFoundException e) {
94 logger.severe(e.getLocalizedMessage());
95 }
96 return filters;
97 }
98
99 protected boolean innerAccepts(Object o) {
100 if (isEmpty(o)) {
101 return true;
102 }
103 for (Iterator iter = iterator(o); iter.hasNext();) {
104 Filter f = (Filter)iter.next();
105 if (f.accepts(o)) {
106 return true;
107 }
108 }
109 return false;
110 }
111
112 public void addFilter(CrawlerSettings settings, Filter f) {
113 try {
114 getFilters(settings).addElement(settings, f);
115 } catch (InvalidAttributeValueException e) {
116 logger.severe(e.getMessage());
117 }
118 }
119
120 public boolean isEmpty(Object o) {
121 return getFilters(o).isEmpty(o);
122 }
123
124 public Iterator iterator(Object o) {
125 return getFilters(o).iterator(o);
126 }
127
128 protected boolean returnTrueIfMatches(CrawlURI curi) {
129 try {
130 return ((Boolean) getAttribute(ATTR_MATCH_RETURN_VALUE, curi)).
131 booleanValue();
132 } catch (AttributeNotFoundException e) {
133 logger.severe(e.getMessage());
134 return true;
135 }
136 }
137
138 /***
139 * Note that configuration updates may be necessary. Pass to
140 * constituent filters.
141 */
142 public void kickUpdate() {
143
144
145 Iterator iter = iterator(null);
146 while(iter.hasNext()) {
147 ((Filter)iter.next()).kickUpdate();
148 }
149 }
150 }