1 19 20 25 26 package edu.umd.cs.findbugs.gui; 27 28 import java.io.File ; 29 import java.io.IOException ; 30 import java.io.InputStream ; 31 import java.io.StringWriter ; 32 import java.text.MessageFormat ; 33 import java.util.HashMap ; 34 35 import javax.swing.tree.DefaultTreeModel ; 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 61 public class AnalysisRun { 62 private Project project; 63 private FindBugsFrame frame; 64 private String summary; 65 private Logger logger; 66 private IFindBugsEngine findBugs; 67 private SwingGUIBugReporter reporter; 68 private HashMap <String , DefaultTreeModel > treeModelMap; 69 70 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 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 <String , DefaultTreeModel >(); 91 } 92 93 98 public FindBugsFrame getFrame() { 99 return frame; 100 } 101 102 114 public void execute(FindBugsProgress progressCallback) throws IOException , InterruptedException { 115 findBugs.setProgressCallback(progressCallback); 116 117 findBugs.setUserPreferences(UserPreferences.getUserPreferences()); 119 120 findBugs.setAnalysisFeatureSettings(frame.getSettingList()); 122 123 findBugs.execute(); 125 126 if (!SystemProperties.getBoolean("findbugs.noSummary")) { 127 createSummary(reporter.getProjectStats()); 129 } 130 131 } 132 133 private static final String 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 { 140 StringWriter html = new StringWriter (); 141 try { 142 stats.transformSummaryToHTML(html); 143 summary = html.toString(); 144 } catch (Exception e) { 145 logger.logMessage(ConsoleLogger.WARNING, MessageFormat.format(L10N.getLocalString("msg.failedtotransform_txt", "Failed to transform summary: {0}"), new Object []{e.toString()})); 146 summary = MISSING_SUMMARY_MESSAGE; 147 } 148 } 149 150 private static final boolean CREATE_SUMMARY = !SystemProperties.getBoolean("findbugs.noSummary"); 151 152 155 public void loadBugsFromFile(File file) throws IOException , org.dom4j.DocumentException { 156 reporter.getBugCollection().readXML(file, project); 157 158 summary = reporter.getBugCollection().getSummaryHTML(); 160 } 161 162 169 public void loadBugsFromInputStream(InputStream in) throws IOException , DocumentException { 170 reporter.getBugCollection().readXML(in, project); 171 172 summary = reporter.getBugCollection().getSummaryHTML(); 174 } 175 176 179 public void saveBugsToFile(File file) throws IOException { 180 reporter.getBugCollection().writeXML(file, project); 181 } 182 183 186 public void reportAnalysisErrors() { 187 if (reporter.errorsOccurred()) { 188 reporter.getErrorDialog().setSize(750, 520); 189 reporter.getErrorDialog().setLocationRelativeTo(null); reporter.getErrorDialog().setVisible(true); 191 } 192 } 193 194 197 public java.util.Collection <BugInstance> getBugInstances() { 198 return reporter.getBugCollection().getCollection(); 199 } 200 201 207 public void setTreeModel(String groupByOrder, DefaultTreeModel treeModel) { 208 treeModelMap.put(groupByOrder, treeModel); 209 } 210 211 217 public DefaultTreeModel getTreeModel(String groupByOrder) { 218 return treeModelMap.get(groupByOrder); 219 } 220 221 public String getSummary() { 222 return summary; 223 } 224 } 225 | Popular Tags |