1 23 24 25 package com.sun.jdo.api.persistence.enhancer.util; 26 27 import java.util.StringTokenizer ; 28 import java.util.Enumeration ; 29 import java.util.NoSuchElementException ; 30 import java.io.File ; 31 import java.io.IOException ; 32 import java.io.FilenameFilter ; 33 34 35 39 40 public class ClassPath { 41 42 private String theClassPathSpec; 43 44 45 private ClassPathElement theClassPath; 46 47 52 public ClassPath(String path) { 53 theClassPathSpec = path; 54 parsePath(); 55 } 56 57 60 public ClassFileSource findClass(String className) { 61 return findClass(className, theClassPath); 62 } 63 64 68 static ClassFileSource findClass(String className, ClassPathElement path) { 69 for (ClassPathElement e = path; e != null; e = e.next()) { 70 ClassFileSource source = e.sourceOf(className); 71 if (source != null) { 72 source.setSourceElement(e); 73 return source; 74 } 75 } 76 77 return null; 78 } 79 80 84 public static String fileNameOf(String className, char separator) { 85 StringBuffer path = new StringBuffer (); 86 StringTokenizer parser = new StringTokenizer (className, "./", false); for (boolean first = true; parser.hasMoreElements(); first = false) { 88 if (!first) 89 path.append(separator); 90 path.append(parser.nextToken()); 91 } 92 path.append(".class"); return path.toString(); 94 } 95 96 97 101 public static String fileNameOf(String className) { 102 return fileNameOf(className, File.separatorChar); 103 } 104 105 106 110 public static String zipFileNameOf(String className) { 111 return fileNameOf(className, '/'); 112 } 113 114 115 120 public static String classNameOf(String fileName) { 121 int fnlen = fileName.length(); 122 if (fnlen > 6 && fileName.regionMatches(true, fnlen - 6, ".class", 0, 6)) { 124 fileName = fileName.substring(0, fileName.length()-6); 125 StringBuffer className = new StringBuffer (); 126 StringTokenizer parser = new StringTokenizer (fileName, "\\/", false); for (boolean first = true; parser.hasMoreElements(); first = false) { 128 if (!first) 129 className.append('/'); 130 className.append(parser.nextToken()); 131 } 132 return className.toString(); 133 } 134 return null; 135 } 136 137 140 public boolean remove(File directory) { 141 boolean matched = false; 142 ClassPathElement firstElement = theClassPath; 143 ClassPathElement prevElement = null; 144 for (ClassPathElement cpe = firstElement; cpe != null; cpe = cpe.next()) { 145 if (cpe.matches(directory)) { 146 matched = true; 147 if (prevElement == null) 148 firstElement = cpe.next(); 149 else 150 prevElement.setNext(cpe.next()); 151 } else { 152 prevElement = cpe; 153 } 154 } 155 theClassPath = firstElement; 156 return matched; 157 } 158 159 162 public void append(File directory) { 163 append(ClassPathElement.create(directory.getPath())); 164 } 165 166 169 public void append(ClassPathElement anElement) { 170 if (theClassPath == null) 171 theClassPath = anElement; 172 else 173 theClassPath.append(anElement); 174 } 175 176 188 public Enumeration classesInPackage(String packageName) { 189 return new ClassPackageEnumeration(this, packageName); 190 } 191 192 193 ClassPathElement getPathElements() { 194 return theClassPath; 195 } 196 197 198 199 private void parsePath() { 200 StringTokenizer parser = 201 new StringTokenizer (theClassPathSpec, 202 java.io.File.pathSeparator, 203 false 204 ); 205 206 ClassPathElement lastElement = null; 207 while (parser.hasMoreElements()) { 208 ClassPathElement anElement = ClassPathElement.create(parser.nextToken()); 209 210 if (lastElement == null) 211 theClassPath = anElement; 212 else 213 lastElement.append(anElement); 214 215 lastElement = anElement; 216 } 217 } 218 219 } 220 221 225 226 class ClassPackageEnumeration implements Enumeration { 227 229 private ClassPathElement nextClassPathElement; 230 231 232 private String thePackageName; 233 234 236 private Enumeration currentElementEnumeration; 237 238 243 ClassPackageEnumeration(ClassPath classPath, String packageName) { 244 nextClassPathElement = classPath.getPathElements(); 245 thePackageName = packageName; 246 } 247 248 public boolean hasMoreElements() { 249 while ((currentElementEnumeration == null || 250 !currentElementEnumeration.hasMoreElements()) && 251 nextClassPathElement != null) { 252 currentElementEnumeration = 253 nextClassPathElement.classesInPackage(thePackageName); 254 nextClassPathElement = nextClassPathElement.next(); 255 } 256 257 return (currentElementEnumeration != null && 258 currentElementEnumeration.hasMoreElements()); 259 } 260 261 public Object nextElement() { 262 if (hasMoreElements()) 263 return currentElementEnumeration.nextElement(); 264 265 throw new NoSuchElementException (); 266 } 267 } 268 269 | Popular Tags |