1
2
3
4
5
6
7
8
9
10 package com.eginc.maven.maximo.plugin;
11
12 import java.util.*;
13 import java.io.*;
14
15 /**
16 * @author egiguere
17 * @version = $Id
18 */
19 public class FileListing {
20 private String dirRoot = "";
21
22 /**
23 * Recursively walk a directory tree and return a List of all Files found;
24 * the List is sorted using File.compareTo.
25 *
26 * @param aStartingDir is a valid directory, which can be read.
27 */
28 public List getFileListing( File aStartingDir, boolean includeDirs, boolean stripRoot ) throws FileNotFoundException{
29 validateDirectory(aStartingDir);
30 List result = new ArrayList();
31
32 if (dirRoot.length()==0)
33 dirRoot = aStartingDir.getAbsolutePath();
34
35 File[] filesAndDirs = aStartingDir.listFiles();
36 List filesDirs = Arrays.asList(filesAndDirs);
37 Iterator filesIter = filesDirs.iterator();
38 File file = null;
39 while ( filesIter.hasNext() ) {
40 file = (File)filesIter.next();
41 if ( (includeDirs && file.isDirectory()) || !file.isDirectory()) {
42
43
44
45
46
47
48 result.add(file);
49
50 }
51
52 if (!file.isFile()) {
53
54
55 List deeperList = getFileListing(file,includeDirs,stripRoot);
56 result.addAll(deeperList);
57 }
58
59 }
60 Collections.sort(result);
61 return result;
62 }
63
64 /**
65 * Directory is valid if it exists, does not represent a file, and can be
66 * read.
67 */
68 private void validateDirectory (File aDirectory) throws FileNotFoundException {
69 if (aDirectory == null) {
70 throw new IllegalArgumentException("Directory should not be null.");
71 }
72 if (!aDirectory.exists()) {
73 throw new FileNotFoundException("Directory does not exist: " + aDirectory);
74 }
75 if (!aDirectory.isDirectory()) {
76 throw new IllegalArgumentException("Is not a directory: " + aDirectory);
77 }
78 if (!aDirectory.canRead()) {
79 throw new IllegalArgumentException("Directory cannot be read: " + aDirectory);
80 }
81 }
82 }