StringUtil { public static String collapsePathDots(String path) { String[] stringParts = path.split("/"); List<String> parts = new ArrayList<String>(Arrays.asList(stringParts)); for (int i = 0; i < parts.size() - 1; i++) { String currentDir = parts.get(i); if (currentDir.length() == 0 || currentDir.equals(".")) { parts.remove(i); i--; } else if (currentDir.equals("..")) { parts.remove(i - 1); parts.remove(i - 1); i -= 2; } } StringBuilder result = new StringBuilder(); if (path.startsWith("/")) { result.append('/'); } for (int i = 0; i < parts.size(); i++) { result.append(parts.get(i)); if (i < (parts.size() - 1)) { result.append('/'); } } return result.toString(); }  static String collapsePathDots(String path); static boolean isNotBlank(String text); static boolean isBlank(String text); static boolean isEmpty(String text); static boolean endsWithIgnoreCase(String source, String suffix); static String defaultIfNull(String text); static String defaultIfNull(String text, String defaultValue); static boolean equals(String text1, String text2); static String toString(Object... keyValues); static int hashCode(String... values); static String substringBefore(String text, char separator); static String substringBeforeLast(String text, char separator); static String substringAfterLast(String text, char separator); static String substringAfter(String text, char c);  }
@Test public void testReplacementForCollapsePathDots() throws IOException { String[] testData = new String[] { "/foo/bar.html", "/foo/bar.html", "/foo/../bar.html", "/bar.html", "/foo/moo/../../bar.html", "/bar.html", "/foo "/foo/bar.html", "/foo/./bar.html", "/foo/bar.html", "/a/b/../../c", "/c", "/foo/../sub/bar.html", "/sub/bar.html" }; for (int i = 0; i < testData.length; i += 2) { String actualResult = StringUtil.collapsePathDots(testData[i]); assertEquals(testData[i], testData[i + 1], actualResult); } }