package net.sourceforge.phpeclipse.mover; import java.io.File; import java.io.IOException; public class DirectoryWalker { protected IMover[] fMover; protected IFilter[] fFilter; /** * creates a new DirectoryWalker * mover and filter array should have the same length ! */ public DirectoryWalker(IMover[] mover, IFilter[] filter) { this.fMover = mover; this.fFilter = filter; } /** * creates a new DirectoryWalker with a IFilter.DEFAULT_FILTER */ public DirectoryWalker(IMover[] mover) { this.fMover = mover; this.fFilter = new IFilter[mover.length]; for (int i = 0; i < mover.length; i++) { this.fFilter[i] = IFilter.DEFAULT_FILTER; } } /** * walks through the source directory, processing files as it * goes along to create the target directory * @param source source directory * @param target target directory * @throws IOException error with the file system * @throws XMException error caught by the application */ public void walk(String source, String target) throws IOException { try { walk(new File(source), new File(target)); } finally { } } /** * actual implementation of the walking * @param source source directory * @param target target directory * @throws IOException error with the file system * @return true if walking should continue, false if the messenger * has asked for the end of the walking */ protected boolean walk(File source, File target) throws IOException { if (!(target.exists() && target.isDirectory())) if (!target.mkdirs()) return false; for (int j = 0; j < fMover.length; j++) { File[] dirs; File[] docs; int idirs = 0, idocs = 0; if (source.isDirectory()) { File[] files = source.listFiles(); dirs = new File[files.length]; docs = new File[files.length]; String fileName; for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { if (fFilter[j].isDirectoryOk(files[i])) { dirs[idirs++] = files[i]; } } else if (files[i].isFile()) { if (fFilter[j].isFileOk(files[i])) { docs[idocs++] = files[i]; } } else return false; } } else { dirs = new File[0]; docs = new File[1]; docs[0] = source; idocs = 1; } for (int i = 0; i < idocs; i++) { System.out.println(docs[i].getAbsolutePath()); File result = fMover[j].move(docs[i], target); } System.out.println("directories"); for (int i = 0; i < idirs; i++) { System.out.println(dirs[i].getAbsolutePath()); if (!walk(dirs[i], new File(target, dirs[i].getName()))) return false; } } return true; } }