Search in sources :

Example 11 with IFilter

use of org.omegat.filters2.IFilter in project omegat by omegat-org.

the class RealProject method loadSourceFiles.

/**
 * Load source files for project.
 */
private void loadSourceFiles() throws Exception {
    long st = System.currentTimeMillis();
    FilterMaster fm = Core.getFilterMaster();
    File root = new File(config.getSourceRoot());
    List<String> srcPathList = FileUtil.buildRelativeFilesList(root, Collections.emptyList(), config.getSourceRootExcludes()).stream().sorted(StreamUtil.comparatorByList(getSourceFilesOrder())).collect(Collectors.toList());
    for (String filepath : srcPathList) {
        Core.getMainWindow().showStatusMessageRB("CT_LOAD_FILE_MX", filepath);
        LoadFilesCallback loadFilesCallback = new LoadFilesCallback(existSource, existKeys, transMemories);
        FileInfo fi = new FileInfo();
        fi.filePath = filepath;
        loadFilesCallback.setCurrentFile(fi);
        IFilter filter = fm.loadFile(config.getSourceRoot() + filepath, new FilterContext(config), loadFilesCallback);
        loadFilesCallback.fileFinished();
        if (filter != null && !fi.entries.isEmpty()) {
            // Don't store the instance, because every file gets an instance and
            fi.filterClass = filter.getClass();
            // then we consume a lot of memory for all instances.
            // See also IFilter "TODO: each filter should be stateless"
            fi.filterFileFormatName = filter.getFileFormatName();
            try {
                fi.fileEncoding = filter.getInEncodingLastParsedFile();
            } catch (Error e) {
                // In case a filter doesn't have getInEncodingLastParsedFile() (e.g., Okapi plugin)
                fi.fileEncoding = "";
            }
            projectFilesList.add(fi);
        }
    }
    findNonUniqueSegments();
    Core.getMainWindow().showStatusMessageRB("CT_LOAD_SRC_COMPLETE");
    long en = System.currentTimeMillis();
    Log.log("Load project source files: " + (en - st) + "ms");
}
Also used : IFilter(org.omegat.filters2.IFilter) FilterMaster(org.omegat.filters2.master.FilterMaster) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FilterContext(org.omegat.filters2.FilterContext)

Example 12 with IFilter

use of org.omegat.filters2.IFilter in project omegat by omegat-org.

the class Aligner method parseFile.

/**
 * Parse the specified file and return the contents as a pair of lists:
 * <ul>
 * <li>Key: A list of IDs for the parsed text units
 * <li>Value: A list of parsed text units
 * </ul>
 *
 * @param file
 *            Path to input file
 * @return Pair of lists
 * @throws Exception
 *             If parsing fails
 */
private Entry<List<String>, List<String>> parseFile(String file) throws Exception {
    final List<String> ids = new ArrayList<>();
    final List<String> rawSegs = new ArrayList<>();
    Core.getFilterMaster().loadFile(file, new FilterContext(srcLang, trgLang, true).setRemoveAllTags(removeTags), new IParseCallback() {

        @Override
        public void linkPrevNextSegments() {
        }

        @Override
        public void addEntry(String id, String source, String translation, boolean isFuzzy, String comment, IFilter filter) {
            process(source, id);
        }

        @Override
        public void addEntry(String id, String source, String translation, boolean isFuzzy, String comment, String path, IFilter filter, List<ProtectedPart> protectedParts) {
            process(source, id != null ? id : path != null ? path : null);
        }

        @Override
        public void addEntryWithProperties(String id, String source, String translation, boolean isFuzzy, String[] props, String path, IFilter filter, List<ProtectedPart> protectedParts) {
            process(source, id != null ? id : path != null ? path : null);
        }

        private void process(String text, String id) {
            boolean removeSpaces = Core.getFilterMaster().getConfig().isRemoveSpacesNonseg();
            text = StringUtil.normalizeUnicode(ParseEntry.stripSomeChars(text, new ParseEntryResult(), removeTags, removeSpaces));
            if (!text.trim().isEmpty()) {
                if (id != null) {
                    ids.add(id);
                }
                rawSegs.add(text);
            }
        }
    });
    return new AbstractMap.SimpleImmutableEntry<>(ids, rawSegs);
}
Also used : IParseCallback(org.omegat.filters2.IParseCallback) ProtectedPart(org.omegat.core.data.ProtectedPart) ArrayList(java.util.ArrayList) ParseEntryResult(org.omegat.core.data.ParseEntry.ParseEntryResult) IFilter(org.omegat.filters2.IFilter) FilterContext(org.omegat.filters2.FilterContext)

Example 13 with IFilter

use of org.omegat.filters2.IFilter in project omegat by omegat-org.

the class FilterMaster method getDefaultSettingsFromFilter.

/**
 * Create default filter's config.
 *
 * @param filterClassname
 *            filter's classname
 * @return default filter's config
 */
public static Filter getDefaultSettingsFromFilter(final String filterClassname) {
    IFilter f = getFilterInstance(filterClassname);
    if (f == null) {
        return null;
    }
    Filter fc = new Filter();
    fc.setClassName(f.getClass().getName());
    fc.setEnabled(true);
    for (Instance ins : f.getDefaultInstances()) {
        Files ff = new Files();
        ff.setSourceEncoding(ins.getSourceEncoding());
        ff.setSourceFilenameMask(ins.getSourceFilenameMask());
        ff.setTargetEncoding(ins.getTargetEncoding());
        ff.setTargetFilenamePattern(ins.getTargetFilenamePattern());
        fc.getFiles().add(ff);
    }
    return fc;
}
Also used : IFilter(org.omegat.filters2.IFilter) IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter) Instance(org.omegat.filters2.Instance) Files(gen.core.filters.Files)

Example 14 with IFilter

use of org.omegat.filters2.IFilter in project omegat by omegat-org.

the class FilterMaster method alignFile.

public void alignFile(String sourceDir, String fileName, String targetdir, FilterContext fc, IAlignCallback alignCallback) throws Exception {
    LookupInformation lookup = lookupFilter(new File(sourceDir, fileName), fc);
    if (lookup == null) {
        // Skip it
        return;
    }
    File inFile = new File(sourceDir, fileName);
    File outFile = new File(targetdir, getTargetForSource(fileName, lookup, fc.getTargetLang()));
    if (!outFile.exists()) {
        // out file not exist - skip
        return;
    }
    fc.setInEncoding(lookup.outFilesInfo.getSourceEncoding());
    fc.setOutEncoding(lookup.outFilesInfo.getTargetEncoding());
    IFilter filterObject = lookup.filterObject;
    try {
        filterObject.alignFile(inFile, outFile, lookup.config, fc, alignCallback);
    } catch (Exception ex) {
        Log.log(ex);
    }
}
Also used : IFilter(org.omegat.filters2.IFilter) File(java.io.File) TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException)

Example 15 with IFilter

use of org.omegat.filters2.IFilter in project omegat by omegat-org.

the class TmxComplianceBase method loadTexts.

protected List<String> loadTexts(final IFilter filter, final File sourceFile, final String inCharset, final FilterContext context, final Map<String, String> config) throws Exception {
    final List<String> result = new ArrayList<String>();
    IParseCallback callback = new IParseCallback() {

        public void addEntry(String id, String source, String translation, boolean isFuzzy, String comment, IFilter filter) {
        }

        public void addEntry(String id, String source, String translation, boolean isFuzzy, String comment, String path, IFilter filter, List<ProtectedPart> protectedParts) {
            String[] props = comment == null ? null : new String[] { SegmentProperties.COMMENT, comment };
            addEntryWithProperties(id, source, translation, isFuzzy, props, path, filter, protectedParts);
        }

        @Override
        public void addEntryWithProperties(String id, String source, String translation, boolean isFuzzy, String[] props, String path, IFilter filter, List<ProtectedPart> protectedParts) {
            result.addAll(Core.getSegmenter().segment(context.getSourceLang(), source, null, null));
        }

        public void linkPrevNextSegments() {
        }
    };
    filter.parseFile(sourceFile, config, context, callback);
    return result;
}
Also used : IParseCallback(org.omegat.filters2.IParseCallback) IFilter(org.omegat.filters2.IFilter) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

IFilter (org.omegat.filters2.IFilter)19 File (java.io.File)8 Files (gen.core.filters.Files)5 ArrayList (java.util.ArrayList)5 FilterContext (org.omegat.filters2.FilterContext)5 IParseCallback (org.omegat.filters2.IParseCallback)5 Filter (gen.core.filters.Filter)4 Test (org.junit.Test)4 ProtectedPart (org.omegat.core.data.ProtectedPart)4 IAlignCallback (org.omegat.filters2.IAlignCallback)4 IOException (java.io.IOException)3 AbstractFilter (org.omegat.filters2.AbstractFilter)3 TranslationException (org.omegat.filters2.TranslationException)3 OneFilterTableModel (org.omegat.filters2.master.OneFilterTableModel)2 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 RandomAccessFile (java.io.RandomAccessFile)1 List (java.util.List)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1