001 /*
002 * Copyright 2000-2006 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util.logging;
027
028 /**
029 * A Formatter provides support for formatting LogRecords.
030 * <p>
031 * Typically each logging Handler will have a Formatter associated
032 * with it. The Formatter takes a LogRecord and converts it to
033 * a string.
034 * <p>
035 * Some formatters (such as the XMLFormatter) need to wrap head
036 * and tail strings around a set of formatted records. The getHeader
037 * and getTail methods can be used to obtain these strings.
038 *
039 * @version 1.24, 05/05/07
040 * @since 1.4
041 */
042
043 public abstract class Formatter {
044
045 /**
046 * Construct a new formatter.
047 */
048 protected Formatter() {
049 }
050
051 /**
052 * Format the given log record and return the formatted string.
053 * <p>
054 * The resulting formatted String will normally include a
055 * localized and formated version of the LogRecord's message field.
056 * It is recommended to use the {@link Formatter#formatMessage}
057 * convenience method to localize and format the message field.
058 *
059 * @param record the log record to be formatted.
060 * @return the formatted log record
061 */
062 public abstract String format(LogRecord record);
063
064 /**
065 * Return the header string for a set of formatted records.
066 * <p>
067 * This base class returns an empty string, but this may be
068 * overriden by subclasses.
069 *
070 * @param h The target handler (can be null)
071 * @return header string
072 */
073 public String getHead(Handler h) {
074 return "";
075 }
076
077 /**
078 * Return the tail string for a set of formatted records.
079 * <p>
080 * This base class returns an empty string, but this may be
081 * overriden by subclasses.
082 *
083 * @param h The target handler (can be null)
084 * @return tail string
085 */
086 public String getTail(Handler h) {
087 return "";
088 }
089
090 /**
091 * Localize and format the message string from a log record. This
092 * method is provided as a convenience for Formatter subclasses to
093 * use when they are performing formatting.
094 * <p>
095 * The message string is first localized to a format string using
096 * the record's ResourceBundle. (If there is no ResourceBundle,
097 * or if the message key is not found, then the key is used as the
098 * format string.) The format String uses java.text style
099 * formatting.
100 * <ul>
101 * <li>If there are no parameters, no formatter is used.
102 * <li>Otherwise, if the string contains "{0" then
103 * java.text.MessageFormat is used to format the string.
104 * <li>Otherwise no formatting is performed.
105 * </ul>
106 * <p>
107 *
108 * @param record the log record containing the raw message
109 * @return a localized and formatted message
110 */
111 public synchronized String formatMessage(LogRecord record) {
112 String format = record.getMessage();
113 java.util.ResourceBundle catalog = record.getResourceBundle();
114 if (catalog != null) {
115 try {
116 format = catalog.getString(record.getMessage());
117 } catch (java.util.MissingResourceException ex) {
118 // Drop through. Use record message as format
119 format = record.getMessage();
120 }
121 }
122 // Do the formatting.
123 try {
124 Object parameters[] = record.getParameters();
125 if (parameters == null || parameters.length == 0) {
126 // No parameters. Just return format string.
127 return format;
128 }
129 // Is it a java.text style format?
130 // Ideally we could match with
131 // Pattern.compile("\\{\\d").matcher(format).find())
132 // However the cost is 14% higher, so we cheaply check for
133 // 1 of the first 4 parameters
134 if (format.indexOf("{0") >= 0 || format.indexOf("{1") >= 0
135 || format.indexOf("{2") >= 0
136 || format.indexOf("{3") >= 0) {
137 return java.text.MessageFormat.format(format,
138 parameters);
139 }
140 return format;
141
142 } catch (Exception ex) {
143 // Formatting failed: use localized format string.
144 return format;
145 }
146 }
147 }
|