view flys-backend/src/main/java/de/intevation/flys/utils/FileTools.java @ 1197:ce3dacc6ea92

PRFParser: extract km from lines. TODO: extract data. flys-backend/trunk@2297 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 07 Jul 2011 09:29:31 +0000
parents 31895d24387e
children c7370734b872
line wrap: on
line source
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 :

http://dive4elements.wald.intevation.org