public final class FileTransformations
extends java.lang.Object
FileTransformer-related utility methods.
A typical application is to wrap file transformers and content transformers as follows:
|
v
directoryTreeTransformer()
|
v
recursiveCompressedAndArchiveFileTransformer()
|
v
(some contents fransformer, e.g. 'ContentsTransformerUtil.NOP')
The directoryTreeTransformer()) can be left out if you don't need recursive directory
traversal.
The recursiveCompressedAndArchiveFileTransformer() can be left out if
you don't want to look into archive /
compressed files.
| Modifier and Type | Class and Description |
|---|---|
static interface |
FileTransformations.ArchiveCombiner |
static interface |
FileTransformations.DirectoryCombiner |
static interface |
FileTransformations.NameAndContents |
| Modifier and Type | Field and Description |
|---|---|
static FileTransformer |
UNCHANGED
If
in equals out ("in-place transformation"), then file is left untouched. |
| Modifier and Type | Method and Description |
|---|---|
static FileTransformer |
compressedAndArchiveFileTransformer(Predicate<? super java.lang.String> lookIntoFormat,
Predicate<? super java.lang.String> archiveEntryRemoval,
Glob archiveEntryRenaming,
FileTransformations.ArchiveCombiner archiveCombiner,
ContentsTransformer archiveEntryContentsTransformer,
ContentsTransformer compressedContentsTransformer,
ContentsTransformer normalContentsTransformer,
boolean keepOriginals,
ExceptionHandler<java.io.IOException> exceptionHandler)
Returns a
FileTransformer which transforms files by feeding their contents through the normalContentsTransformer (just like the FileContentsTransformer), or, iff the file is an archive
file, by feeding the archive file's entries through the archiveEntryContentsTransformer, or, iff the
file is a compressed file, be feeding the compressed file's decompressed contents through the compressedContentsTransformer. |
static DirectoryTransformer |
directoryTreeTransformer(java.util.Comparator<java.lang.Object> directoryMemberNameComparator,
Predicate<? super java.lang.String> directoryMemberRemoval,
Glob directoryMemberRenaming,
FileTransformations.DirectoryCombiner directoryCombiner,
FileTransformer regularFileTransformer,
boolean saveSpace,
boolean keepOriginals,
ExceptionHandler<java.io.IOException> exceptionHandler)
Creates and returns a
DirectoryTransformer which implements the following features:
Recursive processing of directories
Removal, renaming and addition of directory members
|
static java.io.File |
newFile(java.io.File file) |
static java.io.File |
origFile(java.io.File file) |
static FileTransformer |
recursiveCompressedAndArchiveFileTransformer(Predicate<? super java.lang.String> lookIntoFormat,
Predicate<? super java.lang.String> archiveEntryRemoval,
Glob archiveEntryRenaming,
FileTransformations.ArchiveCombiner archiveCombiner,
ContentsTransformer delegate,
boolean keepOriginals,
ExceptionHandler<java.io.IOException> exceptionHandler)
Returns a
FileTransformer which transforms files by feeding their contents through the delegate
(just like the FileContentsTransformer), but automagically detects various archive and compression
formats (also nested) and processes the entries of the archive and the uncompressed contents
instead of the "raw" contents. |
static FileTransformer |
remove()
If
in equals out ("in-place transformation"), then file in is deleted. |
static FileTransformer |
renameRemoveFileTransformer(Predicate<? super java.lang.String> removal,
Glob renaming,
FileTransformer delegate,
boolean keepOriginals)
Wraps the given delegate in a
FileTransformer which handles removal and renaming of files. |
static void |
transform(java.lang.String[] args,
FileTransformer fileTransformer,
FileTransformer.Mode mode,
ExceptionHandler<java.io.IOException> exceptionHandler)
Transforms a set of files based on an array of strings, similar to many UNIX™ command line tools.
|
public static final FileTransformer UNCHANGED
in equals out ("in-place transformation"), then file is left untouched. Otherwise
("out-of-place transformation") in is copied byte-by-byte to out.public static FileTransformer remove()
in equals out ("in-place transformation"), then file in is deleted. Otherwise
("out-of-place transformation") nothing is done, i.e. a file out is not created.public static DirectoryTransformer directoryTreeTransformer(@Nullable java.util.Comparator<java.lang.Object> directoryMemberNameComparator, Predicate<? super java.lang.String> directoryMemberRemoval, Glob directoryMemberRenaming, FileTransformations.DirectoryCombiner directoryCombiner, FileTransformer regularFileTransformer, boolean saveSpace, boolean keepOriginals, ExceptionHandler<java.io.IOException> exceptionHandler)
DirectoryTransformer which implements the following features:
directoryMemberNameComparator - The comparator used to sort a directory's members; a null value
means to NOT sort the members, i.e. leave them in their 'natural' order as
File.list() returns themdirectoryMemberRemoval - Whether to remove (i.e. not copy) a member; the subject is the path of the
memberdirectoryMemberRenaming - Transforms the name of a membersaveSpace - If true, then the method attempts to save file system space for
in-place transformations by transforming each file, otherwise it
creates a copy of the entire directory tree before deleting the originalpublic static FileTransformer renameRemoveFileTransformer(Predicate<? super java.lang.String> removal, Glob renaming, FileTransformer delegate, boolean keepOriginals)
FileTransformer which handles removal and renaming of files.public static FileTransformer compressedAndArchiveFileTransformer(Predicate<? super java.lang.String> lookIntoFormat, Predicate<? super java.lang.String> archiveEntryRemoval, Glob archiveEntryRenaming, FileTransformations.ArchiveCombiner archiveCombiner, ContentsTransformer archiveEntryContentsTransformer, ContentsTransformer compressedContentsTransformer, ContentsTransformer normalContentsTransformer, boolean keepOriginals, ExceptionHandler<java.io.IOException> exceptionHandler)
FileTransformer which transforms files by feeding their contents through the normalContentsTransformer (just like the FileContentsTransformer), or, iff the file is an archive
file, by feeding the archive file's entries through the archiveEntryContentsTransformer, or, iff the
file is a compressed file, be feeding the compressed file's decompressed contents through the compressedContentsTransformer.
Archive files and compressed files are introspected iff lookIntoFormat evaluates true for
"format:path".
public static FileTransformer recursiveCompressedAndArchiveFileTransformer(Predicate<? super java.lang.String> lookIntoFormat, Predicate<? super java.lang.String> archiveEntryRemoval, Glob archiveEntryRenaming, FileTransformations.ArchiveCombiner archiveCombiner, ContentsTransformer delegate, boolean keepOriginals, ExceptionHandler<java.io.IOException> exceptionHandler)
FileTransformer which transforms files by feeding their contents through the delegate
(just like the FileContentsTransformer), but automagically detects various archive and compression
formats (also nested) and processes the entries of the archive and the uncompressed contents
instead of the "raw" contents.lookIntoFormat - Whether the file/entry should be introspected; the subject is
"format:container-path"archiveEntryRemoval - Whether to remove (i.e. not copy) a subject file or an archive entry; the subject is
the path of the subject file resp. the archive entryarchiveEntryRenaming - Transforms the name of a subject file or an archive
entryarchiveCombiner - Is invoked after the processing of each archivepublic static void transform(java.lang.String[] args,
FileTransformer fileTransformer,
FileTransformer.Mode mode,
ExceptionHandler<java.io.IOException> exceptionHandler)
throws java.io.IOException
If args.length == 1, then the file or directory args[0] is transformed in-place.
If args.length == 2 and args[1] is not an existing directory, then the file args[0] is
transformed into the file args[1].
If args.length >= 2 and args[args.length - 1] is an existing directory, then the files args[0] ... args[args.length - 2] are transformed into files with the same names in the directory
args[args.length - 1].
exceptionHandler - If the transformation of one of the inputFiles throws an IOException or
a RuntimeException, then ExceptionHandler.handle(Exception) resp. ExceptionHandler.handle(RuntimeException) is called. Iff that call completes normally,
then processing continues with the next inputFile.java.io.IOException - args.length == 0java.io.IOException - args.length > 2 and args[args.length - 1] is not an existing directorypublic static java.io.File newFile(java.io.File file)
file which is typically used to create a temporary file which is later
renamed to replace some 'original' filepublic static java.io.File origFile(java.io.File file)
file which is typically used as a container to keep an 'original' file