Class FileUtil


  • public final class FileUtil
    extends Object
    Helper methods for manipulating files.
    • Constructor Detail

      • FileUtil

        public FileUtil()
    • Method Detail

      • getFilteredFiles

        public static Collection<File> getFilteredFiles​(File directory,
                                                        FilenameFilter filter,
                                                        boolean recurse)
        Gets a collection of the files in a given directory.

        This method search all Files recursively that pass the FilenameFilter. Adapted from http://snippets.dzone.com/posts/show/1875

        Parameters:
        directory - root directory
        filter - FilenameFilter used to filter the File candidates
        recurse - true to recurse in the sub-directories, false to not.
        Returns:
        Collection of Files
      • guessLanguages

        public static List<String> guessLanguages​(String path)
        Tries to guess the language(s) declared in the given input file. The method should work with XLIFF, TMX, TTX and TS files.

        The methods looks in the file line by line, in the 10 first KB, or until a source and at least one target are detected, whichever comes first.

        The encoding for the file is determined based on the BOM, if present.

        Parameters:
        path - the full path of the file to process.
        Returns:
        a list of strings that can be empty (never null). The first string is the possible source language, the next strings are the potential target languages.
      • guessLanguages

        public static List<String> guessLanguages​(Reader reader)
        Tries to guess the language(s) declared in the given input. The method should work with XLIFF, TMX, TTX and TS files.

        The methods looks in the file line by line, in the 10 first KB, or until a source and at least one target are detected, whichever comes first.

        Parameters:
        reader - a reader providing the content to examine. This reader will be closed by this method.
        Returns:
        a list of strings that can be empty (never null). The first string is the possible source language, the next strings are the potential target languages.
      • isXliffSegmented

        public static boolean isXliffSegmented​(String path)
        Scans xliff file to see if it is segmented (has <seg-source>)
        Parameters:
        path - the full path of the xliff file to process.
        Returns:
        a true if xliff is segmented, false otherwise.
      • isXliffSegmented

        public static boolean isXliffSegmented​(Reader reader)
        Scans xliff file to see if it is segmented (has <seg-source>)
        Parameters:
        reader - a reader providing the xliff content to examine. This reader will be closed by this method.
        Returns:
        a true if xliff is segmented, false otherwise.
      • deleteAllFilesInDirectory

        public static void deleteAllFilesInDirectory​(String directoryPath)
        Deletes all files in the specified directory.
        Parameters:
        directoryPath - - the path to the directory
        Throws:
        OkapiIOException - if a file cannot be deleted.
      • getLocaleBasedFile

        public static String getLocaleBasedFile​(String baseName,
                                                String extension,
                                                LocaleId locale)
        Return a path to a locale based resource using the standard java property resource resolution. Works with any kind of files e.g., segmenter_en_US.srx or content_fr_FR.html

        WARNING: Assumes default classLoader only!!

        Parameters:
        baseName - base name of the resource
        extension - resource file extension
        locale - locale of the resource we are looking for
        Returns:
        the path to the resource or null if not found
      • getPartBeforeFile

        public static String getPartBeforeFile​(URI uri)
        Gets the URI part before the file name.
        Parameters:
        uri - The URI to process.
        Returns:
        the URI part before the file name.
      • createTempFile

        public static File createTempFile​(String prefix,
                                          String extension)
      • createTempFile

        public static File createTempFile​(String prefix)
      • deleteFile

        public static boolean deleteFile​(String fileName)
      • fileToUrl

        public static URL fileToUrl​(File file)
      • urlToFile

        public static File urlToFile​(URL url)