1/*******************************************************************************
2 * Copyright (c) 2009, 2018 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 *    Marc R. Hoffmann - initial API and implementation
10 *
11 *******************************************************************************/
12package org.jacoco.report;
13
14import java.io.IOException;
15import java.io.Reader;
16import java.util.ArrayList;
17import java.util.List;
18
19/**
20 * Locator that searches source files in multiple {@link ISourceFileLocator}
21 * instances. For each lookup request the first locator that returns a
22 * {@link Reader} for source content is selected.
23 */
24public class MultiSourceFileLocator implements ISourceFileLocator {
25
26	private final int tabWidth;
27
28	private final List<ISourceFileLocator> delegates;
29
30	/**
31	 * Creates a new empty locator.
32	 *
33	 * @param tabWidth
34	 *            tab width in source files as number of blanks used for all
35	 *            source files
36	 */
37	public MultiSourceFileLocator(final int tabWidth) {
38		this.tabWidth = tabWidth;
39		this.delegates = new ArrayList<ISourceFileLocator>();
40	}
41
42	/**
43	 * Adds the given locator. Locators are queried in the sequence they have
44	 * been added.
45	 *
46	 * @param locator
47	 *            Additional locator to query
48	 */
49	public void add(final ISourceFileLocator locator) {
50		delegates.add(locator);
51	}
52
53	public Reader getSourceFile(final String packageName, final String fileName)
54			throws IOException {
55		for (final ISourceFileLocator d : delegates) {
56			final Reader reader = d.getSourceFile(packageName, fileName);
57			if (reader != null) {
58				return reader;
59			}
60		}
61		return null;
62	}
63
64	public int getTabWidth() {
65		return tabWidth;
66	}
67
68}
69