001: /* Copyright (C) 2003 Internet Archive.
002: *
003: * This file is part of the Heritrix web crawler (crawler.archive.org).
004: *
005: * Heritrix is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU Lesser Public License as published by
007: * the Free Software Foundation; either version 2.1 of the License, or
008: * any later version.
009: *
010: * Heritrix is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU Lesser Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser Public License
016: * along with Heritrix; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * OrFilter.java
020: * Created on Nov 13, 2003
021: *
022: * $Header$
023: */
024: package org.archive.crawler.filter;
025:
026: import java.util.Iterator;
027: import java.util.logging.Logger;
028:
029: import javax.management.AttributeNotFoundException;
030: import javax.management.InvalidAttributeValueException;
031:
032: import org.archive.crawler.datamodel.CrawlURI;
033: import org.archive.crawler.deciderules.DecideRule;
034: import org.archive.crawler.deciderules.DecidingFilter;
035: import org.archive.crawler.framework.Filter;
036: import org.archive.crawler.settings.CrawlerSettings;
037: import org.archive.crawler.settings.MapType;
038: import org.archive.crawler.settings.SimpleType;
039:
040: /**
041: * OrFilter allows any number of other filters to be set up
042: * inside it, as <filter> child elements. If any of those
043: * children accept a presented object, the OrFilter will
044: * also accept it.
045: *
046: * @author gojomo
047: * @deprecated As of release 1.10.0. Replaced by {@link DecidingFilter} and
048: * {@link DecideRule}.
049: */
050: public class OrFilter extends Filter {
051:
052: private static final long serialVersionUID = -6835737313105835112L;
053:
054: private static final Logger logger = Logger
055: .getLogger(OrFilter.class.getName());
056: public static final String ATTR_MATCH_RETURN_VALUE = "if-matches-return";
057: public static final String ATTR_FILTERS = "filters";
058:
059: public OrFilter(String name, String description) {
060: this (name);
061: setDescription(description);
062: }
063:
064: /**
065: * @param name
066: */
067: public OrFilter(String name) {
068: super (
069: name,
070: "OR Filter *Deprecated* Use DecidingFilter instead. "
071: + "A filter that serves as a placeholder for other"
072: + " filters whose functionality should be logically OR'ed together.");
073:
074: addElementToDefinition(new SimpleType(
075: ATTR_MATCH_RETURN_VALUE,
076: "What to return when one of the filters matches. \nIf true, "
077: + "this filter will return true if one of the subfilters "
078: + "return true, false otherwise. If false, this filter "
079: + "will return false if one of the subfilters"
080: + " returns true, false otherwise.",
081: new Boolean(true)));
082:
083: addElementToDefinition(new MapType(ATTR_FILTERS,
084: "List of filters whose functionality should be"
085: + " logically or'ed together by the OrFilter.",
086: Filter.class));
087: }
088:
089: private MapType getFilters(Object o) {
090: MapType filters = null;
091: try {
092: filters = (MapType) getAttribute(o, ATTR_FILTERS);
093: } catch (AttributeNotFoundException e) {
094: logger.severe(e.getLocalizedMessage());
095: }
096: return filters;
097: }
098:
099: 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,
131: curi)).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: // TODO: figure out if there's any way to reconcile this with
144: // overrides/refinement filters
145: Iterator iter = iterator(null);
146: while (iter.hasNext()) {
147: ((Filter) iter.next()).kickUpdate();
148: }
149: }
150: }
|