Class ExtensionUtilities
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 Summary
FieldsModifier and TypeFieldDescriptionParticular extensions with a double period, that are exceptionally checked for, and treated as a single extension. -
Method Summary
Modifier and TypeMethodDescriptionstatic String
appendExtension
(String pathWithoutExtension, Optional<String> extension) Appends an (optional) extension to a string, adding in the leading period if the extension is defined.static Path
appendExtension
(Path pathWithoutExtension, Optional<String> extension) LikeappendExtension(String, Optional)
but accepts and returns aPath
.extractExtension
(String filename) Extracts the extension from a filename.extractExtension
(Path path) static String
filenameWithoutExtension
(File file) Retrieves filename from aFile
but without any extension.static String
removeExtension
(String filename) Removes an extension from a filename or path.static Path
removeExtension
(Path path) LikeremoveExtension(String)
but accepts and returns aPath
.static FilenameSplitExtension
splitFilename
(String path) Splits the file-name of a path into a base part and an extension part.
-
Field Details
-
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
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
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
- Parameters:
path
- the path to extract an extension for- Returns:
- the extension (excluding any leading period), if it exists.
-
removeExtension
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
LikeremoveExtension(String)
but accepts and returns aPath
.- Parameters:
path
- the path to remove the extension from- Returns:
path
without the extension and any leading period to the extension
-
filenameWithoutExtension
Retrieves filename from aFile
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
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 periodextension
- an extension, if it exists- Returns:
- a string that combines
pathWithoutExtension
and theextension
inserting a leading period, if necessary.
-
appendExtension
LikeappendExtension(String, Optional)
but accepts and returns aPath
.- Parameters:
pathWithoutExtension
- a filename or path without an extension and without any tailing periodextension
- an extension, if it exists- Returns:
- a string that combines
pathWithoutExtension
and theextension
inserting a leading period, if necessary.
-