View Javadoc

    1   /* Copyright (C) 2003 Internet Archive.
    2    *
    3    * This file is part of the Heritrix web crawler (crawler.archive.org).
    4    *
    5    * Heritrix is free software; you can redistribute it and/or modify
    6    * it under the terms of the GNU Lesser Public License as published by
    7    * the Free Software Foundation; either version 2.1 of the License, or
    8    * any later version.
    9    *
    10   * Heritrix is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU Lesser Public License for more details.
    14   *
    15   * You should have received a copy of the GNU Lesser Public License
    16   * along with Heritrix; if not, write to the Free Software
    17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    18   *
    19   * OrFilter.java
    20   * Created on Nov 13, 2003
    21   *
    22   * $Header$
    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         // 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 }