KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > umd > cs > findbugs > gui > AnalysisRun


1 /*
2  * FindBugs - Find bugs in Java programs
3  * Copyright (C) 2003-2005, 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 /*
21  * AnalysisRun.java
22  *
23  * Created on April 1, 2003, 2:24 PM
24  */

25
26 package edu.umd.cs.findbugs.gui;
27
28 import java.io.File JavaDoc;
29 import java.io.IOException JavaDoc;
30 import java.io.InputStream JavaDoc;
31 import java.io.StringWriter JavaDoc;
32 import java.text.MessageFormat JavaDoc;
33 import java.util.HashMap JavaDoc;
34
35 import javax.swing.tree.DefaultTreeModel JavaDoc;
36
37 import org.dom4j.DocumentException;
38
39 import edu.umd.cs.findbugs.BugInstance;
40 import edu.umd.cs.findbugs.Detector;
41 import edu.umd.cs.findbugs.DetectorFactoryCollection;
42 import edu.umd.cs.findbugs.FindBugs;
43 import edu.umd.cs.findbugs.FindBugs2;
44 import edu.umd.cs.findbugs.FindBugsProgress;
45 import edu.umd.cs.findbugs.IFindBugsEngine;
46 import edu.umd.cs.findbugs.L10N;
47 import edu.umd.cs.findbugs.Project;
48 import edu.umd.cs.findbugs.ProjectStats;
49 import edu.umd.cs.findbugs.SystemProperties;
50 import edu.umd.cs.findbugs.config.UserPreferences;
51
52
53
54 /**
55  * Representation of a run of the FindBugs analysis on a Project.
56  * This class has convenient methods which can be used to extract
57  * bug reports in various interesting ways.
58  *
59  * @author David Hovemeyer
60  */

61 public class AnalysisRun {
62     private Project project;
63     private FindBugsFrame frame;
64     private String JavaDoc summary;
65     private Logger logger;
66     private IFindBugsEngine findBugs;
67     private SwingGUIBugReporter reporter;
68     private HashMap JavaDoc<String JavaDoc, DefaultTreeModel JavaDoc> treeModelMap;
69
70     /**
71      * Creates a new instance of AnalysisRun.
72      */

73     public AnalysisRun(Project project, FindBugsFrame frame) {
74         this.project = project;
75         this.frame = frame;
76         this.logger = frame.getLogger();
77         this.reporter = new SwingGUIBugReporter(this);
78         this.reporter.setPriorityThreshold(Detector.EXP_PRIORITY);
79         if (true) {
80             // Eat our own dogfood
81
FindBugs2 engine = new FindBugs2();
82             engine.setBugReporter(reporter);
83             engine.setProject(project);
84             engine.setDetectorFactoryCollection(DetectorFactoryCollection.instance());
85             
86             this.findBugs = engine;
87         } else {
88             this.findBugs = new FindBugs(reporter, project);
89         }
90         this.treeModelMap = new HashMap JavaDoc<String JavaDoc, DefaultTreeModel JavaDoc>();
91     }
92     
93     /**
94      * Get the FindBugsFrame which created this analysis run.
95      *
96      * @return the FindBugsFrame
97      */

98     public FindBugsFrame getFrame() {
99         return frame;
100     }
101
102     /**
103      * Run the analysis.
104      * This should be done in a separate thread (not the GUI event thread).
105      * The progress callback can be used to update the user interface to
106      * reflect the progress of the analysis. The GUI may cancel the analysis
107      * by interrupting the analysis thread, in which case InterruptedException
108      * will be thrown by this method.
109      *
110      * @param progressCallback the progress callback
111      * @throws IOException if an I/O error occurs during the analysis
112      * @throws InterruptedException if the analysis thread is interrupted
113      */

114     public void execute(FindBugsProgress progressCallback) throws IOException JavaDoc, InterruptedException JavaDoc {
115         findBugs.setProgressCallback(progressCallback);
116         
117         // Honor current UserPreferences
118
findBugs.setUserPreferences(UserPreferences.getUserPreferences());
119         
120         // Set analysis feature settings
121
findBugs.setAnalysisFeatureSettings(frame.getSettingList());
122
123         // Run the analysis!
124
findBugs.execute();
125
126         if (!SystemProperties.getBoolean("findbugs.noSummary")) {
127             // Get the summary!
128
createSummary(reporter.getProjectStats());
129         }
130
131     }
132
133     private static final String JavaDoc MISSING_SUMMARY_MESSAGE =
134             "<html><head><title>Could not format summary</title></head>" +
135             "<body><h1>Could not format summary</h1>" +
136             "<p> Please report this failure to <a HREF=\"findbugs-discuss@cs.umd.edu\">" +
137             "findbugs-discuss@cs.umd.edu</a>.</body></html>";
138
139     private void createSummary(ProjectStats stats) throws IOException JavaDoc {
140         StringWriter JavaDoc html = new StringWriter JavaDoc();
141         try {
142             stats.transformSummaryToHTML(html);
143             summary = html.toString();
144         } catch (Exception JavaDoc e) {
145             logger.logMessage(ConsoleLogger.WARNING, MessageFormat.format(L10N.getLocalString("msg.failedtotransform_txt", "Failed to transform summary: {0}"), new Object JavaDoc[]{e.toString()}));
146             summary = MISSING_SUMMARY_MESSAGE;
147         }
148     }
149
150     private static final boolean CREATE_SUMMARY = !SystemProperties.getBoolean("findbugs.noSummary");
151
152     /**
153      * Load bugs from a file.
154      */

155     public void loadBugsFromFile(File JavaDoc file) throws IOException JavaDoc, org.dom4j.DocumentException {
156         reporter.getBugCollection().readXML(file, project);
157
158         // Update summary stats
159
summary = reporter.getBugCollection().getSummaryHTML();
160     }
161     
162     /**
163      * Load bugs from an InputStream.
164      *
165      * @param in the InputStream
166      * @throws IOException
167      * @throws DocumentException
168      */

169     public void loadBugsFromInputStream(InputStream JavaDoc in) throws IOException JavaDoc, DocumentException {
170         reporter.getBugCollection().readXML(in, project);
171         
172         // Update summary stats
173
summary = reporter.getBugCollection().getSummaryHTML();
174     }
175
176     /**
177      * Save bugs to a file.
178      */

179     public void saveBugsToFile(File JavaDoc file) throws IOException JavaDoc {
180         reporter.getBugCollection().writeXML(file, project);
181     }
182
183     /**
184      * Report any errors that may have occurred during analysis.
185      */

186     public void reportAnalysisErrors() {
187         if (reporter.errorsOccurred()) {
188             reporter.getErrorDialog().setSize(750, 520);
189             reporter.getErrorDialog().setLocationRelativeTo(null); // center the dialog
190
reporter.getErrorDialog().setVisible(true);
191         }
192     }
193
194     /**
195      * Return the collection of BugInstances.
196      */

197     public java.util.Collection JavaDoc<BugInstance> getBugInstances() {
198         return reporter.getBugCollection().getCollection();
199     }
200
201     /**
202      * Set the tree model to be used in the BugTree.
203      *
204      * @param groupByOrder the grouping order that the tree model will conform to
205      * @param treeModel the tree model
206      */

207     public void setTreeModel(String JavaDoc groupByOrder, DefaultTreeModel JavaDoc treeModel) {
208         treeModelMap.put(groupByOrder, treeModel);
209     }
210
211     /**
212      * Get the tree model to be used in the BugTree.
213      *
214      * @param groupByOrder the grouping order that the tree model conforms to
215      * @return the tree model
216      */

217     public DefaultTreeModel JavaDoc getTreeModel(String JavaDoc groupByOrder) {
218         return treeModelMap.get(groupByOrder);
219     }
220
221     public String JavaDoc getSummary() {
222         return summary;
223     }
224 }
225
Popular Tags