private FSTree buildTree(Collection<AbsFile> l) { FSTree tree = new FSTree(null); FSTree root = tree.getDir("absolute"); root.setParent(root); for (AbsFile f : l) { String path = f.getPath(); boolean absolute = path.startsWith("/"); FSTree crt = absolute ? tree.getDir("absolute") : tree.getDir("relative"); StringTokenizer st = new StringTokenizer(path, "/", false); while (st.hasMoreTokens()) { String token = st.nextToken(); if (!st.hasMoreTokens()) { // file crt.addFile(token); } else if (token.equals("..")) { crt = crt.getParent(); } else if (token.equals(".")) { // skip } else { crt = crt.getDir(token); } } } return tree; }
@Override public List<AbsFile> listFiles(AbsFile dir, FilenameFilter filter) { String path = dir.getPath(); boolean absolute = path.startsWith("/"); FSTree crt = absolute ? tree.getDir("absolute") : tree.getDir("relative"); StringTokenizer st = new StringTokenizer(path, "/", false); while (st.hasMoreTokens() && crt != null) { String token = st.nextToken(); if (token.equals("..")) { crt = crt.getParent(); } else if (token.equals(".")) { // skip } else { crt = crt.findDir(token); } } if (crt == null) { throw new RuntimeException("No such directory: " + path); } List<AbsFile> l = new ArrayList<AbsFile>(); for (String name : crt.listFiles()) { if (filter.accept(null, name)) { l.add(new AbsFile(dir, name)); } } return l; }