@Test public void testGetCanonicalFragments() { final Object[][] testParameters = new Object[][] {{"/Users/tester", new String[] {"Users", "tester"}}, {"/Users/admin/systems", new String[] {"Users", "admin", "systems"}}, {"./tests/./subtest1", new String[] {"tests", "subtest1"}}, {"././tests/../subtest2", new String[] {"subtest2"}}}; for (final Object[] testParameter : testParameters) { String[] expected = (String[]) testParameter[1]; String path = (String) testParameter[0]; for (int i = 0; i < expected.length; ++i) { assertEquals(expected[i], FilepathUtils.getCanonicalFragments(path)[i]); } } }
public static String[] getCanonicalFragments(final String path) { final String safe = path.replace(File.separatorChar, DIRECTORY_SEPARATOR_CHAR); final Stack<String> stack = new Stack<String>(); for (final String fragment : safe.split(DIRECTORY_SEPARATOR)) { if (isEmpty(fragment)) { continue; } else if (CURRENT_DIR.equals(fragment)) { continue; } else if (PARENT_DIR.equals(fragment)) { if (stack.isEmpty()) { throw new IllegalArgumentException(); } stack.pop(); } else { stack.push(fragment); } } return stack.toArray(new String[stack.size()]); }
FilepathUtils { public static String[] getCanonicalFragments(final String path) { final String safe = path.replace(File.separatorChar, DIRECTORY_SEPARATOR_CHAR); final Stack<String> stack = new Stack<String>(); for (final String fragment : safe.split(DIRECTORY_SEPARATOR)) { if (isEmpty(fragment)) { continue; } else if (CURRENT_DIR.equals(fragment)) { continue; } else if (PARENT_DIR.equals(fragment)) { if (stack.isEmpty()) { throw new IllegalArgumentException(); } stack.pop(); } else { stack.push(fragment); } } return stack.toArray(new String[stack.size()]); } }
FilepathUtils { public static String[] getCanonicalFragments(final String path) { final String safe = path.replace(File.separatorChar, DIRECTORY_SEPARATOR_CHAR); final Stack<String> stack = new Stack<String>(); for (final String fragment : safe.split(DIRECTORY_SEPARATOR)) { if (isEmpty(fragment)) { continue; } else if (CURRENT_DIR.equals(fragment)) { continue; } else if (PARENT_DIR.equals(fragment)) { if (stack.isEmpty()) { throw new IllegalArgumentException(); } stack.pop(); } else { stack.push(fragment); } } return stack.toArray(new String[stack.size()]); }  }
FilepathUtils { public static String[] getCanonicalFragments(final String path) { final String safe = path.replace(File.separatorChar, DIRECTORY_SEPARATOR_CHAR); final Stack<String> stack = new Stack<String>(); for (final String fragment : safe.split(DIRECTORY_SEPARATOR)) { if (isEmpty(fragment)) { continue; } else if (CURRENT_DIR.equals(fragment)) { continue; } else if (PARENT_DIR.equals(fragment)) { if (stack.isEmpty()) { throw new IllegalArgumentException(); } stack.pop(); } else { stack.push(fragment); } } return stack.toArray(new String[stack.size()]); }  static String[] getCanonicalFragments(final String path); static String getCanonicalForm(final String path); static String[] split(final String path); static String getParentPath(final String path); static String getFilename(final String path); static String concat(final String... fragments); static String append(final String... fragments); static String append(final String[] fragments, final int start, final int end); }
FilepathUtils { public static String[] getCanonicalFragments(final String path) { final String safe = path.replace(File.separatorChar, DIRECTORY_SEPARATOR_CHAR); final Stack<String> stack = new Stack<String>(); for (final String fragment : safe.split(DIRECTORY_SEPARATOR)) { if (isEmpty(fragment)) { continue; } else if (CURRENT_DIR.equals(fragment)) { continue; } else if (PARENT_DIR.equals(fragment)) { if (stack.isEmpty()) { throw new IllegalArgumentException(); } stack.pop(); } else { stack.push(fragment); } } return stack.toArray(new String[stack.size()]); }  static String[] getCanonicalFragments(final String path); static String getCanonicalForm(final String path); static String[] split(final String path); static String getParentPath(final String path); static String getFilename(final String path); static String concat(final String... fragments); static String append(final String... fragments); static String append(final String[] fragments, final int start, final int end);  }