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)
| 702 | * @since 11.0 |
| 703 | */ |
| 704 | public static String simplifyPath(String pathname) { |
| 705 | checkNotNull(pathname); |
| 706 | if (pathname.length() == 0) { |
| 707 | return "."; |
| 708 | } |
| 709 | |
| 710 | // split the path apart |
| 711 | Iterable<String> components = Splitter.on('/').omitEmptyStrings().split(pathname); |
| 712 | List<String> path = new ArrayList<String>(); |
| 713 | |
| 714 | // resolve ., .., and // |
| 715 | for (String component : components) { |
| 716 | if (component.equals(".")) { |
| 717 | continue; |
| 718 | } else if (component.equals("..")) { |
| 719 | if (path.size() > 0 && !path.get(path.size() - 1).equals("..")) { |
| 720 | path.remove(path.size() - 1); |
| 721 | } else { |
| 722 | path.add(".."); |
| 723 | } |
| 724 | } else { |
| 725 | path.add(component); |
| 726 | } |
| 727 | } |
| 728 | |
| 729 | // put it back together |
| 730 | String result = Joiner.on('/').join(path); |
| 731 | if (pathname.charAt(0) == '/') { |
| 732 | result = "/" + result; |
| 733 | } |
| 734 | |
| 735 | while (result.startsWith("/../")) { |
| 736 | result = result.substring(3); |
| 737 | } |
| 738 | if (result.equals("/..")) { |
| 739 | result = "/"; |
| 740 | } else if ("".equals(result)) { |
| 741 | result = "."; |
| 742 | } |
| 743 | |
| 744 | return result; |
| 745 | } |
| 746 | |
| 747 | /** |
| 748 | * Returns the <a href="http://en.wikipedia.org/wiki/Filename_extension">file extension</a> for |
nothing calls this directly
no test coverage detected