1 10 package org.mmbase.util; 11 12 import org.mmbase.util.logging.Logger; 13 import org.mmbase.util.logging.Logging; 14 import org.xml.sax.ErrorHandler ; 15 import org.xml.sax.SAXException ; 16 import org.xml.sax.SAXParseException ; 17 18 26 27 public class XMLErrorHandler implements ErrorHandler { 28 public static final int WARNING = 1; 29 public static final int ERROR = 2; 30 public static final int FATAL_ERROR = 3; 31 public static final int NEVER = 4; 32 33 private static Logger log = Logging.getLoggerInstance(XMLErrorHandler.class); 34 private int exceptionLevel; 35 private boolean logMessages; 36 private boolean warning = false; 37 private boolean error = false; 38 private boolean fatal = false; 39 40 private StringBuffer messages = new StringBuffer (); 41 42 43 public XMLErrorHandler() { 44 logMessages = true; 46 exceptionLevel = NEVER; 47 } 48 49 public XMLErrorHandler(boolean log, int exceptionLevel) { 50 this.logMessages = log; 51 this.exceptionLevel = exceptionLevel; 52 } 53 54 public void warning(SAXParseException ex) throws SAXException { 55 String message = getLocationString(ex)+": "+ ex.getMessage(); 56 messages.append(message + "\n"); 57 warning = true; 58 if(logMessages) { 59 log.warn(message); 60 } 61 if(exceptionLevel<=WARNING) { 62 throw ex; 63 } 64 } 65 66 public void error(SAXParseException ex) throws SAXException { 67 String message = getLocationString(ex)+": "+ ex.getMessage(); 68 messages.append(message + "\n"); 69 error = true; 70 if(logMessages) { 71 log.error(message); 72 log.debug(Logging.stackTrace(new Throwable ())); 73 } 74 if(exceptionLevel<=ERROR) { 75 throw ex; 76 } 77 } 78 79 public void fatalError(SAXParseException ex) throws SAXException { 80 String message = getLocationString(ex)+": "+ ex.getMessage(); 81 messages.append(message + "\n"); 82 fatal = true; 83 if(logMessages) { 84 log.fatal(message); 85 } 86 if(exceptionLevel<=FATAL_ERROR) { 87 throw ex; 88 } 89 } 90 91 public boolean foundWarning() { 92 return warning; 93 } 94 95 public boolean foundError() { 96 return error; 97 } 98 99 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 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 } 129 | Popular Tags |