HTMLFormatter.java revision 88018f08a57638a9b528e94ee712b3ae182ca0f2
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 * $Id: $
12 *******************************************************************************/
13package org.jacoco.report.html;
14
15import java.io.IOException;
16import java.util.Arrays;
17import java.util.Comparator;
18import java.util.List;
19
20import org.jacoco.core.analysis.CounterComparator;
21import org.jacoco.core.analysis.ICoverageNode;
22import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
23import org.jacoco.core.analysis.ICoverageNode.ElementType;
24import org.jacoco.core.data.SessionInfo;
25import org.jacoco.report.ILanguageNames;
26import org.jacoco.report.IMultiReportOutput;
27import org.jacoco.report.IReportFormatter;
28import org.jacoco.report.IReportVisitor;
29import org.jacoco.report.JavaNames;
30import org.jacoco.report.ReportOutputFolder;
31import org.jacoco.report.html.resources.Resources;
32import org.jacoco.report.html.resources.Styles;
33
34/**
35 * Formatter for coverage reports in multiple HTML pages.
36 *
37 * @author Marc R. Hoffmann
38 * @version $Revision: $
39 */
40public class HTMLFormatter implements IReportFormatter, IHTMLReportContext {
41
42	private IMultiReportOutput output;
43
44	private ILanguageNames languageNames = new JavaNames();
45
46	private String footerText = "";
47
48	private String outputEncoding = "UTF-8";
49
50	private Resources resources;
51
52	private SessionsPage infoPage;
53
54	/**
55	 * The default sorting which is absolute not covered instructions and
56	 * absolute total instructions as the second criterion.
57	 */
58	public static final Comparator<ICoverageNode> DEFAULT_SORTING = CounterComparator.MISSEDITEMS
59			.reverse().on(CounterEntity.INSTRUCTION).second(
60					CounterComparator.TOTALITEMS.reverse().on(
61							CounterEntity.INSTRUCTION));
62
63	private final CoverageTable defaultTable;
64
65	/**
66	 * New instance with default settings.
67	 */
68	public HTMLFormatter() {
69		defaultTable = createDefaultTable();
70	}
71
72	private static CoverageTable createDefaultTable() {
73		return new CoverageTable(Arrays.asList(
74
75		new LabelColumn(),
76
77		new BarColumn("Instruction Coverage", CounterEntity.INSTRUCTION),
78
79		new PercentageColumn("", CounterEntity.INSTRUCTION),
80
81		new CounterColumn("Missed Classes", CounterEntity.CLASS),
82
83		new CounterColumn("Missed Methods", CounterEntity.METHOD),
84
85		new CounterColumn("Missed Blocks", CounterEntity.BLOCK),
86
87		new CounterColumn("Missed Lines", CounterEntity.LINE)), DEFAULT_SORTING);
88	}
89
90	/**
91	 * Defines the output for files created by the formatter. This is a
92	 * mandatory property.
93	 *
94	 * @param output
95	 *            file output
96	 */
97	public void setReportOutput(final IMultiReportOutput output) {
98		this.output = output;
99	}
100
101	/**
102	 * Sets the implementation for language name display. Java language names
103	 * are defined by default.
104	 *
105	 * @param languageNames
106	 *            converter for language specific names
107	 */
108	public void setLanguageNames(final ILanguageNames languageNames) {
109		this.languageNames = languageNames;
110	}
111
112	/**
113	 * Sets the optional text that should be included in every footer page.
114	 *
115	 * @param footerText
116	 *            footer text
117	 */
118	public void setFooterText(final String footerText) {
119		this.footerText = footerText;
120	}
121
122	/**
123	 * Sets the encoding used for generated HTML pages. Default is UTF-8.
124	 *
125	 * @param outputEncoding
126	 *            HTML output encoding
127	 */
128	public void setOutputEncoding(final String outputEncoding) {
129		this.outputEncoding = outputEncoding;
130	}
131
132	// === IHTMLReportContext ===
133
134	public ILanguageNames getLanguageNames() {
135		return languageNames;
136	}
137
138	public Resources getResources() {
139		return resources;
140	}
141
142	public CoverageTable getTable(final ElementType type) {
143		return defaultTable;
144	}
145
146	public String getFooterText() {
147		return footerText;
148	}
149
150	public String getSessionsPageLink(final ReportOutputFolder base) {
151		return infoPage.getLink(base);
152	}
153
154	public String getOutputEncoding() {
155		return outputEncoding;
156	}
157
158	// === IReportFormatter ===
159
160	public IReportVisitor createReportVisitor(final ICoverageNode rootNode,
161			final List<SessionInfo> sessionInfos) throws IOException {
162		if (output == null) {
163			throw new IllegalStateException("No report output set.");
164		}
165		final ReportOutputFolder root = new ReportOutputFolder(output);
166		resources = new Resources(root);
167		resources.copyResources();
168		final GroupPage rootpage = new GroupPage(rootNode, null, root, this) {
169			@Override
170			protected String getElementStyle() {
171				return Styles.EL_REPORT;
172			}
173		};
174		infoPage = new SessionsPage(sessionInfos, rootpage, root, this);
175		infoPage.renderDocument();
176		return rootpage;
177	}
178
179}
180