class 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()]);
  }

}