PackagePage.java

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