KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > optional > ssh > Directory


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs.optional.ssh;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.StringTokenizer JavaDoc;
24 import java.io.File JavaDoc;
25
26 /**
27  * A helper object for Scp representing a directory in a file system.
28  */

29 public class Directory {
30
31     private File JavaDoc directory;
32     private ArrayList JavaDoc childDirectories;
33     private ArrayList JavaDoc files;
34     private Directory parent;
35
36     /**
37      * Constructor for a Directory.
38      * @param directory a directory.
39      */

40     public Directory(File JavaDoc directory) {
41         this(directory, null);
42     }
43
44     /**
45      * Constructor for a Directory.
46      * @param directory a directory
47      * @param parent a parent Directory
48      */

49     public Directory(File JavaDoc directory , Directory parent) {
50         this.parent = parent;
51         this.childDirectories = new ArrayList JavaDoc();
52         this.files = new ArrayList JavaDoc();
53         this.directory = directory;
54     }
55
56     /**
57      * Add a directory to the child directories.
58      * @param directory a Directory
59      */

60     public void addDirectory(Directory directory) {
61         if (!childDirectories.contains(directory)) {
62             childDirectories.add(directory);
63         }
64     }
65
66     /**
67      * Add a file to the list of files.
68      * @param file a file to add
69      */

70     public void addFile(File JavaDoc file) {
71         files.add(file);
72     }
73
74     /**
75      * Get an iterator over the child Directories.
76      * @return an iterator
77      */

78     public Iterator JavaDoc directoryIterator() {
79         return childDirectories.iterator();
80     }
81
82     /**
83      * Get an iterator over the files.
84      * @return an iterator
85      */

86     public Iterator JavaDoc filesIterator() {
87         return files.iterator();
88     }
89
90     /**
91      * Get the parent Directory.
92      * @return the parent Directory.
93      */

94     public Directory getParent() {
95         return parent;
96     }
97
98     /**
99      * Is this a root Directory?
100      * @return true if there is no parent Directory
101      */

102     public boolean isRoot() {
103         return parent == null;
104     }
105
106     /**
107      * Get the directory file.
108      * @return the directory file
109      */

110     public File JavaDoc getDirectory() {
111         return directory;
112     }
113
114     /**
115      * Get a child directory of this directory.
116      * @param dir the directory to look for
117      * @return the child directory, or null if not found
118      */

119     public Directory getChild(File JavaDoc dir) {
120         for (int i = 0; i < childDirectories.size(); i++) {
121             Directory current = (Directory) childDirectories.get(i);
122             if (current.getDirectory().equals(dir)) {
123                 return current;
124             }
125         }
126
127         return null;
128     }
129
130     /**
131      * The equality method.
132      * This checks if the directory field is the same.
133      * @param obj the object to compare to
134      * @return true if this object has an equal directory field as the other object
135      */

136     public boolean equals(Object JavaDoc obj) {
137         if (obj == this) {
138             return true;
139         }
140
141         if (!(obj instanceof Directory)) {
142             return false;
143         }
144
145         Directory d = (Directory) obj;
146
147         return this.directory.equals(d.directory);
148     }
149
150     /**
151      * The hashcode method.
152      * @return the hash code of the directory field
153      */

154     public int hashCode() {
155         return directory.hashCode();
156     }
157
158     /**
159      * Get the path components of this directory.
160      * @return the path components as an array of strings.
161      */

162     public String JavaDoc[] getPath() {
163         return getPath(directory.getAbsolutePath());
164     }
165
166     /**
167      * Convert a file path to an array of path components.
168      * This uses File.sepatator to split the file path string.
169      * @param thePath the file path string to convert
170      * @return an array of path components
171      */

172     public static String JavaDoc[] getPath(String JavaDoc thePath) {
173         StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(thePath,
174                 File.separator);
175         String JavaDoc[] path = new String JavaDoc[ tokenizer.countTokens() ];
176
177         int i = 0;
178         while (tokenizer.hasMoreTokens()) {
179             path[i] = tokenizer.nextToken();
180             i++;
181         }
182
183         return path;
184     }
185
186     /**
187      * Get the number of files in the files attribute.
188      * @return the number of files
189      */

190     public int fileSize() {
191         return files.size();
192     }
193 }
194
Popular Tags