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. package org.apache.tools.ant.taskdefs.optional.ssh;
  18. import java.util.ArrayList;
  19. import java.util.Iterator;
  20. import java.util.StringTokenizer;
  21. import java.io.File;
  22. public class Directory {
  23. private File directory;
  24. private ArrayList childDirectories;
  25. private ArrayList files;
  26. private Directory parent;
  27. public Directory(File directory) {
  28. this(directory, null);
  29. }
  30. public Directory(File directory , Directory parent) {
  31. this.parent = parent;
  32. this.childDirectories = new ArrayList();
  33. this.files = new ArrayList();
  34. this.directory = directory;
  35. }
  36. public void addDirectory(Directory directory) {
  37. if (!childDirectories.contains(directory)) {
  38. childDirectories.add(directory);
  39. }
  40. }
  41. public void addFile(File file) {
  42. files.add(file);
  43. }
  44. public Iterator directoryIterator() {
  45. return childDirectories.iterator();
  46. }
  47. public Iterator filesIterator() {
  48. return files.iterator();
  49. }
  50. public Directory getParent() {
  51. return parent;
  52. }
  53. public boolean isRoot() {
  54. return parent == null;
  55. }
  56. public File getDirectory() {
  57. return directory;
  58. }
  59. public Directory getChild(File dir) {
  60. for (int i = 0; i < childDirectories.size(); i++) {
  61. Directory current = (Directory) childDirectories.get(i);
  62. if (current.getDirectory().equals(dir)) {
  63. return current;
  64. }
  65. }
  66. return null;
  67. }
  68. public boolean equals(Object obj) {
  69. if (obj == this) {
  70. return true;
  71. }
  72. if (!(obj instanceof Directory)) {
  73. return false;
  74. }
  75. Directory d = (Directory) obj;
  76. return this.directory.equals(d.directory);
  77. }
  78. public int hashCode() {
  79. return directory.hashCode();
  80. }
  81. public String[] getPath() {
  82. return getPath(directory.getAbsolutePath());
  83. }
  84. public static String[] getPath(String thePath) {
  85. StringTokenizer tokenizer = new StringTokenizer(thePath,
  86. File.separator);
  87. String[] path = new String[ tokenizer.countTokens() ];
  88. int i = 0;
  89. while (tokenizer.hasMoreTokens()) {
  90. path[i] = tokenizer.nextToken();
  91. i++;
  92. }
  93. return path;
  94. }
  95. public int fileSize() {
  96. return files.size();
  97. }
  98. }