1. /*
  2. * @(#)Formatter.java 1.13 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.util.logging;
  8. /**
  9. * A Formatter provides support for formatting LogRecords.
  10. * <p>
  11. * Typically each logging Handler will have a Formatter associated
  12. * with it. The Formatter takes a LogRecord and converts it to
  13. * a string.
  14. * <p>
  15. * Some formatters (such as the XMLFormatter) need to wrap head
  16. * and tail strings around a set of formatted records. The getHeader
  17. * and getTail methods can be used to obtain these strings.
  18. *
  19. * @version 1.13, 01/23/03
  20. * @since 1.4
  21. */
  22. public abstract class Formatter {
  23. /**
  24. * Construct a new formatter.
  25. */
  26. protected Formatter() {
  27. }
  28. /**
  29. * Format the given log record and return the formatted string.
  30. * <p>
  31. * The resulting formatted String will normally include a
  32. * localized and formated version of the LogRecord's message field.
  33. * The Formatter.formatMessage convenience method can (optionally)
  34. * be used to localize and format the message field.
  35. *
  36. * @param record the log record to be formatted.
  37. * @return the formatted log record
  38. */
  39. public abstract String format(LogRecord record);
  40. /**
  41. * Return the header string for a set of formatted records.
  42. * <p>
  43. * This base class returns an empty string, but this may be
  44. * overriden by subclasses.
  45. *
  46. * @param h The target handler.
  47. * @return header string
  48. */
  49. public String getHead(Handler h) {
  50. return "";
  51. }
  52. /**
  53. * Return the tail string for a set of formatted records.
  54. * <p>
  55. * This base class returns an empty string, but this may be
  56. * overriden by subclasses.
  57. *
  58. * @param h The target handler.
  59. * @return tail string
  60. */
  61. public String getTail(Handler h) {
  62. return "";
  63. }
  64. /**
  65. * Localize and format the message string from a log record. This
  66. * method is provided as a convenience for Formatter subclasses to
  67. * use when they are performing formatting.
  68. * <p>
  69. * The message string is first localized to a format string using
  70. * the record's ResourceBundle. (If there is no ResourceBundle,
  71. * or if the message key is not found, then the key is used as the
  72. * format string.) The format String uses java.text style
  73. * formatting.
  74. * <ul>
  75. * <li>If there are no parameters, no formatter is used.
  76. * <li>Otherwise, if the string contains "{0" then
  77. * java.text.MessageFormat is used to format the string.
  78. * <li>Otherwise no formatting is performed.
  79. * </ul>
  80. * <p>
  81. *
  82. * @param record the log record containing the raw message
  83. * @return a localized and formatted message
  84. */
  85. public synchronized String formatMessage(LogRecord record) {
  86. String format = record.getMessage();
  87. java.util.ResourceBundle catalog = record.getResourceBundle();
  88. if (catalog != null) {
  89. // // We cache catalog lookups. This is mostly to avoid the
  90. // // cost of exceptions for keys that are not in the catalog.
  91. // if (catalogCache == null) {
  92. // catalogCache = new HashMap();
  93. // }
  94. // format = (String)catalogCache.get(record.essage);
  95. // if (format == null) {
  96. try {
  97. format = catalog.getString(record.getMessage());
  98. } catch (java.util.MissingResourceException ex) {
  99. // Drop through. Use record message as format
  100. format = record.getMessage();
  101. }
  102. // catalogCache.put(record.message, format);
  103. // }
  104. }
  105. // Do the formatting.
  106. try {
  107. Object parameters[] = record.getParameters();
  108. if (parameters == null || parameters.length == 0) {
  109. // No parameters. Just return format string.
  110. return format;
  111. }
  112. // Is is a java.text style format?
  113. if (format.indexOf("{0") >= 0) {
  114. return java.text.MessageFormat.format(format, parameters);
  115. }
  116. return format;
  117. } catch (Exception ex) {
  118. // Formatting failed: use localized format string.
  119. return format;
  120. }
  121. }
  122. }