PackageSourcePage.java revision 34cd880f4e52a32b9f88ed4ea687b8f3f892395b
1/*******************************************************************************
2 * Copyright (c) 2009, 2017 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.internal.html.page;
13
14import java.io.IOException;
15import java.io.Reader;
16import java.util.HashMap;
17import java.util.Map;
18
19import org.jacoco.core.analysis.IPackageCoverage;
20import org.jacoco.core.analysis.ISourceFileCoverage;
21import org.jacoco.report.ISourceFileLocator;
22import org.jacoco.report.internal.ReportOutputFolder;
23import org.jacoco.report.internal.html.HTMLElement;
24import org.jacoco.report.internal.html.IHTMLReportContext;
25import org.jacoco.report.internal.html.ILinkable;
26import org.jacoco.report.internal.html.resources.Styles;
27
28/**
29 * Page showing coverage information for a Java package. The page contains a
30 * table with all classes of the package.
31 */
32public class PackageSourcePage extends TablePage<IPackageCoverage> {
33
34	private final ISourceFileLocator locator;
35	private final Map<String, ILinkable> sourceFilePages;
36	private final ILinkable packagePage;
37
38	/**
39	 * Creates a new visitor in the given context.
40	 *
41	 * @param node
42	 *            coverage data for this package
43	 * @param parent
44	 *            optional hierarchical parent
45	 * @param locator
46	 *            source locator
47	 * @param folder
48	 *            base folder to create this page in
49	 * @param context
50	 *            settings context
51	 * @param packagePage
52	 *            page listing the classes of this package
53	 */
54	public PackageSourcePage(final IPackageCoverage node,
55			final ReportPage parent, final ISourceFileLocator locator,
56			final ReportOutputFolder folder, final IHTMLReportContext context,
57			final ILinkable packagePage) {
58		super(node, parent, folder, context);
59		this.locator = locator;
60		this.packagePage = packagePage;
61		this.sourceFilePages = new HashMap<String, ILinkable>();
62	}
63
64	@Override
65	public void render() throws IOException {
66		renderSourceFilePages();
67		super.render();
68	}
69
70	/**
71	 * Returns the link to the source file page of the source file with the
72	 * given name. If no source file was located, <code>null</code> is
73	 * returned..
74	 */
75	ILinkable getSourceFilePage(final String name) {
76		return sourceFilePages.get(name);
77	}
78
79	private final void renderSourceFilePages() throws IOException {
80		final String packagename = getNode().getName();
81		for (final ISourceFileCoverage s : getNode().getSourceFiles()) {
82			final String sourcename = s.getName();
83			final Reader reader = locator
84					.getSourceFile(packagename, sourcename);
85			if (reader == null) {
86				addItem(new SourceFileItem(s));
87			} else {
88				final SourceFilePage sourcePage = new SourceFilePage(s, reader,
89						locator.getTabWidth(), this, folder, context);
90				sourcePage.render();
91				sourceFilePages.put(sourcename, sourcePage);
92				addItem(sourcePage);
93			}
94
95		}
96	}
97
98	@Override
99	protected String getOnload() {
100		return "initialSort(['breadcrumb', 'coveragetable'])";
101	}
102
103	@Override
104	protected String getFileName() {
105		return "index.source.html";
106	}
107
108	@Override
109	public String getLinkLabel() {
110		return context.getLanguageNames().getPackageName(getNode().getName());
111	}
112
113	@Override
114	protected void infoLinks(final HTMLElement span) throws IOException {
115		final String link = packagePage.getLink(folder);
116		span.a(link, Styles.EL_CLASS).text("Classes");
117		super.infoLinks(span);
118	}
119
120}
121