/* XMLFormatter.java
-- a class for formatting log messages into a standard XML format
Copyright (C) 2002 Free Software Foundation, Inc.
This file is part of GNU Classpath.
GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA.
Linking this library statically or dynamically with other modules is
making a combined work based on this library. Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module. An independent module is a module which is not derived from
or based on this library. If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so. If you do not wish to do so, delete this
exception statement from your version.
*/
package java.util.logging;
import java.util.Date;
import java.util.ResourceBundle;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
/**
* An XMLFormatter
formats LogRecords into
* a standard XML format.
*
* @author Sascha Brawer (brawer@acm.org)
*/
public class XMLFormatter
extends Formatter
{
/**
* Constructs a new XMLFormatter.
*/
public XMLFormatter()
{}
/**
* The value of the system property line.separator
.
*/
private static final String lineSep = System.getProperty("line.separator");
/**
* A DateFormat for emitting time in the ISO 8601 format.
*/
private final SimpleDateFormat iso8601
= new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
/* FIXME: If SimpleDateFormat was thread-safe, we could share a
* singleton instance in all XMLFormatters. Check this.
*/
/* FIXME: Does this really emit the date and time in ISO-8601?
* (Pattern taken from Brian Jones's implementation, not
* verified).
*/
/**
* Appends a line consisting of indentation, opening element tag,
* element content, closing element tag and line separator to
* a StringBuffer, provided that the element content is
* actually existing.
*
* @param buf the StringBuffer to which the line will be appended.
*
* @param indent the indentation level.
*
* @param tag the element tag name, for instance method
.
*
* @param content the element content, or null
to
* have no output whatsoever appended to buf
.
*/
private static final void appendTag(StringBuffer buf,
int indent,
String tag,
String content)
{
if (content == null)
return;
for (int i = 0; i < indent * 2; i++)
buf.append(' ');
buf.append("<");
buf.append(tag);
buf.append('>');
buf.append(content);
buf.append("");
buf.append(tag);
buf.append('>');
buf.append(lineSep);
}
/**
* Appends a line consisting of indentation, opening element tag,
* numeric element content, closing element tag and line separator
* to a StringBuffer.
*
* @param buf the StringBuffer to which the line will be appended.
*
* @param indent the indentation level.
*
* @param tag the element tag name, for instance method
.
*
* @param content the element content.
*/
private static final void appendTag(StringBuffer buf,
int indent,
String tag,
long content)
{
appendTag(buf, indent, tag, Long.toString(content));
}
/**
* Determines whether or not a Level is one of the standard
* levels specified in the Logging API. Since the constructor
* of Level is protected, custom levels must be an instance
* of a sub-class of java.util.logging.Level.
*
*
Actually, this is not entirely true: Someone could write * a sub-class of Level with a static method that creates a * new Level instance. Does this need fixing? FIXME: Review. * *
FIXME: Should this be a package-private method of Level?
*
* @param level the level in question.
*/
private static final boolean isStandardLevel(Level level)
{
return level.getClass() == Level.class;
}
public String format(LogRecord record)
{
StringBuffer buf = new StringBuffer(400);
Level level = record.getLevel();
long millis = record.getMillis();
Object[] params = record.getParameters();
ResourceBundle bundle = record.getResourceBundle();
String key, message;
buf.append("