Mercurial > dive4elements > river
diff flys-backend/src/main/java/de/intevation/flys/utils/FileTools.java @ 177:31895d24387e
Importer: Added info gew parser.
flys-backend/trunk@1485 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Wed, 16 Mar 2011 17:01:55 +0000 |
parents | |
children | c7370734b872 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/utils/FileTools.java Wed Mar 16 17:01:55 2011 +0000 @@ -0,0 +1,78 @@ +package de.intevation.flys.utils; + +import java.io.File; + +import java.util.Stack; + +import org.apache.log4j.Logger; + +public class FileTools +{ + private static Logger log = Logger.getLogger(FileTools.class); + + private FileTools() { + } + + public static File repair(File file) { + file = file.getAbsoluteFile(); + if (file.exists()) { + return file; + } + Stack<String> parts = new Stack<String>(); + File curr = file; + while (curr != null) { + String name = curr.getName(); + if (name.length() > 0) { + parts.push(curr.getName()); + } + curr = curr.getParentFile(); + } + + curr = null; + OUTER: while (!parts.isEmpty()) { + String f = parts.pop(); + log.debug("fixing: '" + f + "'"); + if (curr == null) { + // XXX: Not totaly correct because there + // more than one root on none unix systems. + for (File root: File.listRoots()) { + File [] files = root.listFiles(); + if (files == null) { + log.warn("cannot list '" + root); + continue; + } + for (File candidate: files) { + if (candidate.getName().equalsIgnoreCase(f)) { + curr = new File(root, candidate.getName()); + continue OUTER; + } + } + } + break; + } + else { + File [] files = curr.listFiles(); + if (files == null) { + log.warn("cannot list: '" + curr + "'"); + return file; + } + for (File candidate: files) { + if (candidate.getName().equalsIgnoreCase(f)) { + curr = new File(curr, candidate.getName()); + continue OUTER; + } + } + curr = null; + break; + } + } + + if (curr == null) { + log.warn("cannot repair path '" + file + "'"); + return file; + } + + return curr; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :