Search in sources :

Example 1 with FilterContext

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

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

the class RealProject method align.

/**
 * Align project.
 */
public Map<String, TMXEntry> align(final ProjectProperties props, final File translatedDir) throws Exception {
    FilterMaster fm = Core.getFilterMaster();
    File root = new File(config.getSourceRoot());
    List<File> srcFileList = FileUtil.buildFileList(root, true);
    AlignFilesCallback alignFilesCallback = new AlignFilesCallback(props);
    String srcRoot = config.getSourceRoot();
    for (File file : srcFileList) {
        // shorten filename to that which is relative to src root
        String midName = file.getPath().substring(srcRoot.length());
        fm.alignFile(srcRoot, midName, translatedDir.getPath(), new FilterContext(props), alignFilesCallback);
    }
    return alignFilesCallback.data;
}
Also used : FilterMaster(org.omegat.filters2.master.FilterMaster) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FilterContext(org.omegat.filters2.FilterContext)

Example 3 with FilterContext

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

the class RealProject method compileProjectAndCommit.

/**
 * Builds translated files corresponding to sourcePattern and creates fresh TM files.
 *
 * @param sourcePattern
 *            The regexp of files to create
 * @param doPostProcessing
 *            Whether or not we should perform external post-processing.
 * @param commitTargetFiles
 *            Whether or not we should commit target files
 * @throws Exception
 */
@Override
public void compileProjectAndCommit(String sourcePattern, boolean doPostProcessing, boolean commitTargetFiles) throws Exception {
    Log.logInfoRB("LOG_DATAENGINE_COMPILE_START");
    UIThreadsUtil.mustNotBeSwingThread();
    Pattern filePattern = Pattern.compile(sourcePattern);
    // - TMX Level 2, with OmegaT formatting tags wrapped in TMX inline tags
    try {
        // build TMX with OmegaT tags
        String fname = config.getProjectRoot() + config.getProjectName() + OConsts.OMEGAT_TMX + OConsts.TMX_EXTENSION;
        projectTMX.exportTMX(config, new File(fname), false, false, false);
        // build TMX level 1 compliant file
        fname = config.getProjectRoot() + config.getProjectName() + OConsts.LEVEL1_TMX + OConsts.TMX_EXTENSION;
        projectTMX.exportTMX(config, new File(fname), true, false, false);
        // build three-quarter-assed TMX level 2 file
        fname = config.getProjectRoot() + config.getProjectName() + OConsts.LEVEL2_TMX + OConsts.TMX_EXTENSION;
        projectTMX.exportTMX(config, new File(fname), false, true, false);
    } catch (Exception e) {
        Log.logErrorRB("CT_ERROR_CREATING_TMX");
        Log.log(e);
        throw new IOException(OStrings.getString("CT_ERROR_CREATING_TMX") + "\n" + e.getMessage());
    }
    String srcRoot = config.getSourceRoot();
    String locRoot = config.getTargetRoot();
    // build translated files
    FilterMaster fm = Core.getFilterMaster();
    List<String> pathList = FileUtil.buildRelativeFilesList(new File(srcRoot), Collections.emptyList(), config.getSourceRootExcludes());
    TranslateFilesCallback translateFilesCallback = new TranslateFilesCallback();
    int numberOfCompiled = 0;
    for (String midName : pathList) {
        // shorten filename to that which is relative to src root
        Matcher fileMatch = filePattern.matcher(midName);
        if (fileMatch.matches()) {
            File fn = new File(locRoot, midName);
            if (!fn.getParentFile().exists()) {
                // target directory doesn't exist - create it
                if (!fn.getParentFile().mkdirs()) {
                    throw new IOException(OStrings.getString("CT_ERROR_CREATING_TARGET_DIR") + fn.getParentFile());
                }
            }
            Core.getMainWindow().showStatusMessageRB("CT_COMPILE_FILE_MX", midName);
            translateFilesCallback.fileStarted(midName);
            fm.translateFile(srcRoot, midName, locRoot, new FilterContext(config), translateFilesCallback);
            translateFilesCallback.fileFinished();
            numberOfCompiled++;
        }
    }
    if (remoteRepositoryProvider != null && config.getTargetDir().isUnderRoot() && commitTargetFiles) {
        tmxPrepared = null;
        glossaryPrepared = null;
        // commit translations
        try {
            Core.getMainWindow().showStatusMessageRB("TF_COMMIT_TARGET_START");
            remoteRepositoryProvider.switchAllToLatest();
            remoteRepositoryProvider.copyFilesFromProjectToRepo(config.getTargetDir().getUnderRoot(), null);
            remoteRepositoryProvider.commitFiles(config.getTargetDir().getUnderRoot(), "Project translation");
            Core.getMainWindow().showStatusMessageRB("TF_COMMIT_TARGET_DONE");
        } catch (Exception e) {
            Log.logErrorRB("TF_COMMIT_TARGET_ERROR");
            Log.log(e);
            throw new IOException(OStrings.getString("TF_COMMIT_TARGET_ERROR") + "\n" + e.getMessage());
        }
    }
    if (numberOfCompiled == 1) {
        Core.getMainWindow().showStatusMessageRB("CT_COMPILE_DONE_MX_SINGULAR");
    } else {
        Core.getMainWindow().showStatusMessageRB("CT_COMPILE_DONE_MX");
    }
    CoreEvents.fireProjectChange(IProjectEventListener.PROJECT_CHANGE_TYPE.COMPILE);
    if (doPostProcessing) {
        // Kill any processes still not complete
        flushProcessCache();
        if (Preferences.isPreference(Preferences.ALLOW_PROJECT_EXTERN_CMD)) {
            doExternalCommand(config.getExternalCommand());
        }
        doExternalCommand(Preferences.getPreference(Preferences.EXTERNAL_COMMAND));
    }
    Log.logInfoRB("LOG_DATAENGINE_COMPILE_END");
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) FilterMaster(org.omegat.filters2.master.FilterMaster) IOException(java.io.IOException) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) KnownException(org.omegat.core.KnownException) IOException(java.io.IOException) SAXParseException(org.xml.sax.SAXParseException) FilterContext(org.omegat.filters2.FilterContext)

Example 4 with FilterContext

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

the class RealProject method loadSourceFiles.

/**
 * Load source files for project.
 *
 * @param projectRoot
 *            project root dir
 */
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 5 with FilterContext

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

the class FilterMaster method translateFile.

/**
 * OmegaT core calls this method to translate a source file.
 * <ul>
 * <li>OmegaT first looks through registered filter instances to find filter(s) that can handle this file.
 * <li>Tests if filter(s) want to handle it.
 * <li>If the filter accepts the file,
 * <li>Filter is asked to process the file.
 * </ul>
 * If no filter is found, that processes this file, we simply copy it to target folder.
 *
 * @param sourcedir
 *            The folder of the source inFile.
 * @param filename
 *            The name of the source inFile to process (only the part, relative to source folder).
 * @param targetdir
 *            The folder to place the translated inFile to.
 * @param fc
 *            Filter context.
 */
public void translateFile(String sourcedir, String filename, String targetdir, FilterContext fc, ITranslateCallback translateCallback) throws IOException, TranslationException {
    LookupInformation lookup = lookupFilter(new File(sourcedir, filename), fc);
    if (lookup == null) {
        // The file is not supported by any of the filters.
        // Copying it
        FileUtils.copyFile(new File(sourcedir, filename), new File(targetdir, filename));
        return;
    }
    File inFile = new File(sourcedir, filename).getCanonicalFile();
    File outFile = new File(targetdir, getTargetForSource(filename, lookup, fc.getTargetLang())).getCanonicalFile();
    if (inFile.equals(outFile)) {
        throw new TranslationException(StringUtil.format(OStrings.getString("FILTERMASTER_ERROR_SRC_TRG_SAME_FILE"), inFile.getPath()));
    }
    fc.setInEncoding(lookup.outFilesInfo.getSourceEncoding());
    fc.setOutEncoding(lookup.outFilesInfo.getTargetEncoding());
    IFilter filterObject = lookup.filterObject;
    try {
        filterObject.translateFile(inFile, outFile, lookup.config, fc, translateCallback);
    } catch (Exception ex) {
        Log.log(ex);
    }
}
Also used : IFilter(org.omegat.filters2.IFilter) TranslationException(org.omegat.filters2.TranslationException) File(java.io.File) TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException)

Aggregations

File (java.io.File)16 FilterContext (org.omegat.filters2.FilterContext)16 IFilter (org.omegat.filters2.IFilter)8 IOException (java.io.IOException)7 Test (org.junit.Test)7 TranslationException (org.omegat.filters2.TranslationException)7 Language (org.omegat.util.Language)5 FilterMaster (org.omegat.filters2.master.FilterMaster)4 XHTMLFilter (org.omegat.filters3.xml.xhtml.XHTMLFilter)4 RandomAccessFile (java.io.RandomAccessFile)3 TreeMap (java.util.TreeMap)3 IProject (org.omegat.core.data.IProject)3 HTMLFilter2 (org.omegat.filters2.html2.HTMLFilter2)3 Files (gen.core.filters.Files)2 Filter (gen.core.filters.Filter)2 FileOutputStream (java.io.FileOutputStream)2 ArrayList (java.util.ArrayList)2 ZipEntry (java.util.zip.ZipEntry)2 ZipFile (java.util.zip.ZipFile)2 ZipOutputStream (java.util.zip.ZipOutputStream)2