aboutsummaryrefslogtreecommitdiff
path: root/org.jacoco.ant/src/org/jacoco/ant/AntResourcesLocator.java
blob: 41c8352d07279acf7978081b4ab1c2af0edea7a4 (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
/*******************************************************************************
 * Copyright (c) 2009, 2019 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
 *    Dominik Stadler - source folder support
 *    
 *******************************************************************************/
package org.jacoco.ant;

import java.util.Iterator;

import org.apache.tools.ant.types.Resource;
import org.apache.tools.ant.types.resources.FileResource;
import org.jacoco.report.DirectorySourceFileLocator;
import org.jacoco.report.MultiSourceFileLocator;

/**
 * Source file locator based on Ant resources. The locator supports files as
 * well as directories. The lookup is first performed on files (matching the
 * local file name) and afterwards on directories, by the order the directory
 * resources have been added. The directories are considered as source folders
 * that are searched for source files with the fully qualified name (package and
 * local name).
 */
class AntResourcesLocator extends MultiSourceFileLocator {

	private final String encoding;
	private final AntFilesLocator filesLocator;

	private boolean empty;

	AntResourcesLocator(final String encoding, final int tabWidth) {
		super(tabWidth);
		this.encoding = encoding;
		this.filesLocator = new AntFilesLocator(encoding, tabWidth);
		this.empty = true;
		super.add(filesLocator);
	}

	/**
	 * Adds the given file or directory resource to the locator.
	 * 
	 * @param resource
	 *            resource to add
	 */
	void add(final Resource resource) {
		empty = false;
		if (resource.isDirectory()) {
			final FileResource dir = (FileResource) resource;
			super.add(new DirectorySourceFileLocator(dir.getFile(), encoding,
					getTabWidth()));
		} else {
			filesLocator.add(resource);
		}
	}

	void addAll(final Iterator<?> iterator) {
		while (iterator.hasNext()) {
			add((Resource) iterator.next());
		}
	}

	/**
	 * Checks, whether resources have been added.
	 * 
	 * @return <code>true</code>, if no resources have been added
	 */
	boolean isEmpty() {
		return empty;
	}

}