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 29 package org.jf.util; 30 31 import com.google.common.collect.Lists; 32 33 import java.io.File; 34 import java.io.IOException; 35 import java.util.ArrayList; 36 import java.util.List; 37 38 public class PathUtil { PathUtil()39 private PathUtil() { 40 } 41 getRelativeFile(File baseFile, File fileToRelativize)42 public static File getRelativeFile(File baseFile, File fileToRelativize) throws IOException { 43 if (baseFile.isFile()) { 44 baseFile = baseFile.getParentFile(); 45 } 46 47 return new File(getRelativeFileInternal(baseFile.getCanonicalFile(), fileToRelativize.getCanonicalFile())); 48 } 49 getRelativeFileInternal(File canonicalBaseFile, File canonicalFileToRelativize)50 static String getRelativeFileInternal(File canonicalBaseFile, File canonicalFileToRelativize) { 51 List<String> basePath = getPathComponents(canonicalBaseFile); 52 List<String> pathToRelativize = getPathComponents(canonicalFileToRelativize); 53 54 //if the roots aren't the same (i.e. different drives on a windows machine), we can't construct a relative 55 //path from one to the other, so just return the canonical file 56 if (!basePath.get(0).equals(pathToRelativize.get(0))) { 57 return canonicalFileToRelativize.getPath(); 58 } 59 60 int commonDirs; 61 StringBuilder sb = new StringBuilder(); 62 63 for (commonDirs=1; commonDirs<basePath.size() && commonDirs<pathToRelativize.size(); commonDirs++) { 64 if (!basePath.get(commonDirs).equals(pathToRelativize.get(commonDirs))) { 65 break; 66 } 67 } 68 69 boolean first = true; 70 for (int i=commonDirs; i<basePath.size(); i++) { 71 if (!first) { 72 sb.append(File.separatorChar); 73 } else { 74 first = false; 75 } 76 77 sb.append(".."); 78 } 79 80 first = true; 81 for (int i=commonDirs; i<pathToRelativize.size(); i++) { 82 if (first) { 83 if (sb.length() != 0) { 84 sb.append(File.separatorChar); 85 } 86 first = false; 87 } else { 88 sb.append(File.separatorChar); 89 } 90 91 sb.append(pathToRelativize.get(i)); 92 } 93 94 if (sb.length() == 0) { 95 return "."; 96 } 97 98 return sb.toString(); 99 } 100 getPathComponents(File file)101 private static List<String> getPathComponents(File file) { 102 ArrayList<String> path = new ArrayList<String>(); 103 104 while (file != null) { 105 File parentFile = file.getParentFile(); 106 107 if (parentFile == null) { 108 path.add(file.getPath()); 109 } else { 110 path.add(file.getName()); 111 } 112 113 file = parentFile; 114 } 115 116 return Lists.reverse(path); 117 } 118 } 119