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 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 breakUp(String list) { StringTokenizer t = new StringTokenizer(list, File.pathSeparator, false); Vector v = new Vector(); 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 exts) { int index = name.lastIndexOf("."); if (index != -1) { String ext = name.substring(index + 1); Enumeration 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 getList() { this.exts = breakUp(this.extList); Vector dirs = breakUp(this.dirList); Vector v = new Vector(); Enumeration 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; } }