public class ExtensionUtilities extends Object
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.
Modifier and Type | Field and Description |
---|---|
static List<String> |
EXCEPTED_DOUBLE_EXTENSIONS
Particular extensions with a double period, that are exceptionally checked for, and treated
as a single extension.
|
Modifier and Type | Method and Description |
---|---|
static Path |
appendExtension(Path pathWithoutExtension,
Optional<String> extension)
Like
appendExtension(String, Optional) but accepts and returns a
Path . |
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.
|
static Optional<String> |
extractExtension(Path path)
|
static Optional<String> |
extractExtension(String filename)
Extracts the extension from a filename.
|
static String |
filenameWithoutExtension(File file)
Retrieves filename from a
File but without any extension. |
static Path |
removeExtension(Path path)
Like
removeExtension(String) but accepts and returns a Path . |
static String |
removeExtension(String filename)
Removes an extension from a filename or path.
|
static FilenameSplitExtension |
splitFilename(String path)
Splits the file-name of a path into a base part and an extension part.
|
public static final List<String> EXCEPTED_DOUBLE_EXTENSIONS
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.
public static FilenameSplitExtension splitFilename(String path)
The rules for how this split occurs are described in the class-level documentation.
path
- the path containing the filename to split (the directory components of the path
are ignored)public static Optional<String> extractExtension(String filename)
The rules for what is considered an extension are described in the class-level documentation.
filename
- the filename from which an extension will be extracted, or a path (in which
case the directory components are ignored)public static Optional<String> extractExtension(Path path)
path
- the path to extract an extension forpublic static String removeExtension(String filename)
filename
- the filename or path to remove the extension fromfilename
without the extension and any leading period to the extensionpublic static Path removeExtension(Path path)
removeExtension(String)
but accepts and returns a Path
.path
- the path to remove the extension frompath
without the extension and any leading period to the extensionpublic static String filenameWithoutExtension(File file)
File
but without any extension.
Note any directory components are ignored! Only the name of the file is returned.
file
- a file to remove the extension fromfile
without the extension and any leading period to the
extension..public static String appendExtension(String pathWithoutExtension, Optional<String> extension)
pathWithoutExtension
- a filename or path without an extension and without any
tailing periodextension
- an extension, if it existspathWithoutExtension
and the extension
inserting a leading period, if necessary.public static Path appendExtension(Path pathWithoutExtension, Optional<String> extension)
appendExtension(String, Optional)
but accepts and returns a
Path
.pathWithoutExtension
- a filename or path without an extension and without any
tailing periodextension
- an extension, if it existspathWithoutExtension
and the extension
inserting a leading period, if necessary.Copyright © 2010–2023 Owen Feehan, ETH Zurich, University of Zurich, Hoffmann-La Roche. All rights reserved.