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