View Javadoc

    1   /***
    2    * BSD-style license; for more info see http://pmd.sourceforge.net/license.html
    3    */
    4   package net.sourceforge.pmd.cpd;
    5
    6   import java.io.File;
    7   import java.io.FilenameFilter;
    8   import java.util.ArrayList;
    9   import java.util.List;
    10
    11  public class FileFinder {
    12
    13      private FilenameFilter filter;
    14      private static final String FILE_SEP = System.getProperty("file.separator");
    15
    16      public List findFilesFrom(String dir, FilenameFilter filter, boolean recurse) {
    17          this.filter = filter;
    18          List files = new ArrayList();
    19          scanDirectory(new File(dir), files, recurse);
    20          return files;
    21      }
    22
    23      /***
    24       * Implements a tail recursive file scanner
    25       */
    26      private void scanDirectory(File dir, List list, boolean recurse) {
    27          String[] candidates = dir.list(filter);
    28          if (candidates == null) {
    29              return;
    30          }
    31          for (int i = 0; i < candidates.length; i++) {
    32              File tmp = new File(dir + FILE_SEP + candidates[i]);
    33              if (tmp.isDirectory()) {
    34                  if (recurse) {
    35                      scanDirectory(tmp, list, true);
    36                  }
    37              } else {
    38                  list.add(new File(dir + FILE_SEP + candidates[i]));
    39              }
    40          }
    41      }
    42  }