KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openi > util > Folder


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.util;
21
22 import org.apache.log4j.Logger;
23 import org.openi.web.controller.admin.ManageFilesFormController;
24 import java.util.*;
25 import java.util.Collection JavaDoc;
26
27
28 public class Folder {
29     private static Logger logger = Logger.getLogger(Folder.class);
30     private String JavaDoc path = "";
31     private String JavaDoc displayName = "";
32     private Collection JavaDoc children = null;
33
34     public String JavaDoc getPath() {
35         return path;
36     }
37
38     public String JavaDoc getDisplayName() {
39         return displayName;
40     }
41
42     public Collection JavaDoc getChildren() {
43         return children;
44     }
45
46     public void setPath(String JavaDoc path) {
47         this.path = path;
48     }
49
50     public void setDisplayName(String JavaDoc displayName) {
51         this.displayName = displayName;
52     }
53
54     public void setChildren(Collection JavaDoc children) {
55         this.children = children;
56     }
57
58     public static Folder findChildFolder(String JavaDoc path, Folder folder) {
59         Folder current = folder;
60         path = path.replace('\\', '/');
61         logger.info("checking:" + folder.getPath());
62
63         if (path.equals(current.getPath().replace('\\', '/'))) {
64             logger.info("found path:" + path + " at folder:"
65                 + current.getPath());
66
67             return current;
68         }
69
70         if ((current.getChildren() == null)
71                 || (current.getChildren().size() == 0)) {
72             return null;
73         }
74
75         Iterator iter = current.getChildren().iterator();
76
77         while (iter.hasNext()) {
78             Object JavaDoc item = iter.next();
79
80             if (item instanceof Folder) {
81                 Folder fol = findChildFolder(path, (Folder) item);
82
83                 if (fol != null) {
84                     return fol;
85                 }
86             }
87         }
88
89         return null;
90     }
91
92     public static void buildChildList(Folder folder, List folders, List files) {
93         if (folder == null) {
94             return;
95         }
96
97         if ((folder.getChildren() == null)
98                 || (folder.getChildren().size() == 0)) {
99             return;
100         }
101
102         Iterator iter = folder.getChildren().iterator();
103
104         while (iter.hasNext()) {
105             Object JavaDoc item = iter.next();
106
107             if ((files != null) && item instanceof FileItem) {
108                 files.add(((FileItem) item).getPath());
109             } else if (item instanceof Folder) {
110                 Folder current = (Folder) item;
111
112                 if (folders != null) {
113                     folders.add(current.getPath());
114                 }
115
116                 buildChildList(current, folders, files);
117             }
118         }
119     }
120 }
121
Popular Tags