CSVFormatter.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 *******************************************************************************/
   12package org.jacoco.report.csv;
   13
   14import java.io.IOException;
   15import java.io.OutputStreamWriter;
   16import java.util.Collection;
   17import java.util.List;
   18
   19import org.jacoco.core.analysis.ICoverageNode;
   20import org.jacoco.core.data.ExecutionData;
   21import org.jacoco.core.data.SessionInfo;
   22import org.jacoco.report.ILanguageNames;
   23import org.jacoco.report.IReportFormatter;
   24import org.jacoco.report.IReportVisitor;
   25import org.jacoco.report.ISingleReportOutput;
   26import org.jacoco.report.ISourceFileLocator;
   27import org.jacoco.report.JavaNames;
   28
   29/**
   30 * Report formatter that will create a single CSV file. By default the filename
   31 * used will be the name of the session.
   32 * 
   33 * @author Brock Janiczak
   34 * @version 0.4.1.20101007204400
   35 */
   36public class CSVFormatter implements IReportFormatter {
   37
   38    private ISingleReportOutput output;
   39
   40    private ILanguageNames languageNames = new JavaNames();
   41
   42    private String outputEncoding = "UTF-8";
   43
   44    public IReportVisitor createReportVisitor(final ICoverageNode root,
   45            final List<SessionInfo> sessionInfos,
   46            final Collection<ExecutionData> executionData) throws IOException {
   47
   48        if (output == null) {
   49            throw new IllegalStateException("No report output set.");
   50        }
   51        final DelimitedWriter writer = new DelimitedWriter(
   52                new OutputStreamWriter(output.createFile(), outputEncoding));
   53        final ClassRowWriter rowWriter = new ClassRowWriter(writer,
   54                languageNames);
   55        return new CSVGroupHandler(rowWriter, root.getName()) {
   56            @Override
   57            public void visitEnd(final ISourceFileLocator sourceFileLocator)
   58                    throws IOException {
   59                writer.close();
   60                super.visitEnd(sourceFileLocator);
   61            }
   62        };
   63    }
   64
   65    /**
   66     * Sets the report output callback for this report formatter. This is a
   67     * mandatory property.
   68     * 
   69     * @param output
   70     *            file output
   71     */
   72    public void setReportOutput(final ISingleReportOutput output) {
   73        this.output = output;
   74    }
   75
   76    /**
   77     * Sets the implementation for language name display. Java language names
   78     * are defined by default.
   79     * 
   80     * @param languageNames
   81     *            converter for language specific names
   82     */
   83    public void setLanguageNames(final ILanguageNames languageNames) {
   84        this.languageNames = languageNames;
   85    }
   86
   87    /**
   88     * Returns the language names call-back used in this report.
   89     * 
   90     * @return language names
   91     */
   92    public ILanguageNames getLanguageNames() {
   93        return languageNames;
   94    }
   95
   96    /**
   97     * Sets the encoding used for generated CSV document. Default is UTF-8.
   98     * 
   99     * @param outputEncoding
  100     *            CSV output encoding
  101     */
  102    public void setOutputEncoding(final String outputEncoding) {
  103        this.outputEncoding = outputEncoding;
  104    }
  105
  106}