Returns the lexically cleaned form of the path name, usually (but not always) equivalent to the original. The following heuristics are used: empty string becomes . . stays as . fold out ./ fold out ../ when possible collapse multiple slashes delete trailing slash
(String pathname)
| 739 | |
| 740 | |
| 741 | public static String simplifyPath(String pathname) { |
| 742 | checkNotNull(pathname); |
| 743 | if (pathname.length() == 0) { |
| 744 | return "."; |
| 745 | } |
| 746 | |
| 747 | // split the path apart |
| 748 | Iterable<String> components = Splitter.on('/').omitEmptyStrings().split(pathname); |
| 749 | List<String> path = new ArrayList<String>(); |
| 750 | |
| 751 | // resolve ., .., and // |
| 752 | for (String component : components) { |
| 753 | if (component.equals(".")) { |
| 754 | continue; |
| 755 | } else if (component.equals("..")) { |
| 756 | if (path.size() > 0 && !path.get(path.size() - 1).equals("..")) { |
| 757 | path.remove(path.size() - 1); |
| 758 | } else { |
| 759 | path.add(".."); |
| 760 | } |
| 761 | } else { |
| 762 | path.add(component); |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | // put it back together |
| 767 | String result = Joiner.on('/').join(path); |
| 768 | if (pathname.charAt(0) == '/') { |
| 769 | result = "/" + result; |
| 770 | } |
| 771 | |
| 772 | while (result.startsWith("/../")) { |
| 773 | result = result.substring(3); |
| 774 | } |
| 775 | if (result.equals("/..")) { |
| 776 | result = "/"; |
| 777 | } else if ("".equals(result)) { |
| 778 | result = "."; |
| 779 | } |
| 780 | return result; |
| 781 | } |
| 782 | |
| 783 | /** |
| 784 | * Returns the <a href="http://en.wikipedia.org/wiki/Filename_extension">file extension</a> for |
nothing calls this directly
no test coverage detected