Class TexFileUtils


  • class TexFileUtils
    extends Object
    Sole interface to org.apache.commons.io..
    • Constructor Detail

    • Method Detail

      • listFilesOrWarn

        File[] listFilesOrWarn​(File dir)
        Returns the listing of the directory dir or null if 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 dir or null if it is not readable.
      • listFilesOrWarn

        File[] listFilesOrWarn​(File dir,
                               FileFilter filter)
        Returns the listing of the directory dir filtered by filter or null if dir is 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 dir accepted by filter or null if dir is not readable.
      • warnIfNull

        private void warnIfNull​(File[] files,
                                File dir)
      • getTargetDirectory

        File getTargetDirectory​(File srcFile,
                                File srcBaseDir,
                                File targetBaseDir)
                         throws BuildFailureException
        Returns the directory containing sourceFile with the prefix sourceBaseDir replaced by targetBaseDir. E.g. sourceFile=/tmp/adir/afile, sourceBaseDir=/tmp, targetBaseDir=/home returns /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, sourceFile shall be in sourceBaseDir.
        targetBaseDir - the base directory of the target.
        Returns:
        the directory below targetBaseDir which corresponds to the parent directory of sourceFile which is below sourceBaseDir.
        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 nor texFile but else all files with names matching pattern, where the special sequence PATTERN_INS_LATEX_MAIN is replaced by the prefix of texFile.
        Parameters:
        texFile - a latex main file for which a file filter has to be created.
        pattern - a pattern for which the special sequence PATTERN_INS_LATEX_MAIN is replaced by the prefix of texFile before a file filter is created from it.
        Returns:
        a non-null file filter matching neither directories nor texFile but else all files with names matching pattern, where the special sequence PATTERN_INS_LATEX_MAIN is replaced by the prefix of texFile.
      • getFileFilterReplace

        FileFilter getFileFilterReplace​(File idxFile,
                                        String pattern)
        Returns a file filter matching no directories but else all files with names matching xxx.idx, where idxFile has the form xxx.idx.
        Parameters:
        idxFile - an idx file for which a file filter has to be created.
        pattern - a pattern which is inserted in the name of idxFile right 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 of texFile, all its files passing fileFilter are considered as output files and are copied to targetDir. This is invoked by LatexProcessor.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) InputStream to an OutputStream.

        This method uses the provided buffer, so there is no need to use a BufferedInputStream.

        Parameters:
        input - the InputStream to read from
        output - the OutputStream to write to
        Throws:
        IOException - if an I/O error occurs while reading or writing
      • closeQuietly

        private void closeQuietly​(Closeable closeable)
        Unconditionally close a Closeable.

        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)
      • matchInFile

        Boolean matchInFile​(File file,
                            String regex)
        Returns whether the given file file (which shall exist) contains the given pattern pattern or null in case of problems reading file. 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 case null is 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 in file.
        Returns:
        whether the given file file (which shall exist) contains the given pattern pattern. If the file does not exist or an IOException occurs while reading, null is returned.
      • collectMatches

        Collection<String> collectMatches​(File file,
                                          String regex,
                                          int idxGroup)
        Returns the set of strings representing the idxGroup of the pattern regex matching a line in file file or returns null in case of problems reading file.

        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 in file.
        idxGroup - the number of a group of the pattern regex.
        Returns:
        the set of strings representing the idxGroup of the pattern regex matching a line in file file or returns null in case of problems reading file.
      • deleteX

        void deleteX​(File pFile,
                     FileFilter filter)
        Deletes all files in the same folder as pFile directly, i.e. not in subfolders, which are accepted by filter.

        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 of pFile to delete.
      • deleteOrError

        void deleteOrError​(File delFile)
        Deletes delFile or 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 file fromFile to toFile or 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 in texDir including subdirectories which are not in orgNode. The background is, that orgNode represents the files originally in texDir.

        Logging:

        • WFU01: Cannot read directory
        • EFU05: Cannot delete...
        Parameters:
        orgNode -
        texDir -
      • cleanUpRec

        private void cleanUpRec​(File dir,
                                DirNode orgNode,
                                DirNode currNode)
        Deletes all files in currNode which are not in orgNode recursively including subdirectories. The background is, that orgNode represents the files originally in the directory and currNode the 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)