1 22 package org.objectweb.petals.classloader; 23 24 import java.io.File ; 25 import java.io.FileFilter ; 26 import java.io.FilenameFilter ; 27 import java.util.ArrayList ; 28 import java.util.Arrays ; 29 import java.util.Collection ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 34 38 public class ClassPath { 39 40 private static class DirectoryFilter implements FileFilter { 41 42 45 public boolean accept(File pathname) { 46 return pathname.isDirectory(); 47 } 48 49 } 50 51 private static class ExtensionFileFilter implements FilenameFilter { 52 53 private String [] extensions; 54 55 public ExtensionFileFilter(String [] exts) { 56 super(); 57 this.extensions = exts; 58 } 59 60 63 public boolean accept(File dir, String name) { 64 65 boolean accept = false; 66 67 String lcName = name.toLowerCase(); 68 69 for (int i=0; i<extensions.length; i++) { 70 String x = extensions[i]; 71 if (lcName.endsWith(x)) { 72 accept = true; 73 break; 74 } 75 } 76 77 return accept; 78 } 79 80 } 81 82 private List <File > pathComponents = new ArrayList <File >(); 83 84 private boolean recursiveScanner = false; 85 86 89 public ClassPath() { 90 super(); 91 } 92 93 public ClassPath(boolean recursive) { 94 super(); 95 this.recursiveScanner = recursive; 96 } 97 98 public void setClasspath(String classpath) { 99 pathComponents.clear(); 100 101 String [] elements = classpath.split(File.pathSeparator); 102 setClasspath(elements); 103 } 104 105 public void setClasspath(String [] classpath) { 106 for (int i=0; i<classpath.length; i++) { 107 addPathElement(classpath[i]); 108 } 109 } 110 111 public void addPathElement(String path) { 112 addPathElement(path, recursiveScanner, null); 113 } 114 115 public void addPathElement(String path, boolean recursive, 116 String [] extensions) { 117 118 File pathComponent = new File (path); 119 addPathFile(pathComponent, recursive, extensions); 120 } 121 122 public void addPathFile(File component, boolean recursive) { 123 addPathFile(component, recursive, null); 124 } 125 126 public void addPathFile(File pathElement, boolean recursive, 127 String [] extensions) { 128 129 if (pathElement == null) { 130 throw new IllegalArgumentException ("Classpath component must not " + 131 "be null"); 132 } 133 134 if (pathElement.isDirectory()) { 136 Collection <File > components; 137 138 if (recursive) { 139 components = scanComponentsRecursively(pathElement, 140 extensions); 141 } else { 142 components = scanComponents(pathElement, extensions); 143 } 144 145 for (File file : components) { 146 pathComponents.add(file); 147 } 148 } else { 150 pathComponents.add(pathElement); 151 } 152 } 153 154 private Collection <File > scanComponentsRecursively(File element, 155 String [] extensions) { 156 157 Collection <File > componentsFound = new ArrayList <File >(); 158 159 componentsFound.addAll(scanComponents(element, extensions)); 160 161 File [] directories = element.listFiles(new DirectoryFilter()); 162 163 for (int i=0; i<directories.length; i++) { 164 File d = directories[i]; 165 componentsFound.addAll(scanComponentsRecursively(d, extensions)); 166 } 167 168 return componentsFound; 169 } 170 171 private Collection <File > scanComponents(File element, 172 String [] extensions) { 173 174 Collection <File > componentsFound = new ArrayList <File >(); 175 176 if (extensions == null) { 177 componentsFound.add(element); 178 179 } else { 180 File [] elements = element.listFiles( 181 new ExtensionFileFilter(extensions)); 182 183 componentsFound.addAll(Arrays.asList(elements)); 184 } 185 186 return componentsFound; 187 } 188 189 192 public Iterator iterator() { 193 return pathComponents.iterator(); 194 } 195 196 199 public int size() { 200 return pathComponents.size(); 201 } 202 203 206 public File elementAt(int index) { 207 return (File ) pathComponents.get(index); 208 } 209 210 } 211 | Popular Tags |