HTMLFormatter.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 static org.jacoco.core.analysis.ICoverageNode.CounterEntity.BLOCK;
15import static org.jacoco.core.analysis.ICoverageNode.CounterEntity.CLASS;
16import static org.jacoco.core.analysis.ICoverageNode.CounterEntity.INSTRUCTION;
17import static org.jacoco.core.analysis.ICoverageNode.CounterEntity.LINE;
18import static org.jacoco.core.analysis.ICoverageNode.CounterEntity.METHOD;
19
20import java.io.IOException;
21import java.util.Collection;
22import java.util.List;
23import java.util.Locale;
24
25import org.jacoco.core.analysis.ICoverageNode;
26import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
27import org.jacoco.core.data.ExecutionData;
28import org.jacoco.core.data.SessionInfo;
29import org.jacoco.report.ILanguageNames;
30import org.jacoco.report.IMultiReportOutput;
31import org.jacoco.report.IReportFormatter;
32import org.jacoco.report.IReportVisitor;
33import org.jacoco.report.ISourceFileLocator;
34import org.jacoco.report.JavaNames;
35import org.jacoco.report.ReportOutputFolder;
36import org.jacoco.report.html.index.ElementIndex;
37import org.jacoco.report.html.index.IIndexUpdate;
38import org.jacoco.report.html.resources.Resources;
39import org.jacoco.report.html.resources.Styles;
40import org.jacoco.report.html.table.BarColumn;
41import org.jacoco.report.html.table.CounterColumn;
42import org.jacoco.report.html.table.LabelColumn;
43import org.jacoco.report.html.table.PercentageColumn;
44import org.jacoco.report.html.table.Table;
45
46/**
47 * Formatter for coverage reports in multiple HTML pages.
48 *
49 * @author Marc R. Hoffmann
50 * @version 0.4.1.20101007204400
51 */
52public class HTMLFormatter implements IReportFormatter, IHTMLReportContext {
53
54 private IMultiReportOutput output;
55
56 private ILanguageNames languageNames = new JavaNames();
57
58 private Locale locale = Locale.getDefault();
59
60 private String footerText = "";
61
62 private String outputEncoding = "UTF-8";
63
64 private Resources resources;
65
66 private ElementIndex index;
67
68 private SessionsPage sessionsPage;
69
70 private Table table;
71
72 /**
73 * New instance with default settings.
74 */
75 public HTMLFormatter() {
76 }
77
78 /**
79 * Defines the output for files created by the formatter. This is a
80 * mandatory property.
81 *
82 * @param output
83 * file output
84 */
85 public void setReportOutput(final IMultiReportOutput output) {
86 this.output = output;
87 }
88
89 /**
90 * Sets the implementation for language name display. Java language names
91 * are defined by default.
92 *
93 * @param languageNames
94 * converter for language specific names
95 */
96 public void setLanguageNames(final ILanguageNames languageNames) {
97 this.languageNames = languageNames;
98 }
99
100 /**
101 * Sets the locale used for report rendering. The current default locale is
102 * used by default.
103 *
104 * @param locale
105 * locale used for report rendering
106 */
107 public void setLocale(final Locale locale) {
108 this.locale = locale;
109 }
110
111 /**
112 * Sets the optional text that should be included in every footer page.
113 *
114 * @param footerText
115 * footer text
116 */
117 public void setFooterText(final String footerText) {
118 this.footerText = footerText;
119 }
120
121 /**
122 * Sets the encoding used for generated HTML pages. Default is UTF-8.
123 *
124 * @param outputEncoding
125 * HTML output encoding
126 */
127 public void setOutputEncoding(final String outputEncoding) {
128 this.outputEncoding = outputEncoding;
129 }
130
131 // === IHTMLReportContext ===
132
133 public ILanguageNames getLanguageNames() {
134 return languageNames;
135 }
136
137 public Resources getResources() {
138 return resources;
139 }
140
141 public Table getTable() {
142 if (table == null) {
143 table = createTable();
144 }
145 return table;
146 }
147
148 private Table createTable() {
149 final Table table = new Table();
150 table.add("Element", null, new LabelColumn(), false);
151 table.add("Missed Instructions", null, new BarColumn(INSTRUCTION,
152 locale), true);
153 table.add("Cov.", Styles.CTR2,
154 new PercentageColumn(INSTRUCTION, locale), false);
155 addMissedTotalColumns(table, "Classes", CLASS);
156 addMissedTotalColumns(table, "Methods", METHOD);
157 addMissedTotalColumns(table, "Blocks", BLOCK);
158 addMissedTotalColumns(table, "Lines", LINE);
159 return table;
160 }
161
162 private void addMissedTotalColumns(final Table table, final String label,
163 final CounterEntity entity) {
164 table.add("Missed", Styles.CTR1,
165 CounterColumn.newMissed(entity, locale), false);
166 table.add(label, Styles.CTR2, CounterColumn.newTotal(entity, locale),
167 false);
168 }
169
170 public String getFooterText() {
171 return footerText;
172 }
173
174 public ILinkable getSessionsPage() {
175 return sessionsPage;
176 }
177
178 public String getOutputEncoding() {
179 return outputEncoding;
180 }
181
182 public IIndexUpdate getIndexUpdate() {
183 return index;
184 }
185
186 public Locale getLocale() {
187 return locale;
188 }
189
190 // === IReportFormatter ===
191
192 public IReportVisitor createReportVisitor(final ICoverageNode rootNode,
193 final List<SessionInfo> sessionInfos,
194 final Collection<ExecutionData> executionData) throws IOException {
195 if (output == null) {
196 throw new IllegalStateException("No report output set.");
197 }
198 final ReportOutputFolder root = new ReportOutputFolder(output);
199 resources = new Resources(root);
200 resources.copyResources();
201 index = new ElementIndex(root);
202 final GroupPage rootpage = new GroupPage(rootNode, null, root, this) {
203 @Override
204 public String getLinkStyle() {
205 return Styles.EL_REPORT;
206 }
207
208 @Override
209 public void visitEnd(final ISourceFileLocator sourceFileLocator)
210 throws IOException {
211 super.visitEnd(sourceFileLocator);
212 sessionsPage.renderDocument();
213 }
214 };
215 sessionsPage = new SessionsPage(sessionInfos, executionData, index,
216 rootpage, root, this);
217 return rootpage;
218 }
219
220}