1 21 22 package org.apache.derby.iapi.services.i18n; 23 24 import org.apache.derby.iapi.services.info.JVMInfo; 25 import org.apache.derby.iapi.services.context.ShutdownException; 26 27 import java.util.Locale ; 28 import java.util.MissingResourceException ; 29 import java.util.ResourceBundle ; 30 import java.text.MessageFormat ; 31 32 43 public final class MessageService { 44 45 private static final Locale EN = new Locale ("en", "US"); 46 47 private static BundleFinder finder; 48 49 private MessageService() {} 50 51 52 public static ResourceBundle getBundleForLocale(Locale locale, String msgId) { 53 try { 54 return MessageService.getBundleWithEnDefault("org.apache.derby.loc.m"+hashString50(msgId), locale); 55 } catch (MissingResourceException mre) { 56 } 57 return null; 58 } 59 60 61 public static Object setFinder(BundleFinder theFinder) { 62 finder = theFinder; 63 64 return new MessageService().getClass(); 67 } 68 69 public static String getTextMessage(String messageID) { 70 return getCompleteMessage(messageID, (Object []) null); 71 } 72 public static String getTextMessage(String messageID, Object a1) { 73 74 return getCompleteMessage(messageID, new Object []{a1}); 75 } 76 public static String getTextMessage(String messageID, Object a1, Object a2) { 77 return getCompleteMessage(messageID, new Object []{a1, a2}); 78 } 79 public static String getTextMessage(String messageID, Object a1, Object a2, Object a3) { 80 return getCompleteMessage(messageID, new Object []{a1, a2, a3}); 81 } 82 public static String getTextMessage(String messageID, Object a1, Object a2, Object a3, Object a4) { 83 return getCompleteMessage(messageID, new Object []{a1, a2, a3, a4}); 84 } 85 86 96 public static String getCompleteMessage(String messageId, Object [] arguments) { 97 98 try { 99 return formatMessage(getBundle(messageId), messageId, arguments, true); 100 } catch (MissingResourceException mre) { 101 } catch (ShutdownException se) { 104 } 105 return formatMessage(getBundleForLocale(EN, messageId), messageId, arguments, false); 106 } 107 108 125 public static void getLocalizedMessage(int sqlcode, short errmcLen, String sqlerrmc, 126 String sqlerrp, int errd0, int errd1, int errd2, 127 int errd3, int errd4, int errd5, String warn, 128 String sqlState, String file, String localeStr, 129 String [] msg, int[] rc) 130 { 131 133 int _pos1 = localeStr.indexOf("_"); int _pos2 = localeStr.lastIndexOf("_"); 135 136 Locale locale = EN; if (_pos1 != -1) 138 { 139 String language = localeStr.substring(0, _pos1); 140 if (_pos2 == _pos1) 141 { 142 String country = localeStr.substring(_pos1 + 1); 143 locale = new Locale (language, country); 144 } 145 else 146 { 147 String country = localeStr.substring(_pos1 + 1, _pos2); 148 String variant = localeStr.substring(_pos2 + 1); 149 locale = new Locale (language, country, variant); 150 } 151 } 152 153 157 String messageId = sqlState; Object [] arguments = null; 159 if (sqlerrmc != null && sqlerrmc.length() > 0) 160 { 161 char [] sqlerrmc_chars = sqlerrmc.toCharArray(); 162 int numArgs = 0, lastSepIdx = -1; for (int i = 0; i < sqlerrmc_chars.length; i++) 164 { 165 if (sqlerrmc_chars[i] == 20) { 167 numArgs++; 168 lastSepIdx = i; 169 } 170 } 171 if (numArgs == 0) 172 messageId = new String (sqlerrmc_chars); else 174 { 175 messageId = new String (sqlerrmc_chars, lastSepIdx+1, sqlerrmc_chars.length-lastSepIdx-1); 176 arguments = new Object [numArgs]; 177 for (int start = 0, arg = 0, i = 0; i < lastSepIdx + 1; i++) 178 { 179 if (i == lastSepIdx || sqlerrmc_chars[i] == 20) { 181 arguments[arg++] = new String (sqlerrmc_chars, start, i - start); 182 start = i + 1; 183 } 184 } 185 } 186 } 187 188 try { 189 msg[0] = formatMessage(getBundleForLocale(locale, messageId), messageId, arguments, true); 190 rc[0] = 0; 191 return; 192 } catch (MissingResourceException mre) { 193 } catch (ShutdownException se) { 196 } 197 msg[0] = formatMessage(getBundleForLocale(EN, messageId), messageId, arguments, false); 198 rc[0] = 0; 199 } 200 201 208 public static String getLocalizedMessage(Locale locale, String messageId, Object [] args) 209 { 210 String locMsg = null; 211 212 try { 213 locMsg = formatMessage(getBundleForLocale(locale, messageId), messageId, args, true); 214 return locMsg; 215 } catch (MissingResourceException mre) { 216 } catch (ShutdownException se) { 219 } 220 locMsg = formatMessage(getBundleForLocale(EN, messageId), messageId, args, false); 221 return locMsg; 222 } 223 224 226 public static String getProperty(String messageId, String propertyName) { 227 228 ResourceBundle bundle = getBundle(messageId); 229 230 try { 231 if (bundle != null) 232 return bundle.getString(messageId.concat(".").concat(propertyName)); 233 } catch (MissingResourceException mre) { 234 } 235 return null; 236 } 237 238 public static String formatMessage(ResourceBundle bundle, String messageId, Object [] arguments, boolean lastChance) { 242 243 if (arguments == null) 244 arguments = new Object [0]; 245 246 if (bundle != null) { 247 248 try { 249 messageId = bundle.getString(messageId); 250 251 try { 252 return MessageFormat.format(messageId, arguments); 253 } 254 catch (IllegalArgumentException iae) { 255 } 256 catch (NullPointerException npe) { 257 } 261 262 } catch (MissingResourceException mre) { 263 if (lastChance) 265 throw mre; 266 } 267 } 268 269 if (messageId == null) 270 messageId = "UNKNOWN"; 271 272 273 StringBuffer sb = new StringBuffer (messageId); 274 275 sb.append(" : "); 276 int len = arguments.length; 277 278 for (int i=0; i < len; i++) { 279 if (i > 0) 281 sb.append(", "); 282 283 sb.append('['); 284 sb.append(i); 285 sb.append("] "); 286 if (arguments[i] == null) 287 sb.append("null"); 288 else 289 sb.append(arguments[i].toString()); 290 } 291 292 293 return sb.toString(); 294 } 295 296 private static ResourceBundle getBundle(String messageId) { 297 298 ResourceBundle bundle = null; 299 300 if (finder != null) 301 bundle = finder.getBundle(messageId); 302 303 if (bundle == null) { 304 bundle = MessageService.getBundleForLocale(Locale.getDefault(), messageId); 305 } 306 307 return bundle; 308 } 309 310 316 317 public static ResourceBundle getBundleWithEnDefault(String resource, Locale locale) { 318 319 try { 320 return ResourceBundle.getBundle(resource, locale); 321 } catch (MissingResourceException mre) { 322 323 327 return ResourceBundle.getBundle(resource, EN); 328 } 329 } 330 331 338 public static int hashString50(String key) { 339 int hash = 0; 340 int len = key.length(); 341 if (len > 5) 342 len = 5; 343 344 for (int i = 0; i < len; i++) { 345 hash += key.charAt(i); 346 } 347 hash = hash % 50; 348 return hash; 349 } 350 } 351 | Popular Tags |