01: package org.andromda.maven.utils;
02:
03: import java.io.File;
04: import java.io.FileFilter;
05: import java.util.ArrayList;
06: import java.util.Collections;
07: import java.util.List;
08:
09: /**
10: * Recursively removes CVS directories and their contents from the given directory.
11: */
12: public class CVSDirRemover {
13: /**
14: * A filter only accepting directories.
15: */
16: private static final FileFilter dirFilter = new DirFilter();
17:
18: /**
19: * A filter only accepting CVS directories.
20: */
21: private static final FileFilter cvsDirFilter = new CVSDirFilter();
22:
23: /**
24: * Recursively removes all CVS directories from the specified root directory and returns the names of all
25: * the directories that have been removed.
26: */
27: public static List remove(String rootDir) {
28: final File file = new File(rootDir);
29: final List removedDirs = new ArrayList();
30: remove(file, removedDirs);
31: Collections.sort(removedDirs);
32: return removedDirs;
33: }
34:
35: /**
36: * Recursively removes all CVS directories from the specified root directory and collects the names of all
37: * the directories that have been removed.
38: *
39: * @param file the directory on which to operate
40: * @param removedDirs the names of the directories that have been removed upto this point
41: */
42: private static void remove(File file, List removedDirs) {
43: final File[] cvsDirs = file.listFiles(cvsDirFilter);
44: for (int i = 0; i < cvsDirs.length; i++) {
45: final File cvsDir = cvsDirs[i];
46: final File[] files = cvsDir.listFiles();
47: for (int j = 0; j < files.length; j++) {
48: final File cvsFile = files[j];
49: cvsFile.delete();
50: }
51: cvsDir.delete();
52: removedDirs.add(cvsDir.getAbsolutePath());
53: }
54:
55: final File[] subdirs = file.listFiles(dirFilter);
56: for (int i = 0; i < subdirs.length; i++) {
57: final File subdir = subdirs[i];
58: remove(subdir, removedDirs);
59: }
60: }
61:
62: /**
63: * Helper class only accepting CVS directories when used for filtering.
64: */
65: private static class CVSDirFilter extends DirFilter {
66: /**
67: * @return true if the argument file is a directory called CVS, false otherwise
68: */
69: public boolean accept(File file) {
70: return "CVS".equals(file.getName()) && super .accept(file);
71: }
72: }
73:
74: /**
75: * Helper class only accepting directories when used for filtering.
76: */
77: private static class DirFilter implements FileFilter {
78: /**
79: * @return true if the argument file is a directory, false otherwise
80: */
81: public boolean accept(File file) {
82: return file.isDirectory();
83: }
84:
85: }
86:
87: }
|