new icons
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / mover / DirectoryWalker.java
1 package net.sourceforge.phpeclipse.mover;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 public class DirectoryWalker {
7
8   protected IMover[] fMover;
9   protected IFilter[] fFilter;
10   /**
11    * creates a new DirectoryWalker
12    * mover and filter array should have the same length !
13    */
14   public DirectoryWalker(IMover[] mover, IFilter[] filter) {
15     this.fMover = mover;
16     this.fFilter = filter;
17   }
18
19   /**
20    * creates a new DirectoryWalker with a IFilter.DEFAULT_FILTER
21    */
22   public DirectoryWalker(IMover[] mover) {
23     this.fMover = mover;
24     this.fFilter = new IFilter[mover.length];
25     for (int i = 0; i < mover.length; i++) {
26       this.fFilter[i] = IFilter.DEFAULT_FILTER;
27     }
28   }
29   /**
30    * walks through the source directory, processing files as it
31    * goes along to create the target directory
32    * @param source source directory
33    * @param target target directory
34    * @throws IOException error with the file system
35    * @throws XMException error caught by the application
36    */
37   public void walk(String source, String target) throws IOException {
38     try {
39
40       walk(new File(source), new File(target));
41     } finally {
42
43     }
44   }
45
46   /**
47    * actual implementation of the walking
48    * @param source source directory
49    * @param target target directory
50    * @throws IOException error with the file system
51    * @return true if walking should continue, false if the messenger
52    *    has asked for the end of the walking
53    */
54   protected boolean walk(File source, File target) throws IOException {
55
56     if (!(target.exists() && target.isDirectory()))
57       if (!target.mkdirs())
58         return false;
59
60     for (int j = 0; j < fMover.length; j++) {
61       File[] dirs;
62       File[] docs;
63       int idirs = 0, idocs = 0;
64       if (source.isDirectory()) {
65         File[] files = source.listFiles();
66         dirs = new File[files.length];
67         docs = new File[files.length];
68         String fileName;
69
70         for (int i = 0; i < files.length; i++) {
71           if (files[i].isDirectory()) {
72             if (fFilter[j].isDirectoryOk(files[i])) {
73               dirs[idirs++] = files[i];
74             }
75           } else if (files[i].isFile()) {
76             if (fFilter[j].isFileOk(files[i])) {
77               docs[idocs++] = files[i];
78             }
79           } else
80             return false;
81         }
82       } else {
83         dirs = new File[0];
84         docs = new File[1];
85         docs[0] = source;
86         idocs = 1;
87       }
88
89       for (int i = 0; i < idocs; i++) {
90         System.out.println(docs[i].getAbsolutePath());
91
92         File result = fMover[j].move(docs[i], target);
93       }
94
95       System.out.println("directories");
96       for (int i = 0; i < idirs; i++) {
97         System.out.println(dirs[i].getAbsolutePath());
98         if (!walk(dirs[i], new File(target, dirs[i].getName())))
99           return false;
100       }
101     }
102
103     return true;
104   }
105 }