Class FilterMaster

java.lang.Object
org.omegat.filters2.master.FilterMaster

@NullMarked public class FilterMaster extends Object
A master class that registers and handles all the filters. Singleton - there can be only one instance of this class.
  • Field Details

  • Constructor Details

    • FilterMaster

      public FilterMaster(Filters config)
      Create a new FilterMaster.
  • Method Details

    • setFilterClasses

      public static void setFilterClasses(List<Class<?>> classes)
    • getConfig

      public Filters getConfig()
    • getFilterInstance

      public static @Nullable IFilter getFilterInstance(String classname)
      Get filter's instance by filter class name.
      Parameters:
      classname - filter's class name
      Returns:
      filter instance
    • loadFile

      public @Nullable IFilter loadFile(String filename, FilterContext fc, IParseCallback parseCallback) throws IOException, TranslationException
      OmegaT core calls this method to load a source file.
      Parameters:
      filename - The name of the source file to load.
      Returns:
      Whether the file was handled by one of OmegaT filters.
      Throws:
      IOException
      TranslationException
      See Also:
    • translateFile

      public void translateFile(String sourcedir, String filename, String targetdir, FilterContext fc, ITranslateCallback translateCallback) throws IOException, TranslationException
      OmegaT core calls this method to translate a source file.
      • OmegaT first looks through registered filter instances to find filter(s) that can handle this file.
      • Tests if filter(s) want to handle it.
      • If the filter accepts the file,
      • Filter is asked to process the file.
      If no filter is found, that processes this file, we simply copy it to target folder.
      Parameters:
      sourcedir - The folder of the source inFile.
      filename - The name of the source inFile to process (only the part, relative to source folder).
      targetdir - The folder to place the translated inFile to.
      fc - Filter context.
      Throws:
      IOException
      TranslationException
    • alignFile

      public void alignFile(String sourceDir, String fileName, String targetdir, FilterContext fc, IAlignCallback alignCallback) throws Exception
      Throws:
      Exception
    • isFileSupported

      public boolean isFileSupported(File file, boolean quick)
      Check to see if a file is supported by any filter. When quick is true, only the filename will be checked to see if it matches known supported patterns. When false, the filter may have to actually load some or all of the file in order to determine whether or not it is supported.
      Parameters:
      file - The file to check
      quick - When true, check only the file name
      Returns:
      Whether the file is supported
    • isBilingualFile

      public boolean isBilingualFile(File file) throws Exception
      Throws:
      Exception
    • getSupportedEncodings

      public static List<String> getSupportedEncodings()
      Queries JRE for the list of supported encodings. Also adds the human name for no/automatic inEncoding.
      Returns:
      names of all the encodings in an array
    • createDefaultFiltersConfig

      public static Filters createDefaultFiltersConfig()
      Reverts Filter Configuration to Default values. Basically
      • Sets up built-in filters
      • Reloads the plugins
      • Loads filters from plugins
      • Saves the configuration
    • loadConfig

      public static @Nullable Filters loadConfig(File configFile) throws IOException
      Loads information about the filters from an XML file. If there's an error loading a file, it calls setupDefaultFilters.
      Parameters:
      configFile - the file from which the filter configuration is to be loaded
      Returns:
      an instance of Filters containing the loaded configuration, or a new Filters instance if an error occurs or a new configuration is generated
      Throws:
      IOException - if an I/O error occurs while reading the configuration file
    • saveConfig

      public static void saveConfig(@Nullable Filters config, File configFile) throws IOException
      Saves information about the filters to an XML file. If the configuration is null, the file will be deleted. The configuration is written in a pretty-printed format.
      Parameters:
      config - The configuration object to be saved. If null, the file is deleted.
      configFile - The file to save the configuration to.
      Throws:
      IOException - If an error occurs while saving the configuration or deleting the file.
    • now

      public static String now(String dateFormat)
      Return current system time in the specified date format.
      Parameters:
      dateFormat - Date format for java.text.SimpleDateFormat.
    • getTargetForSource

      public String getTargetForSource(String sourceDir, String srcRelPath, FilterContext fc) throws TranslationException
      Determines the target file path for a given source file path.
      Parameters:
      sourceDir - the root directory of the source file
      srcRelPath - the relative path of the source file within the source directory
      fc - the filter context containing relevant translation parameters
      Returns:
      the target file path corresponding to the source file
      Throws:
      TranslationException - if an error occurs during the translation processing
      IllegalArgumentException - if the specified sourceDir and srcRelPath do not point to an existing file
    • cloneConfig

      public static Filters cloneConfig(Filters orig)
      Clone config for editing
      Returns:
      new config instance
    • cloneFilter

      public static Filter cloneFilter(Filter filter)
      Clone one filter's config for editing.
      Parameters:
      filter - one filter's config
      Returns:
      new config instance
    • getDefaultSettingsFromFilter

      public static @Nullable Filter getDefaultSettingsFromFilter(String filterClassname)
      Create default filter's config.
      Parameters:
      filterClassname - filter's classname
      Returns:
      default filter's config
    • forFilter

      public static Map<String,String> forFilter(List<Filter.Option> options)
      Convert options from xml for filter usage.
      Parameters:
      options - xml options
      Returns:
      options for filter usage
    • setOptions

      public static void setOptions(Filter f, Map<String,String> newOptions)
      Convert options to XML from a map.
      Parameters:
      f - filter
      newOptions - options