1 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 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 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 142 public void kickUpdate() { 143 Iterator iter = iterator(null); 146 while(iter.hasNext()) { 147 ((Filter)iter.next()).kickUpdate(); 148 } 149 } 150 } 151 | Popular Tags |