• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  *    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 files.
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 	 */
56 	@SuppressWarnings("unchecked")
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 	 * Get the includes pattern
64 	 *
65 	 * @return the pattern
66 	 */
getIncludes()67 	public String getIncludes() {
68 		return this.buildPattern(this.includes, DEFAULT_INCLUDES);
69 	}
70 
71 	/**
72 	 * Get the excludes pattern
73 	 *
74 	 * @return the pattern
75 	 */
getExcludes()76 	public String getExcludes() {
77 		return this.buildPattern(this.excludes, DEFAULT_EXCLUDES);
78 	}
79 
buildPattern(final List<String> patterns, final String defaultPattern)80 	private String buildPattern(final List<String> patterns,
81 			final String defaultPattern) {
82 		String pattern = defaultPattern;
83 		if (CollectionUtils.isNotEmpty(patterns)) {
84 			pattern = StringUtils.join(patterns.iterator(), ",");
85 		}
86 		return pattern;
87 	}
88 }
89