0N/A/*
2362N/A * Copyright (c) 2000, 2006, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A
0N/Apackage java.util.logging;
0N/A
0N/A/**
0N/A * A Formatter provides support for formatting LogRecords.
0N/A * <p>
0N/A * Typically each logging Handler will have a Formatter associated
0N/A * with it. The Formatter takes a LogRecord and converts it to
0N/A * a string.
0N/A * <p>
0N/A * Some formatters (such as the XMLFormatter) need to wrap head
0N/A * and tail strings around a set of formatted records. The getHeader
0N/A * and getTail methods can be used to obtain these strings.
0N/A *
0N/A * @since 1.4
0N/A */
0N/A
0N/Apublic abstract class Formatter {
0N/A
0N/A /**
0N/A * Construct a new formatter.
0N/A */
0N/A protected Formatter() {
0N/A }
0N/A
0N/A /**
0N/A * Format the given log record and return the formatted string.
0N/A * <p>
0N/A * The resulting formatted String will normally include a
1664N/A * localized and formatted version of the LogRecord's message field.
0N/A * It is recommended to use the {@link Formatter#formatMessage}
0N/A * convenience method to localize and format the message field.
0N/A *
0N/A * @param record the log record to be formatted.
0N/A * @return the formatted log record
0N/A */
0N/A public abstract String format(LogRecord record);
0N/A
0N/A
0N/A /**
0N/A * Return the header string for a set of formatted records.
0N/A * <p>
0N/A * This base class returns an empty string, but this may be
1664N/A * overridden by subclasses.
0N/A *
0N/A * @param h The target handler (can be null)
0N/A * @return header string
0N/A */
0N/A public String getHead(Handler h) {
0N/A return "";
0N/A }
0N/A
0N/A /**
0N/A * Return the tail string for a set of formatted records.
0N/A * <p>
0N/A * This base class returns an empty string, but this may be
1664N/A * overridden by subclasses.
0N/A *
0N/A * @param h The target handler (can be null)
0N/A * @return tail string
0N/A */
0N/A public String getTail(Handler h) {
0N/A return "";
0N/A }
0N/A
0N/A
0N/A /**
0N/A * Localize and format the message string from a log record. This
0N/A * method is provided as a convenience for Formatter subclasses to
0N/A * use when they are performing formatting.
0N/A * <p>
0N/A * The message string is first localized to a format string using
0N/A * the record's ResourceBundle. (If there is no ResourceBundle,
0N/A * or if the message key is not found, then the key is used as the
0N/A * format string.) The format String uses java.text style
0N/A * formatting.
0N/A * <ul>
0N/A * <li>If there are no parameters, no formatter is used.
0N/A * <li>Otherwise, if the string contains "{0" then
0N/A * java.text.MessageFormat is used to format the string.
0N/A * <li>Otherwise no formatting is performed.
0N/A * </ul>
0N/A * <p>
0N/A *
0N/A * @param record the log record containing the raw message
0N/A * @return a localized and formatted message
0N/A */
0N/A public synchronized String formatMessage(LogRecord record) {
0N/A String format = record.getMessage();
0N/A java.util.ResourceBundle catalog = record.getResourceBundle();
0N/A if (catalog != null) {
0N/A try {
0N/A format = catalog.getString(record.getMessage());
0N/A } catch (java.util.MissingResourceException ex) {
0N/A // Drop through. Use record message as format
0N/A format = record.getMessage();
0N/A }
0N/A }
0N/A // Do the formatting.
0N/A try {
0N/A Object parameters[] = record.getParameters();
0N/A if (parameters == null || parameters.length == 0) {
0N/A // No parameters. Just return format string.
0N/A return format;
0N/A }
0N/A // Is it a java.text style format?
0N/A // Ideally we could match with
0N/A // Pattern.compile("\\{\\d").matcher(format).find())
0N/A // However the cost is 14% higher, so we cheaply check for
0N/A // 1 of the first 4 parameters
0N/A if (format.indexOf("{0") >= 0 || format.indexOf("{1") >=0 ||
0N/A format.indexOf("{2") >=0|| format.indexOf("{3") >=0) {
0N/A return java.text.MessageFormat.format(format, parameters);
0N/A }
0N/A return format;
0N/A
0N/A } catch (Exception ex) {
0N/A // Formatting failed: use localized format string.
0N/A return format;
0N/A }
0N/A }
0N/A}