Search in sources :

Example 1 with Filter

use of gen.core.filters.Filter in project omegat by omegat-org.

the class FilterMaster method cloneFilter.

/**
 * Clone one filter's config for editing.
 *
 * @param f
 *            one filter's config
 * @return new config instance
 */
public static Filter cloneFilter(Filter filter) {
    Filter f = new Filter();
    f.setClassName(filter.getClassName());
    f.setEnabled(filter.isEnabled());
    for (Files ff : filter.getFiles()) {
        f.getFiles().add(cloneFiles(ff));
    }
    for (Option o : filter.getOption()) {
        Option fo = new Option();
        fo.setName(o.getName());
        fo.setValue(o.getValue());
        f.getOption().add(fo);
    }
    return f;
}
Also used : IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter) Option(gen.core.filters.Filter.Option) Files(gen.core.filters.Files)

Example 2 with Filter

use of gen.core.filters.Filter in project omegat by omegat-org.

the class FilterMaster method lookupFilter.

/**
 * Gets the filter according to the source filename provided. In case of failing to find a filter to
 * handle the file returns <code>null</code>.
 *
 * In case of finding an appropriate filter it
 * <ul>
 * <li>Creates the filter (use <code>OneFilter.getFilter()</code> to get it)
 * <li>Creates a reader (use <code>OneFilter.getReader()</code> to get it)
 * <li>Checks whether the filter supports the file.
 * </ul>
 *
 * @param inFile The full path to the source file
 * @return The corresponding LookupInformation
 */
private LookupInformation lookupFilter(File inFile, FilterContext fc) throws TranslationException, IOException {
    for (Filter f : config.getFilters()) {
        if (!f.isEnabled()) {
            continue;
        }
        for (Files ff : f.getFiles()) {
            if (!matchesMask(inFile.getName(), ff.getSourceFilenameMask())) {
                continue;
            }
            IFilter filterObject = getFilterInstance(f.getClassName());
            if (filterObject == null) {
                continue;
            }
            fc.setInEncoding(ff.getSourceEncoding());
            fc.setOutEncoding(ff.getTargetEncoding());
            // only for exist filters
            Map<String, String> config = forFilter(f.getOption());
            if (!filterObject.isFileSupported(inFile, config, fc)) {
                break;
            }
            return new LookupInformation(filterObject, ff, config);
        }
    }
    return null;
}
Also used : IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter) IFilter(org.omegat.filters2.IFilter) Files(gen.core.filters.Files)

Example 3 with Filter

use of gen.core.filters.Filter in project omegat by omegat-org.

the class FilterMaster method isFileSupported.

/**
 * Check to see if a file is supported by any filter. When
 * <code>quick</code> 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.
 *
 * @param file
 *            The file to check
 * @param quick
 *            When true, check only the file name
 * @return Whether or not the file is supported
 */
public boolean isFileSupported(File file, boolean quick) {
    FilterContext fc = new FilterContext(null, null, true);
    for (Filter f : config.getFilters()) {
        if (!f.isEnabled()) {
            continue;
        }
        for (Files ff : f.getFiles()) {
            boolean matchesMask = matchesMask(file.getName(), ff.getSourceFilenameMask());
            if (!matchesMask) {
                continue;
            }
            if (quick && matchesMask) {
                return true;
            }
            IFilter filterObject = getFilterInstance(f.getClassName());
            if (filterObject == null) {
                continue;
            }
            fc.setInEncoding(ff.getSourceEncoding());
            fc.setOutEncoding(ff.getTargetEncoding());
            // only for exist filters
            Map<String, String> config = forFilter(f.getOption());
            if (!filterObject.isFileSupported(file, config, fc)) {
                break;
            }
            return true;
        }
    }
    return false;
}
Also used : IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter) IFilter(org.omegat.filters2.IFilter) Files(gen.core.filters.Files) FilterContext(org.omegat.filters2.FilterContext)

Example 4 with Filter

use of gen.core.filters.Filter in project omegat by omegat-org.

the class FilterMaster method addNewFiltersToConfig.

/**
 * Adds new filters(which was not exist in config yet) into config.
 */
private static boolean addNewFiltersToConfig(final Filters conf) {
    boolean result = false;
    for (Class<?> fclass : filtersClasses) {
        boolean found = false;
        for (Filter fc : conf.getFilters()) {
            if (fclass.getName().equals(fc.getClassName())) {
                // filter already exist in config
                found = true;
                break;
            }
        }
        if (!found) {
            // filter not found in config
            Filter f = getDefaultSettingsFromFilter(fclass.getName());
            if (f != null) {
                conf.getFilters().add(f);
                result = true;
            }
        }
    }
    return result;
}
Also used : IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter)

Example 5 with Filter

use of gen.core.filters.Filter in project omegat by omegat-org.

the class FiltersCustomizerController method doEdit.

private void doEdit(int row) {
    if (!isEditable()) {
        return;
    }
    Filter filter = getFilterAtRow(row);
    FilterEditor editor = new FilterEditor(SwingUtilities.windowForComponent(panel), filter);
    editor.setVisible(true);
    if (editor.result != null) {
        List<Filter> filters = editableFilters.getFilters();
        filters.set(filters.indexOf(filter), editor.result);
    }
}
Also used : Filter(gen.core.filters.Filter) IFilter(org.omegat.filters2.IFilter)

Aggregations

Filter (gen.core.filters.Filter)10 IFilter (org.omegat.filters2.IFilter)9 AbstractFilter (org.omegat.filters2.AbstractFilter)7 Files (gen.core.filters.Files)5 Option (gen.core.filters.Filter.Option)2 Filters (gen.core.filters.Filters)2 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 BeanInfo (java.beans.BeanInfo)1 PropertyDescriptor (java.beans.PropertyDescriptor)1 XMLDecoder (java.beans.XMLDecoder)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 Serializable (java.io.Serializable)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 ListSelectionModel (javax.swing.ListSelectionModel)1 JAXBContext (javax.xml.bind.JAXBContext)1 Marshaller (javax.xml.bind.Marshaller)1 FilterContext (org.omegat.filters2.FilterContext)1 Instance (org.omegat.filters2.Instance)1