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();
}
}
}
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();
});
}
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);
}
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);
}
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);
}
Aggregations