Search in sources :

Example 6 with FilterContext

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

the class FilterMaster method isBilingualFile.

public boolean isBilingualFile(File file) throws Exception {
    FilterContext fc = new FilterContext(null, null, true);
    LookupInformation info = lookupFilter(file, fc);
    return info.filterObject.isBilingual();
}
Also used : FilterContext(org.omegat.filters2.FilterContext)

Example 7 with FilterContext

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

the class FilterMaster method lookupFilter.

/**
 * Gets the filter according to the source filename provided. In case of failing to find a filter to
 * handle the file returns <code>null</code>.
 *
 * In case of finding an appropriate filter it
 * <ul>
 * <li>Creates the filter (use <code>OneFilter.getFilter()</code> to get it)
 * <li>Creates a reader (use <code>OneFilter.getReader()</code> to get it)
 * <li>Checks whether the filter supports the file.
 * </ul>
 *
 * @param inFile The full path to the source file
 * @return The corresponding LookupInformation
 */
private LookupInformation lookupFilter(File inFile, FilterContext fc) throws TranslationException, IOException {
    for (Filter f : config.getFilters()) {
        if (!f.isEnabled()) {
            continue;
        }
        for (Files ff : f.getFiles()) {
            if (!matchesMask(inFile.getName(), ff.getSourceFilenameMask())) {
                continue;
            }
            IFilter filterObject = getFilterInstance(f.getClassName());
            if (filterObject == null) {
                continue;
            }
            fc.setInEncoding(ff.getSourceEncoding());
            fc.setOutEncoding(ff.getTargetEncoding());
            // only for exist filters
            Map<String, String> config = forFilter(f.getOption());
            if (!filterObject.isFileSupported(inFile, config, fc)) {
                break;
            }
            return new LookupInformation(filterObject, ff, config);
        }
    }
    return null;
}
Also used : IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter) IFilter(org.omegat.filters2.IFilter) Files(gen.core.filters.Files)

Example 8 with FilterContext

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

the class FilterMaster method isFileSupported.

/**
 * Check to see if a file is supported by any filter. When
 * <code>quick</code> is true, only the filename will be checked to see if
 * it matches known supported patterns. When false, the filter may have to
 * actually load some or all of the file in order to determine whether or
 * not it is supported.
 *
 * @param file
 *            The file to check
 * @param quick
 *            When true, check only the file name
 * @return Whether or not the file is supported
 */
public boolean isFileSupported(File file, boolean quick) {
    FilterContext fc = new FilterContext(null, null, true);
    for (Filter f : config.getFilters()) {
        if (!f.isEnabled()) {
            continue;
        }
        for (Files ff : f.getFiles()) {
            boolean matchesMask = matchesMask(file.getName(), ff.getSourceFilenameMask());
            if (!matchesMask) {
                continue;
            }
            if (quick && matchesMask) {
                return true;
            }
            IFilter filterObject = getFilterInstance(f.getClassName());
            if (filterObject == null) {
                continue;
            }
            fc.setInEncoding(ff.getSourceEncoding());
            fc.setOutEncoding(ff.getTargetEncoding());
            // only for exist filters
            Map<String, String> config = forFilter(f.getOption());
            if (!filterObject.isFileSupported(file, config, fc)) {
                break;
            }
            return true;
        }
    }
    return false;
}
Also used : IFilter(org.omegat.filters2.IFilter) Filter(gen.core.filters.Filter) AbstractFilter(org.omegat.filters2.AbstractFilter) IFilter(org.omegat.filters2.IFilter) Files(gen.core.filters.Files) FilterContext(org.omegat.filters2.FilterContext)

Example 9 with FilterContext

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

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

the class XMLFilter method processFile.

/**
 * Processes an XML file.
 */
@Override
public void processFile(File inFile, File outFile, FilterContext fc) throws IOException, TranslationException {
    try (BufferedReader inReader = createReader(inFile, fc.getInEncoding())) {
        inEncodingLastParsedFile = this.encoding;
        targetLanguage = fc.getTargetLang();
        InputSource source = new InputSource(inReader);
        source.setSystemId(inFile.toURI().toString());
        SAXParser parser = parserFactory.newSAXParser();
        Handler handler = new Handler(this, dialect, inFile, outFile, fc);
        parser.setProperty("http://xml.org/sax/properties/lexical-handler", handler);
        parser.setProperty("http://xml.org/sax/properties/declaration-handler", handler);
        parser.parse(source, handler);
    } catch (ParserConfigurationException e) {
        throw new TranslationException(e);
    } catch (SAXException e) {
        throw new TranslationException(e);
    }
}
Also used : InputSource(org.xml.sax.InputSource) BufferedReader(java.io.BufferedReader) SAXParser(javax.xml.parsers.SAXParser) TranslationException(org.omegat.filters2.TranslationException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException)

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