PathTools { public static String getCompatiblePath(String path) { List<String> pathStack = getPathStack(path, true); if(pathStack.isEmpty()) { return ""; } String first = pathStack.get(0); if(isDotPattern(first)) { pathStack.remove(0); int len = first.length(); for(int i = 1; i < len; i++) { pathStack.add(0, ".."); } } return getPathStackString(pathStack); } private  PathTools(); static String resolvePath(String basePath, String path); static String evaluatePath(String path); static String getAbsolutePath(String basePath, String path); static String getParentPath(String path); static String getCompatiblePath(String path); static boolean isDotPattern(String s); static List<String> getPathStack(String path, boolean evaluateDots); static String getPathStackString(List<String> pathStack);  }
@Test public void getCompatiblePath() throws Exception { internalGetCompatiblePath(".../foo", "../../foo"); internalGetCompatiblePath("./foo", "foo"); internalGetCompatiblePath("/foo/bar/foobar/...", "/foo"); internalGetCompatiblePath("/foo", "/foo"); }