1 3 package jodd.io.findfile; 4 5 import jodd.util.StringUtil; 6 7 import java.io.File ; 8 import java.util.LinkedList ; 9 import java.net.URI ; 10 import java.net.URL ; 11 import java.net.URISyntaxException ; 12 13 23 public class FindFile { 24 25 27 protected boolean recursive = false; 28 31 public FindFile recursive() { 32 this.recursive = true; 33 return this; 34 } 35 36 protected boolean includeDirs = true; 37 protected boolean includeFiles = true; 38 39 42 public FindFile includeDirs() { 43 includeDirs = true; 44 return this; 45 } 46 50 public FindFile excludeDirs() { 51 includeDirs = false; 52 return this; 53 } 54 55 58 public FindFile includeFiles() { 59 includeFiles = true; 60 return this; 61 } 62 63 66 public FindFile excludeFiles() { 67 includeFiles = false; 68 return this; 69 } 70 71 72 75 public FindFile includeAll() { 76 includeDirs = true; 77 includeFiles = true; 78 return this; 79 } 80 81 83 84 public FindFile() { 85 } 86 87 public FindFile(String searchPath) { 88 searchPath(searchPath); 89 } 90 91 public FindFile(String [] searchPath) { 92 searchPath(searchPath); 93 } 94 95 public FindFile(File searchPath) { 96 searchPath(searchPath); 97 } 98 99 public FindFile(URI searchPath) { 100 searchPath(searchPath); 101 } 102 103 public FindFile(URI [] searchPath) { 104 searchPath(searchPath); 105 } 106 107 public FindFile(URL searchPath) { 108 searchPath(searchPath); 109 } 110 111 public FindFile(URL [] searchPath) { 112 searchPath(searchPath); 113 } 114 115 116 protected LinkedList fileList = null; 117 118 123 public FindFile searchPath(String searchPath) { 124 if (searchPath.indexOf(File.pathSeparatorChar) != -1) { 125 String [] paths = StringUtil.split(searchPath, File.pathSeparator); 126 for (int i = 0; i < paths.length; i++) { 127 searchPath(paths[i]); 128 } 129 return this; 130 } 131 searchPath(new File (searchPath)); 132 return this; 133 } 134 135 139 public FindFile searchPath(String [] searchPaths) { 140 for (int i = 0; i < searchPaths.length; i++) { 141 searchPath(searchPaths[i]); 142 } 143 return this; 144 } 145 146 149 public FindFile searchPath(File searchPath) { 150 if (searchPath.exists() == false) { 151 return this; 152 } 153 if (fileList == null) { 154 fileList = new LinkedList (); 155 } 156 if (searchPath.isDirectory() == false) { 157 fileList.add(searchPath); 158 return this; 159 } 160 listFiles(searchPath); 161 return this; 162 } 163 164 167 public FindFile searchPath(URI searchPath) { 168 searchPath(new File (searchPath)); 169 return this; 170 } 171 172 175 public FindFile searchPath(URI [] searchPath) { 176 for (int i = 0; i < searchPath.length; i++) { 177 searchPath(searchPath[i]); 178 } 179 return this; 180 } 181 182 183 186 public FindFile searchPath(URL searchPath) { 187 try { 188 searchPath(new URI (searchPath.toString())); 189 } catch (URISyntaxException usex) { 190 throw new IllegalArgumentException ("Invalid URL: " + usex.toString()); 191 } 192 return this; 193 } 194 195 196 199 public FindFile searchPath(URL [] searchPath) { 200 for (int i = 0; i < searchPath.length; i++) { 201 searchPath(searchPath[i]); 202 } 203 return this; 204 } 205 206 207 208 209 211 protected boolean subfilesAfterFolder = true; 212 213 218 public void subfilesAfterFolder(boolean value) { 219 subfilesAfterFolder = value; 220 } 221 222 223 225 229 public File nextFile() { 230 if (fileList == null) { 231 return null; 232 } 233 234 while (true) { 235 if (fileList.isEmpty()) { 236 fileList = null; 237 return null; 238 } 239 File currentFile = (File ) fileList.removeFirst(); 240 if (currentFile.isDirectory()) { 241 if (recursive == true) { 242 listFiles(currentFile); 243 } 244 if (includeDirs == true) { 245 if (onFileEntry(currentFile) == true) { 246 return currentFile; 247 } 248 } 249 continue; 250 } 251 return currentFile; 252 } 253 } 254 255 256 257 259 264 protected void listFiles(File directory) { 265 File [] list = directory.listFiles(); 266 LinkedList subFolders; 267 LinkedList subFiles; 268 if (subfilesAfterFolder == false) { 269 subFolders = fileList; 270 subFiles = fileList; 271 } else { 272 subFolders = new LinkedList (); 273 subFiles = new LinkedList (); 274 } 275 for (int i = 0; i < list.length; i++) { 276 File currentFile = list[i]; 277 if (currentFile.isFile() == true) { 278 if ((includeFiles == true) && (onFileEntry(currentFile) == true)) { 279 subFiles.addLast(currentFile); 280 } 281 } else if (currentFile.isDirectory() == true) { 282 subFolders.addLast(currentFile); 283 } 284 } 285 if (subfilesAfterFolder == true) { 286 if (subFiles.isEmpty() == false) { 287 fileList.addAll(0, subFiles); 288 } 289 if (subFolders.isEmpty() == false) { 290 fileList.addAll(0, subFolders); 291 } 292 } 293 } 294 295 297 301 protected boolean onFileEntry(File currentFile) { 302 return true; 303 } 304 } 305 | Popular Tags |