@Test public void testRelativize() throws Exception { String from = new File(getClass().getResource("/selenese/command").toURI()).getPath(); String to = new File(getClass().getResource("/htdocs").toURI()).getPath(); String relPath = PathUtils.relativize(from, to); assertThat(relPath, is("../../htdocs/")); }
public static String relativize(String from, String to) { from = new File(normalize(from)).toURI().toASCIIString(); to = new File(normalize(to)).toURI().toASCIIString(); int prefixLen = Strings.commonPrefix(from, to).length(); String fromSuffix = from.substring(prefixLen); String toSuffix = to.substring(prefixLen); int level = (int) fromSuffix.chars().filter(ch -> ch == '/').count(); return Strings.repeat("../", level) + toSuffix; }
PathUtils { public static String relativize(String from, String to) { from = new File(normalize(from)).toURI().toASCIIString(); to = new File(normalize(to)).toURI().toASCIIString(); int prefixLen = Strings.commonPrefix(from, to).length(); String fromSuffix = from.substring(prefixLen); String toSuffix = to.substring(prefixLen); int level = (int) fromSuffix.chars().filter(ch -> ch == '/').count(); return Strings.repeat("../", level) + toSuffix; } }
PathUtils { public static String relativize(String from, String to) { from = new File(normalize(from)).toURI().toASCIIString(); to = new File(normalize(to)).toURI().toASCIIString(); int prefixLen = Strings.commonPrefix(from, to).length(); String fromSuffix = from.substring(prefixLen); String toSuffix = to.substring(prefixLen); int level = (int) fromSuffix.chars().filter(ch -> ch == '/').count(); return Strings.repeat("../", level) + toSuffix; } private  PathUtils(); }
PathUtils { public static String relativize(String from, String to) { from = new File(normalize(from)).toURI().toASCIIString(); to = new File(normalize(to)).toURI().toASCIIString(); int prefixLen = Strings.commonPrefix(from, to).length(); String fromSuffix = from.substring(prefixLen); String toSuffix = to.substring(prefixLen); int level = (int) fromSuffix.chars().filter(ch -> ch == '/').count(); return Strings.repeat("../", level) + toSuffix; } private  PathUtils(); static String normalizeSeparator(String filename); static String normalize(String filename); static String concat(String parent, String child); static String relativize(String from, String to); }
PathUtils { public static String relativize(String from, String to) { from = new File(normalize(from)).toURI().toASCIIString(); to = new File(normalize(to)).toURI().toASCIIString(); int prefixLen = Strings.commonPrefix(from, to).length(); String fromSuffix = from.substring(prefixLen); String toSuffix = to.substring(prefixLen); int level = (int) fromSuffix.chars().filter(ch -> ch == '/').count(); return Strings.repeat("../", level) + toSuffix; } private  PathUtils(); static String normalizeSeparator(String filename); static String normalize(String filename); static String concat(String parent, String child); static String relativize(String from, String to);  }