KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > io > ErrorReporter


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: ErrorReporter.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.io;
25
26 import java.io.PrintStream JavaDoc;
27 import java.io.PrintWriter JavaDoc;
28 import java.io.StringWriter JavaDoc;
29
30 import org.xml.sax.ErrorHandler JavaDoc;
31 import org.xml.sax.Locator JavaDoc;
32 import org.xml.sax.SAXException JavaDoc;
33 import org.xml.sax.SAXParseException JavaDoc;
34
35 //FIXME: Make util.ChainedThrowableUtil more general and used for
36
//print stack trace.
37

38 /**
39  * Object used to report error message for the user. Can write eror messages
40  * to a file or save them in a memory buffer.
41  * This is also a SAX compliant ErrorHandler.
42  */

43 public class ErrorReporter implements ErrorHandler JavaDoc {
44     /*
45      * Error message are written to this file.
46      */

47     private PrintWriter JavaDoc fOut;
48
49     /*
50      * String buffered writer used when an external output stream is not
51      * specified. Is null if going to a stream.
52      */

53     private StringWriter JavaDoc fStringOut;
54
55     /**
56      * Should warnings be printed?
57      */

58     private boolean fPrintWarnings = true;
59
60     /*
61      * This controls the printing of stack traces that are normally supressed.
62      */

63     private boolean fPrintDebug = false;
64
65     /*
66      * Count of errors that have occured.
67      */

68     private int fErrorCnt = 0;
69
70     /*
71      * Count of warning that have occured.
72      */

73     private int fWarningCnt = 0;
74
75     /**
76      * Construct a new error report object.
77      *
78      * @param output Writer to recieve error output.
79      */

80     public ErrorReporter(PrintWriter JavaDoc output) {
81         fOut = output;
82     }
83
84     /**
85      * Construct a new error report object.
86      *
87      * @param output PrintStream to recieve error output.
88      */

89     public ErrorReporter(PrintStream JavaDoc output) {
90         this(new PrintWriter JavaDoc(output, true));
91     }
92
93     /**
94      * Construct a new error report object with output
95      * saved in a string buffer.
96      */

97     public ErrorReporter() {
98         fStringOut = new StringWriter JavaDoc();
99         fOut = new PrintWriter JavaDoc(fStringOut, true);
100     }
101
102     /**
103      * Get the value of the printWarnings flag.
104      */

105     public boolean getPrintWarnings() {
106         return fPrintWarnings;
107     }
108
109     /**
110      * Set the value of the printWarnings flag.
111      */

112     public void setPrintWarnings(boolean value) {
113         fPrintWarnings = value;
114     }
115
116     /**
117      * Get the value of the printDebug flag.
118      */

119     public boolean getPrintDebug() {
120         return fPrintDebug;
121     }
122
123     /**
124      * Set the value of the printDebug flag. This controls the printing
125      * of stack traces that are normally supressed.
126      */

127     public void setPrintDebug(boolean value) {
128         fPrintDebug = value;
129     }
130
131     /**
132      * Get the count of errors that have occured.
133      */

134     public int getErrorCnt() {
135         return fErrorCnt;
136     }
137
138     /**
139      * Get the count of warnings that have occured.
140      */

141     public int getWarningCnt() {
142         return fWarningCnt;
143     }
144
145     /**
146      * Get the buffer containing the output if internal buffering
147      * has been used.
148      *
149      * @return buffer containing output or null if output was written to
150      * an external stream.
151      */

152     public String JavaDoc getOutput() {
153         if (fStringOut == null) {
154             return null;
155         } else {
156             return fStringOut.toString();
157         }
158     }
159
160     /**
161      * Generate a message with file name and line number.
162      * This is in a format parsable by emacs and other
163      * editors.
164      */

165     private void printFileLineMsg(String JavaDoc msg,
166                                   boolean isError,
167                                   String JavaDoc fileName,
168                                   int lineNum) {
169         fOut.println(fileName + ":" + lineNum + ": "
170                      + (isError ? "Error: " : "Warning: ")
171                      + msg);
172     }
173
174     /**
175      * Report an error.
176      */

177     public void error(String JavaDoc msg) {
178         fOut.print("Error: ");
179         fOut.println(msg);
180         fErrorCnt++;
181     }
182
183     /**
184      * Report an error with file name and line number.
185      */

186     public void error(String JavaDoc msg,
187                       String JavaDoc fileName,
188                       int lineNum) {
189         printFileLineMsg(msg, true, fileName, lineNum);
190         fErrorCnt++;
191     }
192
193     /**
194      * Report an error with file name and line number.
195      */

196     public void error(String JavaDoc msg,
197                       Locator JavaDoc locator) {
198         printFileLineMsg(msg, true, locator.getSystemId(),
199                          locator.getLineNumber());
200         fErrorCnt++;
201     }
202
203     /**
204      * Report an error from an exception
205      */

206     public void error(String JavaDoc msg,
207                       Throwable JavaDoc except) {
208         fOut.println("Error: " + msg + ": " + except.toString());
209         except.printStackTrace(fOut);
210         fErrorCnt++;
211     }
212
213     /**
214      * Report a warning.
215      */

216     public void warning(String JavaDoc msg) {
217         if (fPrintWarnings) {
218             fOut.print("Warning: ");
219             fOut.println(msg);
220         }
221         fWarningCnt++;
222     }
223
224     /**
225      * Report a warning with file name and line number.
226      */

227     public void warning(String JavaDoc msg,
228                         String JavaDoc fileName,
229                         int lineNum) {
230         if (fPrintWarnings) {
231             printFileLineMsg(msg, false, fileName, lineNum);
232         }
233         fWarningCnt++;
234     }
235
236     /**
237      * Report a warning with file name and line number.
238      */

239     public void warning(String JavaDoc msg,
240                         Locator JavaDoc locator) {
241         if (fPrintWarnings) {
242             printFileLineMsg(msg, false, locator.getSystemId(),
243                              locator.getLineNumber());
244         }
245         fWarningCnt++;
246     }
247
248     /**
249      * Print a SAX parser message in a consistent style, consistent
250      * with emacs compile mode (same as grep, cc, javac, etc).
251      */

252     private void printSAXMsg(boolean isError,
253                              SAXParseException JavaDoc exception) {
254         printFileLineMsg(exception.getMessage(), isError,
255                          exception.getSystemId(),
256                          exception.getLineNumber());
257         if (fPrintDebug) {
258             fOut.println(exception.toString());
259             Exception JavaDoc cause = exception.getException();
260             if (cause != null) {
261                 fOut.println("Cause: " + cause.getMessage());
262                 cause.printStackTrace(fOut);
263             }
264         }
265     }
266     
267     /**
268      * Receive notification of a SAX warning.
269      */

270     public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
271         if (fPrintWarnings) {
272             printSAXMsg(false, exception);
273         }
274         fWarningCnt++;
275     }
276
277
278     /**
279      * Receive notification of a SAX recoverable error.
280      */

281     public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
282         if (fPrintWarnings) {
283             printSAXMsg(true, exception);
284         }
285         fErrorCnt++;
286     }
287     
288
289     /**
290      * Receive notification of a SAX non-recoverable error.
291      */

292     public void fatalError(SAXParseException JavaDoc exception)
293         throws SAXException JavaDoc {
294         printSAXMsg(true, exception);
295         fErrorCnt++;
296     }
297 }
298
Popular Tags