Source Code Cross Referenced for Formatter.java in  » 6.0-JDK-Core » Collections-Jar-Zip-Logging-regex » java » util » logging » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Home
Java Source Code / Java Documentation
1.6.0 JDK Core
2.6.0 JDK Modules
3.6.0 JDK Modules com.sun
4.6.0 JDK Modules com.sun.java
5.6.0 JDK Modules sun
6.6.0 JDK Platform
7.Ajax
8.Apache Harmony Java SE
9.Aspect oriented
10.Authentication Authorization
11.Blogger System
12.Build
13.Byte Code
14.Cache
15.Chart
16.Chat
17.Code Analyzer
18.Collaboration
19.Content Management System
20.Database Client
21.Database DBMS
22.Database JDBC Connection Pool
23.Database ORM
24.Development
25.EJB Server
26.ERP CRM Financial
27.ESB
28.Forum
29.Game
30.GIS
31.Graphic 3D
32.Graphic Library
33.Groupware
34.HTML Parser
35.IDE
36.IDE Eclipse
37.IDE Netbeans
38.Installer
39.Internationalization Localization
40.Inversion of Control
41.Issue Tracking
42.J2EE
43.J2ME
44.JBoss
45.JMS
46.JMX
47.Library
48.Mail Clients
49.Music
50.Net
51.Parser
52.PDF
53.Portal
54.Profiler
55.Project Management
56.Report
57.RSS RDF
58.Rule Engine
59.Science
60.Scripting
61.Search Engine
62.Security
63.Sevlet Container
64.Source Control
65.Swing Library
66.Template Engine
67.Test Coverage
68.Testing
69.UML
70.Web Crawler
71.Web Framework
72.Web Mail
73.Web Server
74.Web Services
75.Web Services apache cxf 2.2.6
76.Web Services AXIS2
77.Wiki Engine
78.Workflow Engines
79.XML
80.XML UI
Java Source Code / Java Documentation » 6.0 JDK Core » Collections Jar Zip Logging regex » java.util.logging 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001        /*
002         * Copyright 2000-2006 Sun Microsystems, Inc.  All Rights Reserved.
003         * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004         *
005         * This code is free software; you can redistribute it and/or modify it
006         * under the terms of the GNU General Public License version 2 only, as
007         * published by the Free Software Foundation.  Sun designates this
008         * particular file as subject to the "Classpath" exception as provided
009         * by Sun in the LICENSE file that accompanied this code.
010         *
011         * This code is distributed in the hope that it will be useful, but WITHOUT
012         * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013         * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014         * version 2 for more details (a copy is included in the LICENSE file that
015         * accompanied this code).
016         *
017         * You should have received a copy of the GNU General Public License version
018         * 2 along with this work; if not, write to the Free Software Foundation,
019         * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020         *
021         * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022         * CA 95054 USA or visit www.sun.com if you need additional information or
023         * have any questions.
024         */
025
026        package java.util.logging;
027
028        /**
029         * A Formatter provides support for formatting LogRecords.
030         * <p>
031         * Typically each logging Handler will have a Formatter associated
032         * with it.  The Formatter takes a LogRecord and converts it to
033         * a string.
034         * <p>
035         * Some formatters (such as the XMLFormatter) need to wrap head
036         * and tail strings around a set of formatted records. The getHeader
037         * and getTail methods can be used to obtain these strings.
038         *
039         * @version 1.24, 05/05/07
040         * @since 1.4
041         */
042
043        public abstract class Formatter {
044
045            /**
046             * Construct a new formatter.
047             */
048            protected Formatter() {
049            }
050
051            /**
052             * Format the given log record and return the formatted string. 
053             * <p>
054             * The resulting formatted String will normally include a
055             * localized and formated version of the LogRecord's message field.
056             * It is recommended to use the {@link Formatter#formatMessage}
057             * convenience method to localize and format the message field.
058             *
059             * @param record the log record to be formatted.
060             * @return the formatted log record
061             */
062            public abstract String format(LogRecord record);
063
064            /**
065             * Return the header string for a set of formatted records.
066             * <p>  
067             * This base class returns an empty string, but this may be
068             * overriden by subclasses.
069             * 
070             * @param   h  The target handler (can be null)
071             * @return  header string
072             */
073            public String getHead(Handler h) {
074                return "";
075            }
076
077            /**
078             * Return the tail string for a set of formatted records.
079             * <p>  
080             * This base class returns an empty string, but this may be
081             * overriden by subclasses.
082             * 
083             * @param   h  The target handler (can be null)
084             * @return  tail string
085             */
086            public String getTail(Handler h) {
087                return "";
088            }
089
090            /**
091             * Localize and format the message string from a log record.  This
092             * method is provided as a convenience for Formatter subclasses to
093             * use when they are performing formatting.
094             * <p>
095             * The message string is first localized to a format string using
096             * the record's ResourceBundle.  (If there is no ResourceBundle,
097             * or if the message key is not found, then the key is used as the
098             * format string.)  The format String uses java.text style
099             * formatting.
100             * <ul>
101             * <li>If there are no parameters, no formatter is used.
102             * <li>Otherwise, if the string contains "{0" then
103             *     java.text.MessageFormat  is used to format the string.
104             * <li>Otherwise no formatting is performed. 
105             * </ul> 
106             * <p>
107             *
108             * @param  record  the log record containing the raw message
109             * @return   a localized and formatted message
110             */
111            public synchronized String formatMessage(LogRecord record) {
112                String format = record.getMessage();
113                java.util.ResourceBundle catalog = record.getResourceBundle();
114                if (catalog != null) {
115                    try {
116                        format = catalog.getString(record.getMessage());
117                    } catch (java.util.MissingResourceException ex) {
118                        // Drop through.  Use record message as format
119                        format = record.getMessage();
120                    }
121                }
122                // Do the formatting.
123                try {
124                    Object parameters[] = record.getParameters();
125                    if (parameters == null || parameters.length == 0) {
126                        // No parameters.  Just return format string.
127                        return format;
128                    }
129                    // Is it a java.text style format?
130                    // Ideally we could match with
131                    // Pattern.compile("\\{\\d").matcher(format).find())
132                    // However the cost is 14% higher, so we cheaply check for
133                    // 1 of the first 4 parameters
134                    if (format.indexOf("{0") >= 0 || format.indexOf("{1") >= 0
135                            || format.indexOf("{2") >= 0
136                            || format.indexOf("{3") >= 0) {
137                        return java.text.MessageFormat.format(format,
138                                parameters);
139                    }
140                    return format;
141
142                } catch (Exception ex) {
143                    // Formatting failed: use localized format string.
144                    return format;
145                }
146            }
147        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.