KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derby > iapi > services > context > ErrorStringBuilder


1 /*
2
3    Derby - Class org.apache.derby.iapi.services.context.ErrorStringBuilder
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to you under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derby.iapi.services.context;
23
24 import org.apache.derby.iapi.error.StandardException;
25 import org.apache.derby.iapi.services.stream.PrintWriterGetHeader;
26
27 import java.io.StringWriter JavaDoc;
28 import java.io.PrintWriter JavaDoc;
29
30 /**
31  * Class used to form error messages. Primary
32  * reason for existence is to allow a way to call
33  * printStackTrace() w/o automatically writting
34  * to a stream.
35  */

36 public class ErrorStringBuilder
37 {
38     private StringWriter JavaDoc stringWriter;
39     private PrintWriter JavaDoc printWriter;
40     private PrintWriterGetHeader headerGetter;
41
42     /**
43     ** Construct an error string builder
44     */

45     public ErrorStringBuilder(PrintWriterGetHeader headerGetter)
46     {
47         this.headerGetter = headerGetter;
48         this.stringWriter = new StringWriter JavaDoc();
49         this.printWriter = new PrintWriter JavaDoc(stringWriter);
50     }
51
52     /**
53     ** Append an error string
54     **
55     ** @param s the string to append
56     */

57     public void append(String JavaDoc s)
58     {
59         if (headerGetter != null)
60             printWriter.print(headerGetter.getHeader());
61         printWriter.print(s);
62     }
63
64
65     /**
66     ** Append an error string with a newline
67     **
68     ** @param s the string to append
69     */

70     public void appendln(String JavaDoc s)
71     {
72         if (headerGetter != null)
73             printWriter.print(headerGetter.getHeader());
74         printWriter.println(s);
75     }
76
77     /**
78     ** Print a stacktrace from the throwable in the error
79     ** buffer.
80     **
81     ** @param t the error
82     */

83     public void stackTrace(Throwable JavaDoc t)
84     {
85         int level = 0;
86         while(t != null)
87         {
88             if (level > 0)
89                 printWriter.println("============= begin nested exception, level (" +
90                                     level + ") ===========");
91
92             t.printStackTrace(printWriter);
93
94
95             if (t instanceof StandardException) {
96                 t = ((StandardException)t).getNestedException();
97             }
98             else if (t instanceof ExceptionInInitializerError JavaDoc) {
99                 t = ((ExceptionInInitializerError JavaDoc) t).getException();
100             }
101             else if (t instanceof java.lang.reflect.InvocationTargetException JavaDoc) {
102                 t = ((java.lang.reflect.InvocationTargetException JavaDoc) t).getTargetException();
103             }
104             else if (t instanceof java.sql.SQLException JavaDoc) {
105                 t = ((java.sql.SQLException JavaDoc)t).getNextException();
106             } else {
107                 t = null;
108             }
109
110             if (level > 0)
111                 printWriter.println("============= end nested exception, level (" +
112                                     level + ") ===========");
113
114             level++;
115
116         }
117
118     }
119
120     /**
121     ** Reset the buffer -- truncate it down to nothing.
122     **
123     */

124     public void reset()
125     {
126         // Is this the most effecient way to do this?
127
stringWriter.getBuffer().setLength(0);
128     }
129
130     /**
131     ** Get the buffer
132     */

133     public StringBuffer JavaDoc get()
134     {
135         return stringWriter.getBuffer();
136     }
137 }
138
Popular Tags