Class TexFileUtils
- java.lang.Object
-
- eu.simuline.m2latex.core.TexFileUtils
-
class TexFileUtils extends Object
Sole interface toorg.apache.commons.io..
-
-
Field Summary
Fields Modifier and Type Field Description private static intDEFAULT_BUFFER_SIZEThe default buffer size (4096) to use forcopyStream(InputStream, OutputStream)private LogWrapperlogprivate static StringPATTERN_INS_LATEX_MAINprivate static StringPREFIX_HIDDEN
-
Constructor Summary
Constructors Constructor Description TexFileUtils(LogWrapper log)
-
Method Summary
Modifier and Type Method Description (package private) voidcleanUp(DirNode orgNode, File texDir)Deletes all files intexDirincluding subdirectories which are not inorgNode.private voidcleanUpRec(File dir, DirNode orgNode, DirNode currNode)Deletes all files incurrNodewhich are not inorgNoderecursively including subdirectories.private voidcloseQuietly(Closeable closeable)Unconditionally close aCloseable.(package private) Collection<String>collectMatches(File file, String regex, int idxGroup)Returns the set of strings representing theidxGroupof the patternregexmatching a line in filefileor returnsnullin case of problems readingfile.(package private) voidcopyOutputToTargetFolder(File texFile, FileFilter fileFilter, File targetDir)Copies output of the current goal to target folder.private static voidcopyStream(InputStream input, OutputStream output)Copy bytes from a large (over 2GB)InputStreamto anOutputStream.(package private) voiddeleteOrError(File delFile)DeletesdelFileor logs a warning.(package private) voiddeleteX(File pFile, FileFilter filter)Deletes all files in the same folder aspFiledirectly, i.e. not in subfolders, which are accepted byfilter.private voiddoCopyFile(File srcFile, File destFile)Internal copy file method.(package private) FileFiltergetFileFilter(File texFile, String pattern)Returns a file filter matching neither directories nortexFilebut else all files with names matchingpattern, where the special sequencePATTERN_INS_LATEX_MAINis replaced by the prefix oftexFile.(package private) FileFiltergetFileFilterReplace(File idxFile, String pattern)Returns a file filter matching no directories but else all files with names matchingxxx, where.idx idxFilehas the formxxx.idx.(package private) StringgetFileNameWithoutSuffix(File file)Return the name of the given file without the suffix.(package private) StringgetSuffix(File file)Return the suffix of the name of the given file including the., except there is no..(package private) FilegetTargetDirectory(File srcFile, File srcBaseDir, File targetBaseDir)Returns the directory containingsourceFilewith the prefixsourceBaseDirreplaced bytargetBaseDir.(package private) File[]listFilesOrWarn(File dir)Returns the listing of the directorydirornullif it is not readable and emit an according warning if so.(package private) File[]listFilesOrWarn(File dir, FileFilter filter)Returns the listing of the directorydirfiltered byfilterornullifdiris not readable and emit an according warning if so.static voidmain(String[] args)(package private) BooleanmatchInFile(File file, String regex)Returns whether the given filefile(which shall exist) contains the given patternpatternornullin case of problems readingfile.(package private) voidmoveOrError(File fromFile, File toFile)Moves filefromFiletotoFileor logs a warning.(package private) FilereplaceSuffix(File file, String suffix)private voidwarnIfNull(File[] files, File dir)
-
-
-
Field Detail
-
PREFIX_HIDDEN
private static final String PREFIX_HIDDEN
- See Also:
- Constant Field Values
-
PATTERN_INS_LATEX_MAIN
private static final String PATTERN_INS_LATEX_MAIN
- See Also:
- Constant Field Values
-
log
private final LogWrapper log
-
DEFAULT_BUFFER_SIZE
private static final int DEFAULT_BUFFER_SIZE
The default buffer size (4096) to use forcopyStream(InputStream, OutputStream)- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TexFileUtils
TexFileUtils(LogWrapper log)
-
-
Method Detail
-
listFilesOrWarn
File[] listFilesOrWarn(File dir)
Returns the listing of the directorydirornullif it is not readable and emit an according warning if so.Logging: WFU01: Cannot read directory
- Parameters:
dir- an existing directory.- Returns:
- the list of entries of
dirornullif it is not readable.
-
listFilesOrWarn
File[] listFilesOrWarn(File dir, FileFilter filter)
Returns the listing of the directorydirfiltered byfilterornullifdiris not readable and emit an according warning if so.Logging: WFU01: Cannot read directory
- Parameters:
dir- an existing directory.filter- a file filter- Returns:
- the list of entries of
diraccepted byfilterornullifdiris not readable.
-
getTargetDirectory
File getTargetDirectory(File srcFile, File srcBaseDir, File targetBaseDir) throws BuildFailureException
Returns the directory containingsourceFilewith the prefixsourceBaseDirreplaced bytargetBaseDir. E.g.sourceFile=/tmp/adir/afile,sourceBaseDir=/tmp,targetBaseDir=/homereturns/home/adir/.- Parameters:
srcFile- the source file the parent directory of which shall be converted to the target.srcBaseDir- the base directory of the source. Immediately or not,sourceFileshall be insourceBaseDir.targetBaseDir- the base directory of the target.- Returns:
- the directory below
targetBaseDirwhich corresponds to the parent directory ofsourceFilewhich is belowsourceBaseDir. - Throws:
BuildFailureException- TFU01: if the target directory that would be returned exists already as a regular file.
-
getFileFilter
FileFilter getFileFilter(File texFile, String pattern)
Returns a file filter matching neither directories nortexFilebut else all files with names matchingpattern, where the special sequencePATTERN_INS_LATEX_MAINis replaced by the prefix oftexFile.- Parameters:
texFile- a latex main file for which a file filter has to be created.pattern- a pattern for which the special sequencePATTERN_INS_LATEX_MAINis replaced by the prefix oftexFilebefore a file filter is created from it.- Returns:
- a non-null file filter matching neither directories
nor
texFilebut else all files with names matchingpattern, where the special sequencePATTERN_INS_LATEX_MAINis replaced by the prefix oftexFile.
-
getFileFilterReplace
FileFilter getFileFilterReplace(File idxFile, String pattern)
Returns a file filter matching no directories but else all files with names matchingxxx, where.idx idxFilehas the formxxx.idx.- Parameters:
idxFile- an idx file for which a file filter has to be created.pattern- a pattern which is inserted in the name ofidxFileright before the suffix.- Returns:
- a non-null file filter matching no directories
but else all files matching
xxx..idx
-
copyOutputToTargetFolder
void copyOutputToTargetFolder(File texFile, FileFilter fileFilter, File targetDir) throws BuildFailureException
Copies output of the current goal to target folder. The source is the parent folder oftexFile, all its files passingfileFilterare considered as output files and are copied totargetDir. This is invoked byLatexProcessor.create()only.Logging:
- WFU01: Cannot read directory...
- WFU03: Cannot close
- Parameters:
texFile- the latex main file which was processed. Its parent directory is the working directory of the compilation process in which the output files are created. Thus it must be readable (in fact it must also be writable; otherwise the output files could not have been created).fileFilter- the filter accepting the files (and best only the files) which are the result of the processing.targetDir- the target directory the output files have to be copied to. If this exists already, it must be a directory and it must be writable. If it does not exist, it must be creatable.- Throws:
BuildFailureException-- TFU04, TFU05 if the destination file exists and is either a directory (TFU04) or is not writable (TFU05).
- TFU06 if an IO-error orrurs when copying: opening streams, reading or writing.
-
doCopyFile
private void doCopyFile(File srcFile, File destFile) throws IOException
Internal copy file method.Logging: WFU03: Cannot close
- Parameters:
srcFile- the source file.destFile- the destination file.- Throws:
IOException- if an error occurs: opening input/output streams, reading from file/writing to file.
-
copyStream
private static void copyStream(InputStream input, OutputStream output) throws IOException
Copy bytes from a large (over 2GB)InputStreamto anOutputStream.This method uses the provided buffer, so there is no need to use a
BufferedInputStream.- Parameters:
input- theInputStreamto read fromoutput- theOutputStreamto write to- Throws:
IOException- if an I/O error occurs while reading or writing
-
closeQuietly
private void closeQuietly(Closeable closeable)
Unconditionally close aCloseable.Equivalent to
Closeable.close(), except any exceptions will be ignored. FIXME This is typically used in finally blocks.Example code:
Closeable closeable = null; try { closeable = new FileReader("foo.txt"); // process closeable closeable.close(); } catch (Exception e) { // error handling } finally { IOUtils.closeQuietly(closeable); }Logging: WFU03: Cannot close
- Parameters:
closeable- the object to close, may be null or already closed
-
getFileNameWithoutSuffix
String getFileNameWithoutSuffix(File file)
Return the name of the given file without the suffix. If the suffix is empty, this is just the name of that file.- See Also:
getSuffix(File)
-
getSuffix
String getSuffix(File file)
Return the suffix of the name of the given file including the., except there is no.. Then the suffix is empty.- See Also:
getFileNameWithoutSuffix(File)
-
matchInFile
Boolean matchInFile(File file, String regex)
Returns whether the given filefile(which shall exist) contains the given patternpatternornullin case of problems readingfile. This is typically applied to log files, but also to latex-files to find the latex main files.Logging: WFU03 cannot close
Note that in casenullis returned, no error/warning is logged. This must be done by the invoking method.- Parameters:
file- an existing proper file, not a folder.regex- the pattern (regular expression) to look for infile.- Returns:
- whether the given file
file(which shall exist) contains the given patternpattern. If the file does not exist or an IOException occurs while reading,nullis returned.
-
collectMatches
Collection<String> collectMatches(File file, String regex, int idxGroup)
Returns the set of strings representing theidxGroupof the patternregexmatching a line in filefileor returnsnullin case of problems readingfile.This is used only to collect the identifiers of explicitly given indices in an idx-file.
- Parameters:
file- an existing proper file, not a folder. In practice this is an idx file.regex- the pattern (regular expression) to look for infile.idxGroup- the number of a group of the patternregex.- Returns:
- the set of strings representing the
idxGroupof the patternregexmatching a line in filefileor returnsnullin case of problems readingfile.
-
deleteX
void deleteX(File pFile, FileFilter filter)
Deletes all files in the same folder aspFiledirectly, i.e. not in subfolders, which are accepted byfilter.Logging:
- WFU01: Cannot read directory...
- EFU05: Failed to delete file
- Parameters:
pFile- a file in a folder to be deleted from. This is either a metapost file or a latex main file.filter- a filter which decides which files from the parent directory ofpFileto delete.
-
deleteOrError
void deleteOrError(File delFile)
DeletesdelFileor logs a warning.Logging: EFU05: failed to delete
- Parameters:
delFile- the existing file to be deleted. This must not be a directory.
-
moveOrError
void moveOrError(File fromFile, File toFile)
Moves filefromFiletotoFileor logs a warning.Logging: EFU06: failed to move.
- Parameters:
fromFile- the existing file to be moved. This must not be a directory.toFile- the file to be moved to This must not be a directory.
-
cleanUp
void cleanUp(DirNode orgNode, File texDir)
Deletes all files intexDirincluding subdirectories which are not inorgNode. The background is, thatorgNoderepresents the files originally intexDir.Logging:
- WFU01: Cannot read directory
- EFU05: Cannot delete...
- Parameters:
orgNode-texDir-
-
cleanUpRec
private void cleanUpRec(File dir, DirNode orgNode, DirNode currNode)
Deletes all files incurrNodewhich are not inorgNoderecursively including subdirectories. The background is, thatorgNoderepresents the files originally in the directory andcurrNodethe current ones at the end of the creating goal.Logging: EFU05: Cannot delete...
- Parameters:
orgNode- the node representing the original files. This is the latex source directory or a subdirectory.currNode- the node representing the current files. This is the latex source directory or a subdirectory.
-
main
public static void main(String[] args)
-
-