ClassRowWriter.java revision d05ad7d4bc65e91b6c6efb45687f7a850d07f02a
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;
15
16import org.jacoco.core.analysis.IClassCoverage;
17import org.jacoco.core.analysis.ICounter;
18import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
19import org.jacoco.report.ILanguageNames;
20
21/**
22 * Writer for rows in the CVS report representing the summary data of a single
23 * class.
24 *
25 * @author Brock Janiczak
26 * @version $qualified.bundle.version$
27 */
28class ClassRowWriter {
29
30	private static final CounterEntity[] COUNTERS = { CounterEntity.METHOD,
31			CounterEntity.LINE, CounterEntity.INSTRUCTION, CounterEntity.BRANCH };
32
33	private final DelimitedWriter writer;
34
35	private final ILanguageNames languageNames;
36
37	/**
38	 * Creates a new row writer that writes class information to the given CSV
39	 * writer.
40	 *
41	 * @param writer
42	 *            writer for csv output
43	 * @param languageNames
44	 *            converter for Java identifiers
45	 * @throws IOException
46	 *             in case of problems with the writer
47	 */
48	public ClassRowWriter(final DelimitedWriter writer,
49			final ILanguageNames languageNames) throws IOException {
50		this.writer = writer;
51		this.languageNames = languageNames;
52		writeHeader();
53	}
54
55	private void writeHeader() throws IOException {
56		writer.write("GROUP", "PACKAGE", "CLASS");
57		for (final CounterEntity entity : COUNTERS) {
58			writer.write(entity.name() + "_COVERED");
59			writer.write(entity.name() + "_MISSED");
60		}
61		writer.nextLine();
62	}
63
64	/**
65	 * Writes the class summary information as a row.
66	 *
67	 * @param groupName
68	 *            name of the group
69	 * @param packageName
70	 *            vm name of the package
71	 * @param node
72	 *            class coverage data
73	 * @throws IOException
74	 *             in case of problems with the writer
75	 */
76	public void writeRow(final String groupName, final String packageName,
77			final IClassCoverage node) throws IOException {
78		writer.write(groupName);
79		writer.write(languageNames.getPackageName(packageName));
80		final String className = languageNames.getClassName(node.getName(),
81				node.getSignature(), node.getSuperName(),
82				node.getInterfaceNames());
83		writer.write(className);
84
85		for (final CounterEntity entity : COUNTERS) {
86			final ICounter counter = node.getCounter(entity);
87			writer.write(counter.getCoveredCount());
88			writer.write(counter.getMissedCount());
89		}
90
91		writer.nextLine();
92	}
93
94}
95