mirror of https://github.com/micromata/borgbackup-butler.git

Kai Reinhard
16.04.2018 fa2417db7c301f8cb6b721b45dd827846f0918bc
Black list and white list of keywords in search for files in file system is now supported.
1 files modified
47 ■■■■ changed files
borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java 47 ●●●● patch | view | raw | blame | history
borgbutler-core/src/main/java/de/micromata/borgbutler/data/FileSystemFilter.java
@@ -5,6 +5,9 @@
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import java.util.ArrayList;
import java.util.List;
public class FileSystemFilter {
    @Getter
    private String searchString;
@@ -12,29 +15,55 @@
    @Setter
    private int maxResultSize;
    private String[] searchKeyWords;
    private String[] blackListSearchKeyWords;
    public boolean matches(BorgFilesystemItem item) {
        if (searchKeyWords == null) {
            return true;
        if (searchKeyWords != null) {
            for (String searchKeyWord : searchKeyWords) {
                if (!StringUtils.containsIgnoreCase(item.getPath(), searchKeyWord))
                    return false;
            }
        }
        for (String searchKeyWord : searchKeyWords) {
            if (!StringUtils.containsIgnoreCase(item.getPath(), searchKeyWord))
                return false;
        if (blackListSearchKeyWords != null) {
            for (String blackListSearchKeyWord : blackListSearchKeyWords) {
                if (StringUtils.containsIgnoreCase(item.getPath(), blackListSearchKeyWord))
                    return false;
            }
        }
        return true;
    }
    /**
     *
     * @param searchString The search string. If this string contains several key words separated by white chars,
     *                     all key words must be found.
     * @return this for chaining.
     */
    public FileSystemFilter setSearchString(String searchString) {
        this.searchString = searchString;
        searchKeyWords = StringUtils.split(searchString);
        if (searchKeyWords != null && searchKeyWords.length == 0) {
            searchKeyWords = null;
        String[] keyWords = StringUtils.split(searchString);
        this.searchKeyWords = null;
        this.blackListSearchKeyWords = null;
        if (keyWords != null) {
            List<String> whiteList = new ArrayList<>();
            List<String> blackList = new ArrayList<>();
            for (String keyWord : keyWords) {
                if (StringUtils.isEmpty(keyWord)) {
                    continue;
                }
                if (keyWord.startsWith("!") && keyWord.length() > 1) {
                    blackList.add(keyWord.substring(1));
                } else {
                    whiteList.add(keyWord);
                }
            }
            if (whiteList.size() > 0) {
                this.searchKeyWords = new String[whiteList.size()];
                this.searchKeyWords = whiteList.toArray(this.searchKeyWords);
            }
            if (blackList.size() > 0) {
                this.blackListSearchKeyWords = new String[blackList.size()];
                this.blackListSearchKeyWords = blackList.toArray(this.blackListSearchKeyWords);
            }
        }
        return this;
    }