2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / java / util / logging / Formatter.java
blobc4819695165a04b01a5cf89829b42b50aff2caf1
1 /* Formatter.java
2 -- a class for formatting log messages by localizing message texts
3 and performing substitution of parameters
5 Copyright (C) 2002 Free Software Foundation, Inc.
7 This file is part of GNU Classpath.
9 GNU Classpath is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GNU Classpath is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GNU Classpath; see the file COPYING. If not, write to the
21 Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
22 02111-1307 USA.
24 Linking this library statically or dynamically with other modules is
25 making a combined work based on this library. Thus, the terms and
26 conditions of the GNU General Public License cover the whole
27 combination.
29 As a special exception, the copyright holders of this library give you
30 permission to link this library with independent modules to produce an
31 executable, regardless of the license terms of these independent
32 modules, and to copy and distribute the resulting executable under
33 terms of your choice, provided that you also meet, for each linked
34 independent module, the terms and conditions of the license of that
35 module. An independent module is a module which is not derived from
36 or based on this library. If you modify this library, you may extend
37 this exception to your version of the library, but you are not
38 obligated to do so. If you do not wish to do so, delete this
39 exception statement from your version.
44 package java.util.logging;
46 import java.util.ResourceBundle;
47 import java.text.MessageFormat;
49 /**
50 * A <code>Formatter</code> supports handlers by localizing
51 * message texts and by subsituting parameter values for their
52 * placeholders.
54 * @author Sascha Brawer (brawer@acm.org)
56 public abstract class Formatter
58 /**
59 * Constructs a new Formatter.
61 protected Formatter()
66 /**
67 * Formats a LogRecord into a string. Usually called by handlers
68 * which need a string for a log record, for example to append
69 * a record to a log file or to transmit a record over the network.
71 * @param record the log record for which a string form is requested.
73 public abstract String format(LogRecord record);
76 /**
77 * Returns a string that handlers are supposed to emit before
78 * the first log record. The base implementation returns an
79 * empty string, but subclasses such as {@link XMLFormatter}
80 * override this method in order to provide a suitable header.
82 * @return a string for the header.
84 * @param handler the handler which will prepend the returned
85 * string in front of the first log record. This method
86 * may inspect certain properties of the handler, for
87 * example its encoding, in order to construct the header.
89 public String getHead(Handler handler)
91 return "";
95 /**
96 * Returns a string that handlers are supposed to emit after
97 * the last log record. The base implementation returns an
98 * empty string, but subclasses such as {@link XMLFormatter}
99 * override this method in order to provide a suitable tail.
101 * @return a string for the header.
103 * @param handler the handler which will append the returned
104 * string after the last log record. This method
105 * may inspect certain properties of the handler
106 * in order to construct the tail.
108 public String getTail(Handler handler)
110 return "";
115 * Formats the message part of a log record.
117 * <p>First, the Formatter localizes the record message to the
118 * default locale by looking up the message in the record's
119 * localization resource bundle. If this step fails because there
120 * is no resource bundle associated with the record, or because the
121 * record message is not a key in the bundle, the raw message is
122 * used instead.
124 * <p>Second, the Formatter substitutes appropriate strings for
125 * the message parameters. If the record returns a non-empty
126 * array for <code>getParameters()</code> and the localized
127 * message string contains the character sequence "{0", the
128 * formatter uses <code>java.text.MessageFormat</code> to format
129 * the message. Otherwise, no parameter substitution is performed.
131 * @param record the log record to be localized and formatted.
133 * @return the localized message text where parameters have been
134 * substituted by suitable strings.
136 * @throws NullPointerException if <code>record</code>
137 * is <code>null</code>.
139 public String formatMessage(LogRecord record)
141 String msg;
142 ResourceBundle bundle;
143 Object[] params;
145 /* This will throw a NullPointerExceptionif record is null. */
146 msg = record.getMessage();
147 if (msg == null)
148 msg = "";
150 /* Try to localize the message. */
151 bundle = record.getResourceBundle();
152 if (bundle != null)
156 msg = bundle.getString(msg);
158 catch (java.util.MissingResourceException _)
163 /* Format the message if there are parameters. */
164 params = record.getParameters();
165 if ((params != null)
166 && (params.length > 0)
167 && (msg.indexOf("{0") >= 0))
169 msg = MessageFormat.format(msg, params);
172 return msg;