Class WildcardFileFilter

  • All Implemented Interfaces:
    java.io.Serializable, FileFilter

    public class WildcardFileFilter
    extends java.lang.Object
    implements FileFilter, java.io.Serializable
    Filters files using the supplied wildcards.

    This filter selects files and directories based on one or more wildcards. Testing is case-sensitive by default, but this can be configured.

    The wildcard matcher uses the characters '?' and '*' to represent a single or multiple wildcard characters. This is the same as often found on Dos/Unix command lines.

    For example, to retrieve and print all java files that have the expression test in the name in the current directory:

     FileSystemManager fsManager = VFS.getManager();
     FileObject dir = fsManager.toFileObject(new File("."));
     FileObject[] files;
     files = dir.findFiles(new FileFilterSelector(new WildcardFileFilter("*test*.java")));
     for (int i = 0; i < files.length; i++) {
         System.out.println(files[i]);
     }
     
    Since:
    2.4
    See Also:
    "http://commons.apache.org/proper/commons-io/", Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private IOCase caseSensitivity
      Whether the comparison is case sensitive.
      private static long serialVersionUID  
      private java.util.List<java.lang.String> wildcards
      The wildcards that will be used to match file names.
    • Constructor Summary

      Constructors 
      Constructor Description
      WildcardFileFilter​(java.lang.String... wildcards)
      Construct a new case-sensitive wildcard filter for an array of wildcards.
      WildcardFileFilter​(java.util.List<java.lang.String> wildcards)
      Construct a new case-sensitive wildcard filter for a list of wildcards.
      WildcardFileFilter​(IOCase caseSensitivity, java.lang.String... wildcards)
      Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.
      WildcardFileFilter​(IOCase caseSensitivity, java.util.List<java.lang.String> wildcards)
      Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      boolean accept​(FileSelectInfo fileSelectInfo)
      Checks to see if the file name matches one of the wildcards.
      (package private) static java.lang.String[] splitOnTokens​(java.lang.String text)
      Splits a string into a number of tokens.
      java.lang.String toString()
      Provide a String representation of this file filter.
      (package private) static boolean wildcardMatch​(java.lang.String fileName, java.lang.String wildcardMatcher, IOCase caseSensitivity)
      Checks a file name to see if it matches the specified wildcard matcher allowing control over case-sensitivity.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • caseSensitivity

        private final IOCase caseSensitivity
        Whether the comparison is case sensitive.
      • wildcards

        private final java.util.List<java.lang.String> wildcards
        The wildcards that will be used to match file names.
    • Constructor Detail

      • WildcardFileFilter

        public WildcardFileFilter​(java.util.List<java.lang.String> wildcards)
        Construct a new case-sensitive wildcard filter for a list of wildcards.
        Parameters:
        wildcards - the list of wildcards to match, not null
      • WildcardFileFilter

        public WildcardFileFilter​(IOCase caseSensitivity,
                                  java.util.List<java.lang.String> wildcards)
        Construct a new wildcard filter for a list of wildcards specifying case-sensitivity.
        Parameters:
        caseSensitivity - how to handle case sensitivity, null means case-sensitive
        wildcards - the list of wildcards to match, not null
      • WildcardFileFilter

        public WildcardFileFilter​(java.lang.String... wildcards)
        Construct a new case-sensitive wildcard filter for an array of wildcards.

        The array is not cloned, so could be changed after constructing the instance. This would be inadvisable however.

        Parameters:
        wildcards - the array of wildcards to match
      • WildcardFileFilter

        public WildcardFileFilter​(IOCase caseSensitivity,
                                  java.lang.String... wildcards)
        Construct a new wildcard filter for an array of wildcards specifying case-sensitivity.
        Parameters:
        caseSensitivity - how to handle case sensitivity, null means case-sensitive
        wildcards - the array of wildcards to match, not null
    • Method Detail

      • accept

        public boolean accept​(FileSelectInfo fileSelectInfo)
        Checks to see if the file name matches one of the wildcards.
        Specified by:
        accept in interface FileFilter
        Parameters:
        fileSelectInfo - the file to check
        Returns:
        true if the file name matches one of the wildcards
      • toString

        public java.lang.String toString()
        Provide a String representation of this file filter.
        Overrides:
        toString in class java.lang.Object
        Returns:
        a String representation
      • splitOnTokens

        static java.lang.String[] splitOnTokens​(java.lang.String text)
        Splits a string into a number of tokens. The text is split by '?' and '*'. Where multiple '*' occur consecutively they are collapsed into a single '*'.
        Parameters:
        text - the text to split
        Returns:
        the array of tokens, never null
      • wildcardMatch

        static boolean wildcardMatch​(java.lang.String fileName,
                                     java.lang.String wildcardMatcher,
                                     IOCase caseSensitivity)
        Checks a file name to see if it matches the specified wildcard matcher allowing control over case-sensitivity.

        The wildcard matcher uses the characters '?' and '*' to represent a single or multiple (zero or more) wildcard characters. N.B. the sequence "*?" does not work properly at present in match strings.

        Parameters:
        fileName - the file name to match on
        wildcardMatcher - the wildcard string to match against
        caseSensitivity - what case sensitivity rule to use, null means case-sensitive
        Returns:
        true if the file name matches the wilcard string