1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
|
/*
* Created on Jul 8, 2005 by @author Tom Jacobs
*
*/
package tjacobs.io;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//import tjacobs.util.Observable;
/**
* Find is pretty groovy. The point of it is to find files ala windows search.
* doesn't have the advanced search options tho.
*
* Extends Observable so all you need to do is add an observer to it.
*/
public class Find extends Observable implements Runnable {
private Thread mMyThread;
private Pattern mPattern;
private String mDirContains;
private File mStartDir;
private boolean mDescOnly = false;
private List mMatches = new ArrayList();
private List mDirectoriesToSearch = new ArrayList();
private Set mDirectoriesSearched = new TreeSet();
private boolean mStop;
private boolean mPause;
public static final boolean DEBUG = false;
Find(Pattern pattern, File dir, boolean desc) {
super();
mPattern = pattern;
mStartDir = dir;
mDescOnly = desc;
}
public int size() {
return mMatches.size();
}
public File get(int i) {
return (File)mMatches.get(i);
}
public Iterator iterator() {
return Collections.unmodifiableList(mMatches).iterator();
}
public static Find fsFind(Pattern pattern, File startDir) {
return fsFind(pattern, startDir, false);
}
public static Find fsFind(String pattern, File startDir, boolean descendOnly) {
return fsFind(Pattern.compile(pattern), startDir, descendOnly);
}
public static Find fsFind(Pattern pattern, File startDir, boolean descendOnly) {
Find find = new Find(pattern, startDir, descendOnly);
Thread t = new Thread(find, "Find");
t.setDaemon(true);
t.start();
return find;
}
public static Find fsFind(String pattern, File startDir) {
return fsFind(Pattern.compile(pattern), startDir);
}
public static Find fsFind(Pattern pattern, String startDir) {
return fsFind(pattern, new File(startDir));
}
public static Find fsFind(String pattern, String startDir) {
return fsFind(Pattern.compile(pattern), startDir);
}
public static Find fsFind(String pattern, String startDir, boolean descendOnly) {
return fsFind(Pattern.compile(pattern), startDir, descendOnly);
}
public static Find fsFind(Pattern pattern, String startDir, boolean descendOnly) {
return fsFind(pattern, new File(startDir), descendOnly);
}
public static Find fsFind(String pattern) {
return fsFind(Pattern.compile(pattern));
}
public static Find fsFind(Pattern pattern) {
return fsFind(pattern, (File)null);
}
public void join() {
try {
while (mMyThread == null) {
Thread.yield();
}
mMyThread.join();
}
catch (InterruptedException ex) {
//TODO: Log this
}
}
public synchronized void addObserver(Observer o) {
if (o == null) return;
int size = size();
for (int i = 0; i < size; i++) {
o.update(this, get(i));
}
super.addObserver(o);
}
// super.addObserver(o);
// if (mMatches != null && mMatches.size() > 0) {
// for (int i = 0; i < mMatches.size(); i++) {o.update(this, mMatches.)
// }
// }
public void run() {
mStop = false;
mMyThread = Thread.currentThread();
if (mStartDir == null) {
mStartDir = new File(".");
}
mStartDir = mStartDir.getAbsoluteFile();
if (mStartDir.isDirectory()) {
mDirectoriesToSearch.add(mStartDir);
}
else {
mDirectoriesToSearch.add(mStartDir.getParentFile());
}
while (mDirectoriesToSearch.size() > 0 && !mStop) {
if (mPause) {
synchronized(this) {
try {
wait();
}
catch (Exception ex) {ex.printStackTrace();}
}
}
File toSearch = (File) mDirectoriesToSearch.remove(0);
//mDirectoriesSearched.add(toSearch.getAbsolutePath());
search(toSearch);
}
}
private void addToMatches(File dir) {
mMatches.add(dir);
notifyObservers(dir);
}
private void search(File dir) {
if (DEBUG) System.out.println("dir = " + dir);
if (mDirectoriesSearched.contains(dir.getAbsolutePath())) {
return;
}
mDirectoriesSearched.add(dir.getAbsolutePath());
if (!mDescOnly) {
File par = dir.getParentFile();
if (par != null) mDirectoriesToSearch.add(par);
}
Matcher matcher = mPattern.matcher(dir.getAbsolutePath());
if (matcher.matches()) {
addToMatches(dir);
}
boolean shouldMatch = true;
if (mDirContains != null) {
shouldMatch = contains(dir.getAbsolutePath().toLowerCase(), mDirContains);
}
File files[] = dir.listFiles();
if (files == null) {
return;
}
for (int i =0; i < files.length; i++) {
if (files[i].isDirectory()) {
if (DEBUG) {
System.out.println("adding to search: " + files[i]);
}
mDirectoriesToSearch.add(files[i]);
}
if (shouldMatch) {
matcher = mPattern.matcher(files[i].getAbsolutePath());
if (DEBUG) {
System.out.println ("Testing: " + files[i].getAbsolutePath());
}
//String name = files[i].getName();
if (matcher.matches()) {
super.setChanged();
addToMatches(files[i]);
}
}
}
}
public void setDirContains(String str) {
mDirContains = str.toLowerCase();
}
public String getDirContains() {
return mDirContains;
}
public void pause() {
mPause = !mPause;
if (!mPause) {
synchronized(this) {
notify();
}
}
}
public void stop() {
mStop = true;
}
/**
* Returns true if and only if this string contains the specified
* sequence of char values.
*
* @param s the sequence to search for
* @return true if this string contains <code>s</code>, false otherwise
* @throws NullPointerException if <code>s</code> is <code>null</code>
* @since 1.5
*/
public boolean contains(String st, CharSequence s) {
return st.indexOf(s.toString()) > -1;
}
} |
Partager