Search in sources :

Example 21 with FilterContext

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

the class XHTMLFilterTest method testLoad.

@Test
public void testLoad() 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));
    IProject.FileInfo fi = loadSourceFiles(filter, f);
    checkMultiStart(fi, f);
    checkMulti("en", null, null, "", "en", null);
    checkMulti("en", null, null, "en", "XHTML 1.0 Example", null);
    checkMulti("XHTML 1.0 Example", null, null, "en", "Extensible HyperText Markup Language", null);
    checkMulti("Extensible HyperText Markup Language", null, null, "XHTML 1.0 Example", "http://www.w3.org/Icons/valid-xhtml10", null);
}
Also used : XHTMLFilter(org.omegat.filters3.xml.xhtml.XHTMLFilter) Language(org.omegat.util.Language) File(java.io.File) IProject(org.omegat.core.data.IProject) FilterContext(org.omegat.filters2.FilterContext) Test(org.junit.Test)

Example 22 with FilterContext

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

the class XHTMLFilterTest method testTagsOptimization.

@Test
public void testTagsOptimization() throws Exception {
    String f = "test/data/filters/xhtml/file-XHTMLFilter-tags-optimization.html";
    XHTMLFilter filter = new XHTMLFilter();
    Core.getFilterMaster().getConfig().setRemoveTags(false);
    filter.isFileSupported(new File(f), new TreeMap<String, String>(), new FilterContext(new Language("en"), new Language("be"), false));
    IProject.FileInfo fi = loadSourceFiles(filter, f);
    checkMultiStart(fi, f);
    checkMultiNoPrevNext("en", null, null, null);
    checkMultiNoPrevNext("en", null, null, null);
    checkMultiNoPrevNext("<i0/><b1><c2>This</c2> is <i3>first</i3> line.</b1>", null, null, null);
    translateXML(filter, f);
    Core.getFilterMaster().getConfig().setRemoveTags(true);
    filter.isFileSupported(new File(f), new TreeMap<String, String>(), new FilterContext(new Language("en"), new Language("be"), false));
    fi = loadSourceFiles(filter, f);
    checkMultiStart(fi, f);
    checkMultiNoPrevNext("en", null, null, null);
    checkMultiNoPrevNext("en", null, null, null);
    checkMultiNoPrevNext("<c0>This</c0> is <i1>first</i1> line.", null, null, null);
    translateXML(filter, f);
}
Also used : XHTMLFilter(org.omegat.filters3.xml.xhtml.XHTMLFilter) Language(org.omegat.util.Language) File(java.io.File) IProject(org.omegat.core.data.IProject) FilterContext(org.omegat.filters2.FilterContext) Test(org.junit.Test)

Example 23 with FilterContext

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

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

the class OpenDocFilter method processFile.

/**
 * Processes a single OpenDocument 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 {
    ZipFile zipfile = new ZipFile(inFile);
    ZipOutputStream zipout = null;
    if (outFile != null) {
        zipout = new ZipOutputStream(new FileOutputStream(outFile));
    }
    Enumeration<? extends ZipEntry> zipcontents = zipfile.entries();
    while (zipcontents.hasMoreElements()) {
        ZipEntry zipentry = zipcontents.nextElement();
        String shortname = zipentry.getName();
        if (shortname.lastIndexOf('/') >= 0) {
            shortname = shortname.substring(shortname.lastIndexOf('/') + 1);
        }
        if (TRANSLATABLE.contains(shortname)) {
            File tmpin = tmp();
            FileUtils.copyInputStreamToFile(zipfile.getInputStream(zipentry), tmpin);
            File tmpout = null;
            if (zipout != null) {
                tmpout = tmp();
            }
            try {
                createXMLFilter(processOptions).processFile(tmpin, tmpout, fc);
            } catch (Exception e) {
                zipfile.close();
                throw new TranslationException(e.getLocalizedMessage() + "\n" + OStrings.getString("OpenDoc_ERROR_IN_FILE") + inFile);
            }
            if (zipout != null) {
                ZipEntry outentry = new ZipEntry(zipentry.getName());
                outentry.setMethod(ZipEntry.DEFLATED);
                zipout.putNextEntry(outentry);
                FileUtils.copyFile(tmpout, zipout);
                zipout.closeEntry();
            }
            if (!tmpin.delete()) {
                tmpin.deleteOnExit();
            }
            if (tmpout != null) {
                if (!tmpout.delete()) {
                    tmpout.deleteOnExit();
                }
            }
        } else {
            if (zipout != null) {
                ZipEntry outentry = new ZipEntry(zipentry.getName());
                zipout.putNextEntry(outentry);
                IOUtils.copy(zipfile.getInputStream(zipentry), zipout);
                zipout.closeEntry();
            }
        }
    }
    if (zipout != null) {
        zipout.close();
    }
    zipfile.close();
}
Also used : ZipFile(java.util.zip.ZipFile) ZipOutputStream(java.util.zip.ZipOutputStream) FileOutputStream(java.io.FileOutputStream) ZipEntry(java.util.zip.ZipEntry) TranslationException(org.omegat.filters2.TranslationException) File(java.io.File) ZipFile(java.util.zip.ZipFile) IOException(java.io.IOException) TranslationException(org.omegat.filters2.TranslationException)

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