• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Evgeny Mandrikov - initial API and implementation
11  *    Kyle Lieber - implementation of CheckMojo
12  *
13  *******************************************************************************/
14 package org.jacoco.maven;
15 
16 import java.io.File;
17 import java.io.IOException;
18 import java.util.List;
19 
20 import org.codehaus.plexus.util.FileUtils;
21 import org.codehaus.plexus.util.StringUtils;
22 
23 /**
24  * A file filter using includes/excludes patterns.
25  */
26 public class FileFilter {
27 
28 	private static final String DEFAULT_INCLUDES = "**";
29 	private static final String DEFAULT_EXCLUDES = "";
30 
31 	private final List<String> includes;
32 	private final List<String> excludes;
33 
34 	/**
35 	 * Construct a new FileFilter
36 	 *
37 	 * @param includes
38 	 *            list of includes patterns
39 	 * @param excludes
40 	 *            list of excludes patterns
41 	 */
FileFilter(final List<String> includes, final List<String> excludes)42 	public FileFilter(final List<String> includes,
43 			final List<String> excludes) {
44 		this.includes = includes;
45 		this.excludes = excludes;
46 	}
47 
48 	/**
49 	 * Returns a list of file names.
50 	 *
51 	 * @param directory
52 	 *            the directory to scan
53 	 * @return a list of files
54 	 * @throws IOException
55 	 *             if file system access fails
56 	 */
getFileNames(final File directory)57 	public List<String> getFileNames(final File directory) throws IOException {
58 		return FileUtils.getFileNames(directory, getIncludes(), getExcludes(),
59 				false);
60 	}
61 
62 	/**
63 	 * Returns a list of files.
64 	 *
65 	 * @param directory
66 	 *            the directory to scan
67 	 * @return a list of files
68 	 * @throws IOException
69 	 *             if file system access fails
70 	 */
getFiles(final File directory)71 	public List<File> getFiles(final File directory) throws IOException {
72 		return FileUtils.getFiles(directory, getIncludes(), getExcludes());
73 	}
74 
75 	/**
76 	 * Get the includes pattern
77 	 *
78 	 * @return the pattern
79 	 */
getIncludes()80 	public String getIncludes() {
81 		return this.buildPattern(this.includes, DEFAULT_INCLUDES);
82 	}
83 
84 	/**
85 	 * Get the excludes pattern
86 	 *
87 	 * @return the pattern
88 	 */
getExcludes()89 	public String getExcludes() {
90 		return this.buildPattern(this.excludes, DEFAULT_EXCLUDES);
91 	}
92 
buildPattern(final List<String> patterns, final String defaultPattern)93 	private String buildPattern(final List<String> patterns,
94 			final String defaultPattern) {
95 		String pattern = defaultPattern;
96 		if (patterns != null && !patterns.isEmpty()) {
97 			pattern = StringUtils.join(patterns.iterator(), ",");
98 		}
99 		return pattern;
100 	}
101 }
102