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