001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package java.util.logging;
019:
020: import java.text.MessageFormat;
021: import java.util.ResourceBundle;
022:
023: /**
024: * <code>Formatter</code> objects are used to format <code>LogRecord</code>
025: * objects into a string representation. Head and tail strings are sometime used
026: * to wrap a set of records. The <code>getHead</code> and <code>getTail</code>
027: * methods are presented for this purpose.
028: */
029: public abstract class Formatter {
030:
031: /**
032: * Constructs a <code>Formatter</code> object.
033: */
034: protected Formatter() {
035: super ();
036: }
037:
038: /**
039: * Formats a <code>LogRecord</code> object into a string representation.
040: * The resulted string is usually localized and includes the message field
041: * of the supplied <code>LogRecord</code> object.
042: *
043: * @param r
044: * the log record to be formatted into a string
045: * @return the string resulted from the formatting
046: */
047: public abstract String format(LogRecord r);
048:
049: /**
050: * Formats a <code>LogRecord</code> object into a localized string
051: * representation. This method can be regarded as a convenience for
052: * subclasses of <code>Formatter</code> to use.
053: * <p>
054: * The message string is firstly localized using the
055: * <code>ResourceBundle</code> object associated with the supplied
056: * <code>LogRecord</code>.
057: * </p>
058: *
059: * @param r
060: * the log record to be formatted
061: * @return the string resulted from the formatting
062: */
063: public String formatMessage(LogRecord r) {
064: String pattern = r.getMessage();
065: ResourceBundle rb = null;
066: // try to localize the message string first
067: if (null != (rb = r.getResourceBundle())) {
068: try {
069: pattern = rb.getString(pattern);
070: } catch (Exception e) {
071: pattern = r.getMessage();
072: }
073: }
074: if (null != pattern) {
075: Object[] params = r.getParameters();
076: /*
077: * if the message contains "{0", use java.text.MessageFormat to
078: * format the string
079: */
080: if (pattern.indexOf("{0") >= 0 && null != params //$NON-NLS-1$
081: && params.length > 0) {
082: try {
083: pattern = MessageFormat.format(pattern, params);
084: } catch (IllegalArgumentException e) {
085: pattern = r.getMessage();
086: }
087: }
088: }
089: return pattern;
090: }
091:
092: /**
093: * Gets the head string used to wrap a set of log records. This base class
094: * always returns the empty string.
095: *
096: * @param h
097: * the target handler
098: * @return the head string used to wrap a set of log records
099: */
100: @SuppressWarnings("unused")
101: public String getHead(Handler h) {
102: return ""; //$NON-NLS-1$
103: }
104:
105: /**
106: * Gets the tail string used to wrap a set of log records. This base class
107: * always returns the empty string.
108: *
109: * @param h
110: * the target handler
111: * @return the tail string used to wrap a set of log records
112: */
113: @SuppressWarnings("unused")
114: public String getTail(Handler h) {
115: return ""; //$NON-NLS-1$
116: }
117: }
|