Interface ResourcePath


public sealed interface ResourcePath
Represents a module name and a URI used for loading a document's resources.

Page paths are made up of 3 elements: the module name, the path and the query.

Module Name

Name of a specific directory/zip archive in the DelphiResources.getModuleDirectory() directory.

Path

The file path of a file/directory in the directory/zip archive the module name points to.

Query

Optional search parameters that scripts can use.
  • Field Details

    • VALID_ELEMENT

      static final Pattern VALID_ELEMENT
    • VALID_QUERY

      static final Pattern VALID_QUERY
  • Method Details

    • create

      static ResourcePath create(@NotNull @NotNull String moduleName)
      Creates a page path with a specified moduleName
      Parameters:
      moduleName - Path Module name
      Returns:
      Created path
    • validateFilename

      static void validateFilename(String element)
      Validates a filename.

      For a filename to be "valid" it must not be null and match the VALID_ELEMENT regex pattern.

      Parameters:
      element - Element to validate
      Throws:
      NullPointerException - If the element is null
      IllegalArgumentException - If the element is an invalid filename.
    • validateQuery

      static void validateQuery(String query)
      Validates a query key/value.

      For a query key or value to be "valid" it must not be null and must match the VALID_QUERY regex pattern.

      Parameters:
      query - Query element to validate.
      Throws:
      NullPointerException - If the query is null.
      IllegalArgumentException - If the query is invalid.
    • getModuleName

      @NotNull @NotNull String getModuleName()
      Gets the module name element of the path.
      Returns:
      Module name
    • setModuleName

      ResourcePath setModuleName(@NotNull @NotNull String moduleName)
      Sets the module name.
      Parameters:
      moduleName - New module name
      Returns:
      A copy of this path with the specified module name
      Throws:
      NullPointerException - If the module name is null
      IllegalArgumentException - If the module name fails validateQuery(String)
    • addElement

      ResourcePath addElement(@NotNull @NotNull String element)
      Adds an element to the path's filepath.
      Parameters:
      element - Filename
      Returns:
      A copy of this path with the specified element added
      Throws:
      NullPointerException - If the element is null
      IllegalArgumentException - If the element fails validateFilename(String)
    • setElement

      ResourcePath setElement(int index, @NotNull @NotNull String element) throws IndexOutOfBoundsException
      Sets the element at a specific index.
      Parameters:
      index - Element index
      element - New element
      Returns:
      A copy of this path with the specified element changed
      Throws:
      IndexOutOfBoundsException - If the index is less than 0 or greater/equal to elementCount().
      NullPointerException - If the element is null
      IllegalArgumentException - If the element fails validateFilename(String)
    • setQuery

      ResourcePath setQuery(@NotNull @NotNull String key, @Nullable @Nullable String value)
      Sets a query value.
      Parameters:
      key - Query key
      value - Query value
      Returns:
      A copy of this path with the specified query changed
      Throws:
      NullPointerException - If the key is null
      IllegalArgumentException - If the key or value (if not null) fails validateQuery(String).
    • addAllElements

      ResourcePath addAllElements(@NotNull @NotNull ResourcePath path)
      Adds all elements from the specified path.
      Parameters:
      path - Path to copy elements from
      Returns:
      A copy of this path with all the elements of the specified path added
    • setElements

      ResourcePath setElements(ResourcePath path)
      Sets the elements of this path
      Parameters:
      path - Path elements
      Returns:
      A copy of this path with the elements of the specified path
    • removeElement

      ResourcePath removeElement(int index)
      Removes a path element
      Parameters:
      index - Index of the element to remove
      Returns:
      A copy of this path with the specified element removed
    • clearElements

      ResourcePath clearElements()
      Clears all of this path's file elements.
      Returns:
      A copy of this path with no elements
    • getQuery

      @Nullable @Nullable String getQuery(String key)
      Gets the value of a query
      Parameters:
      key - Query key
      Returns:
      Query value, or null, if there's no query element with the specified key.
    • getQueryKeys

      Set<String> getQueryKeys()
      Gets an unmodifiable set of the query keys.
      Returns:
      Query keys
    • getElements

      List<String> getElements()
      Gets an unmodifiable list of the path's elements
      Returns:
      Unmodifiable path element list
    • elementCount

      int elementCount()
      Gets the amount of elements in the path
      Returns:
      Element count
    • getElement

      @NotNull @NotNull String getElement(int index) throws IndexOutOfBoundsException
      Gets an element at a specific index

      Assume we have the path foobar/filepath/filename.ext, these would be the indexes:

      Index Element
      0 foobar
      1 filepath
      2 filename.ext
      Parameters:
      index - Element index
      Returns:
      Element
      Throws:
      IndexOutOfBoundsException - If index is less than 0, or greater/equal to elementCount().
    • path

      @NotNull @NotNull String path()
      Gets the file path part of this page path.

      Example: foo/bar/foobar.json

      Returns:
      File path string, or "", if no file elements were specified
    • query

      @NotNull @NotNull String query()
      Gets the query part of this path as a string.

      Example: ?foo=bar&bar=foo

      Returns:
      Query string, or "" if there are no query elements.
    • elements

      String elements()
      Gets the file path of this page path combined with the query parameters.

      Example: dir/file.xml?foo=bar

      Returns:
      File path + query parameters.
      See Also:
    • toString

      String toString()
      Combines the module name, elements and queries into a string that can be given to Delphi.parsePath(String) to parse.

      Examples:

      • module
      • module:file.ext
      • module:'file with space.ext'
      • module:'dir with space'/file.xml
      • module:foobar/file.ext
      • module:foobar/file.ext?foo=bar&bar=foo
      • module:foobar/file.ext?foo&bar=false
      • module:?foo=bar&bar=foo
      Overrides:
      toString in class Object
      Returns:
      String representation of this path