KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > PrintingBugReporter


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003,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;
21
22 import java.io.FileOutputStream JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.PrintStream JavaDoc;
25 import java.util.HashSet JavaDoc;
26 import java.util.Iterator JavaDoc;
27
28 import edu.umd.cs.findbugs.classfile.ClassDescriptor;
29 import edu.umd.cs.findbugs.config.CommandLine;
30
31
32 /**
33  * A simple BugReporter which simply prints the formatted message
34  * to the output stream.
35  */

36 public class PrintingBugReporter extends TextUIBugReporter {
37     private String JavaDoc stylesheet = null;
38     private boolean annotationUploadFormat = false;
39     private HashSet JavaDoc<BugInstance> seenAlready = new HashSet JavaDoc<BugInstance>();
40
41     public void observeClass(ClassDescriptor classDescriptor) {
42         // Don't need to do anything special, since we won't be
43
// reporting statistics.
44
}
45
46     @Override JavaDoc
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         /* (non-Javadoc)
67          * @see edu.umd.cs.findbugs.config.CommandLine#handleOption(java.lang.String, java.lang.String)
68          */

69         @Override JavaDoc
70         protected void handleOption(String JavaDoc option, String JavaDoc optionExtraPart) throws IOException JavaDoc {
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 JavaDoc("Unknown option '"+option+"'");
82         }
83
84         /* (non-Javadoc)
85          * @see edu.umd.cs.findbugs.config.CommandLine#handleOptionWithArgument(java.lang.String, java.lang.String)
86          */

87         @Override JavaDoc
88         protected void handleOptionWithArgument(String JavaDoc option, String JavaDoc argument) throws IOException JavaDoc {
89             // TODO Auto-generated method stub
90

91         }
92         
93     }
94     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
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         // Load plugins, in order to get message files
103
DetectorFactoryCollection.instance();
104         
105         if (reporter.stylesheet != null) {
106             // actually do xsl via HTMLBugReporter instead of PrintingBugReporter
107
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 JavaDoc(new FileOutputStream JavaDoc(args[argCount++]), true));
119         RuntimeException JavaDoc storedException = null;
120         if (reporter.annotationUploadFormat) {
121             bugCollection.computeBugHashes();
122             for (Iterator JavaDoc<BugInstance> i = bugCollection.iterator(); i.hasNext();) {
123                 BugInstance warning = i.next();
124                 try {
125                     String JavaDoc fHash = "fb-"+ warning.getInstanceHash() +"-"+ warning.getInstanceOccurrenceNum()
126                     +"-"+warning.getInstanceOccurrenceMax();
127                     
128                     
129                 System.out.print("#" + fHash);
130                 String JavaDoc 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 JavaDoc e) {
142                     if (storedException == null)
143                     storedException = e;
144                 }
145             }
146         }
147         else {
148         for (Iterator JavaDoc<BugInstance> i = bugCollection.iterator(); i.hasNext();) {
149             BugInstance warning = i.next();
150             try {
151             reporter.printBug(warning);
152             } catch (RuntimeException JavaDoc 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 JavaDoc stylesheet, String JavaDoc[] args, int argCount) throws Exception JavaDoc {
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 JavaDoc(new FileOutputStream JavaDoc(args[argCount++]), true));
176
177         reporter.finish();
178     }
179 }
180
181 // vim:ts=4
182
Popular Tags