• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Brock Janiczak - initial API and implementation
11  *
12  *******************************************************************************/
13 package org.jacoco.report.csv;
14 
15 import java.io.IOException;
16 import java.io.OutputStream;
17 import java.io.OutputStreamWriter;
18 import java.util.Collection;
19 import java.util.List;
20 
21 import org.jacoco.core.data.IExecutionData;
22 import org.jacoco.core.data.SessionInfo;
23 import org.jacoco.report.ILanguageNames;
24 import org.jacoco.report.IReportVisitor;
25 import org.jacoco.report.JavaNames;
26 
27 /**
28  * Report formatter that will create a single CSV file. By default the filename
29  * used will be the name of the session.
30  */
31 public class CSVFormatter {
32 
33 	private ILanguageNames languageNames = new JavaNames();
34 
35 	private String outputEncoding = "UTF-8";
36 
37 	/**
38 	 * Sets the implementation for language name display. Java language names
39 	 * are defined by default.
40 	 *
41 	 * @param languageNames
42 	 *            converter for language specific names
43 	 */
setLanguageNames(final ILanguageNames languageNames)44 	public void setLanguageNames(final ILanguageNames languageNames) {
45 		this.languageNames = languageNames;
46 	}
47 
48 	/**
49 	 * Returns the language names call-back used in this report.
50 	 *
51 	 * @return language names
52 	 */
getLanguageNames()53 	public ILanguageNames getLanguageNames() {
54 		return languageNames;
55 	}
56 
57 	/**
58 	 * Sets the encoding used for generated CSV document. Default is UTF-8.
59 	 *
60 	 * @param outputEncoding
61 	 *            CSV output encoding
62 	 */
setOutputEncoding(final String outputEncoding)63 	public void setOutputEncoding(final String outputEncoding) {
64 		this.outputEncoding = outputEncoding;
65 	}
66 
67 	/**
68 	 * Creates a new visitor to write a report to the given stream.
69 	 *
70 	 * @param output
71 	 *            output stream to write the report to
72 	 * @return visitor to emit the report data to
73 	 * @throws IOException
74 	 *             in case of problems with the output stream
75 	 */
createVisitor(final OutputStream output)76 	public IReportVisitor createVisitor(final OutputStream output)
77 			throws IOException {
78 		final DelimitedWriter writer = new DelimitedWriter(
79 				new OutputStreamWriter(output, outputEncoding));
80 		final ClassRowWriter rowWriter = new ClassRowWriter(writer,
81 				languageNames);
82 		class Visitor extends CSVGroupHandler implements IReportVisitor {
83 			Visitor() {
84 				super(rowWriter);
85 			}
86 
87 			public void visitInfo(final List<SessionInfo> sessionInfos,
88 					// BEGIN android-change
89 					final Collection<IExecutionData> executionData)
90 					// END android-change
91 					throws IOException {
92 				// Info not used for CSV report
93 			}
94 
95 			public void visitEnd() throws IOException {
96 				writer.close();
97 			}
98 		}
99 		return new Visitor();
100 	}
101 
102 }
103