001: // enumerateFiles.java
002: // -------------------------------------
003: // (C) by Michael Peter Christen; mc@anomic.de
004: // first published on http://www.anomic.de
005: // Frankfurt, Germany, 2004
006: // last major change: 26.12.2004
007: //
008: // This program is free software; you can redistribute it and/or modify
009: // it under the terms of the GNU General Public License as published by
010: // the Free Software Foundation; either version 2 of the License, or
011: // (at your option) any later version.
012: //
013: // This program is distributed in the hope that it will be useful,
014: // but WITHOUT ANY WARRANTY; without even the implied warranty of
015: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: // GNU General Public License for more details.
017: //
018: // You should have received a copy of the GNU General Public License
019: // along with this program; if not, write to the Free Software
020: // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
021: //
022: // Using this software in any meaning (reading, learning, copying, compiling,
023: // running) means that you agree that the Author(s) is (are) not responsible
024: // for cost, loss of data or any harm that may be caused directly or indirectly
025: // by usage of this softare or this documentation. The usage of this software
026: // is on your own risk. The installation and usage (starting/running) of this
027: // software may allow other people or application to access your computer and
028: // any attached devices and is highly dependent on the configuration of the
029: // software which must be done by the user of the software; the author(s) is
030: // (are) also not responsible for proper configuration and usage of the
031: // software, even if provoked by documentation provided together with
032: // the software.
033: //
034: // Any changes to this file according to the GPL as documented in the file
035: // gpl.txt aside this file in the shipment you received can be done to the
036: // lines that follows this copyright notice here, but changes must not be
037: // done inside the copyright notive above. A re-distribution must contain
038: // the intact and unchanged copyright notice.
039: // Contributions and changes to the program code must be marked as such.
040:
041: package de.anomic.tools;
042:
043: import java.io.File;
044: import java.util.ArrayList;
045: import java.util.Enumeration;
046: import java.util.TreeSet;
047:
048: public class enumerateFiles implements Enumeration<File> {
049:
050: // implements iterative search through recursively defined subdirectories
051: // and return all paths to the files
052:
053: private ArrayList<TreeSet<File>> hierarchy; // contains TreeSet elements, each TreeSet contains File Entries
054: private boolean incOrder; // if true, the smallest value is returned first
055: private File buffer; // the prefetch-buffer
056: private boolean return_files;
057: private boolean return_folders;
058: private boolean delete_emptyFolders;
059:
060: public enumerateFiles(File root, boolean files, boolean folders,
061: boolean increasing, boolean deleteEmptyFolders) {
062: // we define our data structures first
063: this .return_files = files;
064: this .return_folders = folders;
065: this .delete_emptyFolders = deleteEmptyFolders;
066: this .hierarchy = new ArrayList<TreeSet<File>>();
067: this .incOrder = increasing;
068: // the we initially fill the hierarchy with the content of the root folder
069: TreeSet<File> t = new TreeSet<File>();
070: String[] l = root.list();
071: // System.out.println("D " + l.toString());
072: if (l != null)
073: for (int i = 0; i < l.length; i++)
074: t.add(new File(root, l[i]));
075: this .hierarchy.add(t);
076: // start with search by filling the buffer
077: this .buffer = nextElement0();
078: }
079:
080: private File nextElement0() {
081: // the object is a File pointing to the corresponding file
082: File f;
083: TreeSet<File> t;
084: do {
085: // System.out.println("D " + hierarchy.toString());
086: t = null;
087: while ((t == null) && (hierarchy.size() > 0)) {
088: t = hierarchy.get(hierarchy.size() - 1);
089: if (t.size() == 0) {
090: hierarchy.remove(hierarchy.size() - 1); // we step up one hierarchy
091: t = null;
092: }
093: }
094: if ((hierarchy.size() == 0) || (t.size() == 0))
095: return null; // this is the end
096: // fetch value
097: if (incOrder)
098: f = (File) t.first();
099: else
100: f = (File) t.last();
101: t.remove(f);
102: // if the value represents another folder, we step into the next hierarchy
103: if (f.isDirectory()) {
104: t = new TreeSet<File>();
105: String[] l = f.list();
106: if (l == null) {
107: // f has disappeared
108: f = null;
109: } else {
110: if (l.length == 0) {
111: if (delete_emptyFolders) {
112: f.delete();
113: f = null;
114: } else {
115: if (!(return_folders))
116: f = null;
117: }
118: } else {
119: for (int i = 0; i < l.length; i++)
120: t.add(new File(f, l[i]));
121: hierarchy.add(t);
122: if (!(return_folders))
123: f = null;
124: }
125: }
126: } else {
127: if (!(return_files))
128: f = null;
129: }
130: } while (f == null);
131: // thats it
132: return f;
133: }
134:
135: public boolean hasMoreElements() {
136: return buffer != null;
137: }
138:
139: public File nextElement() {
140: File r = buffer;
141: buffer = nextElement0();
142: return r;
143: }
144:
145: }
|