1 18 19 package org.apache.struts.util; 20 21 import java.io.Serializable ; 22 import java.text.MessageFormat ; 23 import java.util.HashMap ; 24 import java.util.Locale ; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 50 public abstract class MessageResources implements Serializable { 51 52 54 57 protected static Log log = LogFactory.getLog(MessageResources.class); 58 59 62 protected String config = null; 63 64 68 public String getConfig() { 69 return (this.config); 70 } 71 72 75 protected Locale defaultLocale = Locale.getDefault(); 76 77 80 protected MessageResourcesFactory factory = null; 81 82 86 public MessageResourcesFactory getFactory() { 87 return (this.factory); 88 } 89 90 94 protected HashMap formats = new HashMap (); 95 96 100 protected boolean returnNull = false; 101 102 107 public boolean getReturnNull() { 108 return (this.returnNull); 109 } 110 111 117 public void setReturnNull(boolean returnNull) { 118 this.returnNull = returnNull; 119 } 120 121 123 129 public MessageResources(MessageResourcesFactory factory, String config) { 130 131 this(factory, config, false); 132 133 } 134 135 142 public MessageResources( 143 MessageResourcesFactory factory, 144 String config, 145 boolean returnNull) { 146 147 super(); 148 this.factory = factory; 149 this.config = config; 150 this.returnNull = returnNull; 151 152 } 153 154 156 161 public String getMessage(String key) { 162 163 return this.getMessage((Locale ) null, key, null); 164 165 } 166 167 174 public String getMessage(String key, Object args[]) { 175 176 return this.getMessage((Locale ) null, key, args); 177 178 } 179 180 187 public String getMessage(String key, Object arg0) { 188 189 return this.getMessage((Locale ) null, key, arg0); 190 191 } 192 193 201 public String getMessage(String key, Object arg0, Object arg1) { 202 203 return this.getMessage((Locale ) null, key, arg0, arg1); 204 205 } 206 207 216 public String getMessage(String key, Object arg0, Object arg1, Object arg2) { 217 218 return this.getMessage((Locale ) null, key, arg0, arg1, arg2); 219 220 } 221 222 232 public String getMessage( 233 String key, 234 Object arg0, 235 Object arg1, 236 Object arg2, 237 Object arg3) { 238 239 return this.getMessage((Locale ) null, key, arg0, arg1, arg2, arg3); 240 241 } 242 243 256 public abstract String getMessage(Locale locale, String key); 257 258 268 public String getMessage(Locale locale, String key, Object args[]) { 269 270 if (locale == null) { 272 locale = defaultLocale; 273 } 274 275 MessageFormat format = null; 276 String formatKey = messageKey(locale, key); 277 278 synchronized (formats) { 279 format = (MessageFormat ) formats.get(formatKey); 280 if (format == null) { 281 String formatString = getMessage(locale, key); 282 283 if (formatString == null) { 284 return returnNull ? null : ("???" + formatKey + "???"); 285 } 286 287 format = new MessageFormat (escape(formatString)); 288 format.setLocale(locale); 289 formats.put(formatKey, format); 290 } 291 292 } 293 294 return format.format(args); 295 } 296 297 307 public String getMessage(Locale locale, String key, Object arg0) { 308 return this.getMessage(locale, key, new Object [] { arg0 }); 309 } 310 311 322 public String getMessage(Locale locale, String key, Object arg0, Object arg1) { 323 return this.getMessage(locale, key, new Object [] { arg0, arg1 }); 324 } 325 326 338 public String getMessage( 339 Locale locale, 340 String key, 341 Object arg0, 342 Object arg1, 343 Object arg2) { 344 345 return this.getMessage(locale, key, new Object [] { arg0, arg1, arg2 }); 346 } 347 348 361 public String getMessage( 362 Locale locale, 363 String key, 364 Object arg0, 365 Object arg1, 366 Object arg2, 367 Object arg3) { 368 369 return this.getMessage(locale, key, new Object [] { arg0, arg1, arg2, arg3 }); 370 } 371 372 378 public boolean isPresent(String key) { 379 380 return this.isPresent(null, key); 381 382 } 383 384 392 public boolean isPresent(Locale locale, String key) { 393 394 String message = getMessage(locale, key); 395 396 if (message == null) { 397 return false; 398 399 } else if (message.startsWith("???") && message.endsWith("???")) { 400 return false; 402 } else { 403 return true; 404 } 405 406 } 407 408 410 416 protected String escape(String string) { 417 418 if ((string == null) || (string.indexOf('\'') < 0)) { 419 return string; 420 } 421 422 int n = string.length(); 423 StringBuffer sb = new StringBuffer (n); 424 425 for (int i = 0; i < n; i++) { 426 char ch = string.charAt(i); 427 428 if (ch == '\'') { 429 sb.append('\''); 430 } 431 432 sb.append(ch); 433 } 434 435 return sb.toString(); 436 437 } 438 439 446 protected String localeKey(Locale locale) { 447 return (locale == null) ? "" : locale.toString(); 448 } 449 450 457 protected String messageKey(Locale locale, String key) { 458 459 return (localeKey(locale) + "." + key); 460 461 } 462 463 470 protected String messageKey(String localeKey, String key) { 471 472 return (localeKey + "." + key); 473 474 } 475 476 478 482 protected static MessageResourcesFactory defaultFactory = null; 483 484 490 public synchronized static MessageResources getMessageResources(String config) { 491 492 if (defaultFactory == null) { 493 defaultFactory = MessageResourcesFactory.createFactory(); 494 } 495 496 return defaultFactory.createResources(config); 497 } 498 499 504 public void log(String message) { 505 log.debug(message); 506 } 507 508 515 public void log(String message, Throwable throwable) { 516 log.debug(message, throwable); 517 } 518 519 } 520 | Popular Tags |