1 19 20 package edu.umd.cs.findbugs; 21 22 import java.io.Serializable ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Comparator ; 26 import java.util.Enumeration ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Locale ; 32 import java.util.MissingResourceException ; 33 import java.util.ResourceBundle ; 34 35 import edu.umd.cs.findbugs.annotations.CheckForNull; 36 import edu.umd.cs.findbugs.annotations.NonNull; 37 38 44 public class I18N { 45 private static final boolean DEBUG = SystemProperties.getBoolean("i18n.debug"); 46 47 public static final Comparator <String > designationKeyComparator = new DesignationKeyComparator(); 48 public static final Locale defaultLocale = Locale.getDefault(); 49 private final ResourceBundle annotationDescriptionBundle; 50 private final ResourceBundle englishAnnotationDescriptionBundle; private final HashMap <String , BugCategory> categoryDescriptionMap; 53 private final ResourceBundle userDesignationBundle; 54 private final HashMap <String , BugPattern> bugPatternMap; 55 private final HashMap <String , BugCode> bugCodeMap; 56 57 private I18N() { 58 annotationDescriptionBundle = ResourceBundle.getBundle("edu.umd.cs.findbugs.FindBugsAnnotationDescriptions", defaultLocale); 59 englishAnnotationDescriptionBundle = ResourceBundle.getBundle("edu.umd.cs.findbugs.FindBugsAnnotationDescriptions", Locale.ENGLISH); 60 categoryDescriptionMap = new HashMap <String , BugCategory>(); 62 userDesignationBundle = ResourceBundle.getBundle("edu.umd.cs.findbugs.UserDesignations", defaultLocale); 63 bugPatternMap = new HashMap <String , BugPattern>(); 64 bugCodeMap = new HashMap <String , BugCode>(); 65 } 66 67 private static final I18N theInstance = new I18N(); 68 69 72 public static I18N instance() { 73 return theInstance; 74 } 75 76 81 public void registerBugPattern(BugPattern bugPattern) { 82 bugPatternMap.put(bugPattern.getType(), bugPattern); 83 } 84 85 91 public @CheckForNull BugPattern lookupBugPattern(String bugType) { 92 DetectorFactoryCollection.instance(); return bugPatternMap.get(bugType); 94 } 95 96 99 public Iterator <BugPattern> bugPatternIterator() { 100 DetectorFactoryCollection.instance(); 102 return bugPatternMap.values().iterator(); 103 } 104 105 110 public void registerBugCode(BugCode bugCode) { 111 bugCodeMap.put(bugCode.getAbbrev(), bugCode); 112 } 113 114 120 public @NonNull String getMessage(String key) { 121 BugPattern bugPattern = bugPatternMap.get(key); 122 if (bugPattern == null) 123 return L10N.getLocalString("err.missing_pattern", "Error: missing bug pattern for key") + " " + key; 124 return bugPattern.getAbbrev() + ": " + bugPattern.getLongDescription(); 125 } 126 127 134 public @NonNull String getShortMessage(String key) { 135 BugPattern bugPattern = bugPatternMap.get(key); 136 if (bugPattern == null) 137 return L10N.getLocalString("err.missing_pattern", "Error: missing bug pattern for key") + " " + key; 138 return bugPattern.getAbbrev() + ": " + bugPattern.getShortDescription(); 139 } 140 public @NonNull String getShortMessageWithoutCode(String key) { 141 BugPattern bugPattern = bugPatternMap.get(key); 142 if (bugPattern == null) 143 return L10N.getLocalString("err.missing_pattern", "Error: missing bug pattern for key") + " " + key; 144 return bugPattern.getShortDescription(); 145 } 146 147 152 public @NonNull String getDetailHTML(String key) { 153 BugPattern bugPattern = bugPatternMap.get(key); 154 if (bugPattern == null) 155 return L10N.getLocalString("err.missing_pattern", "Error: missing bug pattern for key") + " " + key; 156 return bugPattern.getDetailHTML(); 157 } 158 159 167 public String getAnnotationDescription(String key) { 168 try{ 169 return annotationDescriptionBundle.getString(key); 170 } 171 catch(MissingResourceException mre){ 172 if(DEBUG) 173 return "TRANSLATE(key=" + key + ") (param={0}}"; 174 else 175 return englishAnnotationDescriptionBundle.getString(key); 176 } 177 } 178 179 189 public @NonNull String getBugTypeDescription(String shortBugType) { 190 BugCode bugCode = bugCodeMap.get(shortBugType); 191 if (bugCode == null) 192 return L10N.getLocalString("err.missing_code", "Error: missing bug code for key") + " " + shortBugType; 193 return bugCode.getDescription(); 194 } 195 196 204 public boolean registerBugCategory(String category, BugCategory bc) { 205 if (categoryDescriptionMap.get(category) != null) return false; 206 categoryDescriptionMap.put(category, bc); 207 return true; 208 } 209 210 217 public BugCategory getBugCategory(String category) { 218 return categoryDescriptionMap.get(category); 219 } 220 221 228 public String getBugCategoryDescription(String category) { 229 BugCategory bc = categoryDescriptionMap.get(category); 230 return (bc!=null ? bc.getShortDescription() : category); 231 } 232 233 239 public Collection <String > getBugCategories() { 240 DetectorFactoryCollection.instance(); 242 return categoryDescriptionMap.keySet(); } 244 public Collection <BugCategory> getBugCategoryObjects() { 245 DetectorFactoryCollection.instance(); 247 return categoryDescriptionMap.values(); } 249 256 public String getUserDesignation(String key) { 257 return userDesignationBundle.getString(key); 258 } 259 260 266 public List <String > getUserDesignationKeys() { 267 List <String > result = new LinkedList <String >(); 268 for (Enumeration <String > e = userDesignationBundle.getKeys(); e.hasMoreElements(); ) { 269 String key = e.nextElement(); 270 result.add(key); 271 } 272 return result; 273 } 274 275 284 public List <String > getUserDesignationKeys(boolean sort) { 285 List <String > result = getUserDesignationKeys(); 286 if (sort) Collections.sort(result, designationKeyComparator); 287 return result; 288 } 289 290 291 private static class DesignationKeyComparator implements Comparator <String >, Serializable { 292 private static final long serialVersionUID = 1L; 293 295 public int compare(String lKey, String rKey) { 296 int lCat = categoryOf(lKey); 297 int catDiff = lCat - categoryOf(rKey); 298 if (catDiff != 0 || lCat != 0) return catDiff; 299 return lKey.compareTo(rKey); 301 } 302 private static int categoryOf(String key) { 303 if (key == null) return -30; 304 if (key.length() <= 0) return -29; 305 switch (key.charAt(0)) { 306 case 'U': if ("UNCLASSIFIED".equals(key)) return 20; 307 break; 308 case 'B': if ("BAD_ANALYSIS".equals(key)) return 15; 309 break; 310 case 'N': if ("NEEDS_STUDY".equals(key)) return -22; 311 if ("NOT_A_BUG".equals(key)) return -15; 312 break; 313 case 'M': if ("MOSTLY_HARMLESS".equals(key)) return -10; 314 if ("MUST_FIX".equals(key)) return 10; 315 break; 316 case 'S': if ("SHOULD_FIX".equals(key)) return 5; 317 } 318 return 0; } 320 } 321 322 } 323 324 | Popular Tags |