View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Ordina - initial implementation
10   *******************************************************************************/
11  package org.mod4j.dslcommon.io;
12  
13  import java.io.File;
14  
15  /**
16   * @author anneke
17   * 
18   */
19  public class DirectoryWalker {
20  
21      public DirectoryWalker() {
22          super();
23      }
24  
25      public void walk(String directoryPath, IDirectoryVisitor visitor) throws Exception {
26          File directory = new File(directoryPath);
27          if (directory.exists() && directory.isDirectory()) {
28              walk(directory, visitor);
29          }
30      }
31  
32      public void walk(File directory, IDirectoryVisitor visitor) throws Exception {
33          assert directory.exists() : "visitDirectoryBefore: directory does not exists.";
34          assert directory.isDirectory() : "visitDirectoryBefore: parameter is not a directory.";
35          visitor.visitDirectoryBefore(directory);
36          File[] contents = directory.listFiles();
37          if (contents != null) {
38              for (int i = 0; i < contents.length; i++) {
39                  if (contents[i].isFile()) {
40                      visitor.visitFile(contents[i]);
41                  } else if (contents[i].isDirectory()) {
42                      walk(contents[i], visitor);
43                  }
44              }
45          }
46          visitor.visitDirectoryAfter(directory);
47      }
48  
49  }