1 20 21 26 27 package edu.umd.cs.findbugs.config; 28 29 import java.io.BufferedInputStream ; 30 import java.io.BufferedOutputStream ; 31 import java.io.File ; 32 import java.io.FileInputStream ; 33 import java.io.FileOutputStream ; 34 import java.io.IOException ; 35 import java.io.InputStream ; 36 import java.io.OutputStream ; 37 import java.util.ArrayList ; 38 import java.util.Collections ; 39 import java.util.Comparator ; 40 import java.util.Enumeration ; 41 import java.util.HashMap ; 42 import java.util.Iterator ; 43 import java.util.LinkedList ; 44 import java.util.List ; 45 import java.util.Map ; 46 import java.util.Properties ; 47 import java.util.Set ; 48 import java.util.Vector ; 49 import java.util.Map.Entry; 50 51 import edu.umd.cs.findbugs.DetectorFactory; 52 import edu.umd.cs.findbugs.DetectorFactoryCollection; 53 import edu.umd.cs.findbugs.FindBugs; 54 import edu.umd.cs.findbugs.SystemProperties; 55 56 62 public class UserPreferences implements Cloneable { 63 private static final String PREF_FILE_NAME = ".Findbugs_prefs"; 64 private static final int MAX_RECENT_FILES = 9; 65 private static final String DETECTOR_THRESHOLD_KEY = "detector_threshold"; 66 private static final String FILTER_SETTINGS_KEY = "filter_settings"; 67 private static final String FILTER_SETTINGS2_KEY = "filter_settings_neg"; 68 private static final String DEFAULT_DIRECTORY = "default_directory"; 69 private LinkedList <String > recentProjectsList = new LinkedList <String >(); 70 private Map <String , Boolean > detectorEnablementMap = new HashMap <String , Boolean >(); 71 private ProjectFilterSettings filterSettings; 72 private static UserPreferences preferencesSingleton = new UserPreferences(); 73 74 private UserPreferences() { 75 this.filterSettings = ProjectFilterSettings.createDefault(); 76 } 77 78 83 public static UserPreferences createDefaultUserPreferences() { 84 return new UserPreferences(); 85 } 86 87 94 public static UserPreferences getUserPreferences() { 95 return preferencesSingleton; 96 } 97 98 102 public void read() { 103 File prefFile = new File (SystemProperties.getProperty("user.home"), PREF_FILE_NAME); 104 if (!prefFile.exists() || !prefFile.isFile()) 105 return; 106 try { 107 read(new FileInputStream (prefFile)); 108 } catch (IOException e) { 109 } 111 } 112 113 120 public void read(InputStream in) throws IOException { 121 BufferedInputStream prefStream = null; 122 Properties props = new Properties (); 123 try { 124 prefStream = new BufferedInputStream (in); 125 props.load(prefStream); 126 } finally { 127 try { 128 if (prefStream != null) 129 prefStream.close(); 130 } catch (IOException ioe) { 131 } 133 } 134 135 if (props.size() == 0) 136 return; 137 for (int i = 0; i < MAX_RECENT_FILES; i++) { 138 String key = "recent" + i; 139 String projectName = (String ) props.get(key); 140 if (projectName != null) 141 recentProjectsList.add(projectName); 142 } 143 144 Iterator propKeysIter = props.keySet().iterator(); 145 while(propKeysIter.hasNext()){ 146 String key = (String ) propKeysIter.next(); 147 if(!key.startsWith("detector") || key.startsWith("detector_")){ 148 continue; 150 } 151 String detectorState = (String ) props.get(key); 152 int pipePos = detectorState.indexOf("|"); 153 if (pipePos >= 0) { 154 String name = detectorState.substring(0, pipePos); 155 String enabled = detectorState.substring(pipePos + 1); 156 detectorEnablementMap.put(name, Boolean.valueOf(enabled)); 157 } 158 } 159 160 if (props.get(FILTER_SETTINGS_KEY) != null) { 161 filterSettings = ProjectFilterSettings.fromEncodedString(props.getProperty(FILTER_SETTINGS_KEY)); 163 } else { 164 String threshold = (String ) props.get(DETECTOR_THRESHOLD_KEY); 167 if (threshold != null) { 168 try { 169 int detectorThreshold = Integer.parseInt(threshold); 170 setUserDetectorThreshold(detectorThreshold); 171 } catch (NumberFormatException nfe) { 172 } 174 } 175 } 176 if (props.get(FILTER_SETTINGS2_KEY) != null) { 177 ProjectFilterSettings.hiddenFromEncodedString(filterSettings, props.getProperty(FILTER_SETTINGS2_KEY)); 179 } 180 181 String dd = (String )props.get(DEFAULT_DIRECTORY); 182 if (dd != null) 183 System.setProperty("user.dir", dd); 184 185 } 186 187 191 public void write() { 192 try { 193 File prefFile = new File (SystemProperties.getProperty("user.home"), PREF_FILE_NAME); 194 write(new FileOutputStream (prefFile)); 195 } catch (IOException e) { 196 if (FindBugs.DEBUG) e.printStackTrace(); } 198 } 199 200 207 public void write(OutputStream out) throws IOException { 208 209 Properties props = new Properties () { 210 214 @SuppressWarnings ("unchecked") 215 @Override 216 public synchronized Enumeration <Object > keys() { 217 Set set = keySet(); 219 return sortKeys(set); 220 } 221 }; 222 223 for (int i = 0; i < recentProjectsList.size(); i++) { 224 String projectName = recentProjectsList.get(i); 225 String key = "recent" + i; 226 props.put(key, projectName); 227 } 228 229 Iterator <Entry<String , Boolean >> it = detectorEnablementMap.entrySet().iterator(); 230 while (it.hasNext()) { 231 Entry<String , Boolean > entry = it.next(); 232 props.put("detector" + entry.getKey(), entry.getKey() + "|" + String.valueOf(entry.getValue().booleanValue())); 233 } 234 235 props.put(FILTER_SETTINGS_KEY, filterSettings.toEncodedString()); 237 props.put(FILTER_SETTINGS2_KEY, filterSettings.hiddenToEncodedString()); 238 239 props.put(DETECTOR_THRESHOLD_KEY, String.valueOf(filterSettings.getMinPriorityAsInt())); 243 244 props.put(DEFAULT_DIRECTORY, SystemProperties.getProperty("user.dir")); 245 246 OutputStream prefStream = null; 247 try { 248 prefStream = new BufferedOutputStream (out); 249 props.store(prefStream, "FindBugs User Preferences"); 250 prefStream.flush(); 251 } finally { 252 try { 253 if (prefStream != null) 254 prefStream.close(); 255 } catch (IOException ioe) { 256 } 257 } 258 } 259 260 269 private Enumeration sortKeys(Set <String > keySet) { 270 List <String > sortedList = new ArrayList <String >(); 271 sortedList.addAll(keySet); 272 Collections.sort(sortedList); 273 return Collections.enumeration(sortedList); 274 } 275 276 281 public List <String > getRecentProjects() { 282 return recentProjectsList; 283 } 284 285 291 public void useProject(String projectName) { 292 removeProject(projectName); 293 recentProjectsList.addFirst(projectName); 294 while (recentProjectsList.size() > MAX_RECENT_FILES) 295 recentProjectsList.removeLast(); 296 } 297 298 303 public void removeProject(String projectName) { 304 Iterator <String > it = recentProjectsList.iterator(); 306 while (it.hasNext()) { 307 if (projectName.equals(it.next())) it.remove(); 309 } 310 } 311 312 319 public void enableDetector(DetectorFactory factory, boolean enable) { 320 detectorEnablementMap.put(factory.getShortName(), enable ? Boolean.TRUE : Boolean.FALSE); 321 } 322 323 329 public boolean isDetectorEnabled(DetectorFactory factory) { 330 String detectorName = factory.getShortName(); 331 Boolean enabled = detectorEnablementMap.get(detectorName); 332 if (enabled == null) { 333 enabled = factory.isDefaultEnabled() ? Boolean.TRUE : Boolean.FALSE; 337 detectorEnablementMap.put(detectorName, enabled); 338 } 339 return enabled.booleanValue(); 340 } 341 342 348 public void enableAllDetectors(boolean enable) { 349 detectorEnablementMap.clear(); 350 351 DetectorFactoryCollection factoryCollection = DetectorFactoryCollection.instance(); 352 for (Iterator <DetectorFactory> i = factoryCollection.factoryIterator(); i.hasNext();) { 353 DetectorFactory factory = i.next(); 354 detectorEnablementMap.put( 355 factory.getShortName(), enable ? Boolean.TRUE : Boolean.FALSE); 356 } 357 } 358 359 364 public void setProjectFilterSettings(ProjectFilterSettings filterSettings) { 365 this.filterSettings = filterSettings; 366 } 367 368 373 public ProjectFilterSettings getFilterSettings() { 374 return this.filterSettings; 375 } 376 377 382 public int getUserDetectorThreshold() { 383 return filterSettings.getMinPriorityAsInt(); 384 } 385 386 391 public void setUserDetectorThreshold(int threshold) { 392 String minPriority = ProjectFilterSettings.getIntPriorityAsString(threshold); 393 filterSettings.setMinPriority(minPriority); 394 } 395 396 401 public void setUserDetectorThreshold(String threshold) { 402 filterSettings.setMinPriority(threshold); 403 } 404 @Override 406 public boolean equals(Object obj) { 407 if (obj == null || obj.getClass() != this.getClass()) 408 return false; 409 410 UserPreferences other = (UserPreferences) obj; 411 412 return recentProjectsList.equals(other.recentProjectsList) 413 && detectorEnablementMap.equals(other.detectorEnablementMap) 414 && filterSettings.equals(other.filterSettings); 415 } 416 417 @Override 419 public int hashCode() { 420 return recentProjectsList.hashCode() 421 + detectorEnablementMap.hashCode() 422 + filterSettings.hashCode(); 423 } 424 425 @Override 427 public Object clone() { 428 try { 429 UserPreferences dup = (UserPreferences) super.clone(); 430 431 dup.recentProjectsList = new LinkedList <String >(); 432 dup.recentProjectsList.addAll(this.recentProjectsList); 433 434 dup.detectorEnablementMap = new HashMap <String , Boolean >(); 435 dup.detectorEnablementMap.putAll(this.detectorEnablementMap); 436 437 dup.filterSettings = (ProjectFilterSettings) this.filterSettings.clone(); 438 439 return dup; 440 } catch (CloneNotSupportedException e) { 441 throw new AssertionError (e); 442 } 443 } 444 } 445 446 | Popular Tags |