1 package jdepend.framework; 2 3 import java.io.*; 4 import java.util.*; 5 6 14 15 public class FileManager { 16 17 private ArrayList directories; 18 private boolean acceptInnerClasses; 19 20 21 public FileManager() { 22 directories = new ArrayList(); 23 acceptInnerClasses = true; 24 } 25 26 32 public void acceptInnerClasses(boolean b) { 33 acceptInnerClasses = b; 34 } 35 36 public void addDirectory(String name) throws IOException { 37 38 File directory = new File(name); 39 40 if (directory.isDirectory() || acceptJarFile(directory)) { 41 directories.add(directory); 42 } else { 43 throw new IOException("Invalid directory or JAR file: " + name); 44 } 45 } 46 47 public boolean acceptFile(File file) { 48 return acceptClassFile(file) || acceptJarFile(file); 49 } 50 51 public boolean acceptClassFile(File file) { 52 if (!file.isFile()) { 53 return false; 54 } 55 return acceptClassFileName(file.getName()); 56 } 57 58 public boolean acceptClassFileName(String name) { 59 60 if (!acceptInnerClasses) { 61 if (name.toLowerCase().indexOf("$") > 0) { 62 return false; 63 } 64 } 65 66 if (!name.toLowerCase().endsWith(".class")) { 67 return false; 68 } 69 70 return true; 71 } 72 73 public boolean acceptJarFile(File file) { 74 return isJar(file) || isZip(file) || isWar(file); 75 } 76 77 public Collection extractFiles() { 78 79 Collection files = new ArrayList(); 80 81 for (Iterator i = directories.iterator(); i.hasNext();) { 82 File directory = (File)i.next(); 83 collectFiles(directory, files); 84 } 85 86 return files; 87 } 88 89 private void collectFiles(File directory, Collection files) { 90 91 if (directory.isFile()) { 92 93 addFile(directory, files); 94 95 } else { 96 97 String [] directoryFiles = directory.list(); 98 99 for (int i = 0; i < directoryFiles.length; i++) { 100 101 File file = new File(directory, directoryFiles[i]); 102 if (acceptFile(file)) { 103 addFile(file, files); 104 } else if (file.isDirectory()) { 105 collectFiles(file, files); 106 } 107 } 108 } 109 } 110 111 private void addFile(File f, Collection files) { 112 if (!files.contains(f)) { 113 files.add(f); 114 } 115 } 116 117 private boolean isWar(File file) { 118 return existsWithExtension(file, ".war"); 119 } 120 121 private boolean isZip(File file) { 122 return existsWithExtension(file, ".zip"); 123 } 124 125 private boolean isJar(File file) { 126 return existsWithExtension(file, ".jar"); 127 } 128 129 private boolean existsWithExtension(File file, String extension) { 130 return file.isFile() && 131 file.getName().toLowerCase().endsWith(extension); 132 } 133 134 } | Popular Tags |