001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.html;
015
016import static ch.qos.logback.core.CoreConstants.LINE_SEPARATOR;
017
018import java.util.Map;
019
020import ch.qos.logback.access.PatternLayout;
021import ch.qos.logback.access.spi.IAccessEvent;
022import ch.qos.logback.core.html.HTMLLayoutBase;
023import ch.qos.logback.core.pattern.Converter;
024
025/**
026 * 
027 * HTMLLayout outputs events in an HTML table.
028 * <p>
029 * The content of the table columns are specified using a conversion pattern.
030 * See {@link ch.qos.logback.access.PatternLayout} for documentation on the
031 * available patterns.
032 * <p>
033 * For more information about this layout, please refer to the online manual at
034 * http://logback.qos.ch/manual/layouts.html#AccessHTMLLayout
035 * 
036 * 
037 * @author Ceki G&uuml;lc&uuml;
038 * @author S&eacute;bastien Pennec
039 */
040public class HTMLLayout extends HTMLLayoutBase<IAccessEvent> {
041
042    /**
043     * Default pattern string for log output.
044     */
045    static final String DEFAULT_CONVERSION_PATTERN = "%h%l%u%t%r%s%b";
046
047    /**
048     * Constructs a PatternLayout using the DEFAULT_LAYOUT_PATTERN.
049     * 
050     */
051    public HTMLLayout() {
052        pattern = DEFAULT_CONVERSION_PATTERN;
053        cssBuilder = new DefaultCssBuilder();
054    }
055
056    @Override
057    protected Map<String, String> getDefaultConverterMap() {
058        return PatternLayout.defaultConverterMap;
059    }
060
061    @Override
062    public String doLayout(IAccessEvent event) {
063        StringBuilder buf = new StringBuilder();
064        startNewTableIfLimitReached(buf);
065
066        boolean odd = true;
067        if (((counter++) & 1) == 0) {
068            odd = false;
069        }
070
071        buf.append(LINE_SEPARATOR);
072        buf.append("<tr class=\"");
073        if (odd) {
074            buf.append(" odd\">");
075        } else {
076            buf.append(" even\">");
077        }
078        buf.append(LINE_SEPARATOR);
079
080        Converter<IAccessEvent> c = head;
081        while (c != null) {
082            appendEventToBuffer(buf, c, event);
083            c = c.getNext();
084        }
085        buf.append("</tr>");
086        buf.append(LINE_SEPARATOR);
087
088        return buf.toString();
089    }
090
091    private void appendEventToBuffer(StringBuilder buf, Converter<IAccessEvent> c, IAccessEvent event) {
092        buf.append("<td class=\"");
093        buf.append(computeConverterName(c));
094        buf.append("\">");
095        c.write(buf, event);
096        buf.append("</td>");
097        buf.append(LINE_SEPARATOR);
098    }
099}