1 /*******************************************************************************
2  * Copyright (c) 2009, 2015 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  *******************************************************************************/
12 package org.jacoco.report.csv;
13 
14 import java.io.IOException;
15 import java.io.OutputStream;
16 import java.io.OutputStreamWriter;
17 import java.util.Collection;
18 import java.util.List;
19 
20 import org.jacoco.core.data.ExecutionData;
21 import org.jacoco.core.data.SessionInfo;
22 import org.jacoco.report.ILanguageNames;
23 import org.jacoco.report.IReportVisitor;
24 import 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 public class CSVFormatter {
31 
32 	private ILanguageNames languageNames = new JavaNames();
33 
34 	private String outputEncoding = "UTF-8";
35 
36 	/**
37 	 * Sets the implementation for language name display. Java language names
38 	 * are defined by default.
39 	 *
40 	 * @param languageNames
41 	 *            converter for language specific names
42 	 */
setLanguageNames(final ILanguageNames languageNames)43 	public void setLanguageNames(final ILanguageNames languageNames) {
44 		this.languageNames = languageNames;
45 	}
46 
47 	/**
48 	 * Returns the language names call-back used in this report.
49 	 *
50 	 * @return language names
51 	 */
getLanguageNames()52 	public ILanguageNames getLanguageNames() {
53 		return languageNames;
54 	}
55 
56 	/**
57 	 * Sets the encoding used for generated CSV document. Default is UTF-8.
58 	 *
59 	 * @param outputEncoding
60 	 *            CSV output encoding
61 	 */
setOutputEncoding(final String outputEncoding)62 	public void setOutputEncoding(final String outputEncoding) {
63 		this.outputEncoding = outputEncoding;
64 	}
65 
66 	/**
67 	 * Creates a new visitor to write a report to the given stream.
68 	 *
69 	 * @param output
70 	 *            output stream to write the report to
71 	 * @return visitor to emit the report data to
72 	 * @throws IOException
73 	 *             in case of problems with the output stream
74 	 */
createVisitor(final OutputStream output)75 	public IReportVisitor createVisitor(final OutputStream output)
76 			throws IOException {
77 		final DelimitedWriter writer = new DelimitedWriter(
78 				new OutputStreamWriter(output, outputEncoding));
79 		final ClassRowWriter rowWriter = new ClassRowWriter(writer,
80 				languageNames);
81 		class Visitor extends CSVGroupHandler implements IReportVisitor {
82 			Visitor() {
83 				super(rowWriter);
84 			}
85 
86 			public void visitInfo(final List<SessionInfo> sessionInfos,
87 					final Collection<ExecutionData> executionData)
88 					throws IOException {
89 				// Info not used for CSV report
90 			}
91 
92 			public void visitEnd() throws IOException {
93 				writer.close();
94 			}
95 		}
96 		return new Visitor();
97 	}
98 
99 }
100