1 23 24 package org.enhydra.xml.io; 25 26 import java.io.PrintStream ; 27 import java.io.PrintWriter ; 28 import java.io.StringWriter ; 29 30 import org.xml.sax.ErrorHandler ; 31 import org.xml.sax.Locator ; 32 import org.xml.sax.SAXException ; 33 import org.xml.sax.SAXParseException ; 34 35 38 43 public class ErrorReporter implements ErrorHandler { 44 47 private PrintWriter fOut; 48 49 53 private StringWriter fStringOut; 54 55 58 private boolean fPrintWarnings = true; 59 60 63 private boolean fPrintDebug = false; 64 65 68 private int fErrorCnt = 0; 69 70 73 private int fWarningCnt = 0; 74 75 80 public ErrorReporter(PrintWriter output) { 81 fOut = output; 82 } 83 84 89 public ErrorReporter(PrintStream output) { 90 this(new PrintWriter (output, true)); 91 } 92 93 97 public ErrorReporter() { 98 fStringOut = new StringWriter (); 99 fOut = new PrintWriter (fStringOut, true); 100 } 101 102 105 public boolean getPrintWarnings() { 106 return fPrintWarnings; 107 } 108 109 112 public void setPrintWarnings(boolean value) { 113 fPrintWarnings = value; 114 } 115 116 119 public boolean getPrintDebug() { 120 return fPrintDebug; 121 } 122 123 127 public void setPrintDebug(boolean value) { 128 fPrintDebug = value; 129 } 130 131 134 public int getErrorCnt() { 135 return fErrorCnt; 136 } 137 138 141 public int getWarningCnt() { 142 return fWarningCnt; 143 } 144 145 152 public String getOutput() { 153 if (fStringOut == null) { 154 return null; 155 } else { 156 return fStringOut.toString(); 157 } 158 } 159 160 165 private void printFileLineMsg(String msg, 166 boolean isError, 167 String fileName, 168 int lineNum) { 169 fOut.println(fileName + ":" + lineNum + ": " 170 + (isError ? "Error: " : "Warning: ") 171 + msg); 172 } 173 174 177 public void error(String msg) { 178 fOut.print("Error: "); 179 fOut.println(msg); 180 fErrorCnt++; 181 } 182 183 186 public void error(String msg, 187 String fileName, 188 int lineNum) { 189 printFileLineMsg(msg, true, fileName, lineNum); 190 fErrorCnt++; 191 } 192 193 196 public void error(String msg, 197 Locator locator) { 198 printFileLineMsg(msg, true, locator.getSystemId(), 199 locator.getLineNumber()); 200 fErrorCnt++; 201 } 202 203 206 public void error(String msg, 207 Throwable except) { 208 fOut.println("Error: " + msg + ": " + except.toString()); 209 except.printStackTrace(fOut); 210 fErrorCnt++; 211 } 212 213 216 public void warning(String msg) { 217 if (fPrintWarnings) { 218 fOut.print("Warning: "); 219 fOut.println(msg); 220 } 221 fWarningCnt++; 222 } 223 224 227 public void warning(String msg, 228 String fileName, 229 int lineNum) { 230 if (fPrintWarnings) { 231 printFileLineMsg(msg, false, fileName, lineNum); 232 } 233 fWarningCnt++; 234 } 235 236 239 public void warning(String msg, 240 Locator locator) { 241 if (fPrintWarnings) { 242 printFileLineMsg(msg, false, locator.getSystemId(), 243 locator.getLineNumber()); 244 } 245 fWarningCnt++; 246 } 247 248 252 private void printSAXMsg(boolean isError, 253 SAXParseException exception) { 254 printFileLineMsg(exception.getMessage(), isError, 255 exception.getSystemId(), 256 exception.getLineNumber()); 257 if (fPrintDebug) { 258 fOut.println(exception.toString()); 259 Exception cause = exception.getException(); 260 if (cause != null) { 261 fOut.println("Cause: " + cause.getMessage()); 262 cause.printStackTrace(fOut); 263 } 264 } 265 } 266 267 270 public void warning(SAXParseException exception) throws SAXException { 271 if (fPrintWarnings) { 272 printSAXMsg(false, exception); 273 } 274 fWarningCnt++; 275 } 276 277 278 281 public void error(SAXParseException exception) throws SAXException { 282 if (fPrintWarnings) { 283 printSAXMsg(true, exception); 284 } 285 fErrorCnt++; 286 } 287 288 289 292 public void fatalError(SAXParseException exception) 293 throws SAXException { 294 printSAXMsg(true, exception); 295 fErrorCnt++; 296 } 297 } 298 | Popular Tags |