public final class FileTransformations extends 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 archives nor 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 String> lookIntoFormat,
Predicate<? super String> archiveEntryRemoval,
de.unkrig.commons.text.pattern.Glob archiveEntryRenaming,
FileTransformations.ArchiveCombiner archiveCombiner,
ContentsTransformer archiveEntryContentsTransformer,
ContentsTransformer compressedContentsTransformer,
ContentsTransformer normalContentsTransformer,
boolean keepOriginals,
ExceptionHandler<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(Comparator<Object> directoryMemberNameComparator,
Predicate<? super String> directoryMemberRemoval,
de.unkrig.commons.text.pattern.Glob directoryMemberRenaming,
FileTransformations.DirectoryCombiner directoryCombiner,
FileTransformer regularFileTransformer,
boolean saveSpace,
boolean keepOriginals,
ExceptionHandler<IOException> exceptionHandler)
Creates and returns a
DirectoryTransformer which implements the following features:
Recursive processing of directories
Removal, renaming and addition of directory members
|
static File |
newFile(File file) |
static File |
origFile(File file) |
static FileTransformer |
recursiveCompressedAndArchiveFileTransformer(Predicate<? super String> lookIntoFormat,
Predicate<? super String> archiveEntryRemoval,
de.unkrig.commons.text.pattern.Glob archiveEntryRenaming,
FileTransformations.ArchiveCombiner archiveCombiner,
ContentsTransformer delegate,
boolean keepOriginals,
ExceptionHandler<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 String> removal,
de.unkrig.commons.text.pattern.Glob renaming,
FileTransformer delegate,
boolean keepOriginals)
Wraps the given delegate in a
FileTransformer which handles removal and renaming of files. |
static void |
transform(String[] args,
FileTransformer fileTransformer,
FileTransformer.Mode mode,
ExceptionHandler<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
public static FileTransformer remove()
public static DirectoryTransformer directoryTreeTransformer(@Nullable Comparator<Object> directoryMemberNameComparator, Predicate<? super String> directoryMemberRemoval, de.unkrig.commons.text.pattern.Glob directoryMemberRenaming, FileTransformations.DirectoryCombiner directoryCombiner, FileTransformer regularFileTransformer, boolean saveSpace, boolean keepOriginals, ExceptionHandler<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 String> removal, de.unkrig.commons.text.pattern.Glob renaming, FileTransformer delegate, boolean keepOriginals)
FileTransformer
which handles removal and renaming of files.public static FileTransformer compressedAndArchiveFileTransformer(Predicate<? super String> lookIntoFormat, Predicate<? super String> archiveEntryRemoval, de.unkrig.commons.text.pattern.Glob archiveEntryRenaming, FileTransformations.ArchiveCombiner archiveCombiner, ContentsTransformer archiveEntryContentsTransformer, ContentsTransformer compressedContentsTransformer, ContentsTransformer normalContentsTransformer, boolean keepOriginals, ExceptionHandler<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 to true
for "format:path".
public static FileTransformer recursiveCompressedAndArchiveFileTransformer(Predicate<? super String> lookIntoFormat, Predicate<? super String> archiveEntryRemoval, de.unkrig.commons.text.pattern.Glob archiveEntryRenaming, FileTransformations.ArchiveCombiner archiveCombiner, ContentsTransformer delegate, boolean keepOriginals, ExceptionHandler<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(String[] args, FileTransformer fileTransformer, FileTransformer.Mode mode, ExceptionHandler<IOException> exceptionHandler) throws 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(String, Exception)
resp. ExceptionHandler.handle(String, RuntimeException)
is called. Iff that call
completes normally, then processing continues with the next inputFile.IOException
- args.length == 0
IOException
- args.length > 2
and args[args.length - 1]
is not an existing directorypublic static File newFile(File file)
Copyright © 2018 Arno Unkrig. All rights reserved.