Holger's Java API

## com.antelmann.io
Class ExtensionFileFilter

    java.lang.Object
      javax.swing.filechooser.FileFilter
          com.antelmann.io.ExtensionFileFilter

All Implemented Interfaces:
  Filter<File>, FileFilter, FilenameFilter

    public class ExtensionFileFilter

extends FileFilter
implements FileFilter, FilenameFilter, Filter<File>

ExtensionFileFilter is a flexible multi-purpose FileFilter. ExtensionFileFilter implements java.io.FileFilter as well as extending javax.swing.filechooser.FileFilter, so it can be used for GUIs as well as for other more general purposes.

ExtensionFileFilter keeps a default type, which can be used when saving a file when no file type was given (in the case of using ExtensionFileFilter as a javax.swing.filechooser.FileFilter). If a ExtensionFileFilter is constructed with the argument null for the default type, all file types will be accepted regardless of any additional added types; pattern matching will still take place.

As for the pattern matching capabilities:
By default, no pattern matching takes place until you explicitly add include or exclude patterns. If any patterns are present (i.e. getIncludePattern() or getExcludePattern() return an array with more than zero elements), the file is loaded as a String into memory and is checked against the given patterns. Note, therefore, that include/exclude patterns should only be used for small/medium sized files; if a file is too big to fit into memory to perform the pattern search, `accept()` will return `false`.
If include and/or exclude patterns are set, a file is accepted by this MyFilter only if all include patterns are found AND all exclude patterns are not found in the file. If any patterns are set and - during pattern matching - an IOException is caught, the file will not be accepted.
Note that include and/or exclude patterns should only be used if all files that may run through the accept() method fit into memory.

Author:
  Holger Antelmann

Field Summary

Fields inherited from interface com.antelmann.util.Filter
`EXCLUDE_ALL_FILTER, INCLUDE_ALL_FILTER`

Constructor Summary
`ExtensionFileFilter()` provides a default filter for text files (*.txt)
`ExtensionFileFilter(ExtensionFileFilter filter)`
`ExtensionFileFilter(String defaultExtensionType)`
`ExtensionFileFilter(String defaultType, String description)` calls ExtensionFileFilter(defaultType, description, true)
`ExtensionFileFilter(String defaultType, String description, boolean acceptDirectories)` if the defaultType is null, all files are initially accepted

Method Summary
`boolean` `accept(File f)` checks file type and include/exclude pattern if applicable.
`boolean` `accept(File dir, String name)`
`boolean` `addExcludePattern(String pattern)` adds a pattern to be excluded in a file to be accepted
`boolean` `addIncludePattern(String pattern)` adds a pattern to be included in a file to be accepted
`ExtensionFileFilter` `addType(String... type)` allows to add additional file types to be accepted by this filter.
`boolean` `getAcceptDirectories()`
`String[]` `getAddedTypes()` returns file types other than the default type if they were added through `addType(String)`.
`String` `getDefaultType()` returns the default type for this filter
`String` `getDescription()`
`String[]` `getExcludePattern()` returns all exclude patterns to be checked (empty array returned if none)
`String[]` `getIncludePattern()` returns all include patterns to be checked (empty array returned if none)
`void` `removeAllPattern()` removes all include and exclude patterns and ensures therefore that the accept() method doesn't have to load the content of a file
`boolean` `removeExcludePattern(String pattern)` removes a pattern to be included in a file to be accepted
`boolean` `removeIncludePattern(String pattern)` removes a pattern to be included in a file to be accepted
`boolean` `removeType(String type)` removes previously added type.
`void` `setAcceptDirectories(boolean flag)`
`String` `toString()`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

Constructor Detail

### ExtensionFileFilter

    public ExtensionFileFilter(ExtensionFileFilter filter)

### ExtensionFileFilter

    public ExtensionFileFilter()

  provides a default filter for text files (*.txt)

### ExtensionFileFilter

    public ExtensionFileFilter(String defaultExtensionType)

### ExtensionFileFilter

    public ExtensionFileFilter(String defaultType,
                               String description)

  calls ExtensionFileFilter(defaultType, description, true)

### ExtensionFileFilter

    public ExtensionFileFilter(String defaultType,
                               String description,
                               boolean acceptDirectories)

  if the defaultType is null, all files are initially accepted

  Parameters:
    `defaultType` - a string specifying the default type (example: "txt"); the string will be converted to lowercase
    `description` - a string with a description (example: "Text files (*.txt)")
    `acceptDirectories` - if true, directories are accepted by this filter

Method Detail

### setAcceptDirectories

    public void setAcceptDirectories(boolean flag)

### getAcceptDirectories

    public boolean getAcceptDirectories()

### addType

    public ExtensionFileFilter addType(String... type)

  allows to add additional file types to be accepted by this filter. Note that if the default type is null, added types have no effect as all file types are then accepted (unless restricted by patterns).

  See Also:
    `getDefaultType()`

### removeType

    public boolean removeType(String type)

  removes previously added type. This doesn't ever affect the default type. Note that if the default type is null, added types have no effect as all file types are then accepted (unless restricted by patterns).

  See Also:
    `getDefaultType()`

### getAddedTypes

    public String[] getAddedTypes()

  returns file types other than the default type if they were added through `addType(String)`. Note that if the default type is null, added types have no effect as all file types are then accepted (unless restricted by patterns).

  See Also:
    `getDefaultType()`

### addIncludePattern

    public boolean addIncludePattern(String pattern)

  adds a pattern to be included in a file to be accepted

### addExcludePattern

    public boolean addExcludePattern(String pattern)

  adds a pattern to be excluded in a file to be accepted

### removeIncludePattern

    public boolean removeIncludePattern(String pattern)

  removes a pattern to be included in a file to be accepted

### removeExcludePattern

    public boolean removeExcludePattern(String pattern)

  removes a pattern to be included in a file to be accepted

### getIncludePattern

    public String[] getIncludePattern()

  returns all include patterns to be checked (empty array returned if none)

### getExcludePattern

    public String[] getExcludePattern()

  returns all exclude patterns to be checked (empty array returned if none)

### removeAllPattern

    public void removeAllPattern()

  removes all include and exclude patterns and ensures therefore that the accept() method doesn't have to load the content of a file

### accept

    public boolean accept(File dir,
                          String name)

  Specified by:
    `accept` in interface `FilenameFilter`

### accept

    public boolean accept(File f)

  checks file type and include/exclude pattern if applicable. Directories are always accepted.

  Specified by:
    `accept` in interface `Filter<File>`
  Specified by:
    `accept` in interface `FileFilter`
  Specified by:
    `accept` in class `FileFilter`

### getDescription

    public String getDescription()

  Specified by:
    `getDescription` in class `FileFilter`

### getDefaultType

    public String getDefaultType()

  returns the default type for this filter

### toString

    public String toString()

  Overrides:
    `toString` in class `Object`

(c) Holger Antelmann since 2001- all rights reserved (contact: info@antelmann.com)
see www.antelmann.com/developer for further details and available downloads