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  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) {
55                  if (!visitor.visit(file, estimate)) {
56                      return false;
57                  }
58              }
59          } else if (file.isDirectory()) {
60              final File[] files = file.listFiles();
61              if (files != null) {
62                  for (int i = 0; i < files.length; i++) {
63                      final File child = files[i];
64                      if (ON_MAC_OS_X && child.isDirectory()) {
65                          final String name = child.getName();
66                          if (name.equalsIgnoreCase("automount") || name.equalsIgnoreCase("private") || name.equalsIgnoreCase("Network")
67                                  || name.equalsIgnoreCase("Volumes")) {
68                              continue;
69                              // return true;
70                          }
71                      }
72  
73                      if (!traverse(child, mode, visitor, estimate + estimateIncrement * i / files.length, estimateIncrement / files.length)) {
74                          return false;
75                      }
76                  }
77              }
78  
79              if (mode == MODE_FOLDERS || mode == MODE_FILES_AND_FOLDERS || mode == MODE_ALL) {
80                  if (!visitor.visit(file, estimate)) {
81                      return false;
82                  }
83              }
84          } else if (mode == MODE_ALL) {
85              if (!visitor.visit(file, estimate)) {
86                  return false;
87              }
88          }
89  
90          return true;
91      }
92  
93      public boolean traverse(final int mode, final Visitor visitor) {
94          return traverse(mode, visitor, 0, 1);
95      }
96  
97      private boolean traverse(final int mode, final Visitor visitor, final double estimate, final double estimateIncrement) {
98          File[] roots = File.listRoots();
99  
100         if (ON_MAC_OS_X) {
101             final File Volumes = new File("/Volumes/");
102             roots = Volumes.listFiles();
103         } else {
104             roots = File.listRoots();
105         }
106 
107         if (roots == null) {
108             return false;
109         }
110 
111         for (int i = 0; i < roots.length; i++) {
112             final File root = roots[i];
113 
114             if (root == null || !root.exists()) {
115                 continue;
116             }
117 
118             if (!traverse(roots[i], mode, visitor, estimate + estimateIncrement * i / roots.length, estimateIncrement / roots.length)) {
119                 return false;
120             }
121         }
122 
123         return true;
124     }
125 
126     public boolean traverseAll(final File file, final Visitor visitor) {
127 
128         return traverse(file, MODE_FILES_AND_FOLDERS, visitor);
129     }
130 
131     public boolean traverseAll(final Visitor visitor) {
132 
133         return traverse(MODE_FILES_AND_FOLDERS, visitor);
134     }
135 
136     public boolean traverseFiles(final File file, final Visitor visitor) {
137 
138         return traverse(file, MODE_FILES, visitor);
139     }
140 
141     public boolean traverseFiles(final Visitor visitor) {
142 
143         return traverse(MODE_FILES, visitor);
144     }
145 
146     public boolean traverseFolders(final File file, final Visitor visitor) {
147 
148         return traverse(file, MODE_FOLDERS, visitor);
149     }
150 
151     public boolean traverseFolders(final Visitor visitor) {
152 
153         return traverse(MODE_FOLDERS, visitor);
154     }
155 
156 }