KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > project > DirectoryLister


1 /*********************************************************************************
2  * The contents of this file are subject to the OpenI Public License Version 1.0
3  * ("License"); You may not use this file except in compliance with the
4  * License. You may obtain a copy of the License at
5  * http://www.openi.org/docs/LICENSE.txt
6  *
7  * Software distributed under the License is distributed on an "AS IS" basis,
8  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
9  * the specific language governing rights and limitations under the License.
10  *
11  * The Original Code is: OpenI Open Source
12  *
13  * The Initial Developer of the Original Code is Loyalty Matrix, Inc.
14  * Portions created by Loyalty Matrix, Inc. are
15  * Copyright (C) 2005 Loyalty Matrix, Inc.; All Rights Reserved.
16  *
17  * Contributor(s): ______________________________________.
18  *
19  ********************************************************************************/

20 package org.openi.project;
21
22 import org.apache.log4j.Logger;
23 import org.openi.util.FileItem;
24 import org.openi.util.Folder;
25 import org.openi.util.NameValue;
26 import org.openi.util.NameValueType;
27 import java.io.File JavaDoc;
28 import java.io.IOException JavaDoc;
29 import java.util.ArrayList JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.LinkedList JavaDoc;
32 import java.util.List JavaDoc;
33
34
35
36 /**
37  * @author plucas
38  *
39  * TODO : refactor
40  */

41 public class DirectoryLister {
42     private static Logger logger = Logger.getLogger(DirectoryLister.class);
43
44     /**
45      * Makes list of all folders starting from 'directory'
46      *
47      * @param directory String
48      * @return List
49      */

50     public static List JavaDoc findProjectDirectories(String JavaDoc directory) {
51         return findProjectDirectories(directory, null);
52     }
53
54     /**
55      * Makes list of all folders starting from 'directory'
56      *
57      * @param directory String
58      * @return List
59      */

60     public static List JavaDoc findProjectDirectories(String JavaDoc directory, List JavaDoc modules) {
61         List JavaDoc dirlist = new LinkedList JavaDoc();
62
63         try {
64             int depth = 0;
65
66             if (modules != null) {
67                 Object JavaDoc[] dirs = modules.toArray();
68
69                 for (int i = 0; i < dirs.length; i++) {
70                     String JavaDoc path = directory + "/" + (String JavaDoc) dirs[i];
71                     recurse(depth, dirlist, new File JavaDoc(path));
72                 }
73             } else {
74                 recurse(depth, dirlist, new File JavaDoc(directory));
75             }
76         } catch (IOException JavaDoc e) {
77             logger.error(e);
78         }
79
80         return dirlist;
81     }
82
83     /**
84      * Recurses folder. This method doesn't include files.
85      *
86      * @param depth int
87      * @param dirs List
88      * @param parent File
89      * @throws IOException
90      */

91     private static void recurse(int depth, List JavaDoc dirs, File JavaDoc parent)
92         throws IOException JavaDoc {
93         ++depth;
94
95         if (parent.isDirectory()) {
96             dirs.add(new NameValue(prependName(depth, "-") + parent.getName(),
97                     parent.getCanonicalPath()));
98
99             File JavaDoc[] children = parent.listFiles();
100             java.util.Arrays.sort(children);
101
102             for (int i = 0; i < children.length; i++) {
103                 recurse(depth, dirs, children[i]);
104             }
105         }
106     }
107
108     /**
109      * Builds All available folders and files List avilable on module list
110      * as Folder and FileItem object respectively
111      *
112      * @param directory String
113      * @param modules List
114      * @param includeEmptyFolder boolean
115      * @return Folder
116      */

117     public static Folder buildFolderList(String JavaDoc directory, List JavaDoc modules,
118         boolean includeEmptyFolder) {
119         Folder root = new Folder();
120         root.setDisplayName("root");
121
122         File JavaDoc dir = new File JavaDoc(directory);
123         List JavaDoc dirs = new ArrayList JavaDoc();
124
125         try {
126             String JavaDoc rootPath = dir.getCanonicalPath();
127             Object JavaDoc[] moduleList = modules.toArray();
128
129             for (int i = 0; i < moduleList.length; i++) {
130                 String JavaDoc path = rootPath + "/" + (String JavaDoc) moduleList[i];
131                 dir = new File JavaDoc(path);
132
133                 if (dir.exists()) {
134                     recurse(rootPath, dirs, dir, includeEmptyFolder);
135                 }
136             }
137
138             root.setChildren(dirs);
139         } catch (IOException JavaDoc ex) {
140             logger.error(ex);
141         }
142
143         return root;
144     }
145
146     private static void recurse(String JavaDoc rootPath, List JavaDoc dirs, File JavaDoc dir,
147         boolean includeEmptyFolder) {
148         File JavaDoc[] files = dir.listFiles();
149         String JavaDoc path = null;
150
151         try {
152             path = dir.getCanonicalPath().substring(rootPath.length());
153         } catch (IOException JavaDoc ex) {
154             logger.error(ex);
155         }
156
157         if (dir.isFile()) {
158             FileItem file = new FileItem();
159             file.setPath(path);
160             file.setDisplayName(dir.getName());
161             dirs.add(file);
162         } else if (dir.isDirectory() && (files.length == 0)) {
163             if (includeEmptyFolder) {
164                 Folder folder = new Folder();
165                 folder.setPath(path);
166                 folder.setDisplayName(dir.getName());
167                 dirs.add(folder);
168             }
169         } else {
170             Folder folder = new Folder();
171             folder.setPath(path);
172             folder.setDisplayName(dir.getName());
173
174             dirs.add(folder);
175
176             List JavaDoc childs = new ArrayList JavaDoc();
177
178             for (int i = 0; i < files.length; i++) {
179                 recurse(rootPath, childs, files[i], includeEmptyFolder);
180             }
181
182             folder.setChildren(childs);
183         }
184     }
185
186     /**
187      * This method creates list of public and private modules defined in
188      * project configuration. Also checks user's permission.
189      * File/directory path is relative to project.
190      *
191      * @param directory String
192      * @param includeFiles boolean
193      * @param prepend String
194      * @param includeEmptyFolder boolean
195      * @return List
196      */

197     public static List JavaDoc findProjectDirectories(String JavaDoc directory, List JavaDoc modules,
198         boolean includeFiles, String JavaDoc prepend, boolean includeEmptyFolder) {
199         File JavaDoc dir = new File JavaDoc(directory);
200         List JavaDoc dirs = new LinkedList JavaDoc();
201         int depth = 1;
202         String JavaDoc rootPath = null;
203
204         try {
205             rootPath = dir.getCanonicalPath();
206
207             Object JavaDoc[] moduleList = modules.toArray();
208
209             for (int i = 0; i < moduleList.length; i++) {
210                 String JavaDoc path = rootPath + "/" + (String JavaDoc) moduleList[i];
211                 dir = new File JavaDoc(path);
212
213                 if (dir.exists()) {
214                     recurse(rootPath, depth, dirs, dir, prepend,
215                         includeEmptyFolder);
216                 }
217             }
218         } catch (IOException JavaDoc ex) {
219             logger.error(ex);
220         }
221
222         //if (dir.isDirectory()) {
223
// recurse(rootPath, depth, dirs, dir, prepend, includeEmptyFolder);
224
//}
225
return dirs;
226     }
227
228     private static void recurse(String JavaDoc rootPath, int depth, List JavaDoc dirs,
229         File JavaDoc dir, String JavaDoc prepend, boolean includeEmptyFolder) {
230         File JavaDoc[] files = dir.listFiles();
231         String JavaDoc path = null;
232
233         try {
234             path = dir.getCanonicalPath().substring(rootPath.length());
235         } catch (IOException JavaDoc ex) {
236             logger.error(ex);
237         }
238
239         if (dir.isFile()) {
240             dirs.add(new NameValueType(dir.getName(), path, "file",
241                     prependName(depth, prepend)));
242         } else if (dir.isDirectory() && (files.length == 0)) {
243             if (includeEmptyFolder) {
244                 dirs.add(new NameValueType(dir.getName(), path, "folder",
245                         prependName(depth, prepend)));
246             }
247         } else {
248             //if(depth!=0)
249
dirs.add(new NameValueType(dir.getName(), path, "folder",
250                     prependName(depth, prepend)));
251
252             for (int i = 0; i < files.length; i++) {
253                 recurse(rootPath, depth + 1, dirs, files[i], prepend,
254                     includeEmptyFolder);
255             }
256         }
257     }
258
259     /**
260      * Returns prepend text.
261      *
262      *
263      * @param depth int
264      * @param prepend String
265      * @return String
266      */

267     private static String JavaDoc prependName(int depth, String JavaDoc prepend) {
268         String JavaDoc newName = "";
269
270         for (int i = 1; i < depth; i++) {
271             newName += prepend;
272         }
273
274         return newName;
275     }
276
277     /**
278      * Builds list of project root's sub directory
279      *
280      * @param projectroot String
281      * @return List
282      */

283     public static List JavaDoc buildProjectRootSubDirList(String JavaDoc projectroot) {
284         List JavaDoc folders = new ArrayList JavaDoc();
285         File JavaDoc root = new File JavaDoc(projectroot);
286
287         if (root.exists() && root.isDirectory()) {
288             File JavaDoc[] childs = root.listFiles();
289
290             for (int i = 0; i < childs.length; i++) {
291                 if (childs[i].isDirectory()) {
292                     folders.add("/" + childs[i].getName());
293                 }
294             }
295         }
296
297         return folders;
298     }
299
300     /**
301      * builds file path list filtered by extension
302      * @param projectDir String
303      * @param modules List
304      * @param fileext String
305      * @return List
306      */

307     public static List JavaDoc buildFileListByExtension(String JavaDoc projectDir, List JavaDoc modules, String JavaDoc fileext) {
308         Folder folder;
309         folder = buildFolderList(projectDir, modules, false);
310         List JavaDoc allfiles = new ArrayList JavaDoc();
311         Folder.buildChildList(folder, null, allfiles);
312         Iterator JavaDoc iter = allfiles.iterator();
313         List JavaDoc files = new ArrayList JavaDoc();
314
315         while (iter.hasNext()) {
316             String JavaDoc path = (String JavaDoc) iter.next();
317             if (path != null) {
318                 path = path.replace('\\', '/');
319             }
320             if (path.startsWith("/")) {
321                 path = path.substring(1);
322             }
323             if (path.toLowerCase().endsWith(fileext.toLowerCase())) {
324                 files.add(path);
325             }
326         }
327         return files;
328     }
329
330 }
331
Popular Tags