Class Path

java.lang.Object
com.mastfrog.url.Path
All Implemented Interfaces:
URLComponent, Serializable, Iterable<PathElement>

public final class Path extends Object implements URLComponent, Iterable<PathElement>
The path portion of a URL. In particular, this class deals with normalizing relative (..) references, and leading and trailing slashes.
Author:
Tim Boudreau
See Also:
  • Constructor Details

  • Method Details

    • parse

      public static Path parse(String path)
      Parse a path in the format element1/element2/element3
      Parameters:
      path -
      Returns:
    • parse

      public static Path parse(String path, boolean decode)
    • toURLDecodedPath

      public Path toURLDecodedPath()
    • iterator

      public Iterator<PathElement> iterator()
      Specified by:
      iterator in interface Iterable<PathElement>
    • lastElement

      public PathElement lastElement()
    • normalize

      public Path normalize()
      Normalize a path, resolving .. elements.
      Returns:
      A path
    • replace

      public Path replace(String old, String nue)
      Create a new path, replacing any path elements that exactly match the passed string with new elements that match the replacement.
      Parameters:
      old - The string to look for
      nue - The string to replace it with
      Returns:
      A path
    • elideEmptyElements

      public final Path elideEmptyElements()
    • prepend

      public Path prepend(String part)
    • append

      public Path append(String part)
    • merge

      public static Path merge(Path... paths)
      Merge an array of paths together
      Parameters:
      paths - An array of paths
      Returns:
      A merged path which appends all elements of all passed paths
    • isParentOf

      public boolean isParentOf(Path path)
      Determine if this path is a path to a parent of the passed path. E.g.
       assert Path.parse ("com/foo").isParentOf(Path.parse("com/foo/bar")) == true;
       
      Parameters:
      path - A path
      Returns:
      whether or not this path is a parent of the passed path.
    • isChildOf

      public boolean isChildOf(Path path)
      Determine if this path is a path to a child of the passed path. E.g.
       assert Path.parse ("com/foo/bar").isChildOf(Path.parse("com/foo")) == true;
       
      Parameters:
      path - a path
      Returns:
      whether or not this Path is a child of the passed Path
    • size

      public int size()
      Get the number of elements in this path.
      Returns:
    • getElements

      public PathElement[] getElements()
      Get the individual elements of this path.
      Returns:
      An array of elements
    • getChildPath

      public Path getChildPath()
      Get a Path which does not include the first element of this path. E.g. the child path of com/foo/bar is foo/bar.
      Returns:
      A child path
    • toStringWithLeadingSlash

      public String toStringWithLeadingSlash()
      Create a string version of this path, prepending a leading slash if necessary.
      Returns:
      A string
    • getParentPath

      public Path getParentPath()
      Get the parent of this path. E.g. the parent of com/foo/bar is com/foo.
      Returns:
      A path
    • lastElementMatches

      public boolean lastElementMatches(CharSequence s)
      Returns true if the last path element is a textual match for the passed CharSequence, considering case.
      Parameters:
      s - The string to match
      Returns:
      True if it matches
    • lastElementMatches

      public boolean lastElementMatches(CharSequence s, boolean ignoreCase)
      Returns true if the last path element is a textual match for the passed CharSequence, ignoring case if the passed boolean flag is true.
      Parameters:
      s - The string to match
      ignoreCase - Whether or not to do a case-insensitive match
      Returns:
      True if it matches
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • builder

      public static AbstractBuilder<PathElement,Path> builder()
    • isValid

      public boolean isValid()
      Description copied from interface: URLComponent
      Determine if this component is in compliance with the RFCs governing URLs and DNS.
      Specified by:
      isValid in interface URLComponent
      Returns:
      True if this component is valid.
    • getComponentName

      public String getComponentName()
      Description copied from interface: URLComponent
      Get a human-readable, localized name for this part of the URL. Useful if an error message needs to be shown.
      Specified by:
      getComponentName in interface URLComponent
      Returns:
      The component name
    • isProbableFileReference

      public boolean isProbableFileReference()
      Determine if this path is probably a reference to a file (last element contains a . character). May be used to decide whether or not to append a '/' character.
      Returns:
      true if this is a probable file.
    • appendTo

      public void appendTo(StringBuilder sb)
      Description copied from interface: URLComponent
      Append this component to a StringBuilder.
      Specified by:
      appendTo in interface URLComponent
      Parameters:
      sb - A StringBuilder
    • getElement

      public PathElement getElement(int ix)
    • getLastElement

      public PathElement getLastElement()
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toURI

      public URI toURI()
    • toURIWithLeadingSlash

      public URI toURIWithLeadingSlash()
    • toStringArray

      public String[] toStringArray()