ClassPage.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.List;
18import java.util.Map;
19
20import org.jacoco.core.analysis.ClassCoverage;
21import org.jacoco.core.analysis.ICoverageNode;
22import org.jacoco.core.analysis.MethodCoverage;
23import org.jacoco.report.IReportVisitor;
24import org.jacoco.report.ISourceFileLocator;
25import org.jacoco.report.ReportOutputFolder;
26
27/**
28 * Page showing coverage information for a class as a table of methods. The
29 * methods are linked to the corresponding source file.
30 *
31 * @author Marc R. Hoffmann
32 * @version $Revision: $
33 */
34public class ClassPage extends ReportPage {
35
36 private class MethodItem implements ICoverageTableItem {
37
38 private final MethodCoverage node;
39
40 MethodItem(final MethodCoverage node) {
41 this.node = node;
42 }
43
44 public String getLabel() {
45 return context.getLanguageNames().getMethodName(
46 ClassPage.this.getNode().getName(), node.getName(),
47 node.getDesc());
48 }
49
50 public String getLink(final ReportOutputFolder base) {
51 final SourceFilePage sourceFilePage = sourceFiles
52 .get(((ClassCoverage) ClassPage.this.getNode())
53 .getSourceFileName());
54 if (sourceFilePage == null || !sourceFilePage.exists()) {
55 return null;
56 }
57 final String link = sourceFilePage.getLink(base);
58 final int first = node.getLines().getFirstLine();
59 return first != -1 ? link + "#L" + first : link;
60 }
61
62 public ICoverageNode getNode() {
63 return node;
64 }
65
66 }
67
68 private final List<MethodItem> methods = new ArrayList<MethodItem>();
69
70 private final Map<String, SourceFilePage> sourceFiles;
71
72 /**
73 * Creates a new visitor in the given context.
74 *
75 * @param node
76 * @param parent
77 * @param sourceFiles
78 * @param outputFolder
79 * @param context
80 */
81 public ClassPage(final ICoverageNode node, final ReportPage parent,
82 final Map<String, SourceFilePage> sourceFiles,
83 final ReportOutputFolder outputFolder,
84 final IHTMLReportContext context) {
85 super(node, parent, outputFolder, context);
86 this.sourceFiles = sourceFiles;
87 }
88
89 public IReportVisitor visitChild(final ICoverageNode node) {
90 methods.add(new MethodItem((MethodCoverage) node));
91 return new IReportVisitor() {
92
93 public IReportVisitor visitChild(final ICoverageNode node) {
94 throw new IllegalStateException(
95 "Methods must not have child nodes.");
96 }
97
98 public void visitEnd(final ISourceFileLocator sourceFileLocator) {
99 }
100 };
101 }
102
103 @Override
104 protected void content(final HTMLElement body,
105 final ISourceFileLocator sourceFileLocator) throws IOException {
106 context.getTable(getNode().getElementType()).render(body, methods,
107 getNode(), context.getResources(), outputFolder);
108 }
109
110 @Override
111 protected String getFileName() {
112 return getLabel() + ".html";
113 }
114
115 @Override
116 public String getLabel() {
117 return context.getLanguageNames().getClassName(getNode().getName());
118 }
119
120 @Override
121 protected ReportOutputFolder getFolder(final ReportOutputFolder base) {
122 return base;
123 }
124
125}