Class SelectorUtils


  • public final class SelectorUtils
    extends Object

    This is a utility class used by selectors and DirectoryScanner. The functionality more properly belongs just to selectors, but unfortunately DirectoryScanner exposed these as protected methods. Thus we have to support any subclasses of DirectoryScanner that may access these methods.

    This is a Singleton.

    Since:
    1.5
    Version:
    $Id$
    Author:
    Arnout J. Kuiper ajkuiper@wxs.nl, Magesh Umasankar, Bruce Atherton
    • Method Detail

      • matchPatternStart

        public static boolean matchPatternStart​(String pattern,
                                                String str)

        Tests whether or not a given path matches the start of a given pattern up to the first "**".

        This is not a general purpose test and should only be used if you can live with false positives. For example, pattern=**\a and str=b will yield true.

        Parameters:
        pattern - The pattern to match against. Must not be null.
        str - The path to match, as a String. Must not be null.
        Returns:
        whether or not a given path matches the start of a given pattern up to the first "**".
      • matchPatternStart

        public static boolean matchPatternStart​(String pattern,
                                                String str,
                                                boolean isCaseSensitive)

        Tests whether or not a given path matches the start of a given pattern up to the first "**".

        This is not a general purpose test and should only be used if you can live with false positives. For example, pattern=**\a and str=b will yield true.

        Parameters:
        pattern - The pattern to match against. Must not be null.
        str - The path to match, as a String. Must not be null.
        isCaseSensitive - Whether or not matching should be performed case sensitively.
        Returns:
        whether or not a given path matches the start of a given pattern up to the first "**".
      • matchPatternStart

        public static boolean matchPatternStart​(String pattern,
                                                String str,
                                                String separator,
                                                boolean isCaseSensitive)
      • matchAntPathPatternStart

        public static boolean matchAntPathPatternStart​(String pattern,
                                                       String str,
                                                       String separator,
                                                       boolean isCaseSensitive)
      • matchPath

        public static boolean matchPath​(String pattern,
                                        String str)
        Tests whether or not a given path matches a given pattern.
        Parameters:
        pattern - The pattern to match against. Must not be null.
        str - The path to match, as a String. Must not be null.
        Returns:
        true if the pattern matches against the string, or false otherwise.
      • matchPath

        public static boolean matchPath​(String pattern,
                                        String str,
                                        boolean isCaseSensitive)
        Tests whether or not a given path matches a given pattern.
        Parameters:
        pattern - The pattern to match against. Must not be null.
        str - The path to match, as a String. Must not be null.
        isCaseSensitive - Whether or not matching should be performed case sensitively.
        Returns:
        true if the pattern matches against the string, or false otherwise.
      • matchPath

        public static boolean matchPath​(String pattern,
                                        String str,
                                        String separator,
                                        boolean isCaseSensitive)
      • matchAntPathPattern

        public static boolean matchAntPathPattern​(String pattern,
                                                  String str,
                                                  boolean isCaseSensitive)
      • matchAntPathPattern

        public static boolean matchAntPathPattern​(String pattern,
                                                  String str,
                                                  String separator,
                                                  boolean isCaseSensitive)
      • match

        public static boolean match​(String pattern,
                                    String str)
        Tests whether or not a string matches against a pattern. The pattern may contain two special characters:
        '*' means zero or more characters
        '?' means one and only one character
        Parameters:
        pattern - The pattern to match against. Must not be null.
        str - The string which must be matched against the pattern. Must not be null.
        Returns:
        true if the string matches against the pattern, or false otherwise.
      • match

        public static boolean match​(String pattern,
                                    String str,
                                    boolean isCaseSensitive)
        Tests whether or not a string matches against a pattern. The pattern may contain two special characters:
        '*' means zero or more characters
        '?' means one and only one character
        Parameters:
        pattern - The pattern to match against. Must not be null.
        str - The string which must be matched against the pattern. Must not be null.
        isCaseSensitive - Whether or not matching should be performed case sensitively.
        Returns:
        true if the string matches against the pattern, or false otherwise.
      • equals

        public static boolean equals​(char c1,
                                     char c2,
                                     boolean isCaseSensitive)
        Tests whether two characters are equal.
        Parameters:
        c1 - 1st character
        c2 - 2nd character
        isCaseSensitive - Whether to compare case sensitive
        Returns:
        true if equal characters
      • tokenizePath

        public static List<String> tokenizePath​(String path)
        Breaks a path up into a Vector of path elements, tokenizing on File.separator.
        Parameters:
        path - Path to tokenize. Must not be null.
        Returns:
        a List of path elements from the tokenized path
      • translateToLocalFileSystemPath

        public static String translateToLocalFileSystemPath​(String path,
                                                            char pathSeparator,
                                                            FileSystem fs)
        /** Converts a path to one matching the target file system by applying the "slashification" rules, converting it to a local path and then translating its separator to the target file system one (if different than local one)
        Parameters:
        path - The input path
        pathSeparator - The separator used to build the input path
        fs - The target FileSystem - may not be null
        Returns:
        The transformed path
        See Also:
        translateToLocalFileSystemPath(String, char, String)
      • translateToLocalFileSystemPath

        public static String translateToLocalFileSystemPath​(String path,
                                                            char pathSeparator,
                                                            String fsSeparator)
        Converts a path to one matching the target file system by applying the "slashification" rules, converting it to a local path and then translating its separator to the target file system one (if different than local one)
        Parameters:
        path - The input path
        pathSeparator - The separator used to build the input path
        fsSeparator - The target file system separator
        Returns:
        The transformed path
        See Also:
        applySlashifyRules(String, char), translateToLocalPath(String), translateToFileSystemPath(String, String, String)
      • translateToLocalPath

        public static String translateToLocalPath​(String path)
        Converts a possibly '/' separated path to a local path. Note: takes special care of Windows drive paths - e.g., C: by converting them to "C:\"
        Parameters:
        path - The original path - ignored if null/empty
        Returns:
        The local path
      • isWindowsDriveSpecified

        public static boolean isWindowsDriveSpecified​(CharSequence cs)
      • isWindowsDriveSpecified

        public static boolean isWindowsDriveSpecified​(CharSequence cs,
                                                      int offset,
                                                      int len)
      • translateToFileSystemPath

        public static String translateToFileSystemPath​(String path,
                                                       String pathSeparator,
                                                       FileSystem fs)
        Converts a path containing a specific separator to one using the specified file-system one
        Parameters:
        path - The input path - ignored if null/empty
        pathSeparator - The separator used to build the input path - may not be null/empty
        fs - The target FileSystem - may not be null
        Returns:
        The path where the separator used to build it is replaced by the file-system one (if different)
        See Also:
        FileSystem.getSeparator(), translateToFileSystemPath(String, String, String)
      • translateToFileSystemPath

        public static String translateToFileSystemPath​(String path,
                                                       String pathSeparator,
                                                       String fsSeparator)
        Converts a path containing a specific separator to one using the specified file-system one
        Parameters:
        path - The input path - ignored if null/empty
        pathSeparator - The separator used to build the input path - may not be null/empty
        fsSeparator - The target file system separator - may not be null/empty
        Returns:
        The path where the separator used to build it is replaced by the file-system one (if different)
        Throws:
        IllegalArgumentException - if path or file-system separator are null/empty or if the separators are different and the path contains the target file-system separator as it would create an ambiguity
      • concatPaths

        public static String concatPaths​(String p1,
                                         String p2,
                                         char fsSeparator)
        Creates a single path by concatenating 2 parts and taking care not to create FS separator duplication in the process
        Parameters:
        p1 - prefix part - ignored if null/empty
        p2 - suffix part - ignored if null/empty
        fsSeparator - The expected file-system separator
        Returns:
        Concatenation result
      • removeWhitespace

        public static String removeWhitespace​(String input)
        "Flattens" a string by removing all whitespace (space, tab, line-feed, carriage return, and form-feed). This uses StringTokenizer and the default set of tokens as documented in the single argument constructor.
        Parameters:
        input - a String to remove all whitespace.
        Returns:
        a String that has had all whitespace removed.