FileDocCategorySizeDatePackage
XMLFormatter.javaAPI DocAndroid 1.5 API9412Wed May 06 22:41:04 BST 2009java.util.logging

XMLFormatter

public class XMLFormatter extends Formatter
Formatter to convert a {@link LogRecord} into an XML string. The DTD specified in Appendix A to the Java Logging APIs specification is used. {@code XMLFormatter} uses the output handler's encoding if it is specified, otherwise the default platform encoding is used instead. UTF-8 is the recommended encoding.
since
Android 1.0

Fields Summary
private static final String
lineSeperator
private static final String
indent
Constructors Summary
public XMLFormatter()
Constructs a new {@code XMLFormatter}.

since
Android 1.0

 //$NON-NLS-1$

                  
      
        super();
    
Methods Summary
public java.lang.Stringformat(java.util.logging.LogRecord r)
Converts a {@code LogRecord} into an XML string.

param
r the log record to be formatted.
return
the log record formatted as an XML string.
since
Android 1.0

        //call a method of LogRecord to ensure not null
        long time = r.getMillis();
        //format to date
        String date = MessageFormat.format("{0, date} {0, time}", //$NON-NLS-1$
                new Object[] { new Date(time) });

        StringBuilder sb = new StringBuilder();
        sb.append(("<record>")).append(lineSeperator); //$NON-NLS-1$
        sb.append(indent).append(("<date>")).append(date).append(("</date>")) //$NON-NLS-1$ //$NON-NLS-2$
                .append(lineSeperator);
        sb.append(indent).append(("<millis>")).append(time).append( //$NON-NLS-1$
                ("</millis>")).append(lineSeperator); //$NON-NLS-1$
        sb.append(indent).append(("<sequence>")).append(r.getSequenceNumber()) //$NON-NLS-1$
                .append(("</sequence>")).append(lineSeperator); //$NON-NLS-1$
        if (null != r.getLoggerName()) {
            sb.append(indent).append(("<logger>")).append(r.getLoggerName()) //$NON-NLS-1$
                    .append(("</logger>")).append(lineSeperator); //$NON-NLS-1$
        }
        sb.append(indent).append(("<level>")).append(r.getLevel().getName()) //$NON-NLS-1$
                .append(("</level>")).append(lineSeperator); //$NON-NLS-1$
        if (null != r.getSourceClassName()) {
            sb.append(indent).append(("<class>")) //$NON-NLS-1$
                    .append(r.getSourceClassName()).append(("</class>")) //$NON-NLS-1$
                    .append(lineSeperator);
        }
        if (null != r.getSourceMethodName()) {
            sb.append(indent).append(("<method>")).append( //$NON-NLS-1$
                    r.getSourceMethodName()).append(("</method>")).append( //$NON-NLS-1$
                    lineSeperator);
        }
        sb.append(indent).append(("<thread>")).append(r.getThreadID()).append( //$NON-NLS-1$
                ("</thread>")).append(lineSeperator); //$NON-NLS-1$
        formatMessages(r, sb);
        Object[] params;
        if ((params = r.getParameters()) != null) {
            for (Object element : params) {
                sb.append(indent).append(("<param>")).append(element).append( //$NON-NLS-1$
                        ("</param>")).append(lineSeperator); //$NON-NLS-1$
            }
        }
        formatThrowable(r, sb);
        sb.append(("</record>")).append(lineSeperator); //$NON-NLS-1$
        return sb.toString();
    
private voidformatMessages(java.util.logging.LogRecord r, java.lang.StringBuilder sb)

        //get localized message if has, but don't call Formatter.formatMessage to parse pattern string
        ResourceBundle rb = r.getResourceBundle();
        String pattern = r.getMessage();
        if (null != rb && null != pattern) {
            String message;
            try {
                message = rb.getString(pattern);
            } catch (Exception e) {
                message = null;
            }

            if (message == null) {
                message = pattern;
                sb.append(indent).append(("<message>")).append(message).append( //$NON-NLS-1$
                        ("</message>")).append(lineSeperator); //$NON-NLS-1$
            } else {
                sb.append(indent).append(("<message>")).append(message).append( //$NON-NLS-1$
                        ("</message>")).append(lineSeperator); //$NON-NLS-1$
                sb.append(indent).append(("<key>")).append(pattern).append( //$NON-NLS-1$
                        ("</key>")).append(lineSeperator); //$NON-NLS-1$
                sb.append(indent).append(("<catalog>")).append( //$NON-NLS-1$
                        r.getResourceBundleName()).append(("</catalog>")) //$NON-NLS-1$
                        .append(lineSeperator);
            }
        } else if(null != pattern){
            sb.append(indent).append(("<message>")).append(pattern).append( //$NON-NLS-1$
                    ("</message>")).append(lineSeperator); //$NON-NLS-1$
        } else{
            sb.append(indent).append(("<message/>")); //$NON-NLS-1$
        }
    
private voidformatThrowable(java.util.logging.LogRecord r, java.lang.StringBuilder sb)

        Throwable t;
        if ((t = r.getThrown()) != null) {
            sb.append(indent).append("<exception>").append(lineSeperator); //$NON-NLS-1$
            sb.append(indent).append(indent).append("<message>").append( //$NON-NLS-1$
                    t.toString()).append("</message>").append(lineSeperator); //$NON-NLS-1$
            //format throwable's stack trace
            StackTraceElement[] elements = t.getStackTrace();
            for (StackTraceElement e : elements) {
                sb.append(indent).append(indent).append("<frame>").append( //$NON-NLS-1$
                        lineSeperator);
                sb.append(indent).append(indent).append(indent).append(
                        "<class>").append(e.getClassName()).append("</class>")  //$NON-NLS-1$//$NON-NLS-2$
                        .append(lineSeperator);
                sb.append(indent).append(indent).append(indent).append(
                        "<method>").append(e.getMethodName()).append( //$NON-NLS-1$
                        "</method>").append(lineSeperator); //$NON-NLS-1$
                sb.append(indent).append(indent).append(indent)
                        .append("<line>").append(e.getLineNumber()).append( //$NON-NLS-1$
                                "</line>").append(lineSeperator); //$NON-NLS-1$
                sb.append(indent).append(indent).append("</frame>").append( //$NON-NLS-1$
                        lineSeperator);
            }
            sb.append(indent).append("</exception>").append(lineSeperator); //$NON-NLS-1$
        }
    
public java.lang.StringgetHead(java.util.logging.Handler h)
Returns the header string for a set of log records formatted as XML strings, using the output handler's encoding if it is defined, otherwise using the default platform encoding.

param
h the output handler, may be {@code null}.
return
the header string for log records formatted as XML strings.
since
Android 1.0

        String encoding = null;
        if(null != h) {
            encoding = h.getEncoding();
        }
        if (null == encoding) {
            encoding = getSystemProperty("file.encoding"); //$NON-NLS-1$
        }
        StringBuilder sb = new StringBuilder();
        sb.append("<?xml version=\"1.0\" encoding=\"").append(encoding).append( //$NON-NLS-1$
                "\" standalone=\"no\"?>").append(lineSeperator); //$NON-NLS-1$
        sb.append("<!DOCTYPE log SYSTEM \"logger.dtd\">").append(lineSeperator); //$NON-NLS-1$
        sb.append(("<log>")); //$NON-NLS-1$
        return sb.toString();
    
private static java.lang.StringgetSystemProperty(java.lang.String key)

        return AccessController.doPrivileged(
          new PrivilegedAction<String>() {
            public String run() {
                return System.getProperty(key);
            }
        });
    
public java.lang.StringgetTail(java.util.logging.Handler h)
Returns the tail string for a set of log records formatted as XML strings.

param
h the output handler, may be {@code null}.
return
the tail string for log records formatted as XML strings.
since
Android 1.0

        return "</log>"; //$NON-NLS-1$