1 22 23 package org.meshcms.util; 24 25 import java.io.*; 26 import java.util.*; 27 28 49 public class DirectoryParser extends Thread { 50 protected File initialDir; 51 52 protected boolean recursive = false; 53 protected boolean processStartDir = false; 54 protected boolean processDirBeforeContent = true; 55 56 private Comparator comparator; 57 58 63 public void setRecursive(boolean recursive) { 64 this.recursive = recursive; 65 } 66 67 75 public void setProcessStartDir(boolean processStartDir) { 76 this.processStartDir = processStartDir; 77 } 78 79 86 public void setProcessDirBeforeContent(boolean processDirBeforeContent) { 87 this.processDirBeforeContent = processDirBeforeContent; 88 } 89 90 95 public boolean isRecursive() { 96 return recursive; 97 } 98 99 106 public boolean isProcessStartDir() { 107 return processStartDir; 108 } 109 110 119 public boolean isProcessDirBeforeContent() { 120 return processDirBeforeContent; 121 } 122 123 129 public void setSorted(boolean sorted) { 130 if (sorted) { 131 comparator = new FileNameComparator(); 132 } else { 133 comparator = null; 134 } 135 } 136 137 144 public boolean isSorted() { 145 return comparator != null; 146 } 147 148 154 public void setInitialDir(String dir) { 155 setInitialDir(Utils.isNullOrEmpty(dir) ? null : new File(dir)); 156 } 157 158 163 public void setInitialDir(File dir) { 164 initialDir = dir; 165 } 166 167 170 public File getInitialDir() { 171 return initialDir; 172 } 173 174 177 public void run() { 178 process(); 179 } 180 181 184 public void process() { 185 if (initialDir == null || !initialDir.exists()) { 186 return; 187 } 188 189 if (preProcess()) { 190 parse(initialDir, Path.ROOT); 191 } 192 193 postProcess(); 194 } 195 196 private void parse(File file, Path path) { 197 if (file.isDirectory()) { 198 if (recursive || path.getElementCount() == 0) { 199 boolean ok = true; 200 201 if (mustProcessDir(path)) { 202 ok = preProcessDirectory(file, path); 203 204 if (processDirBeforeContent) { 206 ok &= processDirectory(file, path); 207 } 208 } 209 210 if (ok) { 211 File[] list = file.listFiles(); 212 213 if (comparator != null) { 214 Arrays.sort(list, comparator); 215 } 216 217 for (int i = 0; i < list.length; i++) { 218 parse(list[i], path.add(list[i].getName())); 219 } 220 } 221 } 222 223 if (mustProcessDir(path)) { 224 postProcessDirectory(file, path); 225 226 if (!processDirBeforeContent) { 228 processDirectory(file, path); 229 } 230 } 231 } else if (file.isFile()) { 232 processFile(file, path); 233 } 234 } 235 236 private boolean mustProcessDir(Path path) { 237 return processStartDir || path.getElementCount() != 0; 238 } 239 240 248 protected boolean preProcess() { 249 return true; 250 } 251 252 258 protected void postProcess() { 259 } 260 261 274 protected boolean processDirectory(File file, Path path) { 275 return true; 276 } 277 278 protected boolean preProcessDirectory(File file, Path path) { 279 return true; 280 } 281 282 protected void postProcessDirectory(File file, Path path) { 283 } 284 285 292 protected void processFile(File file, Path path) { 293 } 294 } 295 | Popular Tags |