/** * Gather all virtual files from the input directory into the provided list * * @param files the input list to fill in * @param dir the current directory * @param recursive flag for recursion */ private void gatherFiles(List<VirtualFile> files, VirtualDir dir, boolean recursive) { // Traverse the children for (VirtualPath child : dir.getChildren()) { if (child.isFile()) { // Found a file files.add((VirtualFile) child); } else if (recursive) { // Recurse gatherFiles(files, (VirtualDir) child, recursive); } } }
/** * Main recursive method to gather files matching the provided input paths. * * @param files the list to place the files in * @param path the current path * @param recursive whether the final file search is recursive or not * @param paths the array of the input paths * @param index the current index in the array of input paths */ private void gatherFiles( List<VirtualFile> files, VirtualPath path, boolean recursive, String[] paths, int index) { if (!path.matches(paths[index])) { // No match, stop here return; } if (index == paths.length - 1) { // We have reached a leaf in the path structure if (path.isFile()) files.add((VirtualFile) path); else gatherFiles(files, (VirtualDir) path, recursive); } else { if (path.isDir()) { // We have reached an intermediate directory for (VirtualPath child : ((VirtualDir) path).getChildren()) { gatherFiles(files, child, recursive, paths, index + 1); } } } }
/** * Main recursive method to check for the existence of the provided input paths. * * @param path the current path * @param paths the array of the input paths * @param index the current index in the array of input paths */ private boolean containFiles(VirtualPath path, String[] paths, int index) { if (!path.matches(paths[index])) { // No match, stop here return false; } if (index == paths.length - 1) { // We have reached a leaf in the path structure return true; } if (path.isDir()) { // We have reached an intermediate directory for (VirtualPath child : ((VirtualDir) path).getChildren()) { if (containFiles(child, paths, index + 1)) return true; } } return false; }