01: /*
02: * Copyright 2005 Joe Walker
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16: package org.directwebremoting.fsguide;
17:
18: import java.io.File;
19: import java.util.Arrays;
20: import java.util.Comparator;
21:
22: /**
23: * @author Joe Walker [joe at getahead dot ltd dot uk]
24: */
25: public class FileSystemGuide {
26: /**
27: * @param root
28: */
29: public FileSystemGuide(File root) {
30: this .root = root;
31: }
32:
33: /**
34: * @param visitor
35: */
36: public void visit(Visitor visitor) {
37: visitChild(root, visitor);
38: }
39:
40: /**
41: * @param directory
42: * @param visitor
43: */
44: private void visitDirectory(File directory, Visitor visitor) {
45: File[] files = directory.listFiles();
46:
47: // Sort the list of files
48: Arrays.sort(files, new Comparator<File>() {
49: public int compare(File o1, File o2) {
50: return o1.getName().compareTo(o2.getName());
51: }
52: });
53:
54: // Visit all the files and directories
55: for (File child : files) {
56: visitChild(child, visitor);
57: }
58: }
59:
60: /**
61: * @param child
62: * @param visitor
63: */
64: private void visitChild(File child, Visitor visitor) {
65: if (child.isDirectory()) {
66: if (visitor.visitDirectory(child)) {
67: visitDirectory(child, visitor);
68: }
69: } else {
70: visitor.visitFile(child);
71: }
72: }
73:
74: private File root;
75: }
|