com.google.javascript.jscomp.deps
Class PathUtil

java.lang.Object
  extended by com.google.javascript.jscomp.deps.PathUtil

public final class PathUtil
extends Object

Utility methods for manipulation of UNIX-like paths. NOTE: According to kevinb, equivalent methods will be in the standard library once jsr203 is ready.


Method Summary
static String collapseDots(String path)
          Removes all ../ and ./ entries from within the given path.
static String makeAbsolute(String path)
          Converts the given path into an absolute one.
static String makeAbsolute(String path, String rootPath)
          Converts the given path into an absolute one.
static String makeRelative(String basePath, String targetPath)
          Returns targetPath relative to basePath.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

collapseDots

public static String collapseDots(String path)
Removes all ../ and ./ entries from within the given path. If there are extra ..s that move beyond the first directory given, they are removed. Examples: "a/b/../c" results in "a/c" "./foo/./../bar" results in "bar" "a/.." results in "" "a/../../foo" results in "foo"

Parameters:
path - The path to remove dots from.
Returns:
The path with all dots collapsed.

makeAbsolute

public static String makeAbsolute(String path)
Converts the given path into an absolute one. This prepends the current working directory and removes all .'s from the path. If an absolute path is given, it will not be prefixed.

Unlike File.getAbsolutePath(), this function does remove .'s from the path and unlike File.getCanonicalPath(), this function does not resolve symlinks and does not use filesystem calls.

Parameters:
path - The path to make absolute.
Returns:
The path made absolute.

makeAbsolute

public static String makeAbsolute(String path,
                                  String rootPath)
Converts the given path into an absolute one. This prepends the given rootPath and removes all .'s from the path. If an absolute path is given, it will not be prefixed.

Unlike File.getAbsolutePath(), this function does remove .'s from the path and unlike File.getCanonicalPath(), this function does not resolve symlinks and does not use filesystem calls.

Parameters:
rootPath - The path to prefix to path if path is not already absolute.
path - The path to make absolute.
Returns:
The path made absolute.

makeRelative

public static String makeRelative(String basePath,
                                  String targetPath)
Returns targetPath relative to basePath.

basePath and targetPath must either both be relative, or both be absolute paths.

This function is different from makeRelative in that it is able to add in ../ components and collapse existing ones as well.

Examples: base="some/relative/path" target="some/relative/path/foo" return="foo" base="some/relative/path" target="some/relative" return=".." base="some/relative/path" target="foo/bar" return="../../../foo/bar" base="/some/abs/path" target="/foo/bar" return="../../../foo/bar"

Parameters:
basePath - The path to make targetPath relative to.
targetPath - The path to make relative.
Returns:
A path relative to targetPath. The returned value will never start with a slash.