KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > logging > Formatter


1 /*
2  * @(#)Formatter.java 1.16 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package java.util.logging;
10
11 /**
12  * A Formatter provides support for formatting LogRecords.
13  * <p>
14  * Typically each logging Handler will have a Formatter associated
15  * with it. The Formatter takes a LogRecord and converts it to
16  * a string.
17  * <p>
18  * Some formatters (such as the XMLFormatter) need to wrap head
19  * and tail strings around a set of formatted records. The getHeader
20  * and getTail methods can be used to obtain these strings.
21  *
22  * @version 1.16, 12/19/03
23  * @since 1.4
24  */

25
26 public abstract class Formatter {
27
28     /**
29      * Construct a new formatter.
30      */

31     protected Formatter() {
32     }
33
34     /**
35      * Format the given log record and return the formatted string.
36      * <p>
37      * The resulting formatted String will normally include a
38      * localized and formated version of the LogRecord's message field.
39      * The Formatter.formatMessage convenience method can (optionally)
40      * be used to localize and format the message field.
41      *
42      * @param record the log record to be formatted.
43      * @return the formatted log record
44      */

45     public abstract String JavaDoc format(LogRecord JavaDoc record);
46
47
48     /**
49      * Return the header string for a set of formatted records.
50      * <p>
51      * This base class returns an empty string, but this may be
52      * overriden by subclasses.
53      *
54      * @param h The target handler (can be null)
55      * @return header string
56      */

57     public String JavaDoc getHead(Handler JavaDoc h) {
58     return "";
59     }
60
61     /**
62      * Return the tail string for a set of formatted records.
63      * <p>
64      * This base class returns an empty string, but this may be
65      * overriden by subclasses.
66      *
67      * @param h The target handler (can be null)
68      * @return tail string
69      */

70     public String JavaDoc getTail(Handler JavaDoc h) {
71     return "";
72     }
73
74
75     /**
76      * Localize and format the message string from a log record. This
77      * method is provided as a convenience for Formatter subclasses to
78      * use when they are performing formatting.
79      * <p>
80      * The message string is first localized to a format string using
81      * the record's ResourceBundle. (If there is no ResourceBundle,
82      * or if the message key is not found, then the key is used as the
83      * format string.) The format String uses java.text style
84      * formatting.
85      * <ul>
86      * <li>If there are no parameters, no formatter is used.
87      * <li>Otherwise, if the string contains "{0" then
88      * java.text.MessageFormat is used to format the string.
89      * <li>Otherwise no formatting is performed.
90      * </ul>
91      * <p>
92      *
93      * @param record the log record containing the raw message
94      * @return a localized and formatted message
95      */

96     public synchronized String JavaDoc formatMessage(LogRecord JavaDoc record) {
97     String JavaDoc format = record.getMessage();
98     java.util.ResourceBundle JavaDoc catalog = record.getResourceBundle();
99     if (catalog != null) {
100 // // We cache catalog lookups. This is mostly to avoid the
101
// // cost of exceptions for keys that are not in the catalog.
102
// if (catalogCache == null) {
103
// catalogCache = new HashMap();
104
// }
105
// format = (String)catalogCache.get(record.essage);
106
// if (format == null) {
107
try {
108                 format = catalog.getString(record.getMessage());
109             } catch (java.util.MissingResourceException JavaDoc ex) {
110             // Drop through. Use record message as format
111
format = record.getMessage();
112         }
113 // catalogCache.put(record.message, format);
114
// }
115
}
116     // Do the formatting.
117
try {
118         Object JavaDoc parameters[] = record.getParameters();
119         if (parameters == null || parameters.length == 0) {
120         // No parameters. Just return format string.
121
return format;
122         }
123         // Is is a java.text style format?
124
// Ideally we could match with
125
// Pattern.compile("\\{\\d").matcher(format).find())
126
// However the cost is 14% higher, so we cheaply check for
127
// 1 of the first 4 parameters
128
if (format.indexOf("{0") >= 0 || format.indexOf("{1") >=0 ||
129                         format.indexOf("{2") >=0|| format.indexOf("{3") >=0) {
130             return java.text.MessageFormat.format(format, parameters);
131         }
132         return format;
133
134     } catch (Exception JavaDoc ex) {
135         // Formatting failed: use localized format string.
136
return format;
137     }
138     }
139 }
140
141
142
143
Popular Tags