• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2019 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  *    Evgeny Mandrikov - initial API and implementation
10  *    Kyle Lieber - implementation of CheckMojo
11  *
12  *******************************************************************************/
13 package org.jacoco.maven;
14 
15 import java.io.File;
16 import java.io.IOException;
17 import java.util.List;
18 
19 import org.apache.commons.collections.CollectionUtils;
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, final List<String> excludes) {
43 		this.includes = includes;
44 		this.excludes = excludes;
45 	}
46 
47 	/**
48 	 * Returns a list of file names.
49 	 *
50 	 * @param directory
51 	 *            the directory to scan
52 	 * @return a list of files
53 	 * @throws IOException
54 	 *             if file system access fails
55 	 */
getFileNames(final File directory)56 	public List<String> getFileNames(final File directory) throws IOException {
57 		return FileUtils.getFileNames(directory, getIncludes(), getExcludes(),
58 				false);
59 	}
60 
61 	/**
62 	 * Returns a list of files.
63 	 *
64 	 * @param directory
65 	 *            the directory to scan
66 	 * @return a list of files
67 	 * @throws IOException
68 	 *             if file system access fails
69 	 */
getFiles(final File directory)70 	public List<File> getFiles(final File directory) throws IOException {
71 		return FileUtils.getFiles(directory, getIncludes(), getExcludes());
72 	}
73 
74 	/**
75 	 * Get the includes pattern
76 	 *
77 	 * @return the pattern
78 	 */
getIncludes()79 	public String getIncludes() {
80 		return this.buildPattern(this.includes, DEFAULT_INCLUDES);
81 	}
82 
83 	/**
84 	 * Get the excludes pattern
85 	 *
86 	 * @return the pattern
87 	 */
getExcludes()88 	public String getExcludes() {
89 		return this.buildPattern(this.excludes, DEFAULT_EXCLUDES);
90 	}
91 
buildPattern(final List<String> patterns, final String defaultPattern)92 	private String buildPattern(final List<String> patterns,
93 			final String defaultPattern) {
94 		String pattern = defaultPattern;
95 		if (CollectionUtils.isNotEmpty(patterns)) {
96 			pattern = StringUtils.join(patterns.iterator(), ",");
97 		}
98 		return pattern;
99 	}
100 }
101