| 1 | /* |
| 2 | * Copyright 2008 The Closure Compiler Authors. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.google.javascript.jscomp.deps; |
| 18 | |
| 19 | import com.google.common.base.CharMatcher; |
| 20 | import com.google.common.base.Joiner; |
| 21 | import com.google.common.base.Strings; |
| 22 | import com.google.common.collect.Lists; |
| 23 | |
| 24 | import java.util.Arrays; |
| 25 | import java.util.List; |
| 26 | |
| 27 | /** |
| 28 | * Utility methods for manipulation of UNIX-like paths. |
| 29 | * NOTE: According to kevinb, equivalent methods will be in the standard library once |
| 30 | * jsr203 is ready. |
| 31 | * |
| 32 | */ |
| 33 | public final class PathUtil { |
| 34 | |
| 35 | private static final CharMatcher SLASH_MATCHER = CharMatcher.is('/'); |
| 36 | private static final CharMatcher NON_SLASH_MATCHER = CharMatcher.isNot('/'); |
| 37 | |
| 38 | private PathUtil() { |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Removes all ../ and ./ entries from within the given path. If there are extra ..s that move |
| 43 | * beyond the first directory given, they are removed. |
| 44 | * |
| 45 | * Examples: |
| 46 | * "a/b/../c" results in "a/c" |
| 47 | * "./foo/./../bar" results in "bar" |
| 48 | * "a/.." results in "" |
| 49 | * "a/../../foo" results in "foo" |
| 50 | * |
| 51 | * @param path The path to remove dots from. |
| 52 | * @return The path with all dots collapsed. |
| 53 | */ |
| 54 | public static String collapseDots(String path) { |
| 55 | path = removeExtraneousSlashes(path); |
| 56 | // Optimization: Most paths don't contain dots. |
| 57 | if (!path.contains(".")) { |
| 58 | return path; |
| 59 | } |
| 60 | |
| 61 | String[] srcFragments = path.split("/"); |
| 62 | List<String> dstFragments = Lists.newArrayList(); |
| 63 | for (String fragment : srcFragments) { |
| 64 | if (fragment.equals("..")) { |
| 65 | if (!dstFragments.isEmpty()) { |
| 66 | dstFragments.remove(dstFragments.size() - 1); |
| 67 | } |
| 68 | } else if (!fragment.equals(".")) { |
| 69 | dstFragments.add(fragment); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // Special case for Join.join([""]); -> "/" |
| 74 | if (dstFragments.size() == 1 && dstFragments.get(0).isEmpty()) { |
| 75 | return "/"; |
| 76 | } |
| 77 | return Joiner.on("/").join(dstFragments); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Determines if a path is absolute or not by testing for the presence of "/" |
| 82 | * at the front of the string. |
| 83 | * |
| 84 | * @param path The path to test |
| 85 | * @return true if the path starts with DELIMITER, false otherwise. |
| 86 | */ |
| 87 | static boolean isAbsolute(String path) { |
| 88 | return path.startsWith("/"); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Removes extra slashes from a path. Leading slash is preserved, trailing |
| 93 | * slash is stripped, and any runs of more than one slash in the middle is |
| 94 | * replaced by a single slash. |
| 95 | */ |
| 96 | static String removeExtraneousSlashes(String s) { |
| 97 | int lastNonSlash = NON_SLASH_MATCHER.lastIndexIn(s); |
| 98 | if (lastNonSlash != -1) { |
| 99 | s = s.substring(0, lastNonSlash + 1); |
| 100 | } |
| 101 | |
| 102 | return SLASH_MATCHER.collapseFrom(s, '/'); |
| 103 | } |
| 104 | |
| 105 | |
| 106 | /** |
| 107 | * Converts the given path into an absolute one. This prepends the current |
| 108 | * working directory and removes all .'s from the path. If an absolute path |
| 109 | * is given, it will not be prefixed. |
| 110 | * |
| 111 | * <p>Unlike File.getAbsolutePath(), this function does remove .'s from the |
| 112 | * path and unlike File.getCanonicalPath(), this function does not resolve |
| 113 | * symlinks and does not use filesystem calls.</p> |
| 114 | * |
| 115 | * @param path The path to make absolute. |
| 116 | * @return The path made absolute. |
| 117 | */ |
| 118 | public static String makeAbsolute(String path) { |
| 119 | return makeAbsolute(path, System.getProperty("user.dir")); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Converts the given path into an absolute one. This prepends the given |
| 124 | * rootPath and removes all .'s from the path. If an absolute path is given, |
| 125 | * it will not be prefixed. |
| 126 | * |
| 127 | * <p>Unlike File.getAbsolutePath(), this function does remove .'s from the |
| 128 | * path and unlike File.getCanonicalPath(), this function does not resolve |
| 129 | * symlinks and does not use filesystem calls.</p> |
| 130 | * |
| 131 | * @param rootPath The path to prefix to path if path is not already absolute. |
| 132 | * @param path The path to make absolute. |
| 133 | * @return The path made absolute. |
| 134 | */ |
| 135 | public static String makeAbsolute(String path, String rootPath) { |
| 136 | if (!isAbsolute(path)) { |
| 137 | path = rootPath + "/" + path; |
| 138 | } |
| 139 | return collapseDots(path); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Returns targetPath relative to basePath. |
| 144 | * |
| 145 | * <p>basePath and targetPath must either both be relative, or both be |
| 146 | * absolute paths.</p> |
| 147 | * |
| 148 | * <p>This function is different from makeRelative |
| 149 | * in that it is able to add in ../ components and collapse existing ones as well.</p> |
| 150 | * |
| 151 | * Examples: |
| 152 | * base="some/relative/path" target="some/relative/path/foo" return="foo" |
| 153 | * base="some/relative/path" target="some/relative" return=".." |
| 154 | * base="some/relative/path" target="foo/bar" return="../../../foo/bar" |
| 155 | * base="/some/abs/path" target="/foo/bar" return="../../../foo/bar" |
| 156 | * |
| 157 | * @param basePath The path to make targetPath relative to. |
| 158 | * @param targetPath The path to make relative. |
| 159 | * @return A path relative to targetPath. The returned value will never start |
| 160 | * with a slash. |
| 161 | */ |
| 162 | public static String makeRelative(String basePath, String targetPath) { |
| 163 | // Ensure the paths are both absolute or both relative. |
| 164 | if (isAbsolute(basePath) != |
| 165 | isAbsolute(targetPath)) { |
| 166 | throw new IllegalArgumentException( |
| 167 | "Paths must both be relative or both absolute.\n" + |
| 168 | " basePath: " + basePath + "\n" + |
| 169 | " targetPath: " + targetPath); |
| 170 | } |
| 171 | |
| 172 | basePath = collapseDots(basePath); |
| 173 | targetPath = collapseDots(targetPath); |
| 174 | String[] baseFragments = basePath.split("/"); |
| 175 | String[] targetFragments = targetPath.split("/"); |
| 176 | |
| 177 | int i = -1; |
| 178 | do { |
| 179 | i += 1; |
| 180 | if (i == baseFragments.length && i == targetFragments.length) { |
| 181 | // Eg) base: /java/com/google |
| 182 | // target: /java/com/google |
| 183 | // result: . <-- . is better than "" since "" + "/path" = "/path" |
| 184 | return "."; |
| 185 | } else if (i == baseFragments.length) { |
| 186 | // Eg) base: /java/com/google |
| 187 | // target: /java/com/google/c/ui |
| 188 | // result: c/ui |
| 189 | return Joiner.on("/").join( |
| 190 | Lists.newArrayList( |
| 191 | Arrays.asList(targetFragments).listIterator(i))); |
| 192 | } else if (i == targetFragments.length) { |
| 193 | // Eg) base: /java/com/google/c/ui |
| 194 | // target: /java/com/google |
| 195 | // result: ../.. |
| 196 | return Strings.repeat("../", baseFragments.length - i - 1) + ".."; |
| 197 | } |
| 198 | |
| 199 | } while (baseFragments[i].equals(targetFragments[i])); |
| 200 | |
| 201 | // Eg) base: /java/com/google/c |
| 202 | // target: /java/com/google/common/base |
| 203 | // result: ../common/base |
| 204 | return Strings.repeat("../", baseFragments.length - i) + |
| 205 | Joiner.on("/").join( |
| 206 | Lists.newArrayList(Arrays.asList(targetFragments).listIterator(i))); |
| 207 | } |
| 208 | } |