KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > HTMLFormatter


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 /*
20  * HTMLFormatter.java
21  *
22  * Created on November 24, 2006, 12:57 PM
23  *
24  * To change this template, choose Tools | Template Manager
25  * and open the template in the editor.
26  */

27
28 package org.netbeans.modules.exceptions;
29
30 import java.text.MessageFormat JavaDoc;
31 import java.util.Date JavaDoc;
32 import java.util.logging.Formatter JavaDoc;
33 import java.util.logging.LogRecord JavaDoc;
34
35 /**
36  *
37  * @author jindra
38  */

39 public class HTMLFormatter extends Formatter JavaDoc {
40     
41     private final static String JavaDoc format = "{0,date} {0,time}";
42     
43     private String JavaDoc lineSeparator = "<br/>\n";
44     
45     public String JavaDoc format(LogRecord JavaDoc record) {
46         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
47         Date JavaDoc date = new Date JavaDoc();
48         date.setTime(record.getMillis());
49         MessageFormat.format(format, date);
50         sb.append("<h5>"+record.getLoggerName()+"</h5>");
51         // if (record.getSourceClassName() != null) {
52
// sb.append(": ");
53
// sb.append(record.getSourceClassName());
54
// }
55
// if (record.getSourceMethodName() != null) {
56
// sb.append(".");
57
// sb.append(record.getSourceMethodName());
58
// }
59
// if ((record.getSourceClassName() != null)||(record.getSourceMethodName() != null)) sb.append(lineSeparator);
60
sb.append("<i>"+record.getLevel().getLocalizedName()+"</i>");
61         sb.append(": ");
62         sb.append(formatMessage(record));
63         sb.append(lineSeparator);
64         if (record.getThrown() != null) print(record.getThrown(), sb);
65         return sb.toString();
66     }
67     
68     private void print(Throwable JavaDoc t, StringBuffer JavaDoc sb){
69         sb.append(t.getMessage()+lineSeparator);
70         sb.append("<blockquote>\n");
71         StackTraceElement JavaDoc[] trace = t.getStackTrace();
72         for (int i = 0; i < trace.length; i++) sb.append(trace[i]+lineSeparator);
73         sb.append("</blockquote>\n");
74         if (t.getCause()!= null){
75             printCause(t.getCause(), sb, trace);
76         }
77     }
78     
79     private void printCause(Throwable JavaDoc t, StringBuffer JavaDoc sb, StackTraceElement JavaDoc[] causedTrace){
80         StackTraceElement JavaDoc[] trace = t.getStackTrace();
81         int m = trace.length-1;
82         int n = causedTrace.length-1;
83         while (m >= 0 && n >=0 && trace[m].equals(causedTrace[n])) {
84             m--; n--;
85         }
86         sb.append("Cause by: " + t.getMessage() + lineSeparator);
87         int framesInCommon = trace.length - 1 - m;
88         sb.append("<blockquote>\n");
89         for (int i=0; i <= m; i++) sb.append(trace[i]+lineSeparator);
90         if (framesInCommon != 0) sb.append(framesInCommon + " more");
91         sb.append("</blockquote>\n");
92         if (t.getCause() != null) printCause(t.getCause(), sb, trace);
93     }
94 }
95
Popular Tags