CSVFormatter.java revision 2ccb2fc041b228cec72a256a64bc992f4f7bc331
1/*******************************************************************************
2 * Copyright (c) 2009, 2011 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 *    Brock Janiczak - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.report.csv;
13
14import java.io.IOException;
15import java.io.OutputStreamWriter;
16import java.util.Collection;
17import java.util.List;
18
19import org.jacoco.core.analysis.ICoverageNode;
20import org.jacoco.core.data.ExecutionData;
21import org.jacoco.core.data.SessionInfo;
22import org.jacoco.report.ILanguageNames;
23import org.jacoco.report.IReportFormatter;
24import org.jacoco.report.IReportVisitor;
25import org.jacoco.report.ISingleReportOutput;
26import org.jacoco.report.ISourceFileLocator;
27import org.jacoco.report.JavaNames;
28
29/**
30 * Report formatter that will create a single CSV file. By default the filename
31 * used will be the name of the session.
32 */
33public class CSVFormatter implements IReportFormatter {
34
35	private ISingleReportOutput output;
36
37	private ILanguageNames languageNames = new JavaNames();
38
39	private String outputEncoding = "UTF-8";
40
41	public IReportVisitor createReportVisitor(final ICoverageNode root,
42			final List<SessionInfo> sessionInfos,
43			final Collection<ExecutionData> executionData) throws IOException {
44
45		if (output == null) {
46			throw new IllegalStateException("No report output set.");
47		}
48		final DelimitedWriter writer = new DelimitedWriter(
49				new OutputStreamWriter(output.createFile(), outputEncoding));
50		final ClassRowWriter rowWriter = new ClassRowWriter(writer,
51				languageNames);
52		return new CSVGroupHandler(rowWriter, root.getName()) {
53			@Override
54			public void visitEnd(final ISourceFileLocator sourceFileLocator)
55					throws IOException {
56				writer.close();
57				super.visitEnd(sourceFileLocator);
58			}
59		};
60	}
61
62	/**
63	 * Sets the report output callback for this report formatter. This is a
64	 * mandatory property.
65	 *
66	 * @param output
67	 *            file output
68	 */
69	public void setReportOutput(final ISingleReportOutput output) {
70		this.output = output;
71	}
72
73	/**
74	 * Sets the implementation for language name display. Java language names
75	 * are defined by default.
76	 *
77	 * @param languageNames
78	 *            converter for language specific names
79	 */
80	public void setLanguageNames(final ILanguageNames languageNames) {
81		this.languageNames = languageNames;
82	}
83
84	/**
85	 * Returns the language names call-back used in this report.
86	 *
87	 * @return language names
88	 */
89	public ILanguageNames getLanguageNames() {
90		return languageNames;
91	}
92
93	/**
94	 * Sets the encoding used for generated CSV document. Default is UTF-8.
95	 *
96	 * @param outputEncoding
97	 *            CSV output encoding
98	 */
99	public void setOutputEncoding(final String outputEncoding) {
100		this.outputEncoding = outputEncoding;
101	}
102
103}
104