NodeWithCoverage.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.xml;
14
15import java.io.IOException;
16
17import org.jacoco.core.analysis.ICounter;
18import org.jacoco.core.analysis.ICoverageNode;
19import org.jacoco.core.analysis.ICoverageNode.CounterEntity;
20import org.jacoco.report.IReportVisitor;
21import org.jacoco.report.ISourceFileLocator;
22
23/**
24 * Base class for implementing XML Elements that contain coverage elements
25 *
26 * @author Brock Janiczak
27 * @version $Revision: $
28 */
29public abstract class NodeWithCoverage extends XMLElement implements
30 IReportVisitor {
31 private static final CounterEntity[] DEFAULT_COUNTERS = {
32 CounterEntity.CLASS, CounterEntity.METHOD, CounterEntity.BLOCK,
33 CounterEntity.LINE, CounterEntity.INSTRUCTION, };
34
35 private final ICoverageNode node;
36
37 /**
38 * Creates a new Coverage node under the supplied parent
39 *
40 * @param parent
41 * Parent element
42 * @param elementName
43 * Name of this element
44 * @param node
45 * Coverage node
46 * @throws IOException
47 * IO Error creating this element
48 */
49 public NodeWithCoverage(final XMLElement parent, final String elementName,
50 final ICoverageNode node) throws IOException {
51 super(parent.writer, elementName);
52 parent.addChildElement(this);
53 this.node = node;
54 this.attr("name", node.getName());
55 }
56
57 public final void visitEnd(final ISourceFileLocator sourceFileLocator)
58 throws IOException {
59
60 for (final CounterEntity counterEntity : getCounterEntities()) {
61 createCounterElement(counterEntity);
62 }
63
64 this.close();
65 }
66
67 /**
68 * Retrieves the list of counters supported by this element
69 *
70 * @return Counters supported by this element
71 */
72 protected CounterEntity[] getCounterEntities() {
73 return DEFAULT_COUNTERS;
74 }
75
76 private void createCounterElement(final CounterEntity counterEntity)
77 throws IOException {
78 final ICounter counter = node.getCounter(counterEntity);
79
80 final XMLElement counterNode = this.element("counter");
81 counterNode.attr("type", counterEntity.name());
82 counterNode
83 .attr("covered", Integer.toString(counter.getCoveredCount()));
84 counterNode.attr("notcovered", Integer.toString(counter
85 .getNotCoveredCount()));
86
87 counterNode.close();
88 }
89
90}