KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > tools > html > PrettyPrintBugDescriptions


1 /*
2  * Generate HTML file containing bug descriptions
3  * Copyright (C) 2004, University of Maryland
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19
20 package edu.umd.cs.findbugs.tools.html;
21
22 import java.io.IOException JavaDoc;
23 import java.io.OutputStream JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.io.Serializable JavaDoc;
26 import java.util.Comparator JavaDoc;
27 import java.util.Set JavaDoc;
28 import java.util.TreeSet JavaDoc;
29
30 import edu.umd.cs.findbugs.BugPattern;
31 import edu.umd.cs.findbugs.DetectorFactory;
32 import edu.umd.cs.findbugs.I18N;
33
34 public class PrettyPrintBugDescriptions extends PlainPrintBugDescriptions {
35     private Set JavaDoc<BugPattern> bugPatternSet;
36     private String JavaDoc headerText;
37     private String JavaDoc beginBodyText;
38     private String JavaDoc prologueText;
39     private String JavaDoc endBodyText;
40     private boolean unabridged;
41
42     private static final String JavaDoc[] TABLE_COLORS = new String JavaDoc[]{ "#eeeeee", "#ffffff" };
43
44     private static class BugPatternComparator implements Comparator JavaDoc<BugPattern>, Serializable JavaDoc {
45         public int compare(BugPattern a, BugPattern b) {
46             int cmp = a.getCategory().compareTo(b.getCategory());
47             if (cmp != 0) return cmp;
48             cmp = a.getAbbrev().compareTo(b.getAbbrev());
49             if (cmp != 0) return cmp;
50             return a.getType().compareTo(b.getType());
51         }
52     }
53
54     public PrettyPrintBugDescriptions(String JavaDoc docTitle, OutputStream JavaDoc out) {
55         super(docTitle, out);
56         this.bugPatternSet = new TreeSet JavaDoc<BugPattern>(new BugPatternComparator());
57         this.headerText = this.beginBodyText = this.prologueText = this.endBodyText = "";
58     }
59
60     public void setHeaderText(String JavaDoc headerText) {
61         this.headerText = headerText;
62     }
63
64     public void setBeginBodyText(String JavaDoc beginBodyText) {
65         this.beginBodyText = beginBodyText;
66     }
67
68     public void setPrologueText(String JavaDoc prologueText) {
69         this.prologueText = prologueText;
70     }
71
72     public void setEndBodyText(String JavaDoc endBodyText) {
73         this.endBodyText = endBodyText;
74     }
75
76     @Override JavaDoc
77     protected void prologue() throws IOException JavaDoc {
78         super.prologue();
79         PrintStream JavaDoc out = getPrintStream();
80         out.println(prologueText);
81     }
82
83     @Override JavaDoc
84     protected void emit(BugPattern bugPattern) throws IOException JavaDoc {
85         bugPatternSet.add(bugPattern);
86     }
87
88     @Override JavaDoc
89     protected void epilogue() throws IOException JavaDoc {
90         emitSummaryTable();
91         emitBugDescriptions();
92         super.epilogue();
93     }
94
95     @Override JavaDoc
96     protected void header() throws IOException JavaDoc {
97         PrintStream JavaDoc out = getPrintStream();
98         out.println(headerText);
99     }
100
101     /** Extra stuff printed at the beginning of the &lt;body&gt; element. */
102     @Override JavaDoc
103     protected void beginBody() throws IOException JavaDoc {
104         PrintStream JavaDoc out = getPrintStream();
105         out.println(beginBodyText);
106     }
107
108     /** Extra stuff printed at the end of the &lt;body&gt; element. */
109     @Override JavaDoc
110     protected void endBody() throws IOException JavaDoc {
111         PrintStream JavaDoc out = getPrintStream();
112         out.println(endBodyText);
113     }
114
115     private void emitSummaryTable() {
116         PrintStream JavaDoc out = getPrintStream();
117
118         out.println("<h2>Summary</h2>");
119
120         out.println("<table width=\"100%\">");
121
122         out.println("<tr bgcolor=\"#b9b9fe\"><th>Description</th><th>Category</th></tr>");
123
124         ColorAlternator colorAlternator = new ColorAlternator(TABLE_COLORS);
125
126         for (BugPattern bugPattern : bugPatternSet) {
127             out.print("<tr bgcolor=\"" + colorAlternator.nextColor() + "\">");
128             out.print("<td><a HREF=\"#" + bugPattern.getType() + "\">" +
129                     bugPattern.getAbbrev() + ": " + bugPattern.getShortDescription() +
130                     "</a></td>");
131             out.println("<td>" + I18N.instance().getBugCategoryDescription(bugPattern.getCategory()) + "</td></tr>");
132         }
133
134         out.println("</table>");
135     }
136
137     private void emitBugDescriptions() {
138         PrintStream JavaDoc out = getPrintStream();
139
140         out.println("<h2>Descriptions</h2>");
141
142         for (BugPattern bugPattern : bugPatternSet) {
143             out.println("<h3><a name=\"" +
144                     bugPattern.getType() + "\">" +
145                     bugPattern.getAbbrev() + ": " + bugPattern.getShortDescription() +
146                     " (" + bugPattern.getType() + ")" +
147                     "</a></h3>");
148             out.println(bugPattern.getDetailText());
149         }
150     }
151
152     @Override JavaDoc
153     protected boolean isEnabled(DetectorFactory factory) {
154         return unabridged || super.isEnabled(factory);
155     }
156
157     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
158         int argCount = 0;
159         boolean unabridged = false;
160         
161         if (argCount < args.length && args[argCount].equals("-unabridged")) {
162             ++argCount;
163             // Unabridged mode: emit all warnings reported by at least one
164
// detector, even for disabled detectors.
165
unabridged = true;
166         }
167         
168         if (Boolean.getBoolean("findbugs.bugdesc.unabridged")) {
169             unabridged = true;
170         }
171
172         String JavaDoc docTitle = "FindBugs Bug Descriptions";
173         if (argCount < args.length) {
174             docTitle = args[argCount++];
175         }
176         PrettyPrintBugDescriptions pp = new PrettyPrintBugDescriptions(docTitle, System.out);
177         
178         if (argCount < args.length) {
179             pp.setHeaderText(args[argCount++]);
180         }
181         if (argCount < args.length) {
182             pp.setBeginBodyText(args[argCount++]);
183         }
184         if (argCount < args.length) {
185             pp.setPrologueText(args[argCount++]);
186         }
187         if (argCount < args.length) {
188             pp.setEndBodyText(args[argCount++]);
189         }
190
191         if (unabridged)
192             pp.unabridged = true;
193
194         pp.print();
195     }
196 }
197
198 // vim:ts=3
199
Popular Tags