aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.report/src/org/jacoco/report/MultiSourceFileLocator.java
blob: a059747ad82b23a6161a82cc136e09c7970d3e07 (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
/*******************************************************************************
 * Copyright (c) 2009, 2017 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;

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

/**
 * Locator that searches source files in multiple {@link ISourceFileLocator}
 * instances. For each lookup request the first locator that returns a
 * {@link Reader} for source content is selected.
 */
public class MultiSourceFileLocator implements ISourceFileLocator {

	private final int tabWidth;

	private final List<ISourceFileLocator> delegates;

	/**
	 * Creates a new empty locator.
	 * 
	 * @param tabWidth
	 *            tab width in source files as number of blanks used for all
	 *            source files
	 */
	public MultiSourceFileLocator(final int tabWidth) {
		this.tabWidth = tabWidth;
		this.delegates = new ArrayList<ISourceFileLocator>();
	}

	/**
	 * Adds the given locator. Locators are queried in the sequence they have
	 * been added.
	 * 
	 * @param locator
	 *            Additional locator to query
	 */
	public void add(final ISourceFileLocator locator) {
		delegates.add(locator);
	}

	public Reader getSourceFile(final String packageName, final String fileName)
			throws IOException {
		for (final ISourceFileLocator d : delegates) {
			final Reader reader = d.getSourceFile(packageName, fileName);
			if (reader != null) {
				return reader;
			}
		}
		return null;
	}

	public int getTabWidth() {
		return tabWidth;
	}

}