Search in sources :

Example 11 with FilterContext

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

the class OpenXMLFilter method processFile.

/**
 * Processes a single OpenXML file, which is actually a ZIP file consisting of many XML files, some of
 * which should be translated.
 */
@Override
public void processFile(File inFile, File outFile, FilterContext fc) throws IOException, TranslationException {
    // Define the documents to read
    defineDOCUMENTSOptions(processOptions);
    ZipOutputStream zipout = null;
    try (ZipFile zipfile = new ZipFile(inFile)) {
        if (outFile != null) {
            zipout = new ZipOutputStream(new FileOutputStream(outFile));
        }
        Enumeration<? extends ZipEntry> unsortedZipcontents = zipfile.entries();
        List<? extends ZipEntry> filelist = Collections.list(unsortedZipcontents);
        // Sort filenames, because zipfile.entries give a random order
        // We use a simplified natural sort, to have slide1, slide2 ...
        // slide10
        // instead of slide1, slide10, slide 2
        // We also order files arbitrarily, to have, for instance
        // documents.xml before comments.xml
        Collections.sort(filelist, this::compareZipEntries);
        for (ZipEntry zipentry : filelist) {
            String shortname = removePath(zipentry.getName());
            if (translatable.matcher(shortname).matches()) {
                File tmpin = tmp();
                FileUtils.copyInputStreamToFile(zipfile.getInputStream(zipentry), tmpin);
                File tmpout = null;
                if (zipout != null) {
                    tmpout = tmp();
                }
                try {
                    createXMLFilter().processFile(tmpin, tmpout, fc);
                } catch (Exception e) {
                    LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
                    throw new TranslationException(e.getLocalizedMessage() + "\n" + OStrings.getString("OpenXML_ERROR_IN_FILE") + inFile, e);
                }
                if (zipout != null) {
                    ZipEntry outEntry = new ZipEntry(zipentry.getName());
                    zipout.putNextEntry(outEntry);
                    FileUtils.copyFile(tmpout, zipout);
                    zipout.closeEntry();
                }
                if (!tmpin.delete()) {
                    tmpin.deleteOnExit();
                }
                if (tmpout != null && !tmpout.delete()) {
                    tmpout.deleteOnExit();
                }
            } else {
                if (zipout != null) {
                    ZipEntry outEntry = new ZipEntry(zipentry.getName());
                    zipout.putNextEntry(outEntry);
                    try (InputStream is = zipfile.getInputStream(zipentry)) {
                        IOUtils.copy(is, zipout);
                    }
                    zipout.closeEntry();
                }
            }
        }
    } finally {
        if (zipout != null) {
            zipout.close();
        }
    }
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) TranslationException(org.omegat.filters2.TranslationException) ZipFile(java.util.zip.ZipFile) File(java.io.File) TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException)

Example 12 with FilterContext

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

the class Searcher method searchFiles.

private void searchFiles() throws Exception {
    Path root = Paths.get(expression.rootDir);
    FilterMaster fm = Core.getFilterMaster();
    final SearchCallback searchCallback = new SearchCallback(m_project.getProjectProperties());
    int depth = expression.recursive ? Integer.MAX_VALUE : 0;
    Files.walk(root, depth, FileVisitOption.FOLLOW_LINKS).forEach(path -> {
        String filename = path.toString();
        FileInfo fi = new FileInfo();
        // determine actual file name w/ no root path info
        fi.filePath = root.relativize(path).toString();
        searchCallback.setCurrentFile(fi);
        try {
            fm.loadFile(filename, new FilterContext(m_project.getProjectProperties()), searchCallback);
        } catch (TranslationException | IOException ex) {
            Log.log(ex);
        }
        searchCallback.fileFinished();
        checkStop.checkInterrupted();
    });
}
Also used : Path(java.nio.file.Path) FileInfo(org.omegat.core.data.IProject.FileInfo) FilterMaster(org.omegat.filters2.master.FilterMaster) TranslationException(org.omegat.filters2.TranslationException) IOException(java.io.IOException) FilterContext(org.omegat.filters2.FilterContext)

Example 13 with FilterContext

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

the class TmxComplianceBase method translateUsingTmx.

protected void translateUsingTmx(IFilter filter, Map<String, String> config, final String fileTextIn, String inCharset, String fileTMX, String outCharset, ProjectProperties props, Map<String, TMXEntry> tmxPatch) throws Exception {
    final ProjectTMX tmx = new ProjectTMX(props.getSourceLanguage(), props.getTargetLanguage(), props.isSentenceSegmentingEnabled(), new File("test/data/tmx/TMXComplianceKit/" + fileTMX), orphanedCallback);
    if (tmxPatch != null) {
        tmx.defaults.putAll(tmxPatch);
    }
    FilterContext fc = new FilterContext(props);
    fc.setInEncoding(inCharset);
    fc.setOutEncoding(outCharset);
    ITranslateCallback cb = new TranslateEntry(props) {

        @Override
        protected String getSegmentTranslation(String id, int segmentIndex, String segmentSource, String prevSegment, String nextSegment, String path) {
            TMXEntry e = tmx.getDefaultTranslation(segmentSource);
            assertNotNull(e);
            return e.translation;
        }

        @Override
        String getCurrentFile() {
            return fileTextIn;
        }
    };
    filter.translateFile(new File("test/data/tmx/TMXComplianceKit/" + fileTextIn), outFile, config, fc, cb);
}
Also used : ITranslateCallback(org.omegat.filters2.ITranslateCallback) File(java.io.File) FilterContext(org.omegat.filters2.FilterContext)

Example 14 with FilterContext

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

the class TmxComplianceTests method testExport2A.

/**
 * Test Export2A - HTML
 */
@Test
public void testExport2A() throws Exception {
    File tmxFile = new File("test/data/tmx/TMXComplianceKit/ExportTest2A.tmx");
    File sourceFile = new File("test/data/tmx/TMXComplianceKit/ExportTest2A.htm");
    File translatedFile = new File("test/data/tmx/TMXComplianceKit/ExportTest2A_fr.htm");
    ProjectProperties props = new TestProjectProperties("EN-US", "FR-CA");
    props.setSentenceSegmentingEnabled(true);
    FilterContext fc = new FilterContext(props);
    fc.setInEncoding("windows-1252");
    Map<String, String> config = new TreeMap<String, String>();
    new HTMLOptions(config).setSkipMeta("content=en-us,content=fr-ca");
    List<String> sources = loadTexts(new HTMLFilter2(), sourceFile, null, fc, config);
    List<String> translations = loadTexts(new HTMLFilter2(), translatedFile, null, fc, config);
    assertEquals(sources.size(), translations.size());
    ProjectTMX tmx = new ProjectTMX(props.getSourceLanguage(), props.getTargetLanguage(), props.isSentenceSegmentingEnabled(), outFile, orphanedCallback);
    for (int i = 0; i < sources.size(); i++) {
        tmx.defaults.put(sources.get(i), createTMXEntry(sources.get(i), translations.get(i), true));
    }
    tmx.exportTMX(props, outFile, false, true, true);
    compareTMX(tmxFile, outFile, 12);
}
Also used : HTMLOptions(org.omegat.filters2.html2.HTMLOptions) TreeMap(java.util.TreeMap) HTMLFilter2(org.omegat.filters2.html2.HTMLFilter2) File(java.io.File) FilterContext(org.omegat.filters2.FilterContext) Test(org.junit.Test)

Example 15 with FilterContext

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

the class XHTMLFilterTest method testTranslate.

@Test
public void testTranslate() throws Exception {
    String f = "test/data/filters/xhtml/file-XHTMLFilter.html";
    XHTMLFilter filter = new XHTMLFilter();
    filter.isFileSupported(new File(f), new TreeMap<String, String>(), new FilterContext(new Language("en"), new Language("be"), false));
    translateXML(filter, f);
}
Also used : XHTMLFilter(org.omegat.filters3.xml.xhtml.XHTMLFilter) Language(org.omegat.util.Language) File(java.io.File) FilterContext(org.omegat.filters2.FilterContext) Test(org.junit.Test)

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