comparison flys-backend/src/main/java/de/intevation/flys/utils/FileTools.java @ 508:a9c7f6ec3a5a 2.3.1

merged flys-backend/2.3.1
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:12 +0200
parents 31895d24387e
children c7370734b872
comparison
equal deleted inserted replaced
462:ebf049a1eb53 508:a9c7f6ec3a5a
1 package de.intevation.flys.utils;
2
3 import java.io.File;
4
5 import java.util.Stack;
6
7 import org.apache.log4j.Logger;
8
9 public class FileTools
10 {
11 private static Logger log = Logger.getLogger(FileTools.class);
12
13 private FileTools() {
14 }
15
16 public static File repair(File file) {
17 file = file.getAbsoluteFile();
18 if (file.exists()) {
19 return file;
20 }
21 Stack<String> parts = new Stack<String>();
22 File curr = file;
23 while (curr != null) {
24 String name = curr.getName();
25 if (name.length() > 0) {
26 parts.push(curr.getName());
27 }
28 curr = curr.getParentFile();
29 }
30
31 curr = null;
32 OUTER: while (!parts.isEmpty()) {
33 String f = parts.pop();
34 log.debug("fixing: '" + f + "'");
35 if (curr == null) {
36 // XXX: Not totaly correct because there
37 // more than one root on none unix systems.
38 for (File root: File.listRoots()) {
39 File [] files = root.listFiles();
40 if (files == null) {
41 log.warn("cannot list '" + root);
42 continue;
43 }
44 for (File candidate: files) {
45 if (candidate.getName().equalsIgnoreCase(f)) {
46 curr = new File(root, candidate.getName());
47 continue OUTER;
48 }
49 }
50 }
51 break;
52 }
53 else {
54 File [] files = curr.listFiles();
55 if (files == null) {
56 log.warn("cannot list: '" + curr + "'");
57 return file;
58 }
59 for (File candidate: files) {
60 if (candidate.getName().equalsIgnoreCase(f)) {
61 curr = new File(curr, candidate.getName());
62 continue OUTER;
63 }
64 }
65 curr = null;
66 break;
67 }
68 }
69
70 if (curr == null) {
71 log.warn("cannot repair path '" + file + "'");
72 return file;
73 }
74
75 return curr;
76 }
77 }
78 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org