jdk/src/share/classes/java/util/logging/Formatter.java
author yan
Wed, 30 Apr 2014 15:02:24 +0400
changeset 24196 61c9885d76e2
parent 5506 202f599c92aa
permissions -rw-r--r--
8029451: Tidy warnings cleanup for java.util package; minor changes in java.nio, java.sql Reviewed-by: lancea Contributed-by: Alexander Stepanov <alexander.v.stepanov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     2
 * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 3853
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package java.util.logging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * A Formatter provides support for formatting LogRecords.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Typically each logging Handler will have a Formatter associated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * with it.  The Formatter takes a LogRecord and converts it to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * a string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Some formatters (such as the XMLFormatter) need to wrap head
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * and tail strings around a set of formatted records. The getHeader
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * and getTail methods can be used to obtain these strings.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
public abstract class Formatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
     * Construct a new formatter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    protected Formatter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
     * Format the given log record and return the formatted string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
     * The resulting formatted String will normally include a
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
    55
     * localized and formatted version of the LogRecord's message field.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * It is recommended to use the {@link Formatter#formatMessage}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * convenience method to localize and format the message field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     * @param record the log record to be formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     * @return the formatted log record
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public abstract String format(LogRecord record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
     * Return the header string for a set of formatted records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * This base class returns an empty string, but this may be
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
    69
     * overridden by subclasses.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param   h  The target handler (can be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     * @return  header string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    public String getHead(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
     * Return the tail string for a set of formatted records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
     * This base class returns an empty string, but this may be
3853
9d2382b74894 6882363: 4/4 typos in java.util.logging javadocs
dcubed
parents: 2
diff changeset
    82
     * overridden by subclasses.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param   h  The target handler (can be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @return  tail string
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    public String getTail(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        return "";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * Localize and format the message string from a log record.  This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * method is provided as a convenience for Formatter subclasses to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * use when they are performing formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The message string is first localized to a format string using
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * the record's ResourceBundle.  (If there is no ResourceBundle,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * or if the message key is not found, then the key is used as the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * format string.)  The format String uses java.text style
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * <ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * <li>If there are no parameters, no formatter is used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * <li>Otherwise, if the string contains "{0" then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *     java.text.MessageFormat  is used to format the string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * <li>Otherwise no formatting is performed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * @param  record  the log record containing the raw message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * @return   a localized and formatted message
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    public synchronized String formatMessage(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        String format = record.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        java.util.ResourceBundle catalog = record.getResourceBundle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (catalog != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                format = catalog.getString(record.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
            } catch (java.util.MissingResourceException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
                // Drop through.  Use record message as format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
                format = record.getMessage();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        // Do the formatting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            Object parameters[] = record.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (parameters == null || parameters.length == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
                // No parameters.  Just return format string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // Is it a java.text style format?
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            // Ideally we could match with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            // Pattern.compile("\\{\\d").matcher(format).find())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            // However the cost is 14% higher, so we cheaply check for
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            // 1 of the first 4 parameters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            if (format.indexOf("{0") >= 0 || format.indexOf("{1") >=0 ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
                        format.indexOf("{2") >=0|| format.indexOf("{3") >=0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
                return java.text.MessageFormat.format(format, parameters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // Formatting failed: use localized format string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            return format;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}