PackagePage.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.HashMap;
   17import java.util.List;
   18import java.util.Map;
   19
   20import org.jacoco.core.analysis.ClassCoverage;
   21import org.jacoco.core.analysis.ICoverageNode;
   22import org.jacoco.core.analysis.ICoverageNode.ElementType;
   23import org.jacoco.core.analysis.SourceFileCoverage;
   24import org.jacoco.report.IReportVisitor;
   25import org.jacoco.report.ISourceFileLocator;
   26import org.jacoco.report.ReportOutputFolder;
   27import org.jacoco.report.html.resources.Resources;
   28
   29/**
   30 * Page showing coverage information for a Java package. The page contains a
   31 * table with all classes of the package.
   32 * 
   33 * @author Marc R. Hoffmann
   34 * @version 0.4.1.20101007204400
   35 */
   36public class PackagePage extends NodePage {
   37
   38    private final List<ClassPage> classes = new ArrayList<ClassPage>();
   39
   40    private final Map<String, SourceFilePage> sourceFiles = new HashMap<String, SourceFilePage>();
   41
   42    /**
   43     * Creates a new visitor in the given context.
   44     * 
   45     * @param node
   46     * @param parent
   47     * @param folder
   48     * @param context
   49     */
   50    public PackagePage(final ICoverageNode node, final ReportPage parent,
   51            final ReportOutputFolder folder, final IHTMLReportContext context) {
   52        super(node, parent, folder, context);
   53    }
   54
   55    public IReportVisitor visitChild(final ICoverageNode node) {
   56        final ElementType type = node.getElementType();
   57        switch (type) {
   58        case SOURCEFILE:
   59            final SourceFilePage sourcePage = new SourceFilePage(
   60                    (SourceFileCoverage) node, this, folder, context);
   61            sourceFiles.put(node.getName(), sourcePage);
   62            return sourcePage;
   63        case CLASS:
   64            final ClassPage classPage = new ClassPage((ClassCoverage) node,
   65                    this, sourceFiles, folder, context);
   66            classes.add(classPage);
   67            return classPage;
   68        }
   69        throw new IllegalStateException("Unexpected element type " + type);
   70    }
   71
   72    @Override
   73    public void visitEnd(final ISourceFileLocator sourceFileLocator)
   74            throws IOException {
   75        super.visitEnd(sourceFileLocator);
   76        // free memory, otherwise we will keep the complete tree:
   77        classes.clear();
   78        sourceFiles.clear();
   79    }
   80
   81    @Override
   82    protected void headExtra(final HTMLElement head) throws IOException {
   83        super.headExtra(head);
   84        head.script("text/javascript",
   85                context.getResources().getLink(folder, Resources.SORT_SCRIPT));
   86    }
   87
   88    @Override
   89    protected String getOnload() {
   90        return "initialSort()";
   91    }
   92
   93    @Override
   94    protected void content(final HTMLElement body) throws IOException {
   95        context.getTable().render(body, classes, getNode(),
   96                context.getResources(), folder);
   97    }
   98
   99    @Override
  100    protected String getFileName() {
  101        return "index.html";
  102    }
  103
  104    @Override
  105    public String getLinkLabel() {
  106        return context.getLanguageNames().getPackageName(getNode().getName());
  107    }
  108
  109}