001: /*
002:
003: Derby - Class org.apache.derby.iapi.services.context.ErrorStringBuilder
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derby.iapi.services.context;
023:
024: import org.apache.derby.iapi.error.StandardException;
025: import org.apache.derby.iapi.services.stream.PrintWriterGetHeader;
026:
027: import java.io.StringWriter;
028: import java.io.PrintWriter;
029:
030: /**
031: * Class used to form error messages. Primary
032: * reason for existence is to allow a way to call
033: * printStackTrace() w/o automatically writting
034: * to a stream.
035: */
036: public class ErrorStringBuilder {
037: private StringWriter stringWriter;
038: private PrintWriter printWriter;
039: private PrintWriterGetHeader headerGetter;
040:
041: /**
042: ** Construct an error string builder
043: */
044: public ErrorStringBuilder(PrintWriterGetHeader headerGetter) {
045: this .headerGetter = headerGetter;
046: this .stringWriter = new StringWriter();
047: this .printWriter = new PrintWriter(stringWriter);
048: }
049:
050: /**
051: ** Append an error string
052: **
053: ** @param s the string to append
054: */
055: public void append(String s) {
056: if (headerGetter != null)
057: printWriter.print(headerGetter.getHeader());
058: printWriter.print(s);
059: }
060:
061: /**
062: ** Append an error string with a newline
063: **
064: ** @param s the string to append
065: */
066: public void appendln(String s) {
067: if (headerGetter != null)
068: printWriter.print(headerGetter.getHeader());
069: printWriter.println(s);
070: }
071:
072: /**
073: ** Print a stacktrace from the throwable in the error
074: ** buffer.
075: **
076: ** @param t the error
077: */
078: public void stackTrace(Throwable t) {
079: int level = 0;
080: while (t != null) {
081: if (level > 0)
082: printWriter
083: .println("============= begin nested exception, level ("
084: + level + ") ===========");
085:
086: t.printStackTrace(printWriter);
087:
088: if (t instanceof StandardException) {
089: t = ((StandardException) t).getNestedException();
090: } else if (t instanceof ExceptionInInitializerError) {
091: t = ((ExceptionInInitializerError) t).getException();
092: } else if (t instanceof java.lang.reflect.InvocationTargetException) {
093: t = ((java.lang.reflect.InvocationTargetException) t)
094: .getTargetException();
095: } else if (t instanceof java.sql.SQLException) {
096: t = ((java.sql.SQLException) t).getNextException();
097: } else {
098: t = null;
099: }
100:
101: if (level > 0)
102: printWriter
103: .println("============= end nested exception, level ("
104: + level + ") ===========");
105:
106: level++;
107:
108: }
109:
110: }
111:
112: /**
113: ** Reset the buffer -- truncate it down to nothing.
114: **
115: */
116: public void reset() {
117: // Is this the most effecient way to do this?
118: stringWriter.getBuffer().setLength(0);
119: }
120:
121: /**
122: ** Get the buffer
123: */
124: public StringBuffer get() {
125: return stringWriter.getBuffer();
126: }
127: }
|