KickJava   Java API By Example, From Geeks To Geeks.

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


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.Serializable JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Comparator JavaDoc;
26 import java.util.Enumeration JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.LinkedList JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Locale JavaDoc;
32 import java.util.MissingResourceException JavaDoc;
33 import java.util.ResourceBundle JavaDoc;
34
35 import edu.umd.cs.findbugs.annotations.CheckForNull;
36 import edu.umd.cs.findbugs.annotations.NonNull;
37
38 /**
39  * Singleton responsible for returning localized strings for information
40  * returned to the user.
41  *
42  * @author David Hovemeyer
43  */

44 public class I18N {
45     private static final boolean DEBUG = SystemProperties.getBoolean("i18n.debug");
46     /** a Comparator to compare user designation keys */
47     public static final Comparator JavaDoc<String JavaDoc> designationKeyComparator = new DesignationKeyComparator();
48     public static final Locale JavaDoc defaultLocale = Locale.getDefault();
49     private final ResourceBundle JavaDoc annotationDescriptionBundle;
50     private final ResourceBundle JavaDoc englishAnnotationDescriptionBundle; //used if local one can't be found
51
//private final ResourceBundle bugCategoryDescriptionBundle;
52
private final HashMap JavaDoc<String JavaDoc, BugCategory> categoryDescriptionMap;
53     private final ResourceBundle JavaDoc userDesignationBundle;
54     private final HashMap JavaDoc<String JavaDoc, BugPattern> bugPatternMap;
55     private final HashMap JavaDoc<String JavaDoc, 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         //bugCategoryDescriptionBundle = ResourceBundle.getBundle("edu.umd.cs.findbugs.BugCategoryDescriptions");
61
categoryDescriptionMap = new HashMap JavaDoc<String JavaDoc, BugCategory>();
62         userDesignationBundle = ResourceBundle.getBundle("edu.umd.cs.findbugs.UserDesignations", defaultLocale);
63         bugPatternMap = new HashMap JavaDoc<String JavaDoc, BugPattern>();
64         bugCodeMap = new HashMap JavaDoc<String JavaDoc, BugCode>();
65     }
66
67     private static final I18N theInstance = new I18N();
68
69     /**
70      * Get the single object instance.
71      */

72     public static I18N instance() {
73         return theInstance;
74     }
75
76     /**
77      * Register a BugPattern.
78      *
79      * @param bugPattern the BugPattern
80      */

81     public void registerBugPattern(BugPattern bugPattern) {
82         bugPatternMap.put(bugPattern.getType(), bugPattern);
83     }
84
85     /**
86      * Look up bug pattern.
87      *
88      * @param bugType the bug type for the bug pattern
89      * @return the BugPattern, or null if it can't be found
90      */

91     public @CheckForNull BugPattern lookupBugPattern(String JavaDoc bugType) {
92         DetectorFactoryCollection.instance(); // ensure detectors loaded
93
return bugPatternMap.get(bugType);
94     }
95
96     /**
97      * Get an Iterator over all registered bug patterns.
98      */

99     public Iterator JavaDoc<BugPattern> bugPatternIterator() {
100         DetectorFactoryCollection.instance(); // ensure detectors loaded
101

102         return bugPatternMap.values().iterator();
103     }
104
105     /**
106      * Register a BugCode.
107      *
108      * @param bugCode the BugCode
109      */

110     public void registerBugCode(BugCode bugCode) {
111         bugCodeMap.put(bugCode.getAbbrev(), bugCode);
112     }
113
114     /**
115      * Get a message string.
116      * This is a format pattern for describing an entire bug instance in a single line.
117      *
118      * @param key which message to retrieve
119      */

120     public @NonNull String JavaDoc getMessage(String JavaDoc 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     /**
128      * Get a short message string.
129      * This is a concrete string (not a format pattern) which briefly describes
130      * the type of bug, without mentioning particular a particular class/method/field.
131      *
132      * @param key which short message to retrieve
133      */

134     public @NonNull String JavaDoc getShortMessage(String JavaDoc 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 JavaDoc getShortMessageWithoutCode(String JavaDoc 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     /**
148      * Get an HTML document describing the bug pattern for given key in detail.
149      *
150      * @param key which HTML details for retrieve
151      */

152     public @NonNull String JavaDoc getDetailHTML(String JavaDoc 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     /**
160      * Get an annotation description string.
161      * This is a format pattern which will describe a BugAnnotation in the
162      * context of a particular bug instance. Its single format argument
163      * is the BugAnnotation.
164      *
165      * @param key the annotation description to retrieve
166      */

167     public String JavaDoc getAnnotationDescription(String JavaDoc key) {
168         try{
169             return annotationDescriptionBundle.getString(key);
170         }
171         catch(MissingResourceException JavaDoc mre){
172             if(DEBUG)
173                 return "TRANSLATE(key=" + key + ") (param={0}}";
174             else
175                 return englishAnnotationDescriptionBundle.getString(key);
176         }
177     }
178
179     /**
180      * Get a description for given "bug type".
181      * FIXME: this is referred to elsewhere as the "bug code" or "bug abbrev".
182      * Should make the terminology consistent everywhere.
183      * In this case, the bug type refers to the short prefix code prepended to
184      * the long and short bug messages.
185      *
186      * @param shortBugType the short bug type code
187      * @return the description of that short bug type code means
188      */

189     public @NonNull String JavaDoc getBugTypeDescription(String JavaDoc 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     /**
197      * Set the metadata for a bug category.
198      * If the category's metadata has already been set, this does nothing.
199      *
200      * @param category the category key
201      * @param bc the BugCategory object holding the metadata for the category
202      * @return false if the category's metadata has already been set, true otherwise
203      */

204     public boolean registerBugCategory(String JavaDoc category, BugCategory bc) {
205         if (categoryDescriptionMap.get(category) != null) return false;
206         categoryDescriptionMap.put(category, bc);
207         return true;
208     }
209
210     /**
211      * Get the BugCategory object for a category key.
212      * Returns null if no BugCategory object can be found.
213      *
214      * @param category the category key
215      * @return the BugCategory object (may be null)
216      */

217     public BugCategory getBugCategory(String JavaDoc category) {
218         return categoryDescriptionMap.get(category);
219     }
220
221     /**
222      * Get the description of a bug category.
223      * Returns the category if no description can be found.
224      *
225      * @param category the category
226      * @return the description of the category
227      */

228     public String JavaDoc getBugCategoryDescription(String JavaDoc category) {
229         BugCategory bc = categoryDescriptionMap.get(category);
230         return (bc!=null ? bc.getShortDescription() : category);
231     }
232
233     /**
234      * Get a Collection containing all known bug category keys.
235      * E.g., "CORRECTNESS", "MT_CORRECTNESS", "PERFORMANCE", etc.
236      *
237      * @return Collection of bug category keys.
238      */

239     public Collection JavaDoc<String JavaDoc> getBugCategories() {
240         DetectorFactoryCollection.instance(); // ensure detectors loaded
241

242         return categoryDescriptionMap.keySet(); // backed by the Map
243
}
244     public Collection JavaDoc<BugCategory> getBugCategoryObjects() {
245         DetectorFactoryCollection.instance(); // ensure detectors loaded
246

247         return categoryDescriptionMap.values(); // backed by the Map
248
}
249     /**
250      * Get the localized user designation string.
251      * Returns the key if no user designation can be found.
252      *
253      * @param key the user designation key
254      * @return the localized designation string
255      */

256     public String JavaDoc getUserDesignation(String JavaDoc key) {
257         return userDesignationBundle.getString(key);
258     }
259
260     /**
261      * Get a List containing all known user designation keys keys.
262      * E.g., "MOSTLY_HARMLESS", "MUST_FIX", "NOT_A_BUG", etc.
263      *
264      * @return List of user designation keys
265      */

266     public List JavaDoc<String JavaDoc> getUserDesignationKeys() {
267         List JavaDoc<String JavaDoc> result = new LinkedList JavaDoc<String JavaDoc>();
268         for (Enumeration JavaDoc<String JavaDoc> e = userDesignationBundle.getKeys(); e.hasMoreElements(); ) {
269             String JavaDoc key = e.nextElement();
270             result.add(key);
271         }
272         return result;
273     }
274
275     /**
276      * Get a List containing all known user designation keys keys.
277      * E.g., "MOSTLY_HARMLESS", "MUST_FIX", "NOT_A_BUG", etc.
278      *
279      * If <code>sort == true</code> then it will attempt to sort
280      * the List as appropriate to show the user.
281      *
282      * @return List of user designation keys
283      */

284     public List JavaDoc<String JavaDoc> getUserDesignationKeys(boolean sort) {
285         List JavaDoc<String JavaDoc> result = getUserDesignationKeys();
286         if (sort) Collections.sort(result, designationKeyComparator);
287         return result;
288     }
289
290
291     private static class DesignationKeyComparator implements Comparator JavaDoc<String JavaDoc>, Serializable JavaDoc {
292         private static final long serialVersionUID = 1L;
293         /** Returns a negative integer, zero, or a positive integer as the
294          * left key is less than, equal to, or greater than the right key. */

295         public int compare(String JavaDoc lKey, String JavaDoc rKey) {
296             int lCat = categoryOf(lKey);
297             int catDiff = lCat - categoryOf(rKey);
298             if (catDiff != 0 || lCat != 0) return catDiff;
299             // if we get this far we have two unrecognized strings
300
return lKey.compareTo(rKey);
301         }
302         private static int categoryOf(String JavaDoc 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; // between MOSTLY_HARMLESS and SHOULD_FIX
319
}
320     }
321
322 }
323
324 // vim:ts=4
325
Popular Tags