5075 update comments

This commit is contained in:
William Schaefer 2019-05-22 17:49:07 -04:00
parent 84bbe616f2
commit 58e2a4953e
4 changed files with 94 additions and 18 deletions

View File

@ -20,20 +20,35 @@ package org.sleuthkit.autopsy.texttranslation.translators;
import com.google.cloud.translate.Language; import com.google.cloud.translate.Language;
/**
* Wrapper for the Language class
*/
class GoogleLanguageWrapper { class GoogleLanguageWrapper {
private final Language language; private final Language language;
GoogleLanguageWrapper(Language lang){ /**
* Create a new GoogleLanguageWrapper
*
* @param lang the Language object to wrap
*/
GoogleLanguageWrapper(Language lang) {
language = lang; language = lang;
} }
Language getLanguage(){ /**
* Get the wrapped Language object
*
* @return the wrapped Language
*/
Language getLanguage() {
return language; return language;
} }
@Override @Override
public String toString(){ public String toString() {
return language.getName(); //toString overridden so that the jComboBox in the GoogleTranslatorSettingsPanel will display the name of the language
return language.getName();
} }
} }

View File

@ -35,6 +35,10 @@ import org.openide.util.lookup.ServiceProvider;
import org.sleuthkit.autopsy.texttranslation.TextTranslator; import org.sleuthkit.autopsy.texttranslation.TextTranslator;
import org.sleuthkit.autopsy.texttranslation.TranslationException; import org.sleuthkit.autopsy.texttranslation.TranslationException;
/**
* TextTranslator which utilizes Google Cloud Translation to perform translation
* in Autopsy
*/
@ServiceProvider(service = TextTranslator.class) @ServiceProvider(service = TextTranslator.class)
public final class GoogleTranslator implements TextTranslator { public final class GoogleTranslator implements TextTranslator {
@ -44,6 +48,9 @@ public final class GoogleTranslator implements TextTranslator {
private final GoogleTranslatorSettings settings = new GoogleTranslatorSettings(); private final GoogleTranslatorSettings settings = new GoogleTranslatorSettings();
private Translate translate; private Translate translate;
/**
* Constructs a new GoogleTranslator
*/
public GoogleTranslator() { public GoogleTranslator() {
// Instantiates a client // Instantiates a client
settingsPanel = new GoogleTranslatorSettingsPanel(settings.getCredentialPath(), settings.getTargetLanguageCode()); settingsPanel = new GoogleTranslatorSettingsPanel(settings.getCredentialPath(), settings.getTargetLanguageCode());
@ -97,6 +104,10 @@ public final class GoogleTranslator implements TextTranslator {
return settingsPanel; return settingsPanel;
} }
/**
* Load the Google Cloud Translation service give the currently saved
* settings
*/
private void loadTranslator() { private void loadTranslator() {
InputStream credentialStream = null; InputStream credentialStream = null;
Credentials creds = null; Credentials creds = null;

View File

@ -22,6 +22,9 @@ import com.google.cloud.translate.TranslateOptions;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.sleuthkit.autopsy.coreutils.ModuleSettings; import org.sleuthkit.autopsy.coreutils.ModuleSettings;
/**
* Class to handle the settings associated with the GoogleTranslator
*/
public final class GoogleTranslatorSettings { public final class GoogleTranslatorSettings {
private static final String DEFAULT_TARGET_LANGUAGE = TranslateOptions.getDefaultInstance().getTargetLanguage(); private static final String DEFAULT_TARGET_LANGUAGE = TranslateOptions.getDefaultInstance().getTargetLanguage();
@ -32,22 +35,46 @@ public final class GoogleTranslatorSettings {
private String targetLanguageCode; private String targetLanguageCode;
private String credentialPath; private String credentialPath;
/**
* Construct a new GoogleTranslatorSettingsObject
*/
GoogleTranslatorSettings() { GoogleTranslatorSettings() {
loadSettings(); loadSettings();
} }
/**
* Get the target language code
*
* @return the code used to identify the target language
*/
String getTargetLanguageCode() { String getTargetLanguageCode() {
return targetLanguageCode; return targetLanguageCode;
} }
/**
* Get the path to the JSON credentials file
*
* @return the path to the credentials file
*/
String getCredentialPath() { String getCredentialPath() {
return credentialPath; return credentialPath;
} }
/**
* Set the path to the JSON credentials file
*
* @param path the path to the credentials file
*/
void setCredentialPath(String path) { void setCredentialPath(String path) {
credentialPath = path; credentialPath = path;
} }
/**
* Set the target language code. If a blank code is specified it sets the
* default code instead.
*
* @param code the target language code to set
*/
void setTargetLanguageCode(String code) { void setTargetLanguageCode(String code) {
if (StringUtils.isBlank(code)) { if (StringUtils.isBlank(code)) {
targetLanguageCode = DEFAULT_TARGET_LANGUAGE; targetLanguageCode = DEFAULT_TARGET_LANGUAGE;
@ -56,6 +83,9 @@ public final class GoogleTranslatorSettings {
} }
} }
/**
* Load the settings into memory from their on disk storage
*/
void loadSettings() { void loadSettings() {
if (!ModuleSettings.configExists(GOOGLE_TRANSLATE_NAME)) { if (!ModuleSettings.configExists(GOOGLE_TRANSLATE_NAME)) {
ModuleSettings.makeConfigFile(GOOGLE_TRANSLATE_NAME); ModuleSettings.makeConfigFile(GOOGLE_TRANSLATE_NAME);
@ -72,6 +102,9 @@ public final class GoogleTranslatorSettings {
} }
} }
/**
* Save the setting from memory to their location on disk
*/
void saveSettings() { void saveSettings() {
ModuleSettings.setConfigSetting(GOOGLE_TRANSLATE_NAME, TARGET_LANGUAGE_CODE_KEY, targetLanguageCode); ModuleSettings.setConfigSetting(GOOGLE_TRANSLATE_NAME, TARGET_LANGUAGE_CODE_KEY, targetLanguageCode);
ModuleSettings.setConfigSetting(GOOGLE_TRANSLATE_NAME, CREDENTIAL_PATH_KEY, credentialPath); ModuleSettings.setConfigSetting(GOOGLE_TRANSLATE_NAME, CREDENTIAL_PATH_KEY, credentialPath);

View File

@ -37,6 +37,9 @@ import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.openide.util.NbBundle.Messages; import org.openide.util.NbBundle.Messages;
/**
* Settings panel for the GoogleTranslator
*/
public class GoogleTranslatorSettingsPanel extends javax.swing.JPanel { public class GoogleTranslatorSettingsPanel extends javax.swing.JPanel {
private static final Logger logger = Logger.getLogger(GoogleTranslatorSettingsPanel.class.getName()); private static final Logger logger = Logger.getLogger(GoogleTranslatorSettingsPanel.class.getName());
@ -58,19 +61,19 @@ public class GoogleTranslatorSettingsPanel extends javax.swing.JPanel {
/** /**
* Private method to make a temporary translation service given the current * Private method to make a temporary translation service given the current
* settings and use it to retrieve the available target languages for * settings and use it to retrieve the available target languages for
* population of combobox with target language with unsaved settings * population of combobox with target language with unsaved settings.
* *
* @return A list of Languages * @return A list of Languages
*/ */
@Messages({"GoogleTranslatorSettingsPanel.errorMessage.fileNotFound=Credentials file not found, please set the location to be a valid JSON credentials file.", @Messages({"GoogleTranslatorSettingsPanel.errorMessage.fileNotFound=Credentials file not found, please set the location to be a valid JSON credentials file.",
"GoogleTranslatorSettingsPanel.errorMessage.unableToReadCredentials=Unable to read credentials from credentials file, please set the location to be a valid JSON credentials file.", "GoogleTranslatorSettingsPanel.errorMessage.unableToReadCredentials=Unable to read credentials from credentials file, please set the location to be a valid JSON credentials file.",
"GoogleTranslatorSettingsPanel.errorMessage.unableToMakeCredentials=Unable to construct credentials object from credentials file, please set the location to be a valid JSON credentials file.", "GoogleTranslatorSettingsPanel.errorMessage.unableToMakeCredentials=Unable to construct credentials object from credentials file, please set the location to be a valid JSON credentials file.",
"GoogleTranslatorSettingsPanel.errorMessage.unknownFailureGetting=Failure getting list of supported languages with current credentials file.",}) "GoogleTranslatorSettingsPanel.errorMessage.unknownFailureGetting=Failure getting list of supported languages with current credentials file.",})
private List<Language> getListOfTargetLanguages() { private List<Language> getListOfTargetLanguages() {
//This method also has the side effect of more or less validating the JSON file which was selected as it is necessary to get the list of target languages
try { try {
InputStream credentialStream = null; InputStream credentialStream;
Credentials creds = null; Credentials creds;
try { try {
credentialStream = new FileInputStream(credentialsPathField.getText()); credentialStream = new FileInputStream(credentialsPathField.getText());
} catch (FileNotFoundException ignored) { } catch (FileNotFoundException ignored) {
@ -90,7 +93,7 @@ public class GoogleTranslatorSettingsPanel extends javax.swing.JPanel {
} else { } else {
TranslateOptions.Builder builder = TranslateOptions.newBuilder(); TranslateOptions.Builder builder = TranslateOptions.newBuilder();
builder.setCredentials(creds); builder.setCredentials(creds);
builder.setTargetLanguage(targetLanguageCode); builder.setTargetLanguage(targetLanguageCode); //localize the list to the currently selected target language
warningLabel.setText(""); //clear any previous warning text warningLabel.setText(""); //clear any previous warning text
return builder.build().getService().listSupportedLanguages(); return builder.build().getService().listSupportedLanguages();
} }
@ -102,10 +105,10 @@ public class GoogleTranslatorSettingsPanel extends javax.swing.JPanel {
} }
/** /**
* Populate the target th * Populate the target language selection combo box
*/ */
@Messages({"GoogleTranslatorSettingsPanel.errorMessage.noFileSelected=A JSON file must be selected to provide your credentials for Google Translate.", @Messages({"GoogleTranslatorSettingsPanel.errorMessage.noFileSelected=A JSON file must be selected to provide your credentials for Google Translate.",
"GoogleTranslatorSettingsPanel.errorMessage.unknownFailurePopulating=Failure populating list of supported languages with current credentials file."}) "GoogleTranslatorSettingsPanel.errorMessage.unknownFailurePopulating=Failure populating list of supported languages with current credentials file."})
private void populateTargetLanguageComboBox() { private void populateTargetLanguageComboBox() {
targetLanguageComboBox.removeItemListener(listener); targetLanguageComboBox.removeItemListener(listener);
try { try {
@ -254,14 +257,28 @@ public class GoogleTranslatorSettingsPanel extends javax.swing.JPanel {
private javax.swing.JLabel warningLabel; private javax.swing.JLabel warningLabel;
// End of variables declaration//GEN-END:variables // End of variables declaration//GEN-END:variables
/**
* Get the currently selected target language code
*
* @return the target language code of the language selected in the combobox
*/
String getTargetLanguageCode() { String getTargetLanguageCode() {
return targetLanguageCode; return targetLanguageCode;
} }
/**
* Get the currently set path to the JSON credentials file
*
* @return the path to the credentials file specified in the textarea
*/
String getCredentialsPath() { String getCredentialsPath() {
return credentialsPathField.getText(); return credentialsPathField.getText();
} }
/**
* Listener to identfy when a combo box item has been selected and update
* the combo box to reflect that
*/
private class ComboBoxSelectionListener implements ItemListener { private class ComboBoxSelectionListener implements ItemListener {
@Override @Override