1 19 20 package edu.umd.cs.findbugs.tools.html; 21 22 import java.io.IOException ; 23 import java.io.OutputStream ; 24 import java.io.PrintStream ; 25 import java.io.Serializable ; 26 import java.util.Comparator ; 27 import java.util.Set ; 28 import java.util.TreeSet ; 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 <BugPattern> bugPatternSet; 36 private String headerText; 37 private String beginBodyText; 38 private String prologueText; 39 private String endBodyText; 40 private boolean unabridged; 41 42 private static final String [] TABLE_COLORS = new String []{ "#eeeeee", "#ffffff" }; 43 44 private static class BugPatternComparator implements Comparator <BugPattern>, Serializable { 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 docTitle, OutputStream out) { 55 super(docTitle, out); 56 this.bugPatternSet = new TreeSet <BugPattern>(new BugPatternComparator()); 57 this.headerText = this.beginBodyText = this.prologueText = this.endBodyText = ""; 58 } 59 60 public void setHeaderText(String headerText) { 61 this.headerText = headerText; 62 } 63 64 public void setBeginBodyText(String beginBodyText) { 65 this.beginBodyText = beginBodyText; 66 } 67 68 public void setPrologueText(String prologueText) { 69 this.prologueText = prologueText; 70 } 71 72 public void setEndBodyText(String endBodyText) { 73 this.endBodyText = endBodyText; 74 } 75 76 @Override 77 protected void prologue() throws IOException { 78 super.prologue(); 79 PrintStream out = getPrintStream(); 80 out.println(prologueText); 81 } 82 83 @Override 84 protected void emit(BugPattern bugPattern) throws IOException { 85 bugPatternSet.add(bugPattern); 86 } 87 88 @Override 89 protected void epilogue() throws IOException { 90 emitSummaryTable(); 91 emitBugDescriptions(); 92 super.epilogue(); 93 } 94 95 @Override 96 protected void header() throws IOException { 97 PrintStream out = getPrintStream(); 98 out.println(headerText); 99 } 100 101 102 @Override 103 protected void beginBody() throws IOException { 104 PrintStream out = getPrintStream(); 105 out.println(beginBodyText); 106 } 107 108 109 @Override 110 protected void endBody() throws IOException { 111 PrintStream out = getPrintStream(); 112 out.println(endBodyText); 113 } 114 115 private void emitSummaryTable() { 116 PrintStream 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 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 153 protected boolean isEnabled(DetectorFactory factory) { 154 return unabridged || super.isEnabled(factory); 155 } 156 157 public static void main(String [] args) throws Exception { 158 int argCount = 0; 159 boolean unabridged = false; 160 161 if (argCount < args.length && args[argCount].equals("-unabridged")) { 162 ++argCount; 163 unabridged = true; 166 } 167 168 if (Boolean.getBoolean("findbugs.bugdesc.unabridged")) { 169 unabridged = true; 170 } 171 172 String 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 | Popular Tags |