1/*******************************************************************************
2 * Copyright (c) 2009, 2015 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 *    Marc R. Hoffmann - generalized structure, line info
11 *
12 *******************************************************************************/
13package org.jacoco.report.internal.xml;
14
15import java.io.IOException;
16
17import org.jacoco.core.analysis.IBundleCoverage;
18import org.jacoco.report.ISourceFileLocator;
19import org.jacoco.report.internal.AbstractGroupVisitor;
20
21/**
22 * A {@link org.jacoco.report.IReportGroupVisitor} that transforms the report
23 * structure into XML elements.
24 */
25public class XMLGroupVisitor extends AbstractGroupVisitor {
26
27	/** XML element of this group */
28	protected final XMLElement element;
29
30	/**
31	 * New handler for a group with the given name.
32	 *
33	 * @param element
34	 *            XML-Element representing this coverage node. The start tag
35	 *            must not be closed yet to allow adding additional attributes.
36	 * @param name
37	 *            name of the group
38	 * @throws IOException
39	 *             in case of problems with the underlying writer
40	 */
41	public XMLGroupVisitor(final XMLElement element, final String name)
42			throws IOException {
43		super(name);
44		this.element = element;
45	}
46
47	@Override
48	protected void handleBundle(final IBundleCoverage bundle,
49			final ISourceFileLocator locator) throws IOException {
50		final XMLElement child = createChild(bundle.getName());
51		XMLCoverageWriter.writeBundle(bundle, child);
52	}
53
54	@Override
55	protected AbstractGroupVisitor handleGroup(final String name)
56			throws IOException {
57		final XMLElement child = createChild(name);
58		return new XMLGroupVisitor(child, name);
59	}
60
61	@Override
62	protected void handleEnd() throws IOException {
63		XMLCoverageWriter.writeCounters(total, element);
64	}
65
66	private XMLElement createChild(final String name) throws IOException {
67		return XMLCoverageWriter.createChild(element, "group", name);
68	}
69
70}
71