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