1. /*
  2. * DatatypeMessageFormatter.java
  3. *
  4. * Created on June 28, 2004, 9:15 AM
  5. */
  6. package com.sun.org.apache.xerces.internal.util;
  7. import java.util.Locale;
  8. import java.util.MissingResourceException;
  9. import java.util.PropertyResourceBundle;
  10. import java.util.ResourceBundle;
  11. /**
  12. *
  13. * @author Neeraj Bajaj, Sun Microsystems
  14. */
  15. public class DatatypeMessageFormatter {
  16. static final String BASE_NAME = "com.sun.org.apache.xerces.internal.impl.msg.DatatypeMessages";
  17. /**
  18. * Formats a message with the specified arguments using the given
  19. * locale information.
  20. *
  21. * @param locale The locale of the message.
  22. * @param key The message key.
  23. * @param arguments The message replacement text arguments. The order
  24. * of the arguments must match that of the placeholders
  25. * in the actual message.
  26. *
  27. * @return the formatted message.
  28. *
  29. * @throws MissingResourceException Thrown if the message with the
  30. * specified key cannot be found.
  31. */
  32. public static String formatMessage(Locale locale,
  33. String key, Object[] arguments)
  34. throws MissingResourceException {
  35. ResourceBundle resourceBundle = null;
  36. if (locale != null) {
  37. resourceBundle =
  38. PropertyResourceBundle.getBundle(BASE_NAME, locale);
  39. }
  40. else {
  41. resourceBundle =
  42. PropertyResourceBundle.getBundle(BASE_NAME);
  43. }
  44. // format message
  45. String msg;
  46. try {
  47. msg = resourceBundle.getString(key);
  48. if (arguments != null) {
  49. try {
  50. msg = java.text.MessageFormat.format(msg, arguments);
  51. }
  52. catch (Exception e) {
  53. msg = resourceBundle.getString("FormatFailed");
  54. msg += " " + resourceBundle.getString(key);
  55. }
  56. }
  57. }
  58. // error
  59. catch (MissingResourceException e) {
  60. msg = resourceBundle.getString("BadMessageKey");
  61. throw new MissingResourceException(key, msg, key);
  62. }
  63. // no message
  64. if (msg == null) {
  65. msg = key;
  66. if (arguments.length > 0) {
  67. StringBuffer str = new StringBuffer(msg);
  68. str.append('?');
  69. for (int i = 0; i < arguments.length; i++) {
  70. if (i > 0) {
  71. str.append('&');
  72. }
  73. str.append(String.valueOf(arguments[i]));
  74. }
  75. }
  76. }
  77. return msg;
  78. }
  79. }