jdk/src/share/classes/java/util/logging/XMLFormatter.java
author smarks
Wed, 07 Dec 2011 12:12:50 -0800
changeset 11274 7e7196757acd
parent 5506 202f599c92aa
child 19224 a4283c67519d
permissions -rw-r--r--
7117249: fix warnings in java.util.jar, .logging, .prefs, .zip Reviewed-by: alanb, dholmes, forax, sherman, smarks Contributed-by: Prasannaa <prasannaa_ss@yahoo.com>, Martijn Verburg <martijnverburg@gmail.com>, Goerge_Albrecht <goerge.albrecht@gmx.net>, Graham Allan <grundlefleck@googlemail.com>, Michael Barker <mikeb01@gmail.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: 2
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: 2
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: 2
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: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
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
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.nio.charset.Charset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * Format a LogRecord into a standard XML format.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * The DTD specification is provided as Appendix A to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * Java Logging APIs specification.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * The XMLFormatter can be used with arbitrary character encodings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * but it is recommended that it normally be used with UTF-8.  The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * character encoding can be set on the output Handler.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
public class XMLFormatter extends Formatter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    private LogManager manager = LogManager.getLogManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    // Append a two digit number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private void a2(StringBuffer sb, int x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        if (x < 10) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            sb.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        sb.append(x);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    // Append the time and date in ISO 8601 format
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private void appendISO8601(StringBuffer sb, long millis) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        Date date = new Date(millis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        sb.append(date.getYear() + 1900);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        sb.append('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        a2(sb, date.getMonth() + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        sb.append('-');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        a2(sb, date.getDate());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        sb.append('T');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        a2(sb, date.getHours());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        a2(sb, date.getMinutes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        sb.append(':');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        a2(sb, date.getSeconds());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // Append to the given StringBuffer an escaped version of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // given text string where XML special characters have been escaped.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    // For a null string we append "<null>"
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private void escape(StringBuffer sb, String text) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        if (text == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            text = "<null>";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        for (int i = 0; i < text.length(); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            char ch = text.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            if (ch == '<') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                sb.append("&lt;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            } else if (ch == '>') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                sb.append("&gt;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            } else if (ch == '&') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                sb.append("&amp;");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                sb.append(ch);
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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Format the given message to XML.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * This method can be overridden in a subclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * It is recommended to use the {@link Formatter#formatMessage}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * convenience method to localize and format the message field.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param record the log record to be formatted.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @return a formatted log record
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    public String format(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        StringBuffer sb = new StringBuffer(500);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        sb.append("<record>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        sb.append("  <date>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        appendISO8601(sb, record.getMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        sb.append("</date>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        sb.append("  <millis>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        sb.append(record.getMillis());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        sb.append("</millis>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        sb.append("  <sequence>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        sb.append(record.getSequenceNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        sb.append("</sequence>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        String name = record.getLoggerName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (name != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
            sb.append("  <logger>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
            escape(sb, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
            sb.append("</logger>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        sb.append("  <level>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        escape(sb, record.getLevel().toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        sb.append("</level>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (record.getSourceClassName() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            sb.append("  <class>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            escape(sb, record.getSourceClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            sb.append("</class>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (record.getSourceMethodName() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            sb.append("  <method>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            escape(sb, record.getSourceMethodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            sb.append("</method>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        sb.append("  <thread>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        sb.append(record.getThreadID());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        sb.append("</thread>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        if (record.getMessage() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            // Format the message string and its accompanying parameters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            String message = formatMessage(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            sb.append("  <message>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
            escape(sb, message);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            sb.append("</message>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
            sb.append("\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        // If the message is being localized, output the key, resource
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        // bundle name, and params.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        ResourceBundle bundle = record.getResourceBundle();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (bundle != null && bundle.getString(record.getMessage()) != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
                sb.append("  <key>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                escape(sb, record.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                sb.append("</key>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                sb.append("  <catalog>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
                escape(sb, record.getResourceBundleName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                sb.append("</catalog>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
            // The message is not in the catalog.  Drop through.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        Object parameters[] = record.getParameters();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        //  Check to see if the parameter was not a messagetext format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        //  or was not null or empty
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        if ( parameters != null && parameters.length != 0
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
                && record.getMessage().indexOf("{") == -1 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
            for (int i = 0; i < parameters.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
                sb.append("  <param>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
                try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
                    escape(sb, parameters[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
                } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
                    sb.append("???");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                sb.append("</param>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        if (record.getThrown() != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
            // Report on the state of the throwable.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
            Throwable th = record.getThrown();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
            sb.append("  <exception>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            sb.append("    <message>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            escape(sb, th.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            sb.append("</message>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            StackTraceElement trace[] = th.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
            for (int i = 0; i < trace.length; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                StackTraceElement frame = trace[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
                sb.append("    <frame>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
                sb.append("      <class>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
                escape(sb, frame.getClassName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
                sb.append("</class>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
                sb.append("      <method>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
                escape(sb, frame.getMethodName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                sb.append("</method>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
                // Check for a line number.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
                if (frame.getLineNumber() >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                    sb.append("      <line>");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                    sb.append(frame.getLineNumber());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
                    sb.append("</line>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
                sb.append("    </frame>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
            sb.append("  </exception>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
        sb.append("</record>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
     * Return the header string for a set of XML formatted records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
     * @param   h  The target handler (can be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * @return  a valid XML string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    public String getHead(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
        StringBuffer sb = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        String encoding;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        sb.append("<?xml version=\"1.0\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
        if (h != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
            encoding = h.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            encoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        if (encoding == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
            // Figure out the default encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            encoding = java.nio.charset.Charset.defaultCharset().name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        // Try to map the encoding name to a canonical name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
            Charset cs = Charset.forName(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
            encoding = cs.name();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
            // We hit problems finding a canonical name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
            // Just use the raw encoding name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        sb.append(" encoding=\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        sb.append(encoding);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
        sb.append("\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
        sb.append(" standalone=\"no\"?>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        sb.append("<!DOCTYPE log SYSTEM \"logger.dtd\">\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
        sb.append("<log>\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
        return sb.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
     * Return the tail string for a set of XML formatted records.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
     * @param   h  The target handler (can be null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
     * @return  a valid XML string
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
    public String getTail(Handler h) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return "</log>\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
}