1 | /*
|
---|
2 | * Copyright 2003-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
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 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.taskdefs.optional.ssh;
|
---|
19 |
|
---|
20 | import java.util.ArrayList;
|
---|
21 | import java.util.Iterator;
|
---|
22 | import java.util.StringTokenizer;
|
---|
23 | import java.io.File;
|
---|
24 |
|
---|
25 | public class Directory {
|
---|
26 |
|
---|
27 | private File directory;
|
---|
28 | private ArrayList childDirectories;
|
---|
29 | private ArrayList files;
|
---|
30 | private Directory parent;
|
---|
31 |
|
---|
32 | public Directory(File directory) {
|
---|
33 | this(directory, null);
|
---|
34 | }
|
---|
35 |
|
---|
36 | public Directory(File directory , Directory parent) {
|
---|
37 | this.parent = parent;
|
---|
38 | this.childDirectories = new ArrayList();
|
---|
39 | this.files = new ArrayList();
|
---|
40 | this.directory = directory;
|
---|
41 | }
|
---|
42 |
|
---|
43 | public void addDirectory(Directory directory) {
|
---|
44 | if (!childDirectories.contains(directory)) {
|
---|
45 | childDirectories.add(directory);
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public void addFile(File file) {
|
---|
50 | files.add(file);
|
---|
51 | }
|
---|
52 |
|
---|
53 | public Iterator directoryIterator() {
|
---|
54 | return childDirectories.iterator();
|
---|
55 | }
|
---|
56 |
|
---|
57 | public Iterator filesIterator() {
|
---|
58 | return files.iterator();
|
---|
59 | }
|
---|
60 |
|
---|
61 | public Directory getParent() {
|
---|
62 | return parent;
|
---|
63 | }
|
---|
64 |
|
---|
65 | public boolean isRoot() {
|
---|
66 | return parent == null;
|
---|
67 | }
|
---|
68 |
|
---|
69 | public File getDirectory() {
|
---|
70 | return directory;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public Directory getChild(File dir) {
|
---|
74 | for (int i = 0; i < childDirectories.size(); i++) {
|
---|
75 | Directory current = (Directory) childDirectories.get(i);
|
---|
76 | if (current.getDirectory().equals(dir)) {
|
---|
77 | return current;
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | return null;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public boolean equals(Object obj) {
|
---|
85 | if (obj == this) {
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 |
|
---|
89 | if (!(obj instanceof Directory)) {
|
---|
90 | return false;
|
---|
91 | }
|
---|
92 |
|
---|
93 | Directory d = (Directory) obj;
|
---|
94 |
|
---|
95 | return this.directory.equals(d.directory);
|
---|
96 | }
|
---|
97 |
|
---|
98 | public int hashCode() {
|
---|
99 | return directory.hashCode();
|
---|
100 | }
|
---|
101 |
|
---|
102 | public String[] getPath() {
|
---|
103 | return getPath(directory.getAbsolutePath());
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static String[] getPath(String thePath) {
|
---|
107 | StringTokenizer tokenizer = new StringTokenizer(thePath,
|
---|
108 | File.separator);
|
---|
109 | String[] path = new String[ tokenizer.countTokens() ];
|
---|
110 |
|
---|
111 | int i = 0;
|
---|
112 | while (tokenizer.hasMoreTokens()) {
|
---|
113 | path[i] = tokenizer.nextToken();
|
---|
114 | i++;
|
---|
115 | }
|
---|
116 |
|
---|
117 | return path;
|
---|
118 | }
|
---|
119 |
|
---|
120 | public int fileSize() {
|
---|
121 | return files.size();
|
---|
122 | }
|
---|
123 | }
|
---|