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