annotate src/share/tools/ProjectCreator/DirectoryTree.java @ 1972:f95d63e2154a

6989984: Use standard include model for Hospot Summary: Replaced MakeDeps and the includeDB files with more standardized solutions. Reviewed-by: coleenp, kvn, kamg
author stefank
date Tue, 23 Nov 2010 13:22:55 -0800
parents src/share/tools/MakeDeps/DirectoryTree.java@c18cbe5936b8
children 15d6977f04b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
2 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 /** Encapsulates a notion of a directory tree. Designed to allow fast
a61af66fc99e Initial load
duke
parents:
diff changeset
26 querying of full paths for unique filenames in the hierarchy. */
a61af66fc99e Initial load
duke
parents:
diff changeset
27
a61af66fc99e Initial load
duke
parents:
diff changeset
28 import java.io.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
29 import java.util.*;
a61af66fc99e Initial load
duke
parents:
diff changeset
30
a61af66fc99e Initial load
duke
parents:
diff changeset
31 public class DirectoryTree {
a61af66fc99e Initial load
duke
parents:
diff changeset
32
a61af66fc99e Initial load
duke
parents:
diff changeset
33 /** The root of the read directoryTree */
a61af66fc99e Initial load
duke
parents:
diff changeset
34 private Node rootNode;
a61af66fc99e Initial load
duke
parents:
diff changeset
35
a61af66fc99e Initial load
duke
parents:
diff changeset
36 /** Subdirs to ignore; Vector of Strings */
a61af66fc99e Initial load
duke
parents:
diff changeset
37 private Vector subdirsToIgnore;
a61af66fc99e Initial load
duke
parents:
diff changeset
38
a61af66fc99e Initial load
duke
parents:
diff changeset
39 /** This maps file names to Lists of nodes. */
a61af66fc99e Initial load
duke
parents:
diff changeset
40 private Hashtable nameToNodeListTable;
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42 /** Output "."'s as directories are read. Defaults to false. */
a61af66fc99e Initial load
duke
parents:
diff changeset
43 private boolean verbose;
a61af66fc99e Initial load
duke
parents:
diff changeset
44
a61af66fc99e Initial load
duke
parents:
diff changeset
45 public DirectoryTree() {
a61af66fc99e Initial load
duke
parents:
diff changeset
46 subdirsToIgnore = new Vector();
a61af66fc99e Initial load
duke
parents:
diff changeset
47 verbose = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
48 }
a61af66fc99e Initial load
duke
parents:
diff changeset
49
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
50 public void addSubdirToIgnore(String subdir) {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
51 subdirsToIgnore.add(subdir);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
52 }
a61af66fc99e Initial load
duke
parents:
diff changeset
53
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
54 private class FileIterator implements Iterator {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
55 private Vector nodes = new Vector();
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
56
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
57 public FileIterator(Node rootNode) {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
58 nodes.add(rootNode);
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
59 prune();
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
60 }
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
61 public boolean hasNext() {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
62 return nodes.size() > 0;
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
63 }
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
64 public Object next() {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
65 Node last = (Node)nodes.remove(nodes.size() - 1);
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
66 prune();
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
67 return new File(last.getName());
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
68 }
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
69
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
70 public void remove() {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
71 throw new RuntimeException();
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
72 }
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
73
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
74 private void prune() {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
75 while (nodes.size() > 0) {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
76 Node last = (Node)nodes.get(nodes.size() - 1);
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
77
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
78 if (last.isDirectory()) {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
79 nodes.remove(nodes.size() - 1);
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
80 nodes.addAll(last.children);
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
81 } else {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
82 // Is at file
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
83 return;
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
84 }
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
85 }
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
86 }
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
87 }
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
88
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
89 public Iterator getFileIterator() {
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
90 return new FileIterator(rootNode);
0
a61af66fc99e Initial load
duke
parents:
diff changeset
91 }
a61af66fc99e Initial load
duke
parents:
diff changeset
92
a61af66fc99e Initial load
duke
parents:
diff changeset
93 /** Output "."'s to System.out as directories are read. Defaults
a61af66fc99e Initial load
duke
parents:
diff changeset
94 to false. */
a61af66fc99e Initial load
duke
parents:
diff changeset
95 public void setVerbose(boolean newValue) {
a61af66fc99e Initial load
duke
parents:
diff changeset
96 verbose = newValue;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 }
a61af66fc99e Initial load
duke
parents:
diff changeset
98
a61af66fc99e Initial load
duke
parents:
diff changeset
99 public boolean getVerbose() {
a61af66fc99e Initial load
duke
parents:
diff changeset
100 return verbose;
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 public String getRootNodeName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
104 return rootNode.getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
105 }
a61af66fc99e Initial load
duke
parents:
diff changeset
106
a61af66fc99e Initial load
duke
parents:
diff changeset
107 /** Takes an absolute path to the root directory of this
a61af66fc99e Initial load
duke
parents:
diff changeset
108 DirectoryTree. Throws IllegalArgumentException if the given
a61af66fc99e Initial load
duke
parents:
diff changeset
109 string represents a plain file or nonexistent directory. */
a61af66fc99e Initial load
duke
parents:
diff changeset
110
a61af66fc99e Initial load
duke
parents:
diff changeset
111 public void readDirectory(String baseDirectory)
a61af66fc99e Initial load
duke
parents:
diff changeset
112 throws IllegalArgumentException {
1972
f95d63e2154a 6989984: Use standard include model for Hospot
stefank
parents: 1552
diff changeset
113 File root = new File(Util.normalize(baseDirectory));
0
a61af66fc99e Initial load
duke
parents:
diff changeset
114 if (!root.isDirectory()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
115 throw new IllegalArgumentException("baseDirectory \"" +
a61af66fc99e Initial load
duke
parents:
diff changeset
116 baseDirectory +
a61af66fc99e Initial load
duke
parents:
diff changeset
117 "\" does not exist or " +
a61af66fc99e Initial load
duke
parents:
diff changeset
118 "is not a directory");
a61af66fc99e Initial load
duke
parents:
diff changeset
119 }
a61af66fc99e Initial load
duke
parents:
diff changeset
120 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
121 root = root.getCanonicalFile();
a61af66fc99e Initial load
duke
parents:
diff changeset
122 }
a61af66fc99e Initial load
duke
parents:
diff changeset
123 catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
124 throw new RuntimeException(e.toString());
a61af66fc99e Initial load
duke
parents:
diff changeset
125 }
a61af66fc99e Initial load
duke
parents:
diff changeset
126 rootNode = new Node(root);
a61af66fc99e Initial load
duke
parents:
diff changeset
127 readDirectory(rootNode, root);
a61af66fc99e Initial load
duke
parents:
diff changeset
128 }
a61af66fc99e Initial load
duke
parents:
diff changeset
129
a61af66fc99e Initial load
duke
parents:
diff changeset
130 /** Queries the DirectoryTree for a file or directory name. Takes
a61af66fc99e Initial load
duke
parents:
diff changeset
131 only the name of the file or directory itself (i.e., no parent
a61af66fc99e Initial load
duke
parents:
diff changeset
132 directory information should be in the passed name). Returns a
a61af66fc99e Initial load
duke
parents:
diff changeset
133 List of DirectoryTreeNodes specifying the full paths of all of
a61af66fc99e Initial load
duke
parents:
diff changeset
134 the files or directories of this name in the DirectoryTree.
a61af66fc99e Initial load
duke
parents:
diff changeset
135 Returns null if the directory tree has not been read from disk
a61af66fc99e Initial load
duke
parents:
diff changeset
136 yet or if the file was not found in the tree. */
a61af66fc99e Initial load
duke
parents:
diff changeset
137 public List findFile(String name) {
a61af66fc99e Initial load
duke
parents:
diff changeset
138 if (rootNode == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
139 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
140 }
a61af66fc99e Initial load
duke
parents:
diff changeset
141
a61af66fc99e Initial load
duke
parents:
diff changeset
142 if (nameToNodeListTable == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
143 nameToNodeListTable = new Hashtable();
a61af66fc99e Initial load
duke
parents:
diff changeset
144 try {
a61af66fc99e Initial load
duke
parents:
diff changeset
145 buildNameToNodeListTable(rootNode);
a61af66fc99e Initial load
duke
parents:
diff changeset
146 } catch (IOException e) {
a61af66fc99e Initial load
duke
parents:
diff changeset
147 e.printStackTrace();
a61af66fc99e Initial load
duke
parents:
diff changeset
148 return null;
a61af66fc99e Initial load
duke
parents:
diff changeset
149 }
a61af66fc99e Initial load
duke
parents:
diff changeset
150 }
a61af66fc99e Initial load
duke
parents:
diff changeset
151
a61af66fc99e Initial load
duke
parents:
diff changeset
152 return (List) nameToNodeListTable.get(name);
a61af66fc99e Initial load
duke
parents:
diff changeset
153 }
a61af66fc99e Initial load
duke
parents:
diff changeset
154
a61af66fc99e Initial load
duke
parents:
diff changeset
155 private void buildNameToNodeListTable(Node curNode)
a61af66fc99e Initial load
duke
parents:
diff changeset
156 throws IOException {
a61af66fc99e Initial load
duke
parents:
diff changeset
157 String fullName = curNode.getName();
a61af66fc99e Initial load
duke
parents:
diff changeset
158 String parent = curNode.getParent();
a61af66fc99e Initial load
duke
parents:
diff changeset
159 String separator = System.getProperty("file.separator");
a61af66fc99e Initial load
duke
parents:
diff changeset
160
a61af66fc99e Initial load
duke
parents:
diff changeset
161 if (parent != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
162 if (!fullName.startsWith(parent)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
163 throw new RuntimeException(
a61af66fc99e Initial load
duke
parents:
diff changeset
164 "Internal error: parent of file name \"" + fullName +
a61af66fc99e Initial load
duke
parents:
diff changeset
165 "\" does not match file name \"" + parent + "\""
a61af66fc99e Initial load
duke
parents:
diff changeset
166 );
a61af66fc99e Initial load
duke
parents:
diff changeset
167 }
a61af66fc99e Initial load
duke
parents:
diff changeset
168
a61af66fc99e Initial load
duke
parents:
diff changeset
169 int len = parent.length();
a61af66fc99e Initial load
duke
parents:
diff changeset
170 if (!parent.endsWith(separator)) {
a61af66fc99e Initial load
duke
parents:
diff changeset
171 len += separator.length();
a61af66fc99e Initial load
duke
parents:
diff changeset
172 }
a61af66fc99e Initial load
duke
parents:
diff changeset
173
a61af66fc99e Initial load
duke
parents:
diff changeset
174 String fileName = fullName.substring(len);
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 if (fileName == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 throw new RuntimeException(
a61af66fc99e Initial load
duke
parents:
diff changeset
178 "Internal error: file name was empty"
a61af66fc99e Initial load
duke
parents:
diff changeset
179 );
a61af66fc99e Initial load
duke
parents:
diff changeset
180 }
a61af66fc99e Initial load
duke
parents:
diff changeset
181
a61af66fc99e Initial load
duke
parents:
diff changeset
182 List nodeList = (List) nameToNodeListTable.get(fileName);
a61af66fc99e Initial load
duke
parents:
diff changeset
183 if (nodeList == null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
184 nodeList = new Vector();
a61af66fc99e Initial load
duke
parents:
diff changeset
185 nameToNodeListTable.put(fileName, nodeList);
a61af66fc99e Initial load
duke
parents:
diff changeset
186 }
a61af66fc99e Initial load
duke
parents:
diff changeset
187
a61af66fc99e Initial load
duke
parents:
diff changeset
188 nodeList.add(curNode);
a61af66fc99e Initial load
duke
parents:
diff changeset
189 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
190 if (curNode != rootNode) {
a61af66fc99e Initial load
duke
parents:
diff changeset
191 throw new RuntimeException(
a61af66fc99e Initial load
duke
parents:
diff changeset
192 "Internal error: parent of file + \"" + fullName + "\"" +
a61af66fc99e Initial load
duke
parents:
diff changeset
193 " was null"
a61af66fc99e Initial load
duke
parents:
diff changeset
194 );
a61af66fc99e Initial load
duke
parents:
diff changeset
195 }
a61af66fc99e Initial load
duke
parents:
diff changeset
196 }
a61af66fc99e Initial load
duke
parents:
diff changeset
197
a61af66fc99e Initial load
duke
parents:
diff changeset
198 if (curNode.isDirectory()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
199 Iterator iter = curNode.getChildren();
a61af66fc99e Initial load
duke
parents:
diff changeset
200 if (iter != null) {
a61af66fc99e Initial load
duke
parents:
diff changeset
201 while (iter.hasNext()) {
a61af66fc99e Initial load
duke
parents:
diff changeset
202 buildNameToNodeListTable((Node) iter.next());
a61af66fc99e Initial load
duke
parents:
diff changeset
203 }
a61af66fc99e Initial load
duke
parents:
diff changeset
204 }
a61af66fc99e Initial load
duke
parents:
diff changeset
205 }
a61af66fc99e Initial load
duke
parents:
diff changeset
206 }
a61af66fc99e Initial load
duke
parents:
diff changeset
207
a61af66fc99e Initial load
duke
parents:
diff changeset
208 /** Reads all of the files in the given directory and adds them as
a61af66fc99e Initial load
duke
parents:
diff changeset
209 children of the directory tree node. Requires that the passed
a61af66fc99e Initial load
duke
parents:
diff changeset
210 node represents a directory. */
a61af66fc99e Initial load
duke
parents:
diff changeset
211
a61af66fc99e Initial load
duke
parents:
diff changeset
212 private void readDirectory(Node parentNode, File parentDir) {
a61af66fc99e Initial load
duke
parents:
diff changeset
213 File[] children = parentDir.listFiles();
a61af66fc99e Initial load
duke
parents:
diff changeset
214 if (children == null)
a61af66fc99e Initial load
duke
parents:
diff changeset
215 return;
a61af66fc99e Initial load
duke
parents:
diff changeset
216 if (verbose) {
a61af66fc99e Initial load
duke
parents:
diff changeset
217 System.out.print(".");
a61af66fc99e Initial load
duke
parents:
diff changeset
218 System.out.flush();
a61af66fc99e Initial load
duke
parents:
diff changeset
219 }
a61af66fc99e Initial load
duke
parents:
diff changeset
220 for (int i = 0; i < children.length; i++) {
a61af66fc99e Initial load
duke
parents:
diff changeset
221 File child = children[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
222 children[i] = null;
a61af66fc99e Initial load
duke
parents:
diff changeset
223 boolean isDir = child.isDirectory();
a61af66fc99e Initial load
duke
parents:
diff changeset
224 boolean mustSkip = false;
a61af66fc99e Initial load
duke
parents:
diff changeset
225 if (isDir) {
a61af66fc99e Initial load
duke
parents:
diff changeset
226 for (Iterator iter = subdirsToIgnore.iterator();
a61af66fc99e Initial load
duke
parents:
diff changeset
227 iter.hasNext(); ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
228 if (child.getName().equals((String) iter.next())) {
a61af66fc99e Initial load
duke
parents:
diff changeset
229 mustSkip = true;
a61af66fc99e Initial load
duke
parents:
diff changeset
230 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
231 }
a61af66fc99e Initial load
duke
parents:
diff changeset
232 }
a61af66fc99e Initial load
duke
parents:
diff changeset
233 }
a61af66fc99e Initial load
duke
parents:
diff changeset
234 if (!mustSkip) {
a61af66fc99e Initial load
duke
parents:
diff changeset
235 Node childNode = new Node(child);
a61af66fc99e Initial load
duke
parents:
diff changeset
236 parentNode.addChild(childNode);
a61af66fc99e Initial load
duke
parents:
diff changeset
237 if (isDir) {
a61af66fc99e Initial load
duke
parents:
diff changeset
238 readDirectory(childNode, child);
a61af66fc99e Initial load
duke
parents:
diff changeset
239 }
a61af66fc99e Initial load
duke
parents:
diff changeset
240 }
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242 }
a61af66fc99e Initial load
duke
parents:
diff changeset
243
a61af66fc99e Initial load
duke
parents:
diff changeset
244 private class Node implements DirectoryTreeNode {
a61af66fc99e Initial load
duke
parents:
diff changeset
245 private File file;
a61af66fc99e Initial load
duke
parents:
diff changeset
246 private Vector children;
a61af66fc99e Initial load
duke
parents:
diff changeset
247
a61af66fc99e Initial load
duke
parents:
diff changeset
248 /** file must be a canonical file */
a61af66fc99e Initial load
duke
parents:
diff changeset
249 Node(File file) {
a61af66fc99e Initial load
duke
parents:
diff changeset
250 this.file = file;
a61af66fc99e Initial load
duke
parents:
diff changeset
251 children = new Vector();
a61af66fc99e Initial load
duke
parents:
diff changeset
252 }
a61af66fc99e Initial load
duke
parents:
diff changeset
253
a61af66fc99e Initial load
duke
parents:
diff changeset
254 public boolean isFile() {
a61af66fc99e Initial load
duke
parents:
diff changeset
255 return file.isFile();
a61af66fc99e Initial load
duke
parents:
diff changeset
256 }
a61af66fc99e Initial load
duke
parents:
diff changeset
257
a61af66fc99e Initial load
duke
parents:
diff changeset
258 public boolean isDirectory() {
a61af66fc99e Initial load
duke
parents:
diff changeset
259 return file.isDirectory();
a61af66fc99e Initial load
duke
parents:
diff changeset
260 }
a61af66fc99e Initial load
duke
parents:
diff changeset
261
a61af66fc99e Initial load
duke
parents:
diff changeset
262 public String getName() {
a61af66fc99e Initial load
duke
parents:
diff changeset
263 return file.getPath();
a61af66fc99e Initial load
duke
parents:
diff changeset
264 }
a61af66fc99e Initial load
duke
parents:
diff changeset
265
a61af66fc99e Initial load
duke
parents:
diff changeset
266 public String getParent() {
a61af66fc99e Initial load
duke
parents:
diff changeset
267 return file.getParent();
a61af66fc99e Initial load
duke
parents:
diff changeset
268 }
a61af66fc99e Initial load
duke
parents:
diff changeset
269
a61af66fc99e Initial load
duke
parents:
diff changeset
270 public void addChild(Node n) {
a61af66fc99e Initial load
duke
parents:
diff changeset
271 children.add(n);
a61af66fc99e Initial load
duke
parents:
diff changeset
272 }
a61af66fc99e Initial load
duke
parents:
diff changeset
273
a61af66fc99e Initial load
duke
parents:
diff changeset
274 public Iterator getChildren() throws IllegalArgumentException {
a61af66fc99e Initial load
duke
parents:
diff changeset
275 return children.iterator();
a61af66fc99e Initial load
duke
parents:
diff changeset
276 }
a61af66fc99e Initial load
duke
parents:
diff changeset
277
a61af66fc99e Initial load
duke
parents:
diff changeset
278 public int getNumChildren() throws IllegalArgumentException {
a61af66fc99e Initial load
duke
parents:
diff changeset
279 return children.size();
a61af66fc99e Initial load
duke
parents:
diff changeset
280 }
a61af66fc99e Initial load
duke
parents:
diff changeset
281
a61af66fc99e Initial load
duke
parents:
diff changeset
282 public DirectoryTreeNode getChild(int i)
a61af66fc99e Initial load
duke
parents:
diff changeset
283 throws IllegalArgumentException, ArrayIndexOutOfBoundsException {
a61af66fc99e Initial load
duke
parents:
diff changeset
284 return (DirectoryTreeNode) children.get(i);
a61af66fc99e Initial load
duke
parents:
diff changeset
285 }
a61af66fc99e Initial load
duke
parents:
diff changeset
286 }
a61af66fc99e Initial load
duke
parents:
diff changeset
287 }