Class ResourcePaths


  • public final class ResourcePaths
    extends Object
    Utility class for working with (Servlet) resource paths, providing methods to handle their prefixes, slashes, extensions etc.
    Since:
    1.4
    Author:
    Arjan Tijms
    • Method Detail

      • isDirectory

        public static boolean isDirectory​(String resourcePath)
        Checks if the given resource path obtained from ServletContext.getResourcePaths(String) represents a directory. That is, if it ends with "/".
        Parameters:
        resourcePath - The resource path to check.
        Returns:
        true if the resource path represents a directory, false otherwise.
      • stripPrefixPath

        public static String stripPrefixPath​(String prefix,
                                             String resourcePath)
        Strips the given prefix from the given resource path if any.
        Parameters:
        prefix - The prefix to be stripped.
        resourcePath - The resource path to strip the prefix from.
        Returns:
        The resource without the prefix path, or as-is if it didn't start with this prefix.
      • stripTrailingSlash

        public static String stripTrailingSlash​(String resourcePath)
        Strips the trailing slash(es) from the given resource path if any.
        Parameters:
        resourcePath - The resource path to strip the trailing slash from.
        Returns:
        The resource without the trailing slash, or as-is if it didn't have a trailing slash.
        Since:
        2.6
      • stripExtension

        public static String stripExtension​(String resourcePath)
        Strips the extension from the given resource path if any. This extension is defined as everything after the last occurrence of a period, including the period itself. E.g. input "index.xhtml" will return "index".
        Parameters:
        resourcePath - The resource path to strip the extension from.
        Returns:
        The resource path without its extension, of as-is if it doesn't have an extension.
      • getExtension

        public static String getExtension​(String resourcePath)
        Gets the extension of given resource path if any. This extension is defined as everything after the last occurrence of a period, including the period itself. E.g. input "index.xhtml" will return ".xhtml'.
        Parameters:
        resourcePath - The resource path to get the extension from.
        Returns:
        the extension of the resource path, or null if it doesn't have an extension.
      • isExtensionless

        public static boolean isExtensionless​(String resourcePath)
        Checks if given resource path is extensionless.
        Parameters:
        resourcePath - The resource path to check.
        Returns:
        true if the resource path is extensionless, false otherwise.
      • filterExtension

        public static Set<String> filterExtension​(Set<String> resourcePaths)
        Filters away every resource path in the given set that has an extension.
        Parameters:
        resourcePaths - A set of resource paths to be filtered
        Returns:
        A set where no resource path has an extension. May be empty, but never null.
      • isRoot

        public static boolean isRoot​(String resourcePath)
        Checks if the given resource path represents the root. That is, if it equals "/".
        Parameters:
        resourcePath - The resource path to check.
        Returns:
        true if the resource path represents the root, false otherwise.
        Since:
        3.0
      • addLeadingSlashIfNecessary

        public static String addLeadingSlashIfNecessary​(String resourcePath)
        Add leading slash to given resource path if necessary.
        Parameters:
        resourcePath - The resource paths to add leading slash to.
        Returns:
        Resource path with leading slash.
        Since:
        3.0
      • addTrailingSlashIfNecessary

        public static String addTrailingSlashIfNecessary​(String resourcePath)
        Add trailing slash to given resource path if necessary.
        Parameters:
        resourcePath - The resource paths to add trailing slash to.
        Returns:
        Resource path with trailing slash.
        Since:
        3.0
      • concat

        public static String concat​(String... resourcePaths)
        Concat given resource paths with the path separator.
        Parameters:
        resourcePaths - The resource paths to concat.
        Returns:
        Concatenated resource paths.
        Since:
        3.0