SessionsPage.java

    1/*******************************************************************************
    2 * Copyright (c) 2009, 2010 Mountainminds GmbH & Co. KG and Contributors
    3 * All rights reserved. This program and the accompanying materials
    4 * are made available under the terms of the Eclipse Public License v1.0
    5 * which accompanies this distribution, and is available at
    6 * http://www.eclipse.org/legal/epl-v10.html
    7 *
    8 * Contributors:
    9 *    Marc R. Hoffmann - initial API and implementation
   10 *    
   11 *******************************************************************************/
   12package org.jacoco.report.html;
   13
   14import java.io.IOException;
   15import java.text.DateFormat;
   16import java.util.ArrayList;
   17import java.util.Collection;
   18import java.util.Collections;
   19import java.util.Comparator;
   20import java.util.Date;
   21import java.util.List;
   22
   23import org.jacoco.core.data.ExecutionData;
   24import org.jacoco.core.data.SessionInfo;
   25import org.jacoco.report.ILanguageNames;
   26import org.jacoco.report.ReportOutputFolder;
   27import org.jacoco.report.html.index.ElementIndex;
   28import org.jacoco.report.html.resources.Styles;
   29
   30/**
   31 * Page to display information about sessions covered by this report.
   32 * 
   33 * @author Marc R. Hoffmann
   34 * @version 0.4.1.20101007204400
   35 */
   36public class SessionsPage extends ReportPage {
   37
   38    private static final String MSG_SESSIONS = "This coverage report is based "
   39            + "on execution data from the following sessions:";
   40
   41    private static final String MSG_NO_SESSIONS = "No session information available.";
   42
   43    private static final String MSG_EXECDATA = "Execution data for the "
   44            + "following classes is considered in this report:";
   45
   46    private static final String MSG_NO_EXECDATA = "No execution data available.";
   47
   48    private final List<SessionInfo> sessionInfos;
   49
   50    private final DateFormat dateFormat;
   51
   52    private final List<ExecutionData> executionData;
   53
   54    private final ElementIndex index;
   55
   56    /**
   57     * Creates a new page page to display session information.
   58     * 
   59     * @param sessionInfos
   60     * @param executionData
   61     * @param index
   62     * @param parent
   63     * @param folder
   64     * @param context
   65     */
   66    public SessionsPage(final List<SessionInfo> sessionInfos,
   67            final Collection<ExecutionData> executionData,
   68            final ElementIndex index, final ReportPage parent,
   69            final ReportOutputFolder folder, final IHTMLReportContext context) {
   70        super(parent, folder, context);
   71        this.sessionInfos = sessionInfos;
   72        this.executionData = new ArrayList<ExecutionData>(executionData);
   73        this.index = index;
   74        dateFormat = DateFormat.getDateTimeInstance(DateFormat.DEFAULT,
   75                DateFormat.DEFAULT, context.getLocale());
   76        final ILanguageNames names = context.getLanguageNames();
   77        Collections.sort(this.executionData, new Comparator<ExecutionData>() {
   78            public int compare(final ExecutionData e1, final ExecutionData e2) {
   79                return names.getQualifiedClassName(e1.getName()).compareTo(
   80                        names.getQualifiedClassName(e2.getName()));
   81            }
   82        });
   83    }
   84
   85    @Override
   86    protected void content(final HTMLElement body) throws IOException {
   87        if (sessionInfos.isEmpty()) {
   88            body.p().text(MSG_NO_SESSIONS);
   89        } else {
   90            body.p().text(MSG_SESSIONS);
   91            sessionTable(body);
   92        }
   93        if (executionData.isEmpty()) {
   94            body.p().text(MSG_NO_EXECDATA);
   95        } else {
   96            body.p().text(MSG_EXECDATA);
   97            executionDataTable(body);
   98        }
   99    }
  100
  101    private void sessionTable(final HTMLElement body) throws IOException {
  102        final HTMLElement table = body.table(Styles.COVERAGETABLE);
  103        {
  104            final HTMLElement tr = table.thead().tr();
  105            tr.td().text("Session");
  106            tr.td().text("Start Time");
  107            tr.td().text("Dump Time");
  108        }
  109        final HTMLElement tbody = table.tbody();
  110        for (final SessionInfo i : sessionInfos) {
  111            final HTMLElement tr = tbody.tr();
  112            tr.td().span(Styles.EL_SESSION).text(i.getId());
  113            tr.td().text(dateFormat.format(new Date(i.getStartTimeStamp())));
  114            tr.td().text(dateFormat.format(new Date(i.getDumpTimeStamp())));
  115        }
  116    }
  117
  118    private void executionDataTable(final HTMLElement body) throws IOException {
  119        final HTMLElement table = body.table(Styles.COVERAGETABLE);
  120        {
  121            final HTMLElement tr = table.thead().tr();
  122            tr.td().text("Class");
  123            tr.td().text("Id");
  124        }
  125        final HTMLElement tbody = table.tbody();
  126        final ILanguageNames names = context.getLanguageNames();
  127        for (final ExecutionData e : executionData) {
  128            final HTMLElement tr = tbody.tr();
  129            final String link = index.getLinkToClass(e.getId());
  130            final String qualifiedName = names.getQualifiedClassName(e
  131                    .getName());
  132            if (link == null) {
  133                tr.td().span(Styles.EL_CLASS).text(qualifiedName);
  134            } else {
  135                tr.td().a(link, Styles.EL_CLASS).text(qualifiedName);
  136            }
  137            final String id = String.format("%016x", Long.valueOf(e.getId()));
  138            tr.td().code().text(id);
  139        }
  140    }
  141
  142    @Override
  143    protected String getFileName() {
  144        return ".sessions.html";
  145    }
  146
  147    public String getLinkStyle() {
  148        return Styles.EL_SESSION;
  149    }
  150
  151    public String getLinkLabel() {
  152        return "Sessions";
  153    }
  154
  155}