1 16 19 package com.sun.org.apache.xml.internal.serializer.utils; 20 21 import java.util.ListResourceBundle ; 22 import java.util.Locale ; 23 import java.util.MissingResourceException ; 24 import java.util.ResourceBundle ; 25 26 95 public final class Messages 96 { 97 98 private final Locale m_locale = Locale.getDefault(); 99 100 101 private ListResourceBundle m_resourceBundle; 102 103 104 private String m_resourceBundleName; 105 106 107 108 119 Messages(String resourceBundle) 120 { 121 122 m_resourceBundleName = resourceBundle; 123 } 124 125 133 138 144 private Locale getLocale() 145 { 146 return m_locale; 147 } 148 149 154 private ListResourceBundle getResourceBundle() 155 { 156 return m_resourceBundle; 157 } 158 159 170 public final String createMessage(String msgKey, Object args[]) 171 { 172 if (m_resourceBundle == null) 173 m_resourceBundle = loadResourceBundle(m_resourceBundleName); 174 175 if (m_resourceBundle != null) 176 { 177 return createMsg(m_resourceBundle, msgKey, args); 178 } 179 else 180 return "Could not load the resource bundles: "+ m_resourceBundleName; 181 } 182 183 197 private final String createMsg( 198 ListResourceBundle fResourceBundle, 199 String msgKey, 200 Object args[]) { 202 203 String fmsg = null; 204 boolean throwex = false; 205 String msg = null; 206 207 if (msgKey != null) 208 msg = fResourceBundle.getString(msgKey); 209 else 210 msgKey = ""; 211 212 if (msg == null) 213 { 214 throwex = true; 215 218 try 219 { 220 221 msg = 222 java.text.MessageFormat.format( 223 MsgKey.BAD_MSGKEY, 224 new Object [] { msgKey, m_resourceBundleName }); 225 } 226 catch (Exception e) 227 { 228 231 msg = 232 "The message key '" 233 + msgKey 234 + "' is not in the message class '" 235 + m_resourceBundleName+"'"; 236 } 237 } 238 else if (args != null) 239 { 240 try 241 { 242 int n = args.length; 246 247 for (int i = 0; i < n; i++) 248 { 249 if (null == args[i]) 250 args[i] = ""; 251 } 252 253 fmsg = java.text.MessageFormat.format(msg, args); 254 } 256 catch (Exception e) 257 { 258 throwex = true; 259 try 260 { 261 fmsg = 263 java.text.MessageFormat.format( 264 MsgKey.BAD_MSGFORMAT, 265 new Object [] { msgKey, m_resourceBundleName }); 266 fmsg += " " + msg; 267 } 268 catch (Exception formatfailed) 269 { 270 fmsg = 273 "The format of message '" 274 + msgKey 275 + "' in message class '" 276 + m_resourceBundleName 277 + "' failed."; 278 } 279 } 280 } 281 else 282 fmsg = msg; 283 284 if (throwex) 285 { 286 throw new RuntimeException (fmsg); 287 } 288 289 return fmsg; 290 } 291 292 302 private ListResourceBundle loadResourceBundle(String resourceBundle) 303 throws MissingResourceException 304 { 305 m_resourceBundleName = resourceBundle; 306 Locale locale = getLocale(); 307 308 ListResourceBundle lrb; 309 310 try 311 { 312 313 ResourceBundle rb = 314 ResourceBundle.getBundle(m_resourceBundleName, locale); 315 lrb = (ListResourceBundle ) rb; 316 } 317 catch (MissingResourceException e) 318 { 319 try { 321 322 lrb = 325 (ListResourceBundle ) ResourceBundle.getBundle( 326 m_resourceBundleName, 327 new Locale ("en", "US")); 328 } 329 catch (MissingResourceException e2) 330 { 331 332 throw new MissingResourceException ( 335 "Could not load any resource bundles." + m_resourceBundleName, 336 m_resourceBundleName, 337 ""); 338 } 339 } 340 m_resourceBundle = lrb; 341 return lrb; 342 } 343 344 353 private static String getResourceSuffix(Locale locale) 354 { 355 356 String suffix = "_" + locale.getLanguage(); 357 String country = locale.getCountry(); 358 359 if (country.equals("TW")) 360 suffix += "_" + country; 361 362 return suffix; 363 } 364 } 365 | Popular Tags |