use of org.omegat.filters2.TranslationException 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.TranslationException 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.TranslationException in project omegat by omegat-org.
the class PdfFilterTest method testPasswordProtected.
@Test
public void testPasswordProtected() throws Exception {
String f = "test/data/filters/pdf/file-PdfFilter-password.pdf";
try {
loadSourceFiles(new PdfFilter(), f);
fail("Password-protected PDFs are not supported");
} catch (TranslationException ex) {
// OK
}
}
use of org.omegat.filters2.TranslationException in project omegat by omegat-org.
the class PreferencesXML method load.
/**
* Loads the preferences from disk, from the specified file or, if the
* file is null, it attempts to load from a prefs file bundled inside
* the JAR (not supplied by default).
*/
@Override
public void load(List<String> keys, List<String> values) {
try (XMLStreamReader xml = new XMLStreamReader()) {
xml.killEmptyBlocks();
if (loadFile == null) {
// defaults. Useful for e.g. Web Start.
try (InputStream is = getClass().getResourceAsStream(Preferences.FILE_PREFERENCES)) {
if (is != null) {
xml.setStream(is);
readXmlPrefs(xml, keys, values);
}
}
} else {
xml.setStream(loadFile);
readXmlPrefs(xml, keys, values);
}
} catch (TranslationException te) {
// error loading preference file - keep whatever was
// loaded then return gracefully to calling function
// print an error to the console as an FYI
Log.logWarningRB("PM_WARNING_PARSEERROR_ON_READ");
Log.log(te);
makeBackup(loadFile);
} catch (IndexOutOfBoundsException e3) {
// error loading preference file - keep whatever was
// loaded then return gracefully to calling function
// print an error to the console as an FYI
Log.logWarningRB("PM_WARNING_PARSEERROR_ON_READ");
Log.log(e3);
makeBackup(loadFile);
} catch (UnsupportedEncodingException e3) {
// unsupported encoding - forget about it
Log.logErrorRB(e3, "PM_UNSUPPORTED_ENCODING");
makeBackup(loadFile);
} catch (IOException e4) {
// can't read file - forget about it and move on
Log.logErrorRB(e4, "PM_ERROR_READING_FILE");
makeBackup(loadFile);
}
}
use of org.omegat.filters2.TranslationException in project omegat by omegat-org.
the class ProjectFileStorage method loadPropertiesFile.
static ProjectProperties loadPropertiesFile(File projectDir, Omegat om) throws Exception {
if (!projectDir.isDirectory()) {
throw new IllegalArgumentException("Project directory was not a directory");
}
ProjectProperties result = new ProjectProperties(projectDir);
if (!OConsts.PROJ_CUR_VERSION.equals(om.getProject().getVersion())) {
throw new TranslationException(StringUtil.format(OStrings.getString("PFR_ERROR_UNSUPPORTED_PROJECT_VERSION"), om.getProject().getVersion()));
}
result.setTargetRoot(normalizeLoadedPath(om.getProject().getTargetDir(), OConsts.DEFAULT_TARGET));
result.setSourceRoot(normalizeLoadedPath(om.getProject().getSourceDir(), OConsts.DEFAULT_SOURCE));
result.getSourceRootExcludes().clear();
if (om.getProject().getSourceDirExcludes() != null) {
result.getSourceRootExcludes().addAll(om.getProject().getSourceDirExcludes().getMask());
} else {
// sourceRootExclude was not defined
result.getSourceRootExcludes().addAll(ProjectProperties.getDefaultExcludes());
}
result.setTMRoot(normalizeLoadedPath(om.getProject().getTmDir(), OConsts.DEFAULT_TM));
result.setGlossaryRoot(normalizeLoadedPath(om.getProject().getGlossaryDir(), OConsts.DEFAULT_GLOSSARY));
// Compute glossary file location
String glossaryFile = om.getProject().getGlossaryFile();
if (StringUtil.isEmpty(glossaryFile)) {
glossaryFile = DEFAULT_FOLDER_MARKER;
}
if (glossaryFile.equalsIgnoreCase(DEFAULT_FOLDER_MARKER)) {
glossaryFile = result.computeDefaultWriteableGlossaryFile();
} else {
glossaryFile = result.getGlossaryDir().getAsString() + glossaryFile;
}
result.setWriteableGlossary(glossaryFile);
result.setDictRoot(normalizeLoadedPath(om.getProject().getDictionaryDir(), OConsts.DEFAULT_DICT));
result.setSourceLanguage(om.getProject().getSourceLang());
result.setTargetLanguage(om.getProject().getTargetLang());
result.setSourceTokenizer(loadTokenizer(om.getProject().getSourceTok(), result.getSourceLanguage()));
result.setTargetTokenizer(loadTokenizer(om.getProject().getTargetTok(), result.getTargetLanguage()));
if (om.getProject().isSentenceSeg() != null) {
result.setSentenceSegmentingEnabled(om.getProject().isSentenceSeg());
}
if (om.getProject().isSupportDefaultTranslations() != null) {
result.setSupportDefaultTranslations(om.getProject().isSupportDefaultTranslations());
}
if (om.getProject().isRemoveTags() != null) {
result.setRemoveTags(om.getProject().isRemoveTags());
}
if (om.getProject().getExternalCommand() != null) {
result.setExternalCommand(om.getProject().getExternalCommand());
}
if (om.getProject().getRepositories() != null) {
result.setRepositories(om.getProject().getRepositories().getRepository());
}
return result;
}
Aggregations