aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report/src/org/jacoco/report/check/RulesChecker.java
blob: 59a85916996f9c8a3ac242d20b91beea8089d313 (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
/*******************************************************************************
 * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
 * This program and the accompanying materials are made available under
 * the terms of the Eclipse Public License 2.0 which is available at
 * http://www.eclipse.org/legal/epl-2.0
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *    Marc R. Hoffmann - initial API and implementation
 *
 *******************************************************************************/
package org.jacoco.report.check;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.data.IExecutionData;
import org.jacoco.core.data.SessionInfo;
import org.jacoco.report.ILanguageNames;
import org.jacoco.report.IReportGroupVisitor;
import org.jacoco.report.IReportVisitor;
import org.jacoco.report.ISourceFileLocator;
import org.jacoco.report.JavaNames;

/**
 * Formatter which checks a set of given rules and reports violations to a
 * {@link IViolationsOutput} instance.
 */
public class RulesChecker {

	private List<Rule> rules;
	private ILanguageNames languageNames;

	/**
	 * New formatter instance.
	 */
	public RulesChecker() {
		this.rules = new ArrayList<Rule>();
		this.setLanguageNames(new JavaNames());
	}

	/**
	 * Sets the rules to check by this formatter.
	 *
	 * @param rules
	 *            rules to check
	 */
	public void setRules(final List<Rule> rules) {
		this.rules = rules;
	}

	/**
	 * Sets the implementation for language name display for message formatting.
	 * Java language names are defined by default.
	 *
	 * @param languageNames
	 *            converter for language specific names
	 */
	public void setLanguageNames(final ILanguageNames languageNames) {
		this.languageNames = languageNames;
	}

	/**
	 * Creates a new visitor to process the configured checks.
	 *
	 * @param output
	 *            call-back to report violations to
	 * @return visitor to emit the report data to
	 */
	public IReportVisitor createVisitor(final IViolationsOutput output) {
		final BundleChecker bundleChecker = new BundleChecker(rules,
				languageNames, output);
		return new IReportVisitor() {

			public IReportGroupVisitor visitGroup(final String name)
					throws IOException {
				return this;
			}

			public void visitBundle(final IBundleCoverage bundle,
					final ISourceFileLocator locator) throws IOException {
				bundleChecker.checkBundle(bundle);
			}

			public void visitInfo(final List<SessionInfo> sessionInfos,
					// BEGIN android-change
					final Collection<IExecutionData> executionData)
					// END android-change
					throws IOException {
			}

			public void visitEnd() throws IOException {
			}
		};
	}

}