| | |
| | | import lombok.Setter; |
| | | import org.apache.commons.lang3.StringUtils; |
| | | |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | public class FileSystemFilter { |
| | | @Getter |
| | | private String searchString; |
| | |
| | | @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; |
| | | } |