de.unkrig.commons.file.filetransformation
Class FileTransformations

java.lang.Object
  extended by de.unkrig.commons.file.filetransformation.FileTransformations

public final class FileTransformations
extends java.lang.Object

Various 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.


Nested Class Summary
static interface FileTransformations.ArchiveCombiner
           
static interface FileTransformations.DirectoryCombiner
           
static interface FileTransformations.NameAndContents
           
 
Method Summary
static FileTransformer compressedAndArchiveFileTransformer(Predicate<java.lang.String> lookIntoFormat, Predicate<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<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<java.lang.String> lookIntoFormat, Predicate<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<java.lang.String> removal, Glob renaming, FileTransformer delegate, boolean saveSpace, boolean keepOriginals)
           
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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

remove

public static FileTransformer remove()
If 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.


directoryTreeTransformer

public static DirectoryTransformer directoryTreeTransformer(@Nullable
                                                            java.util.Comparator<java.lang.Object> directoryMemberNameComparator,
                                                            Predicate<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:

Parameters:
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 them
directoryMemberRemoval - Whether to remove (i.e. not copy) a member; the subject is the path of the member
directoryMemberRenaming - Transforms the name of a member
saveSpace - 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 original

renameRemoveFileTransformer

public static FileTransformer renameRemoveFileTransformer(Predicate<java.lang.String> removal,
                                                          Glob renaming,
                                                          FileTransformer delegate,
                                                          boolean saveSpace,
                                                          boolean keepOriginals)
Returns:
A file transformer which implements removal and renaming of files.

compressedAndArchiveFileTransformer

public static FileTransformer compressedAndArchiveFileTransformer(Predicate<java.lang.String> lookIntoFormat,
                                                                  Predicate<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.

Archive files and compressed files are introspected iff lookIntoFormat evaluates true for "format:path".


recursiveCompressedAndArchiveFileTransformer

public static FileTransformer recursiveCompressedAndArchiveFileTransformer(Predicate<java.lang.String> lookIntoFormat,
                                                                           Predicate<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.

Parameters:
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 entry
archiveEntryRenaming - Transforms the name of a subject file or an archive entry
archiveCombiner - Is invoked after the processing of each archive

transform

public static void transform(java.lang.String[] args,
                             FileTransformer fileTransformer,
                             FileTransformer.Mode mode,
                             ExceptionHandler<java.io.IOException> exceptionHandler)
                      throws java.io.IOException
Transforms a set of files based on an array of strings, similar to many UNIX™ command line tools.

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].

Parameters:
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.
Throws:
java.io.IOException - args.length == 0
java.io.IOException - args.length > 2 and args[args.length - 1] is not an existing directory

newFile

public static java.io.File newFile(java.io.File file)
Returns:
A file derived from the file which is typically used to create a temporary file which is later renamed to replace some 'original' file

origFile

public static java.io.File origFile(java.io.File file)
Returns:
A file derived from the file which is typically used as a container to keep an 'original' file