1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.FileOutputStream ; 23 import java.io.IOException ; 24 import java.io.PrintStream ; 25 import java.util.HashSet ; 26 import java.util.Iterator ; 27 28 import edu.umd.cs.findbugs.classfile.ClassDescriptor; 29 import edu.umd.cs.findbugs.config.CommandLine; 30 31 32 36 public class PrintingBugReporter extends TextUIBugReporter { 37 private String stylesheet = null; 38 private boolean annotationUploadFormat = false; 39 private HashSet <BugInstance> seenAlready = new HashSet <BugInstance>(); 40 41 public void observeClass(ClassDescriptor classDescriptor) { 42 } 45 46 @Override 47 protected void doReportBug(BugInstance bugInstance) { 48 if (seenAlready.add(bugInstance)) { 49 printBug(bugInstance); 50 notifyObservers(bugInstance); 51 } 52 } 53 54 public void finish() { 55 outputStream.close(); 56 } 57 58 class PrintingCommandLine extends CommandLine { 59 60 public PrintingCommandLine() { 61 addSwitch("-longBugCodes", "use long bug codes when generating text"); 62 addSwitch("-annotationUpload", "generate annotations in upload format"); 63 addSwitchWithOptionalExtraPart("-html", "stylesheet", 64 "Generate HTML output (default stylesheet is default.xsl)"); 65 } 66 69 @Override 70 protected void handleOption(String option, String optionExtraPart) throws IOException { 71 if (option.equals("-longBugCodes")) 72 setUseLongBugCodes(true); 73 else if (option.equals("-annotationUpload")) 74 annotationUploadFormat = true; 75 else if (option.equals("-html")) { 76 if (!optionExtraPart.equals("")) { 77 stylesheet = optionExtraPart; 78 } else { 79 stylesheet = "default.xsl"; 80 } 81 } else throw new IllegalArgumentException ("Unknown option '"+option+"'"); 82 } 83 84 87 @Override 88 protected void handleOptionWithArgument(String option, String argument) throws IOException { 89 91 } 92 93 } 94 public static void main(String [] args) throws Exception { 95 96 PrintingBugReporter reporter = new PrintingBugReporter(); 97 PrintingCommandLine commandLine = reporter.new PrintingCommandLine(); 98 99 int argCount = commandLine.parse(args, 0, 2, "Usage: " + PrintingCommandLine.class.getName() 100 + " [options] [<xml results> [<test results]] "); 101 102 DetectorFactoryCollection.instance(); 104 105 if (reporter.stylesheet != null) { 106 xslt(reporter.stylesheet, args, argCount); 108 return; 109 } 110 111 SortedBugCollection bugCollection = new SortedBugCollection(); 112 if (argCount < args.length) 113 bugCollection.readXML(args[argCount++], new Project()); 114 else 115 bugCollection.readXML(System.in, new Project()); 116 117 if (argCount < args.length) 118 reporter.setOutputStream(new PrintStream (new FileOutputStream (args[argCount++]), true)); 119 RuntimeException storedException = null; 120 if (reporter.annotationUploadFormat) { 121 bugCollection.computeBugHashes(); 122 for (Iterator <BugInstance> i = bugCollection.iterator(); i.hasNext();) { 123 BugInstance warning = i.next(); 124 try { 125 String fHash = "fb-"+ warning.getInstanceHash() +"-"+ warning.getInstanceOccurrenceNum() 126 +"-"+warning.getInstanceOccurrenceMax(); 127 128 129 System.out.print("#" + fHash); 130 String key = warning.getUserDesignationKey(); 131 if (key.equals(BugDesignation.UNCLASSIFIED) || key.equals("NEEDS_FURTHER_STUDY")) 132 System.out.print("#-1#"+key); 133 else if (key.equals("MUST_FIX") || key.equals("SHOULD_FIX")) 134 System.out.print("#7#"+key); 135 else System.out.print("#0#"+key); 136 SourceLineAnnotation sourceLine = warning.getPrimarySourceLineAnnotation(); 137 if (sourceLine != null) 138 System.out.println("#" + sourceLine.getSourceFile() + "#"+sourceLine.getStartLine()); 139 else System.out.println("##"); 140 System.out.println(warning.getAnnotationText()); 141 } catch (RuntimeException e) { 142 if (storedException == null) 143 storedException = e; 144 } 145 } 146 } 147 else { 148 for (Iterator <BugInstance> i = bugCollection.iterator(); i.hasNext();) { 149 BugInstance warning = i.next(); 150 try { 151 reporter.printBug(warning); 152 } catch (RuntimeException e) { 153 if (storedException == null) 154 storedException = e; 155 } 156 } 157 } 158 if (storedException != null) throw storedException; 159 160 } 161 162 163 public static void xslt(String stylesheet, String [] args, int argCount) throws Exception { 164 Project proj = new Project(); 165 HTMLBugReporter reporter = new HTMLBugReporter(proj, stylesheet); 166 BugCollection bugCollection = reporter.getBugCollection(); 167 168 if (argCount < args.length) { 169 proj.setProjectFileName(args[argCount]); 170 bugCollection.readXML(args[argCount++], new Project()); 171 } else 172 bugCollection.readXML(System.in, new Project()); 173 174 if (argCount < args.length) 175 reporter.setOutputStream(new PrintStream (new FileOutputStream (args[argCount++]), true)); 176 177 reporter.finish(); 178 } 179 } 180 181 | Popular Tags |