KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > util > HtmlExceptionFormatter


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18 package org.apache.beehive.netui.util;
19
20 import org.apache.beehive.netui.util.internal.InternalStringBuilder;
21
22 import java.io.PrintWriter JavaDoc;
23 import java.io.StringWriter JavaDoc;
24
25 import javax.servlet.jsp.JspException JavaDoc;
26 import javax.servlet.jsp.el.ELException JavaDoc;
27 import javax.servlet.ServletException JavaDoc;
28
29 /**
30  * Format a {@link java.lang.Throwable} so that it displays well in HTML.
31  */

32 public class HtmlExceptionFormatter {
33
34     /**
35      * An XHTML line break
36      */

37     private static final String JavaDoc HTML_LINE_BREAK = "<br/>";
38
39     /* @TODO: is the formatting of Throwable.printStackTrace() dependent on the platform? */
40     /**
41      * The end of line character to replace with an HTML line break
42      */

43     private static final String JavaDoc LINE_BREAK = "\n";
44
45     private static final String JavaDoc CAUSED_BY = "caused by ";
46
47     /**
48      * Format an exception into XHTML.
49      * <p/>
50      * Optionally include a message and the stack trace.
51      * <p/>
52      * The formatted exception will have line breaks replaced with XHTML line breaks for display
53      * in HTML. The String message of the cause will be included, and the stack trace of the
54      * cause is optionally included given the value of <code>includeStackTrace</code>
55      *
56      * @param message the message to include with the formatted exception. This is in addition to the message in the stack trace.
57      * @param t a Throwable exception to format
58      * @param includeStackTrace a boolean that determines whether to include the stack trace in the formatted output
59      * @return the formatted error message, optionally including the stack trace.
60      */

61     public static String JavaDoc format(String JavaDoc message, Throwable JavaDoc t, boolean includeStackTrace) {
62         InternalStringBuilder buf = new InternalStringBuilder();
63
64         if(message != null) {
65             buf.append(message);
66
67             Throwable JavaDoc cause = discoverRootCause(t);
68             if(cause != null) {
69                 buf.append(HTML_LINE_BREAK);
70                 buf.append(CAUSED_BY);
71                 buf.append(": ");
72                 buf.append(cause.toString());
73             }
74         }
75
76         if(includeStackTrace) {
77             if(message != null)
78                 buf.append(HTML_LINE_BREAK);
79
80             String JavaDoc st = addStackTrace(t);
81             buf.append(st);
82
83             Throwable JavaDoc rootCause = null;
84             Throwable JavaDoc tmp = t;
85             while(hasRootCause(tmp) && (rootCause = discoverRootCause(tmp)) != null) {
86                 st = addStackTrace(rootCause);
87                 buf.append(HTML_LINE_BREAK);
88                 buf.append(st);
89                 tmp = rootCause;
90             }
91         }
92
93         return buf.toString().replaceAll(LINE_BREAK, HTML_LINE_BREAK);
94     }
95
96     private static final String JavaDoc addStackTrace(Throwable JavaDoc t) {
97         InternalStringBuilder buf = new InternalStringBuilder();
98         StringWriter JavaDoc sw = new StringWriter JavaDoc();
99         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
100         t.printStackTrace(pw);
101         pw.flush();
102         pw.close();
103
104         String JavaDoc error = sw.toString();
105         int pos = error.indexOf(LINE_BREAK);
106         if(pos != -1) {
107             String JavaDoc lineOne = error.substring(0, pos);
108             String JavaDoc rest = error.substring(pos);
109
110             buf.append("<span class='pfErrorLineOne'>");
111             buf.append(lineOne);
112             buf.append("</span>");
113             buf.append(rest);
114         } else {
115             buf.append(sw.toString());
116         }
117
118         return buf.toString();
119     }
120
121     private static final boolean hasRootCause(Throwable JavaDoc t) {
122         if(t.getCause() == null && (t instanceof JspException JavaDoc || t instanceof ServletException JavaDoc || t instanceof ELException JavaDoc))
123             return true;
124         else
125             return false;
126     }
127
128     private static final Throwable JavaDoc discoverRootCause(Throwable JavaDoc t) {
129         Throwable JavaDoc cause = null;
130         if(t instanceof JspException JavaDoc)
131             cause = ((JspException JavaDoc)t).getRootCause();
132         else if(t instanceof ServletException JavaDoc)
133             cause = ((ServletException JavaDoc)t).getRootCause();
134         else if(t instanceof ELException JavaDoc)
135             cause = ((ELException JavaDoc)t).getRootCause();
136         else
137             cause = t.getCause();
138
139         return cause;
140     }
141 }
142
Popular Tags