e.g. Calendar Search Help
You must enter a value before pressing Search
ant

Class: org.apache.tools.ant.taskdefs.optional.depend.DirectoryIterator   ©

 OK to copy?
001 /*
002  * Copyright  2000-2004 The Apache Software Foundation
003  *
004  *  Licensed under the Apache License, Version 2.0 (the "License");
005  *  you may not use this file except in compliance with the License.
006  *  You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  *  Unless required by applicable law or agreed to in writing, software
011  *  distributed under the License is distributed on an "AS IS" BASIS,
012  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  *  See the License for the specific language governing permissions and
014  *  limitations under the License.
015  *
016  */
017 package org.apache.tools.ant.taskdefs.optional.depend;
018 
019 import java.io.File;
020 import java.io.FileInputStream;
021 import java.io.IOException;
022 import java.util.Enumeration;
023 import java.util.Stack;
024 import java.util.Vector;
025 
026 /**
027  * An iterator which iterates through the contents of a java directory. The
028  * iterator should be created with the directory at the root of the Java
029  * namespace.
030  *
031  */
032 public class DirectoryIterator implements ClassFileIterator {
033 
034     /**
035      * This is a stack of current iterators supporting the depth first
036      * traversal of the directory tree.
037      */
038     private Stack enumStack;
039 
040     /**
041      * The current directory iterator. As directories encounter lower level
042      * directories, the current iterator is pushed onto the iterator stack
043      * and a new iterator over the sub directory becomes the current
044      * directory. This implements a depth first traversal of the directory
045      * namespace.
046      */
047     private Enumeration currentEnum;
048 
049     /**
050      * The length of the root directory. This is used to remove the root
051      * directory from full paths.
052      */
053     private int rootLength;
054 
055     /**
056      * Creates a directory iterator. The directory iterator is created to
057      * scan the root directory. If the changeInto flag is given, then the
058      * entries returned will be relative to this directory and not the
059      * current directory.
060      *
061      * @param rootDirectory the root if the directory namespace which is to
062      *      be iterated over
063      * @param changeInto if true then the returned entries will be relative
064      *      to the rootDirectory and not the current directory.
065      * @exception IOException if there is a problem reading the directory
066      *      information.
067      */
068     public DirectoryIterator(File rootDirectory, boolean changeInto)
069          throws IOException {
070         super();
071 
072         enumStack = new Stack();
073 
074         if (rootDirectory.isAbsolute() || changeInto) {
075             rootLength = rootDirectory.getPath().length() + 1;
076         } else {
077             rootLength = 0;
078         }
079 
080         Vector filesInRoot = getDirectoryEntries(rootDirectory);
081 
082         currentEnum = filesInRoot.elements();
083     }
084 
085     /**
086      * Get a vector covering all the entries (files and subdirectories in a
087      * directory).
088      *
089      * @param directory the directory to be scanned.
090      * @return a vector containing File objects for each entry in the
091      *      directory.
092      */
093     private Vector getDirectoryEntries(File directory) {
094         Vector files = new Vector();
095 
096         // File[] filesInDir = directory.listFiles();
Rate097         String[] filesInDir = directory.list();
098 
099         if (filesInDir != null) {
100             int length = filesInDir.length;
101 
102             for (int i = 0; i < length; ++i) {
103                 files.addElement(new File(directory, filesInDir[i]));
104             }
105         }
106 
107         return files;
108     }
109 
110     /**
111      * Template method to allow subclasses to supply elements for the
112      * iteration. The directory iterator maintains a stack of iterators
113      * covering each level in the directory hierarchy. The current iterator
114      * covers the current directory being scanned. If the next entry in that
115      * directory is a subdirectory, the current iterator is pushed onto the
116      * stack and a new iterator is created for the subdirectory. If the
117      * entry is a file, it is returned as the next element and the iterator
118      * remains valid. If there are no more entries in the current directory,
119      * the topmost iterator on the stack is popped off to become the
120      * current iterator.
121      *
122      * @return the next ClassFile in the iteration.
123      */
124     public ClassFile getNextClassFile() {
125         ClassFile nextElement = null;
126 
127         try {
128             while (nextElement == null) {
129                 if (currentEnum.hasMoreElements()) {
130                     File element = (File) currentEnum.nextElement();
131 
132                     if (element.isDirectory()) {
133 
134                         // push the current iterator onto the stack and then
135                         // iterate through this directory.
136                         enumStack.push(currentEnum);
137 
138                         Vector files = getDirectoryEntries(element);
139 
140                         currentEnum = files.elements();
141                     } else {
142 
143                         // we have a file. create a stream for it
144                         FileInputStream inFileStream
145                             = new FileInputStream(element);
146 
147                         if (element.getName().endsWith(".class")) {
148 
149                             // create a data input stream from the jar
150                             // input stream
151                             ClassFile javaClass = new ClassFile();
152 
153                             javaClass.read(inFileStream);
154 
155                             nextElement = javaClass;
156                         }
157                     }
158                 } else {
159                     // this iterator is exhausted. Can we pop one off the stack
160                     if (enumStack.empty()) {
161                         break;
162                     } else {
163                         currentEnum = (Enumeration) enumStack.pop();
164                     }
165                 }
166             }
167         } catch (IOException e) {
168             nextElement = null;
169         }
170 
171         return nextElement;
172     }
173 
174 }


            
All Examples in File:
Example
Line
Rating (found
useful by...)
97 100% of 1