001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: CollectingErrorHandler.java,v 1.2 2006/09/29 12:32:10 drmlipp Exp $
021: *
022: * $Log: CollectingErrorHandler.java,v $
023: * Revision 1.2 2006/09/29 12:32:10 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.1.1.1 2003/06/30 20:05:17 drmlipp
027: * Initial import
028: *
029: * Revision 1.3 2003/06/27 08:51:44 lipp
030: * Fixed copyright/license information.
031: *
032: * Revision 1.2 2003/04/25 14:50:58 lipp
033: * Fixed javadoc errors and warnings.
034: *
035: * Revision 1.1 2003/03/07 08:01:29 huaiyang
036: * initial.
037: *
038: *
039: */
040: package de.danet.an.workflow.util;
041:
042: import java.util.ArrayList;
043: import java.util.List;
044:
045: import org.xml.sax.ErrorHandler;
046: import org.xml.sax.SAXException;
047: import org.xml.sax.SAXParseException;
048:
049: import de.danet.an.workflow.api.PrioritizedMessage;
050:
051: /**
052: * This class collects errors while parsing and validating document.
053: */
054: public class CollectingErrorHandler implements ErrorHandler {
055:
056: private List debugs = new ArrayList();
057: private List infos = new ArrayList();
058: private List warnings = new ArrayList();
059: private List errors = new ArrayList();
060: private List fatals = new ArrayList();
061: private List messages = new ArrayList();
062:
063: /**
064: * Return all accumulated debug messages.
065: * @return list with debug messages (may be an empty list).
066: */
067: public List getDebugs() {
068: return debugs;
069: }
070:
071: /**
072: * Return all accumulated infos messages.
073: * @return list with info messages (may be an empty list).
074: */
075: public List getInfos() {
076: return infos;
077: }
078:
079: /**
080: * Return all accumulated warnings.
081: * @return list with warning messages (may be an empty list).
082: */
083: public List getWarnings() {
084: return warnings;
085: }
086:
087: /**
088: * Return all accumulated errors.
089: * @return list with error messages (may be an empty list).
090: */
091: public List getErrors() {
092: return errors;
093: }
094:
095: /**
096: * Return all accumulated fatal errors.
097: * @return list with fatal error messages (may be an empty list).
098: */
099: public List getFatalErrors() {
100: return fatals;
101: }
102:
103: /**
104: * Return all accumulated messages, i.e. debugs, info, warnings,
105: * errors and fatal errors.
106: * @return list with error messages (may be an empty list).
107: */
108: public List getMessages() {
109: return messages;
110: }
111:
112: private String layoutMessage(SAXParseException exc) {
113: return Integer.toString(exc.getLineNumber()) + ": "
114: + exc.getMessage();
115: }
116:
117: /**
118: * Handles parser warnings.
119: * @param exc Parser exception
120: */
121: public void warning(SAXParseException exc) {
122: PrioritizedMessage pm = new PrioritizedMessage(
123: PrioritizedMessage.Priority.WARN, layoutMessage(exc));
124: warnings.add(pm);
125: messages.add(pm);
126: }
127:
128: /**
129: * Handles parser errors.
130: * @param exc Parser exception
131: * @throws SAXException Propagation of given SAXParseException
132: */
133: public void error(SAXParseException exc) throws SAXException {
134: PrioritizedMessage pm = new PrioritizedMessage(
135: PrioritizedMessage.Priority.ERROR, layoutMessage(exc));
136: errors.add(pm);
137: messages.add(pm);
138: }
139:
140: /**
141: * Handles fatal parser errors.
142: * @param exc Parser exception
143: * @throws SAXException Propagation of given SAXParseException
144: */
145: public void fatalError(SAXParseException exc) throws SAXException {
146: PrioritizedMessage pm = new PrioritizedMessage(
147: PrioritizedMessage.Priority.FATAL, layoutMessage(exc));
148: fatals.add(pm);
149: messages.add(pm);
150: }
151:
152: /**
153: * Append a prioritized message. This method may be used to add a message
154: * from outside the immediate scope of the parser, and handle it like a
155: * parse exception.
156: * @param prioritizedMessage the given message.
157: */
158: public void add(PrioritizedMessage prioritizedMessage) {
159: PrioritizedMessage.Priority priority = prioritizedMessage
160: .priority();
161: if (priority.compareTo(PrioritizedMessage.Priority.DEBUG) == 0) {
162: debugs.add(prioritizedMessage);
163: } else if (priority.compareTo(PrioritizedMessage.Priority.INFO) == 0) {
164: infos.add(prioritizedMessage);
165: } else if (priority.compareTo(PrioritizedMessage.Priority.WARN) == 0) {
166: warnings.add(prioritizedMessage);
167: } else if (priority
168: .compareTo(PrioritizedMessage.Priority.ERROR) == 0) {
169: errors.add(prioritizedMessage);
170: } else if (priority
171: .compareTo(PrioritizedMessage.Priority.FATAL) == 0) {
172: fatals.add(prioritizedMessage);
173: }
174: messages.add(prioritizedMessage);
175: }
176: }
|