001: /* CrawlJobErrorHandler
002: *
003: * $Id: CrawlJobErrorHandler.java 4666 2006-09-26 17:53:28Z paul_jack $
004: *
005: * Created on Apr 2, 2004
006: *
007: * Copyright (C) 2004 Internet Archive.
008: *
009: * This file is part of the Heritrix web crawler (crawler.archive.org).
010: *
011: * Heritrix is free software; you can redistribute it and/or modify
012: * it under the terms of the GNU Lesser Public License as published by
013: * the Free Software Foundation; either version 2.1 of the License, or
014: * any later version.
015: *
016: * Heritrix is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
019: * GNU Lesser Public License for more details.
020: *
021: * You should have received a copy of the GNU Lesser Public License
022: * along with Heritrix; if not, write to the Free Software
023: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024: */
025: package org.archive.crawler.admin;
026:
027: import java.util.ArrayList;
028: import java.util.HashMap;
029: import java.util.List;
030: import java.util.logging.Level;
031:
032: import org.archive.crawler.settings.ValueErrorHandler;
033: import org.archive.crawler.settings.Constraint.FailedCheck;
034:
035: /**
036: * An implementation of the ValueErrorHandler for the UI.
037: *
038: * <p>The UI uses this class to trap errors in the settings of it's jobs and
039: * profiles and manage their presentation to the user.
040: *
041: * @author Kristinn Sigurdsson
042: *
043: * @see org.archive.crawler.settings.ValueErrorHandler
044: */
045: public class CrawlJobErrorHandler implements ValueErrorHandler {
046: /** All encountered errors */
047: HashMap<String, FailedCheck> errors = null;
048: Level level = Level.INFO;
049: Level highestEncounteredLevel = Level.OFF;
050:
051: public CrawlJobErrorHandler() {
052: errors = new HashMap<String, FailedCheck>();
053: }
054:
055: public CrawlJobErrorHandler(Level level) {
056: this ();
057: this .level = level;
058: }
059:
060: public void handleValueError(FailedCheck error) {
061: String key = error.getOwner().getAbsoluteName() + "/"
062: + error.getDefinition().getName();
063: errors.put(key, error);
064: if (error.getLevel().intValue() > highestEncounteredLevel
065: .intValue()) {
066: highestEncounteredLevel = error.getLevel();
067: }
068: }
069:
070: /**
071: * Get error for a specific attribute.
072: *
073: * <p>Uses currently set error level
074: *
075: * @param absoluteName The absolute name of the attribute
076: * @return error for a specific attribute at or above current error
077: * level. null if no matching error is found.
078: */
079: public FailedCheck getError(String absoluteName) {
080: return getError(absoluteName, level);
081: }
082:
083: /**
084: * Get error for a specific attribute
085: *
086: * @param absoluteName
087: * The absolute name of the attribute.
088: * @param level
089: * Limit errors to those at this or higher level.
090: * @return error for a specific attribute at or above specified error level.
091: * null if no matching error is found.
092: */
093: public FailedCheck getError(String absoluteName, Level level) {
094: FailedCheck fc = (FailedCheck) errors.get(absoluteName);
095: if (fc != null && fc.getLevel().intValue() >= level.intValue()) {
096: return fc;
097: }
098: return null;
099: }
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
175: .size());
176: for (FailedCheck fc : errors.values()) {
177: if (fc.getLevel().intValue() >= level.intValue()) {
178: list.add(fc);
179: }
180: }
181: return list;
182: }
183: }
|