001/*******************************************************************************
002 * Copyright (c) 2009, 2017 Mountainminds GmbH & Co. KG and Contributors
003 * All rights reserved. This program and the accompanying materials
004 * are made available under the terms of the Eclipse Public License v1.0
005 * which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v10.html
007 *
008 * Contributors:
009 *    Evgeny Mandrikov - initial API and implementation
010 *    Kyle Lieber - implementation of CheckMojo
011 *
012 *******************************************************************************/
013package de.dentrassi.maven.jacoco;
014
015import java.io.File;
016import java.io.IOException;
017import java.util.List;
018
019import org.apache.commons.collections.CollectionUtils;
020import org.codehaus.plexus.util.FileUtils;
021import org.codehaus.plexus.util.StringUtils;
022
023/**
024 * A file filter using includes/excludes patterns.
025 */
026public class FileFilter {
027
028    private static final String DEFAULT_INCLUDES = "**";
029    private static final String DEFAULT_EXCLUDES = "";
030
031    private final List<String> includes;
032    private final List<String> excludes;
033
034    /**
035     * Construct a new FileFilter
036     *
037     * @param includes
038     *            list of includes patterns
039     * @param excludes
040     *            list of excludes patterns
041     */
042    public FileFilter(final List<String> includes, final List<String> excludes) {
043        this.includes = includes;
044        this.excludes = excludes;
045    }
046
047    /**
048     * Returns a list of file names.
049     *
050     * @param directory
051     *            the directory to scan
052     * @return a list of files
053     * @throws IOException
054     *             if file system access fails
055     */
056    public List<String> getFileNames(final File directory) throws IOException {
057        return FileUtils.getFileNames(directory, getIncludes(), getExcludes(), false);
058    }
059
060    /**
061     * Returns a list of files.
062     *
063     * @param directory
064     *            the directory to scan
065     * @return a list of files
066     * @throws IOException
067     *             if file system access fails
068     */
069    public List<File> getFiles(final File directory) throws IOException {
070        return FileUtils.getFiles(directory, getIncludes(), getExcludes());
071    }
072
073    /**
074     * Get the includes pattern
075     *
076     * @return the pattern
077     */
078    public String getIncludes() {
079        return buildPattern(this.includes, DEFAULT_INCLUDES);
080    }
081
082    /**
083     * Get the excludes pattern
084     *
085     * @return the pattern
086     */
087    public String getExcludes() {
088        return buildPattern(this.excludes, DEFAULT_EXCLUDES);
089    }
090
091    private String buildPattern(final List<String> patterns, final String defaultPattern) {
092        String pattern = defaultPattern;
093        if (CollectionUtils.isNotEmpty(patterns)) {
094            pattern = StringUtils.join(patterns.iterator(), ",");
095        }
096        return pattern;
097    }
098}