--- /dev/null
+/*\r
+\r
+ Derby - Class org.apache.derby.iapi.services.context.ErrorStringBuilder\r
+\r
+ Licensed to the Apache Software Foundation (ASF) under one or more\r
+ contributor license agreements. See the NOTICE file distributed with\r
+ this work for additional information regarding copyright ownership.\r
+ The ASF licenses this file to you under the Apache License, Version 2.0\r
+ (the "License"); you may not use this file except in compliance with\r
+ the License. You may obtain a copy of the License at\r
+\r
+ http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+ Unless required by applicable law or agreed to in writing, software\r
+ distributed under the License is distributed on an "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ See the License for the specific language governing permissions and\r
+ limitations under the License.\r
+\r
+ */\r
+\r
+package org.apache.derby.iapi.services.context;\r
+\r
+import org.apache.derby.iapi.services.stream.PrintWriterGetHeader;\r
+\r
+import java.io.StringWriter;\r
+import java.io.PrintWriter;\r
+\r
+/**\r
+ * Class used to form error messages. Primary\r
+ * reason for existence is to allow a way to call\r
+ * printStackTrace() w/o automatically writting\r
+ * to a stream.\r
+ */\r
+public class ErrorStringBuilder \r
+{\r
+ private StringWriter stringWriter;\r
+ private PrintWriter printWriter;\r
+ private PrintWriterGetHeader headerGetter;\r
+\r
+ /**\r
+ ** Construct an error string builder\r
+ */\r
+ public ErrorStringBuilder(PrintWriterGetHeader headerGetter)\r
+ {\r
+ this.headerGetter = headerGetter;\r
+ this.stringWriter = new StringWriter();\r
+ this.printWriter = new PrintWriter(stringWriter);\r
+ }\r
+\r
+ /**\r
+ ** Append an error string \r
+ **\r
+ ** @param s the string to append\r
+ */\r
+ public void append(String s)\r
+ {\r
+ if (headerGetter != null)\r
+ printWriter.print(headerGetter.getHeader());\r
+ printWriter.print(s);\r
+ }\r
+\r
+\r
+ /**\r
+ ** Append an error string with a newline\r
+ **\r
+ ** @param s the string to append\r
+ */\r
+ public void appendln(String s)\r
+ {\r
+ if (headerGetter != null)\r
+ printWriter.print(headerGetter.getHeader());\r
+ printWriter.println(s);\r
+ }\r
+\r
+ /**\r
+ ** Print a stacktrace from the throwable in the error\r
+ ** buffer.\r
+ **\r
+ ** @param t the error\r
+ */\r
+ public void stackTrace(Throwable t)\r
+ {\r
+ int level = 0;\r
+ while(t != null)\r
+ {\r
+ if (level > 0) \r
+ printWriter.println("============= begin nested exception, level (" +\r
+ level + ") ===========");\r
+\r
+ t.printStackTrace(printWriter);\r
+\r
+ if (t instanceof java.sql.SQLException) {\r
+ Throwable next = ((java.sql.SQLException)t).getNextException();\r
+ t = (next == null) ? t.getCause() : next;\r
+ } else {\r
+ t = t.getCause();\r
+ }\r
+\r
+ if (level > 0) \r
+ printWriter.println("============= end nested exception, level (" + \r
+ level + ") ===========");\r
+\r
+ level++;\r
+\r
+ }\r
+\r
+ }\r
+\r
+ /**\r
+ ** Reset the buffer -- truncate it down to nothing.\r
+ **\r
+ */\r
+ public void reset()\r
+ {\r
+ // Is this the most effecient way to do this?\r
+ stringWriter.getBuffer().setLength(0);\r
+ }\r
+\r
+ /**\r
+ ** Get the buffer\r
+ */\r
+ public StringBuffer get()\r
+ {\r
+ return stringWriter.getBuffer();\r
+ } \r
+} \r