| 1 | |
package org.webslinger.commons.vfs.util; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.List; |
| 5 | |
|
| 6 | |
import org.apache.commons.vfs.FileObject; |
| 7 | |
import org.apache.commons.vfs.FileSelectInfo; |
| 8 | |
import org.apache.commons.vfs.FileSelector; |
| 9 | |
import org.apache.commons.vfs.FileSystemException; |
| 10 | |
|
| 11 | |
public final class Walker { |
| 12 | 0 | private Walker() { |
| 13 | 0 | } |
| 14 | |
|
| 15 | |
public static <T extends FileObject> List<T> walk(T file, FileSelector selector, boolean depthWise) throws FileSystemException { |
| 16 | 0 | final ArrayList<T> files = new ArrayList<T>(); |
| 17 | 0 | FileAcceptor<T> acceptor = new FileAcceptorSelector<T>(file, selector) { |
| 18 | |
public void acceptFile(T file) { |
| 19 | 0 | files.add(file); |
| 20 | 0 | } |
| 21 | |
}; |
| 22 | 0 | walk(file, acceptor, depthWise, 0); |
| 23 | 0 | return files; |
| 24 | |
} |
| 25 | |
|
| 26 | |
public static <T extends FileObject> void walk(T file, FileSelector selector, boolean depthWise, final List<T> files) throws FileSystemException { |
| 27 | 314 | FileAcceptor<T> acceptor = new FileAcceptorSelector<T>(file, selector) { |
| 28 | |
public void acceptFile(T file) { |
| 29 | 322 | files.add(file); |
| 30 | 322 | } |
| 31 | |
}; |
| 32 | 314 | walk(file, acceptor, depthWise, 0); |
| 33 | 314 | } |
| 34 | |
|
| 35 | |
public static <T extends FileObject> void walk(T file, FileAcceptor<T> acceptor, boolean depthWise) throws FileSystemException { |
| 36 | 0 | walk(file, acceptor, depthWise, 0); |
| 37 | 0 | } |
| 38 | |
|
| 39 | |
private static <T extends FileObject> void walk(T file, FileAcceptor<T> acceptor, boolean depthWise, int depth) throws FileSystemException { |
| 40 | 326 | boolean selected = acceptor.includeFile(file, depth); |
| 41 | 326 | if (selected && depthWise) acceptor.acceptFile(file); |
| 42 | 326 | if (file.getType().hasChildren() && acceptor.traverseDescendents(file, depth)) { |
| 43 | 6 | depth++; |
| 44 | 18 | for (FileObject child: file.getChildren()) { |
| 45 | 12 | walk((T) child, acceptor, depthWise, depth); |
| 46 | |
} |
| 47 | |
} |
| 48 | 326 | if (selected && !depthWise) acceptor.acceptFile(file); |
| 49 | 326 | } |
| 50 | |
|
| 51 | |
public static interface FileAcceptor<T extends FileObject> { |
| 52 | |
boolean includeFile(T file, int depth) throws FileSystemException; |
| 53 | |
boolean traverseDescendents(T file, int depth) throws FileSystemException; |
| 54 | |
void acceptFile(T file) throws FileSystemException; |
| 55 | |
} |
| 56 | |
|
| 57 | 436 | public abstract static class FileAcceptorSelector<T extends FileObject> implements FileAcceptor<T> { |
| 58 | |
private FileObject file; |
| 59 | |
private int depth; |
| 60 | |
private final FileSelectInfo info; |
| 61 | |
private final FileSelector selector; |
| 62 | |
|
| 63 | 314 | protected FileAcceptorSelector(final T base, FileSelector selector) { |
| 64 | 314 | info = new FileSelectInfo() { |
| 65 | |
public FileObject getBaseFolder() { |
| 66 | 0 | return base; |
| 67 | |
} |
| 68 | |
|
| 69 | |
public FileObject getFile() { |
| 70 | 0 | return file; |
| 71 | |
} |
| 72 | |
|
| 73 | |
public int getDepth() { |
| 74 | 436 | return depth; |
| 75 | |
} |
| 76 | |
}; |
| 77 | 314 | this.selector = selector; |
| 78 | 314 | } |
| 79 | |
|
| 80 | |
public boolean includeFile(T file, int depth) throws FileSystemException { |
| 81 | 326 | this.file = file; |
| 82 | 326 | this.depth = depth; |
| 83 | |
try { |
| 84 | 326 | return selector.includeFile(info); |
| 85 | 0 | } catch (FileSystemException e) { |
| 86 | 0 | throw e; |
| 87 | 0 | } catch (Exception e) { |
| 88 | 0 | throw (FileSystemException) new FileSystemException(e.getMessage()).initCause(e); |
| 89 | |
} |
| 90 | |
} |
| 91 | |
|
| 92 | |
public boolean traverseDescendents(T file, int depth) throws FileSystemException { |
| 93 | 116 | this.file = file; |
| 94 | 116 | this.depth = depth; |
| 95 | |
try { |
| 96 | 116 | return selector.traverseDescendents(info); |
| 97 | 0 | } catch (FileSystemException e) { |
| 98 | 0 | throw e; |
| 99 | 0 | } catch (Exception e) { |
| 100 | 0 | throw (FileSystemException) new FileSystemException(e.getMessage()).initCause(e); |
| 101 | |
} |
| 102 | |
} |
| 103 | |
} |
| 104 | |
} |