1 package jodd.file; 2 3 import java.io.File; 4 import java.io.FileFilter; 5 import java.util.ArrayList; 6 import java.util.List; 7 8 import jodd.util.StringFlags; 9 10 39 public class FindFile { 40 41 43 private boolean recursive = false; 44 49 public void setRecursive(boolean v) { 50 recursive = v; 51 } 52 57 public boolean getRecursive() { 58 return recursive; 59 } 60 61 private boolean incDirs; 62 67 public void setIncludeDirs(boolean v) { 68 incDirs = v; 69 } 70 73 public boolean getIncludeDirs() { 74 return incDirs; 75 } 76 77 private boolean exFiles; 78 83 public void setExcludeFiles(boolean v) { 84 exFiles = v; 85 } 86 89 public boolean getExcludeFiles() { 90 return exFiles; 91 } 92 93 private static StringFlags flags = new StringFlags(); 94 95 static { 96 flags.addFlag('r', "recursive", true); 97 flags.addFlag('d', "includeDirs", true); 98 flags.addFlag('x', "excludeFiles", true); 99 } 100 101 103 private List ffilters = null; 104 private int ffilters_size = 0; 105 106 public void setFileFilters(List ffs) { 107 ffilters = ffs; 108 if (ffilters != null) { 109 ffilters_size = ffilters.size(); 110 } else { 111 ffilters_size = 0; 112 } 113 } 114 115 public void setFileFilters(FileFilter[] ff) { 116 ArrayList al = new ArrayList(ff.length); 117 for (int i = 0; i < ff.length; i++) { 118 al.add(ff[i]); 119 } 120 setFileFilters(al); 121 } 122 123 public void setFileFilters(FileFilter ff) { 124 ArrayList al = new ArrayList(); 125 al.add(ff); 126 setFileFilters(al); 127 } 128 129 public void setFileFilters() { 130 setFileFilters((List) null); 131 } 132 133 134 public List getFileFilters() { 135 return ffilters; 136 } 137 138 public FileFilter getFileFilter() { 139 if (ffilters != null) { 140 return (FileFilter) ffilters.get(0); 141 } else { 142 return null; 143 } 144 } 145 146 public void addFileFilter(FileFilter ff) { 147 if (ffilters == null) { 148 ffilters = new ArrayList(); 149 } 150 ffilters.add(ff); 151 } 152 153 155 private ArrayList fileList = null; 156 private int ndx = 0; 157 private int fileListSize = 0; 158 159 168 public File first(String dir, String opt, FileFilter ff) { 169 setFileFilters(ff); 170 return dofirst(dir, opt); 171 } 172 180 public File first(String dir, FileFilter ff) { 181 setFileFilters(ff); 182 return dofirst(dir, ""); 183 } 184 193 public File first(String dir, String opt, FileFilter[] ffa) { 194 setFileFilters(ffa); 195 return dofirst(dir, opt); 196 } 197 205 public File first(String dir, FileFilter[] ffa) { 206 setFileFilters(ffa); 207 return dofirst(dir, ""); 208 } 209 218 public File first(String dir, String opt, List ffs) { 219 setFileFilters(ffs); 220 return dofirst(dir, opt); 221 } 222 230 public File first(String dir, List ffs) { 231 setFileFilters(ffs); 232 return dofirst(dir, ""); 233 } 234 242 public File first(String dir, String opt) { 243 setFileFilters(); 244 return dofirst(dir, opt); 245 } 246 253 public File first(String dir) { 254 return dofirst(dir, ""); 255 } 256 257 258 266 private File dofirst(String dir, String opt) { 267 flags.parse(opt, this); 268 269 fileList = new ArrayList(); 271 File f = new File(dir); 272 if (f.exists() == false) { 273 return null; } 275 if (f.isDirectory() == false) { 276 return f; } 278 File[] childs = f.listFiles(); 279 for (int i = 0; i < childs.length; i++) { 280 fileList.add(childs[i]); 281 } 282 ndx = 0; 283 fileListSize = fileList.size(); 284 return next(); 285 } 286 287 292 public File next() { 293 if (ndx == fileListSize) { 294 return end(); 295 } 296 File f = null; 297 boolean found = false; 298 299 loop: while (ndx < fileListSize) { 300 f = (File) fileList.get(ndx); 301 ndx++; 302 303 if (f.isDirectory()) { fileList.subList(0, ndx).clear(); ndx = 0; 306 if (recursive == true) { File[] childs = f.listFiles(); 308 for (int i = 0; i < childs.length; i++) { 309 fileList.add(childs[i]); 310 } 311 } 312 fileListSize = fileList.size(); 313 if (incDirs == false) { continue; 315 } 316 } 317 if (f.isFile()) { if (exFiles == true) { 319 continue; 320 } 321 } 322 if (ffilters == null) { 323 found = true; 324 break; 325 } 326 for (int i = 0; i < ffilters_size; i++) { 327 FileFilter ff = (FileFilter) ffilters.get(i); 328 if (ff.accept(f) == true) { 329 found = true; 330 break loop; 331 } 332 } 333 } 334 if (found == false) { 335 return end(); 336 } 337 return f; 338 } 339 340 341 346 private File end() { 347 fileList = null; 348 ffilters = null; 349 return null; 350 } 351 } 352 353 | |