CsvReportFile.java

    1/*******************************************************************************
    2 * Copyright (c) 2009 Mountainminds GmbH & Co. KG and others
    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;
   16import java.io.OutputStream;
   17import java.io.OutputStreamWriter;
   18import java.io.Writer;
   19
   20import org.jacoco.core.analysis.CoverageNodeImpl;
   21import org.jacoco.core.analysis.ICoverageNode;
   22import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
   23import org.jacoco.core.analysis.ICoverageNode.ElementType;
   24import org.jacoco.report.ILanguageNames;
   25import org.jacoco.report.IReportVisitor;
   26import org.jacoco.report.ISourceFileLocator;
   27
   28/**
   29 * File containing all coverage information in the session.
   30 * 
   31 * @author Brock Janiczak
   32 * @version $Revision: $
   33 */
   34public class CsvReportFile implements IReportVisitor {
   35    /**
   36     * Counters that will be written out at the lowest level of the report. By
   37     * default, this is at the class level
   38     */
   39    public static CounterEntity[] COUNTERS = { CounterEntity.METHOD,
   40            CounterEntity.BLOCK, CounterEntity.LINE, CounterEntity.INSTRUCTION };
   41    /* default */static final IReportVisitor NULL_VISITOR = new NullVisitor();
   42    private final DelimitedWriter writer;
   43    private final ILanguageNames languageNames;
   44
   45    /**
   46     * Creates a new CSV report from the supplied configuration and session data
   47     * 
   48     * @param languageNames
   49     *            Language name callback used for name translation
   50     * @param writer
   51     *            {@link Writer} for CSV output
   52     * @throws IOException
   53     *             Thrown if there were problems creating the output CSV file
   54     */
   55    public CsvReportFile(final ILanguageNames languageNames, final Writer writer)
   56            throws IOException {
   57        this.languageNames = languageNames;
   58        this.writer = new DelimitedWriter(writer);
   59        writeHeader(this.writer);
   60    }
   61
   62    /**
   63     * Creates a new CSV report from the supplied configuration and session data
   64     * 
   65     * @param languageNames
   66     *            Language name callback used for name translation
   67     * @param output
   68     *            {@link OutputStream} to the CSV file to
   69     * @param encoding
   70     *            character encoding of the CSV file
   71     * @throws IOException
   72     *             Thrown if there were problems creating the output CSV file
   73     */
   74    public CsvReportFile(final ILanguageNames languageNames,
   75            final OutputStream output, final String encoding)
   76            throws IOException {
   77        this(languageNames, new OutputStreamWriter(output, encoding));
   78    }
   79
   80    public IReportVisitor visitChild(final ICoverageNode node)
   81            throws IOException {
   82
   83        if (node.getElementType() != ElementType.GROUP) {
   84            final ICoverageNode emptyCoverage = new CoverageNodeImpl(
   85                    ElementType.GROUP, "", false);
   86            final GroupColumn dummy = new GroupColumn(this, emptyCoverage);
   87            return new BundleColumn(this, dummy, node);
   88        }
   89
   90        return new GroupColumn(this, node);
   91    }
   92
   93    public void visitEnd(final ISourceFileLocator sourceFileLocator)
   94            throws IOException {
   95
   96        writer.close();
   97
   98    }
   99
  100    /**
  101     * Returns the writer used for output of this report
  102     * 
  103     * @return delimited writer
  104     */
  105    public DelimitedWriter getWriter() {
  106        return writer;
  107    }
  108
  109    /**
  110     * Returns the language names call-back used in this report.
  111     * 
  112     * @return language names
  113     */
  114    public ILanguageNames getLanguageNames() {
  115        return languageNames;
  116    }
  117
  118    private void writeHeader(final DelimitedWriter writer) throws IOException {
  119        writer.write("GROUP", "BUNDLE", "PACKAGE", "CLASS");
  120
  121        for (final CounterEntity entity : COUNTERS) {
  122            writer.write(entity.name() + "_COVERED");
  123            writer.write(entity.name() + "_NOTCOVERED");
  124        }
  125
  126        writer.nextLine();
  127
  128    }
  129
  130    /**
  131     * Report visitor that ignores its content
  132     */
  133    private static class NullVisitor implements IReportVisitor {
  134
  135        private NullVisitor() {
  136        }
  137
  138        public IReportVisitor visitChild(final ICoverageNode node)
  139                throws IOException {
  140            return this;
  141        }
  142
  143        public void visitEnd(final ISourceFileLocator sourceFileLocator)
  144                throws IOException {
  145        }
  146
  147    }
  148}