KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > archive > crawler > admin > CrawlJobErrorHandler


1 /* CrawlJobErrorHandler
2  *
3  * $Id: CrawlJobErrorHandler.java,v 1.5.14.1 2007/01/13 01:31:07 stack-sf Exp $
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 JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.logging.Level JavaDoc;
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 JavaDoc<String JavaDoc,FailedCheck> errors = null;
49     Level JavaDoc level = Level.INFO;
50     Level JavaDoc highestEncounteredLevel = Level.OFF;
51
52     public CrawlJobErrorHandler(){
53         errors = new HashMap JavaDoc<String JavaDoc,FailedCheck>();
54     }
55
56     public CrawlJobErrorHandler(Level JavaDoc level){
57         this();
58         this.level = level;
59     }
60
61     public void handleValueError(FailedCheck error) {
62         String JavaDoc 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 JavaDoc 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 JavaDoc absoluteName, Level JavaDoc 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 JavaDoc level){
117         return highestEncounteredLevel.intValue() >= level.intValue();
118     }
119
120     /**
121      * @return Returns the level.
122      */

123     public Level JavaDoc getLevel() {
124         return level;
125     }
126
127     /**
128      * @param level The level to set.
129      */

130     public void setLevel(Level JavaDoc 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 JavaDoc<String JavaDoc,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 JavaDoc 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 JavaDoc getErrors(Level JavaDoc level){
174         ArrayList JavaDoc<FailedCheck> list = new ArrayList JavaDoc<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 }
183
Popular Tags