KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > logging > XMLFormatter


1 /*
2  * @(#)XMLFormatter.java 1.24 04/01/12
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package java.util.logging;
10
11 import java.io.*;
12 import java.nio.charset.Charset JavaDoc;
13 import java.util.*;
14
15 /**
16  * Format a LogRecord into a standard XML format.
17  * <p>
18  * The DTD specification is provided as Appendix A to the
19  * Java Logging APIs specification.
20  * <p>
21  * The XMLFormatter can be used with arbitrary character encodings,
22  * but it is recommended that it normally be used with UTF-8. The
23  * character encoding can be set on the output Handler.
24  *
25  * @version 1.24, 01/12/04
26  * @since 1.4
27  */

28
29 public class XMLFormatter extends Formatter JavaDoc {
30     private LogManager JavaDoc manager = LogManager.getLogManager();
31
32     // Append a two digit number.
33
private void a2(StringBuffer JavaDoc sb, int x) {
34     if (x < 10) {
35         sb.append('0');
36     }
37     sb.append(x);
38     }
39
40     // Append the time and date in ISO 8601 format
41
private void appendISO8601(StringBuffer JavaDoc sb, long millis) {
42     Date date = new Date(millis);
43     sb.append(date.getYear() + 1900);
44     sb.append('-');
45     a2(sb, date.getMonth() + 1);
46     sb.append('-');
47     a2(sb, date.getDate());
48     sb.append('T');
49     a2(sb, date.getHours());
50     sb.append(':');
51     a2(sb, date.getMinutes());
52     sb.append(':');
53     a2(sb, date.getSeconds());
54     }
55
56     // Append to the given StringBuffer an escaped version of the
57
// given text string where XML special characters have been escaped.
58
// For a null string we append "<null>"
59
private void escape(StringBuffer JavaDoc sb, String JavaDoc text) {
60     if (text == null) {
61         text = "<null>";
62     }
63     for (int i = 0; i < text.length(); i++) {
64         char ch = text.charAt(i);
65         if (ch == '<') {
66         sb.append("&lt;");
67         } else if (ch == '>') {
68         sb.append("&gt;");
69         } else if (ch == '&') {
70         sb.append("&amp;");
71         } else {
72         sb.append(ch);
73         }
74     }
75     }
76
77     /**
78      * Format the given message to XML.
79      * @param record the log record to be formatted.
80      * @return a formatted log record
81      */

82     public String JavaDoc format(LogRecord JavaDoc record) {
83     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(500);
84     sb.append("<record>\n");
85
86     sb.append(" <date>");
87     appendISO8601(sb, record.getMillis());
88     sb.append("</date>\n");
89
90     sb.append(" <millis>");
91     sb.append(record.getMillis());
92     sb.append("</millis>\n");
93
94     sb.append(" <sequence>");
95     sb.append(record.getSequenceNumber());
96     sb.append("</sequence>\n");
97
98     String JavaDoc name = record.getLoggerName();
99     if (name != null) {
100         sb.append(" <logger>");
101         escape(sb, name);
102         sb.append("</logger>\n");
103     }
104
105     sb.append(" <level>");
106     escape(sb, record.getLevel().toString());
107     sb.append("</level>\n");
108
109     if (record.getSourceClassName() != null) {
110         sb.append(" <class>");
111         escape(sb, record.getSourceClassName());
112         sb.append("</class>\n");
113     }
114
115     if (record.getSourceMethodName() != null) {
116         sb.append(" <method>");
117         escape(sb, record.getSourceMethodName());
118         sb.append("</method>\n");
119     }
120
121     sb.append(" <thread>");
122     sb.append(record.getThreadID());
123     sb.append("</thread>\n");
124
125     if (record.getMessage() != null) {
126         // Format the message string and its accompanying parameters.
127
String JavaDoc message = formatMessage(record);
128         sb.append(" <message>");
129         escape(sb, message);
130         sb.append("</message>");
131         sb.append("\n");
132     }
133
134     // If the message is being localized, output the key, resource
135
// bundle name, and params.
136
ResourceBundle bundle = record.getResourceBundle();
137     try {
138         if (bundle != null && bundle.getString(record.getMessage()) != null) {
139             sb.append(" <key>");
140             escape(sb, record.getMessage());
141             sb.append("</key>\n");
142             sb.append(" <catalog>");
143             escape(sb, record.getResourceBundleName());
144             sb.append("</catalog>\n");
145         }
146     } catch (Exception JavaDoc ex) {
147         // The message is not in the catalog. Drop through.
148
}
149
150     Object JavaDoc parameters[] = record.getParameters();
151         // Check to see if the parameter was not a messagetext format
152
// or was not null or empty
153
if ( parameters != null && parameters.length != 0
154                 && record.getMessage().indexOf("{") == -1 ) {
155             for (int i = 0; i < parameters.length; i++) {
156                 sb.append(" <param>");
157             try {
158                     escape(sb, parameters[i].toString());
159             } catch (Exception JavaDoc ex) {
160                 sb.append("???");
161             }
162                 sb.append("</param>\n");
163         }
164         }
165
166     if (record.getThrown() != null) {
167         // Report on the state of the throwable.
168
Throwable JavaDoc th = record.getThrown();
169         sb.append(" <exception>\n");
170         sb.append(" <message>");
171         escape(sb, th.toString());
172         sb.append("</message>\n");
173         StackTraceElement JavaDoc trace[] = th.getStackTrace();
174         for (int i = 0; i < trace.length; i++) {
175         StackTraceElement JavaDoc frame = trace[i];
176         sb.append(" <frame>\n");
177         sb.append(" <class>");
178         escape(sb, frame.getClassName());
179         sb.append("</class>\n");
180         sb.append(" <method>");
181         escape(sb, frame.getMethodName());
182         sb.append("</method>\n");
183             // Check for a line number.
184
if (frame.getLineNumber() >= 0) {
185             sb.append(" <line>");
186             sb.append(frame.getLineNumber());
187             sb.append("</line>\n");
188         }
189             sb.append(" </frame>\n");
190         }
191         sb.append(" </exception>\n");
192     }
193
194     sb.append("</record>\n");
195     return sb.toString();
196     }
197
198     /**
199      * Return the header string for a set of XML formatted records.
200      *
201      * @param h The target handler (can be null)
202      * @return a valid XML string
203      */

204     public String JavaDoc getHead(Handler JavaDoc h) {
205     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
206     String JavaDoc encoding;
207     sb.append("<?xml version=\"1.0\"");
208
209     if (h != null) {
210         encoding = h.getEncoding();
211     } else {
212         encoding = null;
213     }
214
215     if (encoding == null) {
216         // Figure out the default encoding.
217
encoding = sun.io.Converters.getDefaultEncodingName();
218     }
219     // Try to map the encoding name to a canonical name.
220
try {
221         Charset JavaDoc cs = Charset.forName(encoding);
222         encoding = cs.name();
223     } catch (Exception JavaDoc ex) {
224         // We hit problems finding a canonical name.
225
// Just use the raw encoding name.
226
}
227
228         sb.append(" encoding=\"");
229         sb.append(encoding);
230         sb.append("\"");
231     sb.append(" standalone=\"no\"?>\n");
232     sb.append("<!DOCTYPE log SYSTEM \"logger.dtd\">\n");
233     sb.append("<log>\n");
234     return sb.toString();
235     }
236
237     /**
238      * Return the tail string for a set of XML formatted records.
239      *
240      * @param h The target handler (can be null)
241      * @return a valid XML string
242      */

243     public String JavaDoc getTail(Handler JavaDoc h) {
244     return "</log>\n";
245     }
246 }
247
Popular Tags