1/*
2 * [The "BSD licence"]
3 * Copyright (c) 2010 Ben Gruver
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 *    notice, this list of conditions and the following disclaimer in the
13 *    documentation and/or other materials provided with the distribution.
14 * 3. The name of the author may not be used to endorse or promote products
15 *    derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29package org.jf.util;
30
31import java.io.File;
32import java.io.IOException;
33import java.util.ArrayList;
34
35public class PathUtil {
36    private PathUtil() {
37    }
38
39    public static File getRelativeFile(File baseFile, File fileToRelativize) throws IOException {
40        if (baseFile.isFile()) {
41            baseFile = baseFile.getParentFile();
42        }
43
44        return new File(getRelativeFileInternal(baseFile.getCanonicalFile(), fileToRelativize.getCanonicalFile()));
45    }
46
47    public static String getRelativePath(String basePath, String pathToRelativize) throws IOException {
48        File baseFile = new File(basePath);
49        if (baseFile.isFile()) {
50            baseFile = baseFile.getParentFile();
51        }
52
53        return getRelativeFileInternal(baseFile.getCanonicalFile(),
54                new File(pathToRelativize).getCanonicalFile());
55    }
56
57    static String getRelativeFileInternal(File canonicalBaseFile, File canonicalFileToRelativize) {
58        ArrayList<String> basePath = getPathComponents(canonicalBaseFile);
59        ArrayList<String> pathToRelativize = getPathComponents(canonicalFileToRelativize);
60
61        //if the roots aren't the same (i.e. different drives on a windows machine), we can't construct a relative
62        //path from one to the other, so just return the canonical file
63        if (!basePath.get(0).equals(pathToRelativize.get(0))) {
64            return canonicalFileToRelativize.getPath();
65        }
66
67        int commonDirs;
68        StringBuilder sb = new StringBuilder();
69
70        for (commonDirs=1; commonDirs<basePath.size() && commonDirs<pathToRelativize.size(); commonDirs++) {
71            if (!basePath.get(commonDirs).equals(pathToRelativize.get(commonDirs))) {
72                break;
73            }
74        }
75
76        boolean first = true;
77        for (int i=commonDirs; i<basePath.size(); i++) {
78            if (!first) {
79                sb.append(File.separatorChar);
80            } else {
81                first = false;
82            }
83
84            sb.append("..");
85        }
86
87        first = true;
88        for (int i=commonDirs; i<pathToRelativize.size(); i++) {
89            if (first) {
90                if (sb.length() != 0) {
91                    sb.append(File.separatorChar);
92                }
93                first = false;
94            } else {
95                sb.append(File.separatorChar);
96            }
97
98            sb.append(pathToRelativize.get(i));
99        }
100
101        if (sb.length() == 0) {
102            return ".";
103        }
104
105        return sb.toString();
106    }
107
108    private static ArrayList<String> getPathComponents(File file) {
109        ArrayList<String> path = new ArrayList<String>();
110
111        while (file != null) {
112            File parentFile = file.getParentFile();
113
114            if (parentFile == null) {
115                path.add(0, file.getPath());
116            } else {
117                path.add(0, file.getName());
118            }
119
120            file = parentFile;
121        }
122
123        return path;
124    }
125}
126