BrightSide Workbench Full Report + Source Code
FileResults.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2015 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.file.zul.control;
20 
21 import java.io.File;
22 import java.util.Collection;
23 import java.util.Set;
24 import java.util.TreeSet;
25 import org.turro.string.Strings;
26 
31 public class FileResults {
32 
33  private String searchValue;
34  private boolean path, regexp, partial;
35 
36  public FileResults() {
37  searchValue = "*";
38  path = false;
39  regexp = false;
40  partial = true;
41  }
42 
43  public String getSearchValue() {
44  return searchValue;
45  }
46 
47  public void setSearchValue(String searchValue) {
48  this.searchValue = searchValue;
49  }
50 
51  public boolean isPath() {
52  return path;
53  }
54 
55  public void setPath(boolean path) {
56  this.path = path;
57  }
58 
59  public boolean isRegexp() {
60  return regexp;
61  }
62 
63  public void setRegexp(boolean regexp) {
64  this.regexp = regexp;
65  }
66 
67  public boolean isPartial() {
68  return partial;
69  }
70 
71  public void setPartial(boolean partial) {
72  this.partial = partial;
73  }
74 
75  public boolean matches(File file) {
76  if(Strings.isBlank(searchValue)) return false;
77  if(regexp) {
78  if(path) {
79  if(partial) {
80  return file.getAbsolutePath().matches(".*" + searchValue + ".*");
81  } else {
82  return file.getAbsolutePath().matches(searchValue);
83  }
84  } else {
85  if(partial) {
86  return file.getName().matches(".*" + searchValue + ".*");
87  } else {
88  return file.getName().matches(searchValue);
89  }
90  }
91  } else {
92  if(path) {
93  if(partial) {
94  return file.getAbsolutePath().matches(Strings.convertToRegEx("*" + searchValue + "*"));
95  } else {
96  return file.getAbsolutePath().matches(Strings.convertToRegEx(searchValue));
97  }
98  } else {
99  if(partial) {
100  return file.getName().matches(Strings.convertToRegEx("*" + searchValue + "*"));
101  } else {
102  return file.getName().matches(Strings.convertToRegEx(searchValue));
103  }
104  }
105  }
106  }
107 
108  public Collection<String> getFolders(File folder) {
109  Set<String> subdirs = new TreeSet<>();
110  if(folder.exists()) {
111  for(File file : folder.listFiles()) {
112  if(file.isDirectory() && !file.isHidden()) {
113  subdirs.add(file.getName());
114  }
115  }
116  }
117  return subdirs;
118  }
119 
120  public Collection<File> getFiles(File folder) {
121  Set<File> files = new TreeSet<>();
122  if(folder.exists()) {
123  for(File file : folder.listFiles()) {
124  if(file.isFile() && !file.isHidden()) {
125  if(matches(file)) files.add(file);
126  }
127  }
128  }
129  return files;
130  }
131 
132  public Collection<File> getFileList(File folder) {
133  Set<File> files = new TreeSet<>();
134  if(folder.exists()) {
135  for(File file : folder.listFiles()) {
136  if(!file.isHidden()) {
137  if(file.isDirectory() || matches(file)) files.add(file);
138  }
139  }
140  }
141  return files;
142  }
143 
144 }
Collection< String > getFolders(File folder)
Collection< File > getFiles(File folder)
void setSearchValue(String searchValue)
Collection< File > getFileList(File folder)