001    /* Formatter.java --
002       A class for formatting log messages by localizing message texts
003       and performing substitution of parameters
004       Copyright (C) 2002, 2004 Free Software Foundation, Inc.
005    
006    This file is part of GNU Classpath.
007    
008    GNU Classpath is free software; you can redistribute it and/or modify
009    it under the terms of the GNU General Public License as published by
010    the Free Software Foundation; either version 2, or (at your option)
011    any later version.
012    
013    GNU Classpath is distributed in the hope that it will be useful, but
014    WITHOUT ANY WARRANTY; without even the implied warranty of
015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
016    General Public License for more details.
017    
018    You should have received a copy of the GNU General Public License
019    along with GNU Classpath; see the file COPYING.  If not, write to the
020    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
021    02110-1301 USA.
022    
023    Linking this library statically or dynamically with other modules is
024    making a combined work based on this library.  Thus, the terms and
025    conditions of the GNU General Public License cover the whole
026    combination.
027    
028    As a special exception, the copyright holders of this library give you
029    permission to link this library with independent modules to produce an
030    executable, regardless of the license terms of these independent
031    modules, and to copy and distribute the resulting executable under
032    terms of your choice, provided that you also meet, for each linked
033    independent module, the terms and conditions of the license of that
034    module.  An independent module is a module which is not derived from
035    or based on this library.  If you modify this library, you may extend
036    this exception to your version of the library, but you are not
037    obligated to do so.  If you do not wish to do so, delete this
038    exception statement from your version. */
039    
040    
041    package java.util.logging;
042    
043    import java.text.MessageFormat;
044    import java.util.ResourceBundle;
045    
046    /**
047     * A <code>Formatter</code> supports handlers by localizing
048     * message texts and by subsituting parameter values for their
049     * placeholders.
050     *
051     * @author Sascha Brawer (brawer@acm.org)
052     */
053    public abstract class Formatter
054    {
055      /**
056       * Constructs a new Formatter.
057       */
058      protected Formatter()
059      {
060      }
061    
062    
063      /**
064       * Formats a LogRecord into a string.  Usually called by handlers
065       * which need a string for a log record, for example to append
066       * a record to a log file or to transmit a record over the network.
067       *
068       * @param record the log record for which a string form is requested.
069       */
070      public abstract String format(LogRecord record);
071    
072    
073      /**
074       * Returns a string that handlers are supposed to emit before
075       * the first log record.  The base implementation returns an
076       * empty string, but subclasses such as {@link XMLFormatter}
077       * override this method in order to provide a suitable header.
078       *
079       * @return a string for the header.
080       *
081       * @param handler the handler which will prepend the returned
082       *     string in front of the first log record.  This method
083       *     may inspect certain properties of the handler, for
084       *     example its encoding, in order to construct the header.
085       */
086      public String getHead(Handler handler)
087      {
088        return "";
089      }
090    
091    
092      /**
093       * Returns a string that handlers are supposed to emit after
094       * the last log record.  The base implementation returns an
095       * empty string, but subclasses such as {@link XMLFormatter}
096       * override this method in order to provide a suitable tail.
097       *
098       * @return a string for the header.
099       *
100       * @param handler the handler which will append the returned
101       *     string after the last log record.  This method
102       *     may inspect certain properties of the handler
103       *     in order to construct the tail.
104       */
105      public String getTail(Handler handler)
106      {
107        return "";
108      }
109    
110    
111      /**
112       * Formats the message part of a log record.
113       *
114       * <p>First, the Formatter localizes the record message to the
115       * default locale by looking up the message in the record's
116       * localization resource bundle.  If this step fails because there
117       * is no resource bundle associated with the record, or because the
118       * record message is not a key in the bundle, the raw message is
119       * used instead.
120       *
121       * <p>Second, the Formatter substitutes appropriate strings for
122       * the message parameters. If the record returns a non-empty
123       * array for <code>getParameters()</code> and the localized
124       * message string contains the character sequence "{0", the
125       * formatter uses <code>java.text.MessageFormat</code> to format
126       * the message.  Otherwise, no parameter substitution is performed.
127       *
128       * @param record the log record to be localized and formatted.
129       *
130       * @return the localized message text where parameters have been
131       *         substituted by suitable strings.
132       *
133       * @throws NullPointerException if <code>record</code>
134       *         is <code>null</code>.
135       */
136      public String formatMessage(LogRecord record)
137      {
138        String          msg;
139        ResourceBundle  bundle;
140        Object[]        params;
141    
142        /* This will throw a NullPointerExceptionif record is null. */
143        msg = record.getMessage();
144        if (msg == null)
145          msg = "";
146    
147        /* Try to localize the message. */
148        bundle = record.getResourceBundle();
149        if (bundle != null)
150        {
151          try
152          {
153            msg = bundle.getString(msg);
154          }
155          catch (java.util.MissingResourceException _)
156          {
157          }
158        }
159    
160        /* Format the message if there are parameters. */
161        params = record.getParameters();
162        if ((params != null)
163            && (params.length > 0)
164            && (msg.indexOf("{0") >= 0))
165        {
166          msg = MessageFormat.format(msg, params);
167        }
168    
169        return msg;
170      }
171    }