Clover coverage report - clover
Coverage timestamp: Sat Oct 8 2005 22:54:17 EDT
file stats: LOC: 92   Methods: 5
NCLOC: 68   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Strings.java 75% 72.7% 80% 74%
coverage coverage
 1   
 package abbot.i18n;
 2   
 
 3   
 import java.text.MessageFormat;
 4   
 import java.util.*;
 5   
 
 6   
 import abbot.Log;
 7   
 
 8   
 /** Provides i18n support. */
 9   
 // TODO: auto-format tooltips (".tip") and dialog messages (".dlg.")
 10   
 public class Strings {
 11   
     private static final String BUNDLE = "abbot.i18n.StringsBundle";
 12   
 
 13   
     private static Set bundles = new HashSet();
 14   
     private static Map formats = new HashMap();
 15   
 
 16   
     static {
 17  100
         String language = System.getProperty("abbot.locale.language");
 18  100
         if (language != null) {
 19  0
             String country = System.getProperty("abbot.locale.country",
 20   
                                                 language.toUpperCase());
 21  0
             String variant = System.getProperty("abbot.locale.variant", "");
 22  0
             Locale locale = new Locale(language, country, variant);
 23  0
             Locale.setDefault(locale);
 24  0
             System.out.println("Using locale " + locale);
 25   
         }
 26  100
         addBundle(BUNDLE);
 27   
     }
 28   
 
 29  0
     private Strings() { }
 30   
 
 31  100
     public static void addBundle(String bundle) {
 32  100
         Locale locale = Locale.getDefault();
 33  100
         try {
 34  100
             bundles.add(ResourceBundle.getBundle(bundle, locale));
 35   
         }
 36   
         catch(MissingResourceException mre) {
 37  0
             String msg = "No resource bundle found in " + bundle;
 38  0
             if (System.getProperty("java.class.path").indexOf("eclipse") != -1) {
 39  0
                 Log.warn(msg + ": copy one into your project output dir or run the ant build");
 40   
             }
 41   
             else {
 42  0
                 throw new Error(msg);
 43   
             }
 44   
         }
 45   
     }
 46   
 
 47   
     /** Returns the localized String for the given key, or the key surrounded
 48   
         by '#' if no corresponding localized string is found.
 49   
     */
 50  2046
     public static String get(String key) {
 51  2046
         return get(key, false);
 52   
     }
 53   
 
 54   
     /** Returns the localized string for the given key.  If optional is true,
 55   
         return null, otherwise returns the key surrounded by '#' if no
 56   
         corresponding localized string is found. 
 57   
     */
 58  2971
     public static String get(String key, boolean optional) {
 59  2971
         String defaultValue = "#" + key + "#";
 60  2971
         String value = null;
 61  2971
         Iterator iter = bundles.iterator();
 62  2971
         while (iter.hasNext()) {
 63  2971
             ResourceBundle local = (ResourceBundle)iter.next();
 64  2971
             try {
 65  2971
                 value = local.getString(key);
 66   
             }
 67   
             catch(MissingResourceException mre) {
 68   
             }
 69   
         }
 70  2971
         if (value == null) {
 71  1679
             if (!optional) {
 72  754
                 Log.log("Missing resource '" + key + "'");
 73  754
                 value = defaultValue;
 74   
             }
 75   
         }
 76  2971
         return value;
 77   
     }
 78   
 
 79   
     /** Returns a formatted localized string for the given key and arguments,
 80   
         or the key if no corresponding localized string is found.  Use
 81   
         java.text.MessageFormat syntax for the format string and arguments.
 82   
     */
 83  1224
     public static String get(String key, Object[] args) {
 84  1224
         MessageFormat fmt = (MessageFormat)formats.get(key);
 85  1224
         if (fmt == null) {
 86  67
             fmt = new MessageFormat(get(key));
 87  67
             formats.put(key, fmt);
 88   
         }
 89  1224
         return fmt.format(args);
 90   
     }
 91   
 }
 92