View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.imaging.test.util;
19  
20  import java.io.File;
21  
22  import org.apache.commons.imaging.util.Debug;
23  
24  public class FileSystemTraversal {
25  
26      public static final int MODE_FILES = 1;
27      public static final int MODE_FOLDERS = 2;
28      public static final int MODE_FILES_AND_FOLDERS = 3;
29      public static final int MODE_ALL = 4;
30  
31      public interface Visitor {
32          public boolean visit(File file, double progressEstimate);
33      }
34  
35      public boolean traverseFiles(final File file, final Visitor visitor) {
36  
37          return traverse(file, MODE_FILES, visitor);
38      }
39  
40      public boolean traverseFolders(final File file, final Visitor visitor) {
41  
42          return traverse(file, MODE_FOLDERS, visitor);
43      }
44  
45      public boolean traverseAll(final File file, final Visitor visitor) {
46  
47          return traverse(file, MODE_FILES_AND_FOLDERS, visitor);
48      }
49  
50      public boolean traverse(final File file, final int mode, final Visitor visitor) {
51          return traverse(file, mode, visitor, 0, 1);
52      }
53  
54      private boolean traverse(final File file, final int mode, final Visitor visitor,
55              final double estimate, final double estimateIncrement) {
56  
57          if (file.isFile()) {
58              if ((mode == MODE_FILES) || (mode == MODE_FILES_AND_FOLDERS)
59                      || (mode == MODE_ALL)) {
60                  if (!visitor.visit(file, estimate)) {
61                      return false;
62                  }
63              }
64          } else if (file.isDirectory()) {
65              final File files[] = file.listFiles();
66              if (files != null) {
67                  for (int i = 0; i < files.length; i++) {
68                      final File child = files[i];
69                      if (ON_MAC_OS_X && child.isDirectory()) {
70                          final String name = child.getName();
71                          if (name.equalsIgnoreCase("automount")
72                                  || name.equalsIgnoreCase("private")
73                                  || name.equalsIgnoreCase("Network")
74                                  || name.equalsIgnoreCase("Volumes")) {
75                              continue;
76                              // return true;
77                          }
78                      }
79  
80                      if (!traverse(child, mode, visitor, estimate
81                              + estimateIncrement * i / files.length,
82                              estimateIncrement / files.length)) {
83                          return false;
84                      }
85                  }
86              }
87  
88              if ((mode == MODE_FOLDERS) || (mode == MODE_FILES_AND_FOLDERS)
89                      || (mode == MODE_ALL)) {
90                  if (!visitor.visit(file, estimate)) {
91                      return false;
92                  }
93              }
94          } else {
95              if (mode == MODE_ALL) {
96                  if (!visitor.visit(file, estimate)) {
97                      return false;
98                  }
99              }
100         }
101 
102         return true;
103     }
104 
105     public boolean traverseFiles(final Visitor visitor) {
106 
107         return traverse(MODE_FILES, visitor);
108     }
109 
110     public boolean traverseFolders(final Visitor visitor) {
111 
112         return traverse(MODE_FOLDERS, visitor);
113     }
114 
115     public boolean traverseAll(final Visitor visitor) {
116 
117         return traverse(MODE_FILES_AND_FOLDERS, visitor);
118     }
119 
120     public boolean traverse(final int mode, final Visitor visitor) {
121         return traverse(mode, visitor, 0, 1);
122     }
123 
124     private static boolean ON_MAC_OS_X;
125 
126     static {
127         try {
128             ON_MAC_OS_X = (System.getProperty("mrj.version") != null);
129         } catch (final Exception e) {
130             Debug.debug(e);
131 
132             ON_MAC_OS_X = false;
133         }
134     }
135 
136     private boolean traverse(final int mode, final Visitor visitor, final double estimate,
137             final double estimateIncrement) {
138         File roots[] = File.listRoots();
139 
140         if (ON_MAC_OS_X) {
141             final File Volumes = new File("/Volumes/");
142             roots = Volumes.listFiles();
143         } else {
144             roots = File.listRoots();
145         }
146 
147         if (roots == null) {
148             return false;
149         }
150 
151         for (int i = 0; i < roots.length; i++) {
152             final File root = roots[i];
153 
154             if (root == null || !root.exists()) {
155                 continue;
156             }
157 
158             if (!traverse(roots[i], mode, visitor, estimate + estimateIncrement
159                     * i / roots.length, estimateIncrement / roots.length)) {
160                 return false;
161             }
162         }
163 
164         return true;
165     }
166 
167 }