ClassPage.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.util.ArrayList;
   16import java.util.List;
   17import java.util.Map;
   18
   19import org.jacoco.core.analysis.ClassCoverage;
   20import org.jacoco.core.analysis.ICoverageNode;
   21import org.jacoco.core.analysis.MethodCoverage;
   22import org.jacoco.report.IReportVisitor;
   23import org.jacoco.report.ReportOutputFolder;
   24import org.jacoco.report.html.resources.Resources;
   25import org.jacoco.report.html.resources.Styles;
   26import org.jacoco.report.html.table.ITableItem;
   27
   28/**
   29 * Page showing coverage information for a class as a table of methods. The
   30 * methods are linked to the corresponding source file.
   31 * 
   32 * @author Marc R. Hoffmann
   33 * @version 0.4.1.20101007204400
   34 */
   35public class ClassPage extends NodePage {
   36
   37    private class MethodItem implements ITableItem {
   38
   39        private final MethodCoverage node;
   40
   41        MethodItem(final MethodCoverage node) {
   42            this.node = node;
   43        }
   44
   45        public String getLinkLabel() {
   46            return context.getLanguageNames().getMethodName(
   47                    ClassPage.this.getNode().getName(), node.getName(),
   48                    node.getDesc(), node.getSignature());
   49        }
   50
   51        public String getLinkStyle() {
   52            return Styles.EL_METHOD;
   53        }
   54
   55        public String getLink(final ReportOutputFolder base) {
   56            final SourceFilePage sourceFilePage = sourceFiles
   57                    .get(sourceFileName);
   58            if (sourceFilePage == null || !sourceFilePage.exists()) {
   59                return null;
   60            }
   61            final String link = sourceFilePage.getLink(base);
   62            final int first = node.getLines().getFirstLine();
   63            return first != -1 ? link + "#L" + first : link;
   64        }
   65
   66        public ICoverageNode getNode() {
   67            return node;
   68        }
   69
   70    }
   71
   72    private final List<MethodItem> methods = new ArrayList<MethodItem>();
   73
   74    private final Map<String, SourceFilePage> sourceFiles;
   75
   76    private final String label;
   77
   78    private final String sourceFileName;
   79
   80    /**
   81     * Creates a new visitor in the given context.
   82     * 
   83     * @param classNode
   84     * @param parent
   85     * @param sourceFiles
   86     * @param folder
   87     * @param context
   88     */
   89    public ClassPage(final ClassCoverage classNode, final ReportPage parent,
   90            final Map<String, SourceFilePage> sourceFiles,
   91            final ReportOutputFolder folder, final IHTMLReportContext context) {
   92        super(classNode, parent, folder, context);
   93        this.sourceFiles = sourceFiles;
   94        this.label = context.getLanguageNames().getClassName(
   95                classNode.getName(), classNode.getSignature(),
   96                classNode.getSuperName(), classNode.getInterfaceNames());
   97        this.sourceFileName = classNode.getSourceFileName();
   98        context.getIndexUpdate().addClass(this, classNode.getId());
   99    }
  100
  101    public IReportVisitor visitChild(final ICoverageNode node) {
  102        methods.add(new MethodItem((MethodCoverage) node));
  103        return IReportVisitor.NOP;
  104    }
  105
  106    @Override
  107    protected void headExtra(final HTMLElement head) throws IOException {
  108        super.headExtra(head);
  109        head.script("text/javascript",
  110                context.getResources().getLink(folder, Resources.SORT_SCRIPT));
  111    }
  112
  113    @Override
  114    protected String getOnload() {
  115        return "initialSort()";
  116    }
  117
  118    @Override
  119    protected void content(final HTMLElement body) throws IOException {
  120        context.getTable().render(body, methods, getNode(),
  121                context.getResources(), folder);
  122    }
  123
  124    @Override
  125    protected String getFileName() {
  126        final String vmname = getNode().getName();
  127        final int pos = vmname.lastIndexOf('/');
  128        final String shortname = pos == -1 ? vmname : vmname.substring(pos + 1);
  129        return shortname + ".html";
  130    }
  131
  132    @Override
  133    public String getLinkLabel() {
  134        return label;
  135    }
  136
  137}