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(), node.getSignature());
48 }
49
50 public String getLink(final ReportOutputFolder base) {
51 final SourceFilePage sourceFilePage = sourceFiles
52 .get(sourceFileName);
53 if (sourceFilePage == null || !sourceFilePage.exists()) {
54 return null;
55 }
56 final String link = sourceFilePage.getLink(base);
57 final int first = node.getLines().getFirstLine();
58 return first != -1 ? link + "#L" + first : link;
59 }
60
61 public ICoverageNode getNode() {
62 return node;
63 }
64
65 }
66
67 private final List<MethodItem> methods = new ArrayList<MethodItem>();
68
69 private final Map<String, SourceFilePage> sourceFiles;
70
71 private final String label;
72
73 private final String sourceFileName;
74
75 /**
76 * Creates a new visitor in the given context.
77 *
78 * @param classNode
79 * @param parent
80 * @param sourceFiles
81 * @param outputFolder
82 * @param context
83 */
84 public ClassPage(final ClassCoverage classNode, final ReportPage parent,
85 final Map<String, SourceFilePage> sourceFiles,
86 final ReportOutputFolder outputFolder,
87 final IHTMLReportContext context) {
88 super(classNode, parent, outputFolder, context);
89 this.sourceFiles = sourceFiles;
90 this.label = context.getLanguageNames().getClassName(
91 classNode.getName(), classNode.getSignature(),
92 classNode.getSuperName(), classNode.getInterfaceNames());
93 this.sourceFileName = classNode.getSourceFileName();
94 }
95
96 public IReportVisitor visitChild(final ICoverageNode node) {
97 methods.add(new MethodItem((MethodCoverage) node));
98 return new IReportVisitor() {
99
100 public IReportVisitor visitChild(final ICoverageNode node) {
101 throw new IllegalStateException(
102 "Methods must not have child nodes.");
103 }
104
105 public void visitEnd(final ISourceFileLocator sourceFileLocator) {
106 }
107 };
108 }
109
110 @Override
111 protected void content(final HTMLElement body,
112 final ISourceFileLocator sourceFileLocator) throws IOException {
113 context.getTable(getNode().getElementType()).render(body, methods,
114 getNode(), context.getResources(), outputFolder);
115 }
116
117 @Override
118 protected String getFileName() {
119 final String vmname = getNode().getName();
120 final int pos = vmname.lastIndexOf('/');
121 final String shortname = pos == -1 ? vmname : vmname.substring(pos + 1);
122 return shortname + ".html";
123 }
124
125 @Override
126 public String getLabel() {
127 return label;
128 }
129
130 @Override
131 protected ReportOutputFolder getFolder(final ReportOutputFolder base) {
132 return base;
133 }
134
135}