|
| 1 | +/* |
| 2 | + * Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved. |
| 3 | + * |
| 4 | + * Sun Microsystems, Inc. has intellectual property rights relating to technology embodied in the product |
| 5 | + * that is described in this document. In particular, and without limitation, these intellectual property |
| 6 | + * rights may include one or more of the U.S. patents listed at http://www.sun.com/patents and one or |
| 7 | + * more additional patents or pending patent applications in the U.S. and in other countries. |
| 8 | + * |
| 9 | + * U.S. Government Rights - Commercial software. Government users are subject to the Sun |
| 10 | + * Microsystems, Inc. standard license agreement and applicable provisions of the FAR and its |
| 11 | + * supplements. |
| 12 | + * |
| 13 | + * Use is subject to license terms. Sun, Sun Microsystems, the Sun logo, Java and Solaris are trademarks or |
| 14 | + * registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries. All SPARC trademarks |
| 15 | + * are used under license and are trademarks or registered trademarks of SPARC International, Inc. in the |
| 16 | + * U.S. and other countries. |
| 17 | + * |
| 18 | + * UNIX is a registered trademark in the U.S. and other countries, exclusively licensed through X/Open |
| 19 | + * Company, Ltd. |
| 20 | + */ |
| 21 | +package com.sun.max.io; |
| 22 | + |
| 23 | +import java.io.*; |
| 24 | + |
| 25 | +/** |
| 26 | + * Provides a facility for walking a file system hierarchy similar to that provided by the Unix find(1) facility. |
| 27 | + * |
| 28 | + * @author Doug Simon |
| 29 | + */ |
| 30 | +public class FileTraversal { |
| 31 | + |
| 32 | + private boolean stopped; |
| 33 | + |
| 34 | + /** |
| 35 | + * Handles a standard file resource encountered during the traversal. |
| 36 | + * |
| 37 | + * @param file a file resource for which {@link File#isFile()} returns {@code true} |
| 38 | + */ |
| 39 | + protected void visitFile(File file) { |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Handles a directory encountered during the traversal. |
| 44 | + * |
| 45 | + * @param directory a file resource for which {@link File#isDirectory()} returns {@code true} |
| 46 | + * @return true if the traversal should process the file system hierarchy rooted at {@code directory}, false if it |
| 47 | + * should be skipped |
| 48 | + */ |
| 49 | + protected boolean visitDirectory(File directory) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Handles a file resource encountered during the traversal that is neither a standard file or directory. |
| 55 | + * |
| 56 | + * @param other a file resource for which neither {@link File#isFile()} nor {@link File#isDirectory()} returns |
| 57 | + * {@code true} |
| 58 | + */ |
| 59 | + protected void visitOther(File other) { |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Stops the traversal after the current file resource has been processed. This can be called from within an |
| 64 | + * overriding implementation of {@link #visitFile(File)}, {@link #visitDirectory(File)} or |
| 65 | + * {@link #visitOther(File)} to prematurely terminate a traversal. |
| 66 | + */ |
| 67 | + protected final void stop() { |
| 68 | + stopped = true; |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Traverses the file hierarchy rooted at a given file. The {@linkplain #wasStopped() stopped} status of this |
| 73 | + * traversal object is reset to {@code false} before the traversal begins. |
| 74 | + * |
| 75 | + * @param file the file or directory at which to start the traversal |
| 76 | + */ |
| 77 | + public void run(File file) { |
| 78 | + stopped = false; |
| 79 | + visit(file); |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Determines if the traversal was stopped before every file in the file hierarchy was traversed. |
| 84 | + */ |
| 85 | + public boolean wasStopped() { |
| 86 | + return stopped; |
| 87 | + } |
| 88 | + |
| 89 | + private boolean visit(File entry) { |
| 90 | + File subdirectoryToTraverse = null; |
| 91 | + if (entry.isFile()) { |
| 92 | + visitFile(entry); |
| 93 | + } else if (entry.isDirectory()) { |
| 94 | + if (visitDirectory(entry)) { |
| 95 | + subdirectoryToTraverse = entry; |
| 96 | + } |
| 97 | + } else { |
| 98 | + visitOther(entry); |
| 99 | + } |
| 100 | + if (stopped) { |
| 101 | + return false; |
| 102 | + } |
| 103 | + if (subdirectoryToTraverse != null) { |
| 104 | + traverse(subdirectoryToTraverse); |
| 105 | + if (stopped) { |
| 106 | + return false; |
| 107 | + } |
| 108 | + } |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | + private void traverse(File directory) { |
| 113 | + final File[] entries = directory.listFiles(); |
| 114 | + if (entries != null) { |
| 115 | + for (File entry : entries) { |
| 116 | + if (!visit(entry)) { |
| 117 | + return; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments