19package org.sleuthkit.autopsy.texttranslation.ui;
21import java.awt.ComponentOrientation;
23import java.io.IOException;
24import java.util.concurrent.CancellationException;
25import java.util.concurrent.ExecutionException;
26import java.util.logging.Level;
27import javax.swing.SwingUtilities;
28import javax.swing.SwingWorker;
29import org.openide.util.NbBundle;
30import org.sleuthkit.autopsy.coreutils.Logger;
31import org.sleuthkit.autopsy.coreutils.TextUtil;
32import org.sleuthkit.autopsy.texttranslation.NoServiceProviderException;
33import org.sleuthkit.autopsy.texttranslation.TextTranslationService;
34import org.sleuthkit.autopsy.texttranslation.TranslationException;
39public abstract class TranslateTextTask extends SwingWorker<TranslateTextTask.TranslateResult, Void> {
91 this.contentDescriptor = fileDescriptor;
101 protected abstract String
retrieveText() throws IOException, InterruptedException, IllegalStateException;
109 protected abstract
void onTextDisplay(String text, ComponentOrientation orientation,
int font);
130 protected void onErrorDisplay(String text, ComponentOrientation orientation,
int font) {
136 "TranslatedContentViewer.translatingText=Translating text, please wait...",
137 "TranslatedContentViewer.fileHasNoText=File has no text.",
138 "TranslatedContentViewer.noServiceProvider=The machine translation software was not found.",
139 "# {0} - exception message",
"TranslatedContentViewer.translationException=An error occurred while translating the text ({0})."
143 if (this.isCancelled()) {
144 throw new InterruptedException();
150 }
catch (IOException | IllegalStateException ex) {
154 if (this.isCancelled()) {
155 throw new InterruptedException();
158 if (fileText ==
null || fileText.isEmpty()) {
162 if (!this.translateText) {
176 SwingUtilities.invokeLater(() -> {
177 onProgressDisplay(Bundle.TranslatedContentViewer_translatingText(), ComponentOrientation.LEFT_TO_RIGHT, Font.ITALIC);
181 String translation =
translate(fileText);
182 if (this.isCancelled()) {
183 throw new InterruptedException();
186 if (translation ==
null || translation.isEmpty()) {
193 logger.log(Level.WARNING,
"Error translating text for file " +
this.contentDescriptor, ex);
196 logger.log(Level.WARNING,
"Error translating text for file " +
this.contentDescriptor, ex);
197 return TranslateResult.
error(Bundle.TranslatedContentViewer_translationException(ex.getMessage()));
206 if (this.isCancelled()) {
207 throw new InterruptedException();
211 String result = executionResult.
getResult();
212 int len = result.length();
213 int maxOrientChars = Math.min(len, 1024);
214 String orientDetectSubstring = result.substring(0, maxOrientChars);
220 }
catch (InterruptedException | CancellationException ignored) {
222 }
catch (ExecutionException ex) {
223 logger.log(Level.WARNING,
"Error occurred during background task execution for file " +
this.contentDescriptor, ex);
224 onErrorDisplay(Bundle.TranslatedContentViewer_translationException(ex.getMessage()), ComponentOrientation.LEFT_TO_RIGHT, Font.ITALIC);
236 "TranslatedContentViewer.emptyTranslation=The machine translation software did not return any text."
240 return translatorInstance.
translate(input);
synchronized static Logger getLogger(String name)
static ComponentOrientation getTextDirection(String text)
synchronized String translate(String input)
static TextTranslationService getInstance()
TranslateResult(String result, String errorMessage, boolean successful)
static TranslateResult success(String content)
static TranslateResult error(String message)
final String errorMessage
TranslateTextTask(boolean translateText, String fileDescriptor)
final String contentDescriptor
TranslateResult translateRetrievedText(String fileText)
static final Logger logger
final boolean translateText
void onErrorDisplay(String text, ComponentOrientation orientation, int font)
void onProgressDisplay(String text, ComponentOrientation orientation, int font)
abstract void onTextDisplay(String text, ComponentOrientation orientation, int font)
abstract String retrieveText()
String translate(String input)
TranslateResult doInBackground()