1 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 ; 28 import java.io.IOException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 import java.util.LinkedList ; 32 import java.util.List ; 33 34 35 36 41 public class DirectoryLister { 42 private static Logger logger = Logger.getLogger(DirectoryLister.class); 43 44 50 public static List findProjectDirectories(String directory) { 51 return findProjectDirectories(directory, null); 52 } 53 54 60 public static List findProjectDirectories(String directory, List modules) { 61 List dirlist = new LinkedList (); 62 63 try { 64 int depth = 0; 65 66 if (modules != null) { 67 Object [] dirs = modules.toArray(); 68 69 for (int i = 0; i < dirs.length; i++) { 70 String path = directory + "/" + (String ) dirs[i]; 71 recurse(depth, dirlist, new File (path)); 72 } 73 } else { 74 recurse(depth, dirlist, new File (directory)); 75 } 76 } catch (IOException e) { 77 logger.error(e); 78 } 79 80 return dirlist; 81 } 82 83 91 private static void recurse(int depth, List dirs, File parent) 92 throws IOException { 93 ++depth; 94 95 if (parent.isDirectory()) { 96 dirs.add(new NameValue(prependName(depth, "-") + parent.getName(), 97 parent.getCanonicalPath())); 98 99 File [] 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 117 public static Folder buildFolderList(String directory, List modules, 118 boolean includeEmptyFolder) { 119 Folder root = new Folder(); 120 root.setDisplayName("root"); 121 122 File dir = new File (directory); 123 List dirs = new ArrayList (); 124 125 try { 126 String rootPath = dir.getCanonicalPath(); 127 Object [] moduleList = modules.toArray(); 128 129 for (int i = 0; i < moduleList.length; i++) { 130 String path = rootPath + "/" + (String ) moduleList[i]; 131 dir = new File (path); 132 133 if (dir.exists()) { 134 recurse(rootPath, dirs, dir, includeEmptyFolder); 135 } 136 } 137 138 root.setChildren(dirs); 139 } catch (IOException ex) { 140 logger.error(ex); 141 } 142 143 return root; 144 } 145 146 private static void recurse(String rootPath, List dirs, File dir, 147 boolean includeEmptyFolder) { 148 File [] files = dir.listFiles(); 149 String path = null; 150 151 try { 152 path = dir.getCanonicalPath().substring(rootPath.length()); 153 } catch (IOException 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 childs = new ArrayList (); 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 197 public static List findProjectDirectories(String directory, List modules, 198 boolean includeFiles, String prepend, boolean includeEmptyFolder) { 199 File dir = new File (directory); 200 List dirs = new LinkedList (); 201 int depth = 1; 202 String rootPath = null; 203 204 try { 205 rootPath = dir.getCanonicalPath(); 206 207 Object [] moduleList = modules.toArray(); 208 209 for (int i = 0; i < moduleList.length; i++) { 210 String path = rootPath + "/" + (String ) moduleList[i]; 211 dir = new File (path); 212 213 if (dir.exists()) { 214 recurse(rootPath, depth, dirs, dir, prepend, 215 includeEmptyFolder); 216 } 217 } 218 } catch (IOException ex) { 219 logger.error(ex); 220 } 221 222 return dirs; 226 } 227 228 private static void recurse(String rootPath, int depth, List dirs, 229 File dir, String prepend, boolean includeEmptyFolder) { 230 File [] files = dir.listFiles(); 231 String path = null; 232 233 try { 234 path = dir.getCanonicalPath().substring(rootPath.length()); 235 } catch (IOException 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 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 267 private static String prependName(int depth, String prepend) { 268 String newName = ""; 269 270 for (int i = 1; i < depth; i++) { 271 newName += prepend; 272 } 273 274 return newName; 275 } 276 277 283 public static List buildProjectRootSubDirList(String projectroot) { 284 List folders = new ArrayList (); 285 File root = new File (projectroot); 286 287 if (root.exists() && root.isDirectory()) { 288 File [] 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 307 public static List buildFileListByExtension(String projectDir, List modules, String fileext) { 308 Folder folder; 309 folder = buildFolderList(projectDir, modules, false); 310 List allfiles = new ArrayList (); 311 Folder.buildChildList(folder, null, allfiles); 312 Iterator iter = allfiles.iterator(); 313 List files = new ArrayList (); 314 315 while (iter.hasNext()) { 316 String path = (String ) 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 |