use of org.omegat.filters2.IFilter 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;
}
use of org.omegat.filters2.IFilter 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);
}
}
use of org.omegat.filters2.IFilter 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;
}
use of org.omegat.filters2.IFilter 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;
}
use of org.omegat.filters2.IFilter in project omegat by omegat-org.
the class FilterEditor method addButtonActionPerformed.
// GEN-LAST:event_toDefaultsButtonActionPerformed
private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {
// GEN-FIRST:event_addButtonActionPerformed
IFilter f = FilterMaster.getFilterInstance(filter.getClassName());
if (f == null) {
return;
}
InstanceEditor ie = new InstanceEditor(this, f.isSourceEncodingVariable(), f.isTargetEncodingVariable(), f.getHint());
ie.setVisible(true);
if (ie.getReturnStatus() == InstanceEditor.RET_OK) {
Files ff = new Files();
ff.setSourceEncoding(setEncodingName(ie.getSourceEncoding()));
ff.setSourceFilenameMask(ie.getSourceFilenameMask());
ff.setTargetEncoding(setEncodingName(ie.getTargetEncoding()));
ff.setTargetFilenamePattern(ie.getTargetFilenamePattern());
filter.getFiles().add(ff);
instances.setModel(new OneFilterTableModel(filter));
}
}
Aggregations