Search in sources :

Example 1 with IParseCallback

use of org.omegat.filters2.IParseCallback 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 2 with IParseCallback

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

the class FilterMaster method loadFile.

/**
 * OmegaT core calls this method to load a source file.
 *
 * @param filename
 *            The name of the source file to load.
 * @return Whether the file was handled by one of OmegaT filters.
 * @see #translateFile(String, String, String, FilterContext,
 *      ITranslateCallback)
 */
public IFilter loadFile(String filename, FilterContext fc, IParseCallback parseCallback) throws IOException, TranslationException {
    IFilter filterObject = null;
    try {
        LookupInformation lookup = lookupFilter(new File(filename), fc);
        if (lookup == null) {
            return null;
        }
        File inFile = new File(filename);
        fc.setInEncoding(lookup.outFilesInfo.getSourceEncoding());
        fc.setOutEncoding(lookup.outFilesInfo.getTargetEncoding());
        filterObject = lookup.filterObject;
        filterObject.parseFile(inFile, lookup.config, fc, parseCallback);
    } catch (Exception ioe) {
        throw new IOException(filename + "\n" + ioe, ioe);
    }
    return filterObject;
}
Also used : IFilter(org.omegat.filters2.IFilter) IOException(java.io.IOException) File(java.io.File) TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException)

Example 3 with IParseCallback

use of org.omegat.filters2.IParseCallback 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)

Example 4 with IParseCallback

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

the class TestFilterBase method parse3.

protected List<ParsedEntry> parse3(AbstractFilter filter, String filename, Map<String, String> options) throws Exception {
    final List<ParsedEntry> result = new ArrayList<ParsedEntry>();
    filter.parseFile(new File(filename), options, context, new IParseCallback() {

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

        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) {
            if (source.isEmpty()) {
                return;
            }
            ParsedEntry e = new ParsedEntry();
            e.id = id;
            e.source = source;
            e.translation = translation;
            e.isFuzzy = isFuzzy;
            e.props = props;
            e.path = path;
            result.add(e);
        }

        public void linkPrevNextSegments() {
        }
    });
    return result;
}
Also used : IParseCallback(org.omegat.filters2.IParseCallback) ProtectedPart(org.omegat.core.data.ProtectedPart) IFilter(org.omegat.filters2.IFilter) ArrayList(java.util.ArrayList) File(java.io.File)

Example 5 with IParseCallback

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

the class TestFilterBase method parse.

protected List<String> parse(AbstractFilter filter, String filename) throws Exception {
    final List<String> result = new ArrayList<String>();
    filter.parseFile(new File(filename), Collections.emptyMap(), context, new IParseCallback() {

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

        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);
        }

        public void addEntryWithProperties(String id, String source, String translation, boolean isFuzzy, String[] props, String path, IFilter filter, List<ProtectedPart> protectedParts) {
            if (!source.isEmpty()) {
                result.add(source);
            }
        }

        public void linkPrevNextSegments() {
        }
    });
    return result;
}
Also used : IParseCallback(org.omegat.filters2.IParseCallback) ProtectedPart(org.omegat.core.data.ProtectedPart) IFilter(org.omegat.filters2.IFilter) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

IFilter (org.omegat.filters2.IFilter)6 ArrayList (java.util.ArrayList)5 IParseCallback (org.omegat.filters2.IParseCallback)5 File (java.io.File)4 ProtectedPart (org.omegat.core.data.ProtectedPart)4 IOException (java.io.IOException)1 List (java.util.List)1 ParseEntryResult (org.omegat.core.data.ParseEntry.ParseEntryResult)1 FilterContext (org.omegat.filters2.FilterContext)1 TranslationException (org.omegat.filters2.TranslationException)1