Source Code Cross Referenced for HtmlExceptionFormatter.java in  » Library » Apache-beehive-1.0.2-src » org » apache » beehive » netui » util » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » Apache beehive 1.0.2 src » org.apache.beehive.netui.util 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *     http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         *
017:         * $Header:$
018:         */
019:        package org.apache.beehive.netui.util;
020:
021:        import java.io.PrintWriter;
022:        import java.io.StringWriter;
023:        import javax.servlet.jsp.JspException;
024:        import javax.servlet.jsp.el.ELException;
025:        import javax.servlet.ServletException;
026:
027:        import org.apache.beehive.netui.util.internal.InternalStringBuilder;
028:
029:        /**
030:         * Format a {@link java.lang.Throwable} so that it displays well in HTML.
031:         */
032:        public class HtmlExceptionFormatter {
033:
034:            /**
035:             * An XHTML line break
036:             */
037:            private static final String HTML_LINE_BREAK = "<br/>";
038:
039:            /**
040:             * The end of line character to replace with an HTML line break
041:             */
042:            private static final String LINE_BREAK = "\n";
043:
044:            private static final String CAUSED_BY = "caused by ";
045:
046:            /**
047:             * Format an exception into XHTML.
048:             * <p/>
049:             * Optionally include a message and the stack trace.
050:             * <p/>
051:             * The formatted exception will have line breaks replaced with XHTML line breaks for display
052:             * in HTML.  The String message of the cause will be included, and the stack trace of the
053:             * cause is optionally included given the value of <code>includeStackTrace</code>
054:             *
055:             * @param message           the message to include with the formatted exception.  This is in addition to the message in the stack trace.
056:             * @param t                 a Throwable exception to format
057:             * @param includeStackTrace a boolean that determines whether to include the stack trace in the formatted output
058:             * @return the formatted error message, optionally including the stack trace.
059:             */
060:            public static String format(String message, Throwable t,
061:                    boolean includeStackTrace) {
062:                InternalStringBuilder buf = new InternalStringBuilder();
063:
064:                if (message != null) {
065:                    buf.append(message);
066:
067:                    Throwable cause = discoverRootCause(t);
068:                    if (cause != null) {
069:                        buf.append(HTML_LINE_BREAK);
070:                        buf.append(CAUSED_BY);
071:                        buf.append(": ");
072:                        buf.append(cause.toString());
073:                    }
074:                }
075:
076:                if (includeStackTrace) {
077:                    if (message != null)
078:                        buf.append(HTML_LINE_BREAK);
079:
080:                    String st = addStackTrace(t);
081:                    buf.append(st);
082:
083:                    Throwable rootCause = null;
084:                    Throwable tmp = t;
085:                    while (hasRootCause(tmp)
086:                            && (rootCause = discoverRootCause(tmp)) != null) {
087:                        st = addStackTrace(rootCause);
088:                        buf.append(HTML_LINE_BREAK);
089:                        buf.append(st);
090:                        tmp = rootCause;
091:                    }
092:                }
093:
094:                return buf.toString().replaceAll(LINE_BREAK, HTML_LINE_BREAK);
095:            }
096:
097:            private static String addStackTrace(Throwable t) {
098:                InternalStringBuilder buf = new InternalStringBuilder();
099:                StringWriter sw = new StringWriter();
100:                PrintWriter pw = new PrintWriter(sw);
101:                t.printStackTrace(pw);
102:                pw.flush();
103:                pw.close();
104:
105:                String error = sw.toString();
106:                int pos = error.indexOf(LINE_BREAK);
107:                if (pos != -1) {
108:                    String lineOne = error.substring(0, pos);
109:                    String rest = error.substring(pos);
110:
111:                    buf.append("<span class='pfErrorLineOne'>");
112:                    buf.append(lineOne);
113:                    buf.append("</span>");
114:                    buf.append(rest);
115:                } else {
116:                    buf.append(sw.toString());
117:                }
118:
119:                return buf.toString();
120:            }
121:
122:            private static boolean hasRootCause(Throwable t) {
123:                return t.getCause() == null
124:                        && (t instanceof  JspException
125:                                || t instanceof  ServletException || t instanceof  ELException);
126:            }
127:
128:            private static Throwable discoverRootCause(Throwable t) {
129:                Throwable cause = null;
130:                if (t instanceof  JspException)
131:                    cause = ((JspException) t).getRootCause();
132:                else if (t instanceof  ServletException)
133:                    cause = ((ServletException) t).getRootCause();
134:                else if (t instanceof  ELException)
135:                    cause = ((ELException) t).getRootCause();
136:                else
137:                    cause = t.getCause();
138:
139:                return cause;
140:            }
141:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.