diff options
Diffstat (limited to 'NET/worlds/scape/FileList.java')
| -rw-r--r-- | NET/worlds/scape/FileList.java | 132 |
1 files changed, 132 insertions, 0 deletions
diff --git a/NET/worlds/scape/FileList.java b/NET/worlds/scape/FileList.java new file mode 100644 index 0000000..c7c8a24 --- /dev/null +++ b/NET/worlds/scape/FileList.java @@ -0,0 +1,132 @@ +package NET.worlds.scape; + +import java.io.File; +import java.io.FilenameFilter; +import java.util.Enumeration; +import java.util.StringTokenizer; +import java.util.Vector; + +public class FileList implements FilenameFilter { + private String dirList; + private String extList; + private Vector<String> exts; + private boolean keepPathInfo; + private boolean sort = true; + + public FileList(String dirList, String extList) { + this.dirList = dirList; + this.extList = extList; + } + + public String getExtList() { + return this.extList; + } + + private static Vector<String> breakUp(String list) { + StringTokenizer t = new StringTokenizer(list, File.pathSeparator, false); + Vector<String> v = new Vector<String>(); + + while (t.hasMoreTokens()) { + v.addElement(t.nextToken()); + } + + return v; + } + + @Override + public boolean accept(File dir, String name) { + return extMatches(name, this.exts); + } + + public static boolean extMatches(String name, Vector<String> exts) { + int index = name.lastIndexOf("."); + if (index != -1) { + String ext = name.substring(index + 1); + Enumeration<String> e = exts.elements(); + + while (e.hasMoreElements()) { + if (ext.equalsIgnoreCase(e.nextElement())) { + return true; + } + } + } + + return false; + } + + public static boolean extMatches(String name, String exts) { + return extMatches(name, breakUp(exts)); + } + + public boolean extMatches(String name) { + return extMatches(name, this.extList); + } + + public FileList keepPathInfo() { + this.keepPathInfo = true; + return this; + } + + public FileList dontSort() { + this.sort = false; + return this; + } + + public static String removeTrailingSlash(String dir) { + if (dir != null) { + int len = dir.length(); + if (len > 0) { + char c = dir.charAt(len - 1); + if (c == '/' || c == '\\') { + dir = dir.substring(0, len - 1); + } + } + } + + return dir; + } + + public Vector<String> getList() { + this.exts = breakUp(this.extList); + Vector<String> dirs = breakUp(this.dirList); + Vector<String> v = new Vector<String>(); + Enumeration<String> edirs = dirs.elements(); + + while (edirs.hasMoreElements()) { + String dir = removeTrailingSlash(edirs.nextElement()); + File f = new File(dir); + if (f.exists()) { + String[] list = f.list(this); + if (this.keepPathInfo && !dir.equals(".")) { + dir = dir + File.separator; + } else { + dir = ""; + } + + for (int i = 0; i < list.length; i++) { + String name = dir + list[i]; + if (!this.sort) { + v.addElement(name); + } else { + int count = v.size(); + String lname = name.toLowerCase(); + + int j; + for (j = 0; j < count; j++) { + if (lname.compareTo(v.elementAt(j).toLowerCase()) < 0) { + v.insertElementAt(name, j); + break; + } + } + + if (j == count) { + v.addElement(name); + } + } + } + } + } + + return v; + } +} |