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