Class ExtensionUtilities

Object
ExtensionUtilities

public class ExtensionUtilities extends Object
Extracting a file-extension from a path according to rules.

When a path contains only a single period, it is easy to reliably infer the extension.

However, some image file formats (e.g. OME-XML and OME-TIFF) have double periods, as specified in EXCEPTED_DOUBLE_EXTENSIONS.

This class will ordinarily assume an extension is what follows the final period in a filename. Any preceding second period will be treated as part of the file-name.

Some hard-coded exceptions exist for the OME file-types, and in these cases the double-period extensions are extracted.

This class is intended as a singular centralized method for extracting extensions from filenames in Anchor, so identical rules can be applied in all situations.

Author:
Owen Feehan
  • Field Details

    • EXCEPTED_DOUBLE_EXTENSIONS

      public static final List<String> EXCEPTED_DOUBLE_EXTENSIONS
      Particular extensions with a double period, that are exceptionally checked for, and treated as a single extension.

      Otherwise, in the presence of two periods, only the characters after the final period are considered the extension.

      An explanation of many of the extensions can be found on imagej.net

      Importantly, note the leading period in each entry in this list.

  • Method Details

    • splitFilename

      public static FilenameSplitExtension splitFilename(String path)
      Splits the file-name of a path into a base part and an extension part.

      The rules for how this split occurs are described in the class-level documentation.

      Parameters:
      path - the path containing the filename to split (the directory components of the path are ignored)
      Returns:
      a newly-created class describing the split filename
    • extractExtension

      public static Optional<String> extractExtension(String filename)
      Extracts the extension from a filename.

      The rules for what is considered an extension are described in the class-level documentation.

      Parameters:
      filename - the filename from which an extension will be extracted, or a path (in which case the directory components are ignored)
      Returns:
      the extension (excluding any leading period), if it exists.
    • extractExtension

      public static Optional<String> extractExtension(Path path)
      Like extractExtension(String) but accepts a Path instead of a String.
      Parameters:
      path - the path to extract an extension for
      Returns:
      the extension (excluding any leading period), if it exists.
    • removeExtension

      public static String removeExtension(String filename)
      Removes an extension from a filename or path.
      Parameters:
      filename - the filename or path to remove the extension from
      Returns:
      filename without the extension and any leading period to the extension
    • removeExtension

      public static Path removeExtension(Path path)
      Like removeExtension(String) but accepts and returns a Path.
      Parameters:
      path - the path to remove the extension from
      Returns:
      path without the extension and any leading period to the extension
    • filenameWithoutExtension

      public static String filenameWithoutExtension(File file)
      Retrieves filename from a File but without any extension.

      Note any directory components are ignored! Only the name of the file is returned.

      Parameters:
      file - a file to remove the extension from
      Returns:
      the name of file without the extension and any leading period to the extension..
    • appendExtension

      public static String appendExtension(String pathWithoutExtension, Optional<String> extension)
      Appends an (optional) extension to a string, adding in the leading period if the extension is defined.
      Parameters:
      pathWithoutExtension - a filename or path without an extension and without any tailing period
      extension - an extension, if it exists
      Returns:
      a string that combines pathWithoutExtension and the extension inserting a leading period, if necessary.
    • appendExtension

      public static Path appendExtension(Path pathWithoutExtension, Optional<String> extension)
      Like appendExtension(String, Optional) but accepts and returns a Path.
      Parameters:
      pathWithoutExtension - a filename or path without an extension and without any tailing period
      extension - an extension, if it exists
      Returns:
      a string that combines pathWithoutExtension and the extension inserting a leading period, if necessary.