1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging.test;
18
19 import java.io.File;
20
21 import org.apache.commons.imaging.internal.Debug;
22
23 public class FileSystemTraversal {
24
25 public interface Visitor {
26 boolean visit(File file, double progressEstimate);
27 }
28
29 public static final int MODE_FILES = 1;
30 public static final int MODE_FOLDERS = 2;
31 public static final int MODE_FILES_AND_FOLDERS = 3;
32
33 public static final int MODE_ALL = 4;
34
35 private static boolean ON_MAC_OS_X;
36
37 static {
38 try {
39 ON_MAC_OS_X = System.getProperty("mrj.version") != null;
40 } catch (final Exception e) {
41 Debug.debug(e);
42
43 ON_MAC_OS_X = false;
44 }
45 }
46
47 public boolean traverse(final File file, final int mode, final Visitor visitor) {
48 return traverse(file, mode, visitor, 0, 1);
49 }
50
51 private boolean traverse(final File file, final int mode, final Visitor visitor, final double estimate, final double estimateIncrement) {
52
53 if (file.isFile()) {
54 if ((mode == MODE_FILES || mode == MODE_FILES_AND_FOLDERS || mode == MODE_ALL) && !visitor.visit(file, estimate)) {
55 return false;
56 }
57 } else if (file.isDirectory()) {
58 final File[] files = file.listFiles();
59 if (files != null) {
60 for (int i = 0; i < files.length; i++) {
61 final File child = files[i];
62 if (ON_MAC_OS_X && child.isDirectory()) {
63 final String name = child.getName();
64 if (name.equalsIgnoreCase("automount") || name.equalsIgnoreCase("private") || name.equalsIgnoreCase("Network")
65 || name.equalsIgnoreCase("Volumes")) {
66 continue;
67
68 }
69 }
70
71 if (!traverse(child, mode, visitor, estimate + estimateIncrement * i / files.length, estimateIncrement / files.length)) {
72 return false;
73 }
74 }
75 }
76
77 if ((mode == MODE_FOLDERS || mode == MODE_FILES_AND_FOLDERS || mode == MODE_ALL) && !visitor.visit(file, estimate)) {
78 return false;
79 }
80 } else if (mode == MODE_ALL && !visitor.visit(file, estimate)) {
81 return false;
82 }
83
84 return true;
85 }
86
87 public boolean traverse(final int mode, final Visitor visitor) {
88 return traverse(mode, visitor, 0, 1);
89 }
90
91 private boolean traverse(final int mode, final Visitor visitor, final double estimate, final double estimateIncrement) {
92 File[] roots = File.listRoots();
93
94 if (ON_MAC_OS_X) {
95 final File Volumes = new File("/Volumes/");
96 roots = Volumes.listFiles();
97 } else {
98 roots = File.listRoots();
99 }
100
101 if (roots == null) {
102 return false;
103 }
104
105 for (int i = 0; i < roots.length; i++) {
106 final File root = roots[i];
107
108 if (root == null || !root.exists()) {
109 continue;
110 }
111
112 if (!traverse(roots[i], mode, visitor, estimate + estimateIncrement * i / roots.length, estimateIncrement / roots.length)) {
113 return false;
114 }
115 }
116
117 return true;
118 }
119
120 public boolean traverseAll(final File file, final Visitor visitor) {
121
122 return traverse(file, MODE_FILES_AND_FOLDERS, visitor);
123 }
124
125 public boolean traverseAll(final Visitor visitor) {
126
127 return traverse(MODE_FILES_AND_FOLDERS, visitor);
128 }
129
130 public boolean traverseFiles(final File file, final Visitor visitor) {
131
132 return traverse(file, MODE_FILES, visitor);
133 }
134
135 public boolean traverseFiles(final Visitor visitor) {
136
137 return traverse(MODE_FILES, visitor);
138 }
139
140 public boolean traverseFolders(final File file, final Visitor visitor) {
141
142 return traverse(file, MODE_FOLDERS, visitor);
143 }
144
145 public boolean traverseFolders(final Visitor visitor) {
146
147 return traverse(MODE_FOLDERS, visitor);
148 }
149
150 }