001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: ExceptionFormattingUtils.java 3784 2007-06-11 16:44:35Z gbevin $
007: */
008: package com.uwyn.rife.tools;
009:
010: import com.uwyn.rife.template.Template;
011:
012: public abstract class ExceptionFormattingUtils {
013: private static final int DEFAULT_STACKTRACELIMIT = 15;
014: private static final int DEFAULT_BUFFERLIMIT = 300000;
015:
016: public static String formatExceptionStackTrace(Throwable exception,
017: Template template) {
018: return formatExceptionStackTrace(exception, template,
019: DEFAULT_STACKTRACELIMIT, DEFAULT_BUFFERLIMIT);
020: }
021:
022: public static String formatExceptionStackTrace(Throwable exception,
023: Template template, int stacktraceLimit) {
024: return formatExceptionStackTrace(exception, template,
025: stacktraceLimit, DEFAULT_BUFFERLIMIT);
026: }
027:
028: public static String formatExceptionStackTrace(Throwable exception,
029: Template template, int stacktraceLimit, int bufferLimit) {
030: if (null == exception)
031: throw new IllegalArgumentException(
032: "exception can't be null.");
033: if (null == template)
034: throw new IllegalArgumentException(
035: "template can't be null.");
036: if (stacktraceLimit <= 0)
037: throw new IllegalArgumentException(
038: "stacktraceLimit has to be bigger than 0.");
039: if (bufferLimit <= 0)
040: throw new IllegalArgumentException(
041: "bufferLimit has to be bigger than 0.");
042:
043: String exception_name = null;
044: String message = null;
045: String class_name = null;
046: String method_name = null;
047: String file_name = null;
048:
049: StringBuilder exceptions = new StringBuilder();
050: while (exception != null) {
051: exception_name = exception.getClass().getName();
052: message = exception.getMessage();
053: if (null == exception_name) {
054: // this should never happen
055: exception_name = "<unknown exception name>";
056: }
057: if (null == message) {
058: message = "<no message>";
059: }
060: template.setValue("exception_class_name", template
061: .getEncoder().encode(exception_name));
062: template.setValue("exception_message", template
063: .getEncoder().encode(message));
064:
065: if (template.hasValueId("exception_stack_trace")) {
066: StackTraceElement[] stack_trace = exception
067: .getStackTrace();
068: StringBuilder stack_trace_out = new StringBuilder();
069: StringBuilder stack_trace_details = null;
070: for (int i = 0; i < stack_trace.length; i++) {
071: class_name = stack_trace[i].getClassName();
072: method_name = stack_trace[i].getMethodName();
073: if (null == class_name) {
074: // this should never happen either
075: class_name = "<unknown class>";
076: }
077: if (null == class_name) {
078: // this should never happen either
079: method_name = "<unknown method>";
080: }
081: template.setValue("class_name", template
082: .getEncoder().encode(class_name));
083: template.setValue("method_name", template
084: .getEncoder().encode(method_name));
085: stack_trace_details = new StringBuilder();
086: file_name = stack_trace[i].getFileName();
087: if (null == file_name) {
088: file_name = "<unknown>";
089: }
090: template.setValue("file_name", template
091: .getEncoder().encode(file_name));
092: stack_trace_details.append(template
093: .getBlock("file_name"));
094: if (stack_trace[i].getLineNumber() > 0) {
095: template.setValue("line_number", stack_trace[i]
096: .getLineNumber());
097: stack_trace_details.append(template
098: .getBlock("line_number"));
099: }
100: template.setValue("details", stack_trace_details
101: .toString());
102:
103: stack_trace_out.append(template
104: .getBlock("stack_trace_line"));
105:
106: if (i > stacktraceLimit) {
107: template.setValue("count", stack_trace.length
108: - 1 - i);
109: stack_trace_out.append(template
110: .getBlock("more_stack_trace"));
111: break;
112: } else if (exceptions.length()
113: + stack_trace_out.length() > bufferLimit) {
114: template.setValue("count", stack_trace.length
115: - 1 - i);
116: stack_trace_out.append(template
117: .getBlock("more_stack_trace"));
118: exception = null;
119: break;
120: }
121: }
122:
123: template.setValue("exception_stack_trace",
124: stack_trace_out.toString());
125: }
126:
127: exceptions.append(template.getBlock("exception"));
128:
129: if (template.hasBlock("more_exceptions")
130: && exceptions.length() > bufferLimit) {
131: exceptions.append(template.getBlock("more_exceptions"));
132: exception = null;
133: break;
134: }
135:
136: if (exception != null) {
137: exception = exception.getCause();
138: }
139: }
140:
141: return exceptions.toString();
142: }
143: }
|