001: /*
002:
003: This software is OSI Certified Open Source Software.
004: OSI Certified is a certification mark of the Open Source Initiative.
005:
006: The license (Mozilla version 1.0) can be read at the MMBase site.
007: See http://www.MMBase.org/license
008:
009: */
010: package org.mmbase.util;
011:
012: import org.mmbase.util.logging.Logger;
013: import org.mmbase.util.logging.Logging;
014: import org.xml.sax.ErrorHandler;
015: import org.xml.sax.SAXException;
016: import org.xml.sax.SAXParseException;
017:
018: /**
019: * Provides ErrorHandler methods
020: *
021: * @move org.mmbase.util.xml
022: * @rename ErrorHandler
023: * @author Gerard van Enk
024: * @version $Id: XMLErrorHandler.java,v 1.17 2005/08/29 08:54:15 michiel Exp $
025: */
026:
027: public class XMLErrorHandler implements ErrorHandler {
028: public static final int WARNING = 1;
029: public static final int ERROR = 2;
030: public static final int FATAL_ERROR = 3;
031: public static final int NEVER = 4;
032:
033: private static Logger log = Logging
034: .getLoggerInstance(XMLErrorHandler.class);
035: private int exceptionLevel;
036: private boolean logMessages;
037: private boolean warning = false;
038: private boolean error = false;
039: private boolean fatal = false;
040:
041: private StringBuffer messages = new StringBuffer();
042:
043: public XMLErrorHandler() {
044: // default keep old behaviour
045: logMessages = true;
046: exceptionLevel = NEVER;
047: }
048:
049: public XMLErrorHandler(boolean log, int exceptionLevel) {
050: this .logMessages = log;
051: this .exceptionLevel = exceptionLevel;
052: }
053:
054: public void warning(SAXParseException ex) throws SAXException {
055: String message = getLocationString(ex) + ": " + ex.getMessage();
056: messages.append(message + "\n");
057: warning = true;
058: if (logMessages) {
059: log.warn(message);
060: }
061: if (exceptionLevel <= WARNING) {
062: throw ex;
063: }
064: }
065:
066: public void error(SAXParseException ex) throws SAXException {
067: String message = getLocationString(ex) + ": " + ex.getMessage();
068: messages.append(message + "\n");
069: error = true;
070: if (logMessages) {
071: log.error(message);
072: log.debug(Logging.stackTrace(new Throwable()));
073: }
074: if (exceptionLevel <= ERROR) {
075: throw ex;
076: }
077: }
078:
079: public void fatalError(SAXParseException ex) throws SAXException {
080: String message = getLocationString(ex) + ": " + ex.getMessage();
081: messages.append(message + "\n");
082: fatal = true;
083: if (logMessages) {
084: log.fatal(message);
085: }
086: if (exceptionLevel <= FATAL_ERROR) {
087: throw ex;
088: }
089: }
090:
091: public boolean foundWarning() {
092: return warning;
093: }
094:
095: public boolean foundError() {
096: return error;
097: }
098:
099: public boolean foundFatalError() {
100: return fatal;
101: }
102:
103: public boolean foundNothing() {
104: return !(warning || error || fatal);
105: }
106:
107: public String getMessageBuffer() {
108: return messages.toString();
109: }
110:
111: /**
112: * Returns a string of the location.
113: */
114: private String getLocationString(SAXParseException ex) {
115: StringBuffer str = new StringBuffer();
116: String systemId = ex.getSystemId();
117: if (systemId != null) {
118: str.append(systemId);
119: } else {
120: str.append("[NO SYSTEM ID]");
121: }
122: str.append(" line:");
123: str.append(ex.getLineNumber());
124: str.append(" column:");
125: str.append(ex.getColumnNumber());
126: return str.toString();
127: }
128: }
|