1 20 package org.apache.derby.shared.common.i18n; 21 22 import org.apache.derby.shared.common.error.ExceptionSeverity; 23 import org.apache.derby.shared.common.sanity.SanityManager; 24 import java.util.Locale ; 25 import java.util.ResourceBundle ; 26 import java.util.MissingResourceException ; 27 import java.text.MessageFormat ; 28 29 32 public class MessageUtil 33 { 34 public static final Locale US = new Locale ("en", "US"); 35 36 40 private String resourceBundleName; 41 42 52 public MessageUtil(String resourceBundleName) 53 { 54 this.resourceBundleName = resourceBundleName; 55 } 56 57 58 public String getTextMessage(String messageID) 59 { 60 return getCompleteMessage(messageID, (Object []) null); 61 } 62 63 64 public String getTextMessage(String messageID, Object a1) 65 { 66 return getCompleteMessage(messageID, new Object []{a1}); 67 } 68 69 70 public String getTextMessage(String messageID, Object a1, Object a2) 71 { 72 return getCompleteMessage(messageID, new Object []{a1, a2}); 73 } 74 75 76 public String getTextMessage(String messageID, Object a1, Object a2, 77 Object a3) 78 { 79 return getCompleteMessage(messageID, new Object []{a1, a2, a3}); 80 } 81 82 83 public String getTextMessage(String messageID, Object a1, Object a2, 84 Object a3, Object a4) 85 { 86 return getCompleteMessage(messageID, new Object []{a1, a2, a3, a4}); 87 } 88 89 97 public String getCompleteMessage(String messageID, Object [] args) 98 { 99 return getCompleteMessage(messageID, resourceBundleName, args); 100 } 101 102 137 public static String getCompleteMessage(Locale locale, 138 String resourceBundleName, String messageId, Object [] arguments, 139 boolean composeDefault) throws MissingResourceException 140 { 141 try 142 { 143 return formatMessage( 144 ResourceBundle.getBundle(resourceBundleName, locale), messageId, 145 arguments, false); 146 } 147 catch ( MissingResourceException mre ) 148 { 149 return formatMessage( 153 ResourceBundle.getBundle(resourceBundleName, US), 154 messageId, arguments, composeDefault); 155 } 156 } 157 158 179 public static String getCompleteMessage(String messageId, 180 String resourceBundleName, Object [] arguments) 181 throws MissingResourceException 182 { 183 return getCompleteMessage(Locale.getDefault(), resourceBundleName, 184 messageId, arguments, true); 185 } 186 187 215 public static String formatMessage(ResourceBundle bundle, String messageId, 216 Object [] arguments, boolean composeDefault) { 217 218 String message = null; 219 String badArgsMessage = null; 220 221 if (arguments == null) 222 arguments = new Object [0]; 223 224 if (bundle != null) { 225 226 try { 227 message = bundle.getString(messageId); 228 229 230 if ( SanityManager.DEBUG ) 232 { 233 int numExpected = countParams(message); 234 SanityManager.ASSERT(numExpected == arguments.length, 235 "Number of parameters expected for message id " + 236 messageId + " (" + numExpected + 237 ") does not match number of arguments received (" + 238 arguments.length + ")"); 239 } 240 241 try { 242 return MessageFormat.format(message, arguments); 243 } 244 catch (IllegalArgumentException iae) { 245 if ( !composeDefault || SanityManager.DEBUG ) 246 throw iae; 247 } 248 catch (NullPointerException npe) { 249 if ( !composeDefault || SanityManager.DEBUG ) 253 throw npe; 254 } 255 256 } catch (MissingResourceException mre) { 257 if (!composeDefault ) 259 throw mre; 260 } 261 } 262 263 return composeDefaultMessage("UNKNOWN MESSAGE, id " + messageId, arguments); 264 } 265 266 269 private static int countParams(String message) 270 { 271 boolean openFound = false; 272 int numparams = 0; 273 274 for ( int i = 0 ; i < message.length() ; i++ ) 275 { 276 char ch = message.charAt(i); 277 if ( ch == '{' ) { 278 openFound = true; 279 } 280 281 if ( ch == '}' && openFound ) 282 { 283 numparams++; 284 openFound = false; 285 } 286 } 287 288 return numparams; 289 } 290 291 302 public static String composeDefaultMessage(String message, Object [] arguments) 303 { 304 if (message == null) 305 { 306 message = "UNKNOWN"; 307 } 308 309 StringBuffer sb = new StringBuffer (message); 310 311 if ( arguments == null ) 312 { 313 return sb.toString(); 314 } 315 316 sb.append(" : "); 317 int len = arguments.length; 318 319 for (int i=0; i < len; i++) { 320 if (i > 0) 322 sb.append(", "); 323 324 sb.append('['); 325 sb.append(i); 326 sb.append("] "); 327 if (arguments[i] == null) 328 sb.append("null"); 329 else 330 sb.append(arguments[i].toString()); 331 } 332 333 return sb.toString(); 334 } 335 } 336 | Popular Tags |