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 *    Marc R. Hoffmann - initial API and implementation
10 *    Dominik Stadler - source folder support
11 *
12 *******************************************************************************/
13package org.jacoco.ant;
14
15import java.util.Iterator;
16
17import org.apache.tools.ant.types.Resource;
18import org.apache.tools.ant.types.resources.FileResource;
19import org.jacoco.report.DirectorySourceFileLocator;
20import org.jacoco.report.MultiSourceFileLocator;
21
22/**
23 * Source file locator based on Ant resources. The locator supports files as
24 * well as directories. The lookup is first performed on files (matching the
25 * local file name) and afterwards on directories, by the order the directory
26 * resources have been added. The directories are considered as source folders
27 * that are searched for source files with the fully qualified name (package and
28 * local name).
29 */
30class AntResourcesLocator extends MultiSourceFileLocator {
31
32	private final String encoding;
33	private final AntFilesLocator filesLocator;
34
35	private boolean empty;
36
37	AntResourcesLocator(final String encoding, final int tabWidth) {
38		super(tabWidth);
39		this.encoding = encoding;
40		this.filesLocator = new AntFilesLocator(encoding, tabWidth);
41		this.empty = true;
42		super.add(filesLocator);
43	}
44
45	/**
46	 * Adds the given file or directory resource to the locator.
47	 *
48	 * @param resource
49	 *            resource to add
50	 */
51	void add(final Resource resource) {
52		empty = false;
53		if (resource.isDirectory()) {
54			final FileResource dir = (FileResource) resource;
55			super.add(new DirectorySourceFileLocator(dir.getFile(), encoding,
56					getTabWidth()));
57		} else {
58			filesLocator.add(resource);
59		}
60	}
61
62	void addAll(final Iterator<?> iterator) {
63		while (iterator.hasNext()) {
64			add((Resource) iterator.next());
65		}
66	}
67
68	/**
69	 * Checks, whether resources have been added.
70	 *
71	 * @return <code>true</code>, if no resources have been added
72	 */
73	boolean isEmpty() {
74		return empty;
75	}
76
77}
78