KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > depend > DirectoryIterator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18 package org.apache.tools.ant.taskdefs.optional.depend;
19
20 import java.io.File JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.util.Enumeration JavaDoc;
24 import java.util.Stack JavaDoc;
25 import java.util.Vector JavaDoc;
26
27 /**
28  * An iterator which iterates through the contents of a java directory. The
29  * iterator should be created with the directory at the root of the Java
30  * namespace.
31  *
32  */

33 public class DirectoryIterator implements ClassFileIterator {
34
35     /**
36      * This is a stack of current iterators supporting the depth first
37      * traversal of the directory tree.
38      */

39     private Stack JavaDoc enumStack;
40
41     /**
42      * The current directory iterator. As directories encounter lower level
43      * directories, the current iterator is pushed onto the iterator stack
44      * and a new iterator over the sub directory becomes the current
45      * directory. This implements a depth first traversal of the directory
46      * namespace.
47      */

48     private Enumeration JavaDoc currentEnum;
49
50     /**
51      * Creates a directory iterator. The directory iterator is created to
52      * scan the root directory. If the changeInto flag is given, then the
53      * entries returned will be relative to this directory and not the
54      * current directory.
55      *
56      * @param rootDirectory the root if the directory namespace which is to
57      * be iterated over
58      * @param changeInto if true then the returned entries will be relative
59      * to the rootDirectory and not the current directory.
60      * @exception IOException if there is a problem reading the directory
61      * information.
62      */

63     public DirectoryIterator(File JavaDoc rootDirectory, boolean changeInto)
64          throws IOException JavaDoc {
65         super();
66
67         enumStack = new Stack JavaDoc();
68
69         Vector JavaDoc filesInRoot = getDirectoryEntries(rootDirectory);
70
71         currentEnum = filesInRoot.elements();
72     }
73
74     /**
75      * Get a vector covering all the entries (files and subdirectories in a
76      * directory).
77      *
78      * @param directory the directory to be scanned.
79      * @return a vector containing File objects for each entry in the
80      * directory.
81      */

82     private Vector JavaDoc getDirectoryEntries(File JavaDoc directory) {
83         Vector JavaDoc files = new Vector JavaDoc();
84
85         // File[] filesInDir = directory.listFiles();
86
String JavaDoc[] 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 JavaDoc(directory, filesInDir[i]));
93             }
94         }
95
96         return files;
97     }
98
99     /**
100      * Template method to allow subclasses to supply elements for the
101      * iteration. The directory iterator maintains a stack of iterators
102      * covering each level in the directory hierarchy. The current iterator
103      * covers the current directory being scanned. If the next entry in that
104      * directory is a subdirectory, the current iterator is pushed onto the
105      * stack and a new iterator is created for the subdirectory. If the
106      * entry is a file, it is returned as the next element and the iterator
107      * remains valid. If there are no more entries in the current directory,
108      * the topmost iterator on the stack is popped off to become the
109      * current iterator.
110      *
111      * @return the next ClassFile in the iteration.
112      */

113     public ClassFile getNextClassFile() {
114         ClassFile nextElement = null;
115
116         try {
117             while (nextElement == null) {
118                 if (currentEnum.hasMoreElements()) {
119                     File JavaDoc element = (File JavaDoc) currentEnum.nextElement();
120
121                     if (element.isDirectory()) {
122
123                         // push the current iterator onto the stack and then
124
// iterate through this directory.
125
enumStack.push(currentEnum);
126
127                         Vector JavaDoc files = getDirectoryEntries(element);
128
129                         currentEnum = files.elements();
130                     } else {
131
132                         // we have a file. create a stream for it
133
FileInputStream JavaDoc inFileStream
134                             = new FileInputStream JavaDoc(element);
135
136                         if (element.getName().endsWith(".class")) {
137
138                             // create a data input stream from the jar
139
// input stream
140
ClassFile javaClass = new ClassFile();
141
142                             javaClass.read(inFileStream);
143
144                             nextElement = javaClass;
145                         }
146                     }
147                 } else {
148                     // this iterator is exhausted. Can we pop one off the stack
149
if (enumStack.empty()) {
150                         break;
151                     } else {
152                         currentEnum = (Enumeration JavaDoc) enumStack.pop();
153                     }
154                 }
155             }
156         } catch (IOException JavaDoc e) {
157             nextElement = null;
158         }
159
160         return nextElement;
161     }
162
163 }
164
165
Popular Tags