Class PathBuilder

java.lang.Object
org.apache.sling.api.resource.path.PathBuilder

public final class PathBuilder extends Object
The PathBuilder offers a convenient way of creating an absolute path from multiple fragments. Use build() to obtain a normalized path. Normalization does not confine the result to the initial path's subtree.
Since:
1.0.0 (Sling API Bundle 2.11.0)
  • Constructor Details

    • PathBuilder

      public PathBuilder(String path)
      Creates a new PathBuilder instance with an absolute initial path. Normalization is performed only when build() is called.
      Parameters:
      path - the nonempty initial path, starting with /
      Throws:
      IllegalArgumentException - if the path is null, empty, or relative
  • Method Details

    • append

      public PathBuilder append(String path)
      Appends a path fragment to the accumulated path. Fragments may be relative and contain . or .. segments, which are resolved when build() is called. A leading / does not replace the accumulated path; the fragment is joined to it with a slash at the boundary.
      Parameters:
      path - the nonempty path fragment to append
      Returns:
      this instance
      Throws:
      IllegalArgumentException - if the fragment is null or empty
    • toString

      public String toString()
      Returns the joined path without normalization. In most cases, build() is the better choice, as it resolves dot segments and removes redundant slashes.
      Overrides:
      toString in class Object
      Returns:
      the unnormalized absolute path
    • build

      @NotNull public @NotNull String build()
      Returns the normalized path without changing the accumulated fragments. Dot segments are resolved, duplicate slashes are collapsed, and trailing slashes are removed except for the root path.
      Returns:
      the normalized absolute path
      Throws:
      IllegalArgumentException - if the path cannot be normalized, for example because it traverses above root
      Since:
      1.3.0