1 18 package org.apache.tools.ant.taskdefs.optional.depend; 19 20 import java.io.File ; 21 import java.io.FileInputStream ; 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.Stack ; 25 import java.util.Vector ; 26 27 33 public class DirectoryIterator implements ClassFileIterator { 34 35 39 private Stack enumStack; 40 41 48 private Enumeration currentEnum; 49 50 63 public DirectoryIterator(File rootDirectory, boolean changeInto) 64 throws IOException { 65 super(); 66 67 enumStack = new Stack (); 68 69 Vector filesInRoot = getDirectoryEntries(rootDirectory); 70 71 currentEnum = filesInRoot.elements(); 72 } 73 74 82 private Vector getDirectoryEntries(File directory) { 83 Vector files = new Vector (); 84 85 String [] filesInDir = directory.list(); 87 88 if (filesInDir != null) { 89 int length = filesInDir.length; 90 91 for (int i = 0; i < length; ++i) { 92 files.addElement(new File (directory, filesInDir[i])); 93 } 94 } 95 96 return files; 97 } 98 99 113 public ClassFile getNextClassFile() { 114 ClassFile nextElement = null; 115 116 try { 117 while (nextElement == null) { 118 if (currentEnum.hasMoreElements()) { 119 File element = (File ) currentEnum.nextElement(); 120 121 if (element.isDirectory()) { 122 123 enumStack.push(currentEnum); 126 127 Vector files = getDirectoryEntries(element); 128 129 currentEnum = files.elements(); 130 } else { 131 132 FileInputStream inFileStream 134 = new FileInputStream (element); 135 136 if (element.getName().endsWith(".class")) { 137 138 ClassFile javaClass = new ClassFile(); 141 142 javaClass.read(inFileStream); 143 144 nextElement = javaClass; 145 } 146 } 147 } else { 148 if (enumStack.empty()) { 150 break; 151 } else { 152 currentEnum = (Enumeration ) enumStack.pop(); 153 } 154 } 155 } 156 } catch (IOException e) { 157 nextElement = null; 158 } 159 160 return nextElement; 161 } 162 163 } 164 165 | Popular Tags |