sascha@177: package de.intevation.flys.utils; sascha@177: sascha@177: import java.io.File; sascha@177: sascha@177: import java.util.Stack; sascha@177: sascha@177: import org.apache.log4j.Logger; sascha@177: sascha@177: public class FileTools sascha@177: { sascha@177: private static Logger log = Logger.getLogger(FileTools.class); sascha@177: sascha@177: private FileTools() { sascha@177: } sascha@177: sascha@177: public static File repair(File file) { sascha@177: file = file.getAbsoluteFile(); sascha@177: if (file.exists()) { sascha@177: return file; sascha@177: } sascha@177: Stack parts = new Stack(); sascha@177: File curr = file; sascha@177: while (curr != null) { sascha@177: String name = curr.getName(); sascha@177: if (name.length() > 0) { sascha@177: parts.push(curr.getName()); sascha@177: } sascha@177: curr = curr.getParentFile(); sascha@177: } sascha@177: sascha@177: curr = null; sascha@177: OUTER: while (!parts.isEmpty()) { sascha@177: String f = parts.pop(); sascha@177: log.debug("fixing: '" + f + "'"); sascha@177: if (curr == null) { sascha@177: // XXX: Not totaly correct because there sascha@177: // more than one root on none unix systems. sascha@177: for (File root: File.listRoots()) { sascha@177: File [] files = root.listFiles(); sascha@177: if (files == null) { sascha@177: log.warn("cannot list '" + root); sascha@177: continue; sascha@177: } sascha@177: for (File candidate: files) { sascha@177: if (candidate.getName().equalsIgnoreCase(f)) { sascha@177: curr = new File(root, candidate.getName()); sascha@177: continue OUTER; sascha@177: } sascha@177: } sascha@177: } sascha@177: break; sascha@177: } sascha@177: else { sascha@177: File [] files = curr.listFiles(); sascha@177: if (files == null) { sascha@177: log.warn("cannot list: '" + curr + "'"); sascha@177: return file; sascha@177: } sascha@177: for (File candidate: files) { sascha@177: if (candidate.getName().equalsIgnoreCase(f)) { sascha@177: curr = new File(curr, candidate.getName()); sascha@177: continue OUTER; sascha@177: } sascha@177: } sascha@177: curr = null; sascha@177: break; sascha@177: } sascha@177: } sascha@177: sascha@177: if (curr == null) { sascha@177: log.warn("cannot repair path '" + file + "'"); sascha@177: return file; sascha@177: } sascha@177: sascha@177: return curr; sascha@177: } sascha@177: } sascha@177: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :