aboutsummaryrefslogtreecommitdiffstats
path: root/org.jacoco.report/src/org/jacoco/report/internal/xml/XMLCoverageWriter.java
blob: f359bd3e94540553cfebea676e1a96056f630d0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*******************************************************************************
 * Copyright (c) 2009, 2012 Mountainminds GmbH & Co. KG and Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *    
 *******************************************************************************/
package org.jacoco.report.internal.xml;

import java.io.IOException;

import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.analysis.ICounter;
import org.jacoco.core.analysis.ICoverageNode;
import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
import org.jacoco.core.analysis.ILine;
import org.jacoco.core.analysis.IMethodCoverage;
import org.jacoco.core.analysis.IPackageCoverage;
import org.jacoco.core.analysis.ISourceFileCoverage;
import org.jacoco.core.analysis.ISourceNode;

/**
 * Serializes coverage data as XML fragments.
 */
public final class XMLCoverageWriter {

	/**
	 * Creates a child element with a name attribute.
	 * 
	 * @param parent
	 *            parent element
	 * @param tagname
	 *            name of the child tag
	 * @param name
	 *            value of the name attribute
	 * @return child element
	 * @throws IOException
	 */
	public static XMLElement createChild(final XMLElement parent,
			final String tagname, final String name) throws IOException {
		final XMLElement child = parent.element(tagname);
		child.attr("name", name);
		return child;
	}

	/**
	 * Writes the structure of a given bundle.
	 * 
	 * @param bundle
	 *            bundle coverage data
	 * @param element
	 *            container element for the bundle data
	 * @throws IOException
	 */
	public static void writeBundle(final IBundleCoverage bundle,
			final XMLElement element) throws IOException {
		for (final IPackageCoverage p : bundle.getPackages()) {
			writePackage(p, element);
		}
		writeCounters(bundle, element);
	}

	private static void writePackage(final IPackageCoverage p,
			final XMLElement parent) throws IOException {
		final XMLElement element = createChild(parent, "package", p.getName());
		for (final IClassCoverage c : p.getClasses()) {
			writeClass(c, element);
		}
		for (final ISourceFileCoverage s : p.getSourceFiles()) {
			writeSourceFile(s, element);
		}
		writeCounters(p, element);
	}

	private static void writeClass(final IClassCoverage c,
			final XMLElement parent) throws IOException {
		final XMLElement element = createChild(parent, "class", c.getName());
		for (final IMethodCoverage m : c.getMethods()) {
			writeMethod(m, element);
		}
		writeCounters(c, element);
	}

	private static void writeMethod(final IMethodCoverage m,
			final XMLElement parent) throws IOException {
		final XMLElement element = createChild(parent, "method", m.getName());
		element.attr("desc", m.getDesc());
		final int line = m.getFirstLine();
		if (line != -1) {
			element.attr("line", line);
		}
		writeCounters(m, element);
	}

	private static void writeSourceFile(final ISourceFileCoverage s,
			final XMLElement parent) throws IOException {
		final XMLElement element = createChild(parent, "sourcefile",
				s.getName());
		writeLines(s, element);
		writeCounters(s, element);
	}

	/**
	 * Writes all non-zero counters of the given node.
	 * 
	 * @param node
	 *            node to retrieve counters from
	 * @param parent
	 *            container for the counter elements
	 * @throws IOException
	 */
	public static void writeCounters(final ICoverageNode node,
			final XMLElement parent) throws IOException {
		for (final CounterEntity counterEntity : CounterEntity.values()) {
			final ICounter counter = node.getCounter(counterEntity);
			if (counter.getTotalCount() > 0) {
				final XMLElement counterNode = parent.element("counter");
				counterNode.attr("type", counterEntity.name());
				writeCounter(counterNode, "missed", "covered", counter);
				counterNode.close();
			}
		}
	}

	private static void writeLines(final ISourceNode source,
			final XMLElement parent) throws IOException {
		final int last = source.getLastLine();
		for (int nr = source.getFirstLine(); nr <= last; nr++) {
			final ILine line = source.getLine(nr);
			if (line.getStatus() != ICounter.EMPTY) {
				final XMLElement element = parent.element("line");
				element.attr("nr", nr);
				writeCounter(element, "mi", "ci", line.getInstructionCounter());
				writeCounter(element, "mb", "cb", line.getBranchCounter());
			}
		}
	}

	private static void writeCounter(final XMLElement element,
			final String missedattr, final String coveredattr,
			final ICounter counter) throws IOException {
		element.attr(missedattr, counter.getMissedCount());
		element.attr(coveredattr, counter.getCoveredCount());
	}

	private XMLCoverageWriter() {
	}

}