View Javadoc

    1   /* CrawlJobErrorHandler
    2    *
    3    * $Id: CrawlJobErrorHandler.java 4666 2006-09-26 17:53:28Z paul_jack $
    4    *
    5    * Created on Apr 2, 2004
    6    *
    7    * Copyright (C) 2004 Internet Archive.
    8    *
    9    * This file is part of the Heritrix web crawler (crawler.archive.org).
    10   *
    11   * Heritrix is free software; you can redistribute it and/or modify
    12   * it under the terms of the GNU Lesser Public License as published by
    13   * the Free Software Foundation; either version 2.1 of the License, or
    14   * any later version.
    15   *
    16   * Heritrix is distributed in the hope that it will be useful,
    17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    19   * GNU Lesser Public License for more details.
    20   *
    21   * You should have received a copy of the GNU Lesser Public License
    22   * along with Heritrix; if not, write to the Free Software
    23   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    24   */
    25  package org.archive.crawler.admin;
    26
    27  import java.util.ArrayList;
    28  import java.util.HashMap;
    29  import java.util.List;
    30  import java.util.logging.Level;
    31
    32  import org.archive.crawler.settings.ValueErrorHandler;
    33  import org.archive.crawler.settings.Constraint.FailedCheck;
    34
    35
    36  /***
    37   * An implementation of the ValueErrorHandler for the UI.
    38   *
    39   * <p>The UI uses this class to trap errors in the settings of it's jobs and
    40   * profiles and manage their presentation to the user.
    41   *
    42   * @author Kristinn Sigurdsson
    43   *
    44   * @see org.archive.crawler.settings.ValueErrorHandler
    45   */
    46  public class CrawlJobErrorHandler implements ValueErrorHandler {
    47      /*** All encountered errors */
    48      HashMap<String,FailedCheck> errors = null;
    49      Level level = Level.INFO;
    50      Level highestEncounteredLevel = Level.OFF;
    51
    52      public CrawlJobErrorHandler(){
    53          errors = new HashMap<String,FailedCheck>();
    54      }
    55
    56      public CrawlJobErrorHandler(Level level){
    57          this();
    58          this.level = level;
    59      }
    60
    61      public void handleValueError(FailedCheck error) {
    62          String key = error.getOwner().getAbsoluteName() +
    63              "/" + error.getDefinition().getName();
    64          errors.put(key,error);
    65          if(error.getLevel().intValue()>highestEncounteredLevel.intValue()){
    66              highestEncounteredLevel = error.getLevel();
    67          }
    68      }
    69
    70      /***
    71       * Get error for a specific attribute.
    72       *
    73       * <p>Uses currently set error level
    74       *
    75       * @param absoluteName The absolute name of the attribute
    76       * @return error for a specific attribute at or above current error
    77       *           level. null if no matching error is found.
    78       */
    79      public FailedCheck getError(String absoluteName){
    80          return getError(absoluteName,level);
    81      }
    82
    83      /***
    84       * Get error for a specific attribute
    85       *
    86       * @param absoluteName
    87       *            The absolute name of the attribute.
    88       * @param level
    89       *            Limit errors to those at this or higher level.
    90       * @return error for a specific attribute at or above specified error level.
    91       *         null if no matching error is found.
    92       */
    93      public FailedCheck getError(String absoluteName, Level level) {
    94          FailedCheck fc = (FailedCheck) errors.get(absoluteName);
    95          if (fc != null && fc.getLevel().intValue() >= level.intValue()) {
    96              return fc;
    97          }
    98          return null;
    99      }
    100
    101     /***
    102      * Has there been an error with severity (level) equal to or higher then
    103      * this handlers set level.
    104      * @return has there ben an error.
    105      */
    106     public boolean hasError(){
    107         return hasError(level);
    108     }
    109
    110     /***
    111      * Has there been an error with severity (level) equal to or higher then
    112      * specified.
    113      * @param level The severity.
    114      * @return has there ben an error.
    115      */
    116     public boolean hasError(Level level){
    117         return highestEncounteredLevel.intValue() >= level.intValue();
    118     }
    119
    120     /***
    121      * @return Returns the level.
    122      */
    123     public Level getLevel() {
    124         return level;
    125     }
    126
    127     /***
    128      * @param level The level to set.
    129      */
    130     public void setLevel(Level level) {
    131         this.level = level;
    132     }
    133
    134     /***
    135      * Reset handler.
    136      *
    137      * <p>Delets all encountered errors of any level.
    138      */
    139     public void clearErrors(){
    140         errors = new HashMap<String,FailedCheck>();
    141     }
    142
    143     /***
    144      * Get an List of all the encountered errors.
    145      *
    146      * <p>The List contains a set of
    147      * {@link org.archive.crawler.settings.Constraint.FailedCheck
    148      * FailedCheck} objects.
    149      *
    150      * @return an list of all encountered errors (with level equal to
    151      *         or higher then current level).
    152      *
    153      * @see org.archive.crawler.settings.Constraint.FailedCheck
    154      */
    155     public List getErrors(){
    156         return getErrors(level);
    157     }
    158
    159     /***
    160      * Get an List of all the encountered errors.
    161      *
    162      * <p>The List contains a set of
    163      * {@link org.archive.crawler.settings.Constraint.FailedCheck
    164      * FailedCheck} objects.
    165      *
    166      * @param level Get all errors of this level or higher
    167      *
    168      * @return an list of all encountered errors (with level equal to
    169      *         or higher then specified level).
    170      *
    171      * @see org.archive.crawler.settings.Constraint.FailedCheck
    172      */
    173     public List getErrors(Level level){
    174         ArrayList<FailedCheck> list = new ArrayList<FailedCheck>(errors.size());
    175         for (FailedCheck fc: errors.values()) {
    176             if(fc.getLevel().intValue() >= level.intValue()){
    177                 list.add(fc);
    178             }
    179         }
    180         return list;
    181     }
    182 }