PathTools { public static String resolvePath(String basePath, String path) { final Logger logger = LoggerFactory.getLogger(PathTools.class); if(basePath == null) { IllegalArgumentException ex = new IllegalArgumentException("basePath must not be null!"); if(logger.isDebugEnabled()) { logger.debug("Parameter 'basePath' of method 'resolvePath' must not be null!", ex); } throw ex; } if(path == null) { IllegalArgumentException ex = new IllegalArgumentException("path must not be null!"); if(logger.isDebugEnabled()) logger.debug("Parameter 'path' of method 'resolvePath' must not be null!", ex); throw ex; } int basePathLength = basePath.length(); if(basePathLength == 0) { return path; } int pathLength = path.length(); if(pathLength == 0) { return basePath; } if(path.charAt(0) == '/') { return path; } StringBuilder result = new StringBuilder(basePathLength + pathLength + 1); result.append(basePath); if(basePath.charAt(basePathLength-1) != '/') { result.append('/'); } result.append(path); return result.toString(); } 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 resolvePath() throws Exception { internalResolvePath("", "", ""); internalResolvePath("", "foo", "foo"); internalResolvePath("foo", "", "foo"); internalResolvePath("foo", "/", "/"); internalResolvePath("foo", "bar", "foo/bar"); internalResolvePath("/foo", "bar", "/foo/bar"); internalResolvePath(".../foo", "../bar", ".../foo/../bar"); internalResolvePath("foo", "/bar", "/bar"); internalResolvePath("/foo", "bar", "/foo/bar"); internalResolvePath("/foo", "../bar", "/foo/../bar"); }