PathUtils { public static String relativize(String path, String rootPath) { if (StringUtils.isNullOrEmpty(path) || StringUtils.isNullOrEmpty(rootPath)) { return path; } if (path.toLowerCase().startsWith("http:/") || path.toLowerCase().startsWith("https:/")) { String prefix = ""; while (rootPath != null) { if (path.startsWith(rootPath)) { path = path.substring(rootPath.length()); if (path.startsWith("/")) { path = path.substring(1); } break; } else { int ix = rootPath.lastIndexOf('/'); rootPath = ix == -1 ? null : rootPath.substring(0, ix); prefix += "../"; } } return prefix + path; } else { String prefix = ""; if (path.toLowerCase().startsWith("file:")) { try { path = new File(new URL(path).toURI()).getAbsolutePath(); } catch (Exception e) { e.printStackTrace(); } } if (rootPath.startsWith("file:")) { try { rootPath = new File(new URL(rootPath).toURI()).getAbsolutePath(); } catch (Exception e) { e.printStackTrace(); } } if (rootPath.toUpperCase().charAt(0) != path.toUpperCase().charAt(0) && ((rootPath.indexOf(":\\") == 1 || rootPath.indexOf(":/") == 1) && (path.indexOf(":\\") == 1 || path .indexOf(":/") == 1))) { return path; } while (rootPath != null) { if (path.startsWith(rootPath)) { path = path.substring(rootPath.length()); if (path.startsWith(File.separator)) { path = path.substring(1); } break; } else { File file = new File(rootPath); rootPath = file.getParent(); prefix += ".." + File.separatorChar; } } return prefix + path; } }  static String getAbsoluteFolder(String path); static String expandPath(String path, AbstractWsdlModelItem<?> modelItem); static String expandPath(String path, AbstractWsdlModelItem<?> modelItem, PropertyExpansionContext context); static String adjustRelativePath(String str, String root, ModelItem contextModelItem); static boolean isHttpPath(String str); static boolean isRelativePath(String str); static String createRelativePath(String path, String root, ModelItem contextModelItem); static String relativizeResourcePath(String path, ModelItem modelItem); static String resolveResourcePath(String path, ModelItem modelItem); static String relativize(String path, String rootPath); static boolean isAbsolutePath(String path); static boolean isFilePath(String path); static String normalizePath(String path); static String denormalizePath(String path); static String getExpandedResourceRoot(ModelItem modelItem); static String getExpandedResourceRoot(ModelItem modelItem, PropertyExpansionContext context); static String ensureFilePathIsUrl(String url); static String fixForwardSlashesInPath(String path);  }
@Test public void relativizesPaths() throws Exception { assertEquals("c:\\test\\file.txt", PathUtils.relativize("c:\\test\\file.txt", "d:\\")); assertEquals("c:/test/file.txt", PathUtils.relativize("c:/test/file.txt", "d:\\")); assertEquals("c:\\test\\file.txt", PathUtils.relativize("c:\\test\\file.txt", "d:/")); }