CSVGroupHandler.java revision a6d2b043f09984e3f2fe77e9f7502564350055f6
1/*******************************************************************************
2 * Copyright (c) 2009, 2016 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.IBundleCoverage;
17import org.jacoco.core.analysis.IClassCoverage;
18import org.jacoco.core.analysis.IPackageCoverage;
19import org.jacoco.report.IReportGroupVisitor;
20import org.jacoco.report.ISourceFileLocator;
21
22/**
23 * Report visitor that handles coverage information for groups.
24 */
25class CSVGroupHandler implements IReportGroupVisitor {
26
27	private final ClassRowWriter writer;
28
29	private final String groupName;
30
31	public CSVGroupHandler(final ClassRowWriter writer) {
32		this(writer, null);
33	}
34
35	private CSVGroupHandler(final ClassRowWriter writer, final String groupName) {
36		this.writer = writer;
37		this.groupName = groupName;
38	}
39
40	public void visitBundle(final IBundleCoverage bundle,
41			final ISourceFileLocator locator) throws IOException {
42		final String name = appendName(bundle.getName());
43		for (final IPackageCoverage p : bundle.getPackages()) {
44			final String packageName = p.getName();
45			for (final IClassCoverage c : p.getClasses()) {
46				writer.writeRow(name, packageName, c);
47			}
48		}
49	}
50
51	public IReportGroupVisitor visitGroup(final String name) throws IOException {
52		return new CSVGroupHandler(writer, appendName(name));
53	}
54
55	private String appendName(final String name) {
56		return groupName == null ? name : (groupName + "/" + name);
57	}
58
59}
60