1 /******************************************************************************* 2 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors 3 * This program and the accompanying materials are made available under 4 * the terms of the Eclipse Public License 2.0 which is available at 5 * http://www.eclipse.org/legal/epl-2.0 6 * 7 * SPDX-License-Identifier: EPL-2.0 8 * 9 * Contributors: 10 * Brock Janiczak -initial API and implementation 11 * 12 *******************************************************************************/ 13 package org.jacoco.report.xml; 14 15 import java.io.IOException; 16 import java.io.OutputStream; 17 import java.util.Collection; 18 import java.util.List; 19 20 import org.jacoco.core.analysis.IBundleCoverage; 21 import org.jacoco.core.data.IExecutionData; 22 import org.jacoco.core.data.SessionInfo; 23 import org.jacoco.report.IReportGroupVisitor; 24 import org.jacoco.report.IReportVisitor; 25 import org.jacoco.report.ISourceFileLocator; 26 import org.jacoco.report.internal.xml.ReportElement; 27 import org.jacoco.report.internal.xml.XMLCoverageWriter; 28 import org.jacoco.report.internal.xml.XMLGroupVisitor; 29 30 /** 31 * Report formatter that creates a single XML file for a coverage session 32 */ 33 public class XMLFormatter { 34 35 private String outputEncoding = "UTF-8"; 36 37 /** 38 * Sets the encoding used for generated XML document. Default is UTF-8. 39 * 40 * @param outputEncoding 41 * XML output encoding 42 */ setOutputEncoding(final String outputEncoding)43 public void setOutputEncoding(final String outputEncoding) { 44 this.outputEncoding = outputEncoding; 45 } 46 47 /** 48 * Creates a new visitor to write a report to the given stream. 49 * 50 * @param output 51 * output stream to write the report to 52 * @return visitor to emit the report data to 53 * @throws IOException 54 * in case of problems with the output stream 55 */ createVisitor(final OutputStream output)56 public IReportVisitor createVisitor(final OutputStream output) 57 throws IOException { 58 class RootVisitor implements IReportVisitor { 59 60 private ReportElement report; 61 private List<SessionInfo> sessionInfos; 62 private XMLGroupVisitor groupVisitor; 63 64 public void visitInfo(final List<SessionInfo> sessionInfos, 65 // BEGIN android-change 66 final Collection<IExecutionData> executionData) 67 // END android-change 68 throws IOException { 69 this.sessionInfos = sessionInfos; 70 } 71 72 public void visitBundle(final IBundleCoverage bundle, 73 final ISourceFileLocator locator) throws IOException { 74 createRootElement(bundle.getName()); 75 XMLCoverageWriter.writeBundle(bundle, report); 76 } 77 78 public IReportGroupVisitor visitGroup(final String name) 79 throws IOException { 80 createRootElement(name); 81 groupVisitor = new XMLGroupVisitor(report, name); 82 return groupVisitor; 83 } 84 85 private void createRootElement(final String name) 86 throws IOException { 87 report = new ReportElement(name, output, outputEncoding); 88 for (final SessionInfo i : sessionInfos) { 89 report.sessioninfo(i); 90 } 91 } 92 93 public void visitEnd() throws IOException { 94 if (groupVisitor != null) { 95 groupVisitor.visitEnd(); 96 } 97 report.close(); 98 } 99 } 100 return new RootVisitor(); 101 } 102 103 } 104