1 23 24 package org.infoglue.cms.util; 25 26 import java.text.MessageFormat ; 27 import java.util.Locale ; 28 import java.util.MissingResourceException ; 29 import java.util.ResourceBundle ; 30 31 import org.apache.log4j.Logger; 32 import org.infoglue.cms.exception.Bug; 33 import org.infoglue.cms.exception.ConfigurationError; 34 35 38 public class SimpleStringManager implements StringManager 39 { 40 41 private final static Logger logger = Logger.getLogger(SimpleStringManager.class.getName()); 42 43 private ResourceBundle bundle; 45 46 47 SimpleStringManager(String bundleName, Locale locale) 48 { 49 if(locale == null || locale.getLanguage() == null || locale.getLanguage().equalsIgnoreCase("")) 50 { 51 logger.info("No locale sent in - must be a bug:" + locale); 52 locale = Locale.ENGLISH; 53 } 54 55 try 56 { 57 logger.info("Created a SimpleStringManager for package bundleName" + bundleName + ":" + locale); 58 this.bundle = ResourceBundle.getBundle(bundleName, locale); 59 } 60 catch(MissingResourceException e) 61 { 62 throw new ConfigurationError("Unable to find resource bundle: " + e.getMessage(), e); 63 } 64 catch(NullPointerException e) 65 { 66 throw new Bug("Unable to create resource bundle.", e); 67 } 68 } 69 70 71 72 75 78 public final String getString(String key) 79 { 80 try 81 { 82 return this.bundle.getString(key); 84 } 85 catch(MissingResourceException e) 86 { 87 logger.warn("Error trying to find a string for key " + key, e); 88 throw new ConfigurationError("Key not found.", e); 89 } 90 catch(Throwable t) 91 { 92 logger.warn("Error trying to find a string for key " + key); 93 throw new Bug("Unable to fetch the value for the specified key.", t); 94 } 95 } 96 97 100 public final String getString(String key, Object args[]) { 101 return MessageFormat.format(getString(key), args); 102 } 103 104 107 public final String getString(String key, Object arg) { 108 return getString(key, new Object []{ arg }); 109 } 110 111 114 public final String getString(String key, Object arg1, Object arg2) { 115 return getString(key, new Object []{ arg1, arg2 }); 116 } 117 118 121 public final String getString(String key, Object arg1, Object arg2, Object arg3) { 122 return getString(key, new Object []{ arg1, arg2, arg3 }); 123 } 124 125 128 131 boolean containsKey(String key) { 132 try { 133 this.bundle.getString(key); 134 return true; 135 } catch(MissingResourceException e) { 136 return false; 137 } 138 } 139 140 141 142 } 146 147 148 149 | Popular Tags |