Added saving of serialization, working towards exceptions catching.

This commit is contained in:
Oliver Spohngellert 2016-03-11 09:02:50 -05:00
parent d67d81dcb5
commit 3d84bf1dfb
2 changed files with 32 additions and 47 deletions

View File

@ -25,6 +25,7 @@ import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import javax.swing.AbstractAction; import javax.swing.AbstractAction;
import javax.swing.JOptionPane; import javax.swing.JOptionPane;
import org.openide.util.Exceptions;
/** /**
* Do the context menu action for adding a new filename extension to the * Do the context menu action for adding a new filename extension to the
@ -43,19 +44,35 @@ class AddFileExtensionAction extends AbstractAction {
@Override @Override
public void actionPerformed(ActionEvent event) { public void actionPerformed(ActionEvent event) {
HashMap<String, String[]> editableMap = FileExtMismatchXML.getDefault().load(); HashMap<String, String[]> editableMap;
try {
editableMap = FileExtMismatchXML.getDefault().load();
} catch (FileExtMismatchXML.FileExtMismatchException ex) {
JOptionPane.showMessageDialog(null,
NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"),
NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"),
JOptionPane.ERROR_MESSAGE);
return;
}
ArrayList<String> editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(mimeTypeStr))); ArrayList<String> editedExtensions = new ArrayList<>(Arrays.asList(editableMap.get(mimeTypeStr)));
editedExtensions.add(extStr); editedExtensions.add(extStr);
// Old array will be replaced by new array for this key // Old array will be replaced by new array for this key
editableMap.put(mimeTypeStr, editedExtensions.toArray(new String[0])); editableMap.put(mimeTypeStr, editedExtensions.toArray(new String[0]));
if (!FileExtMismatchXML.getDefault().save(editableMap)) { try {
//error if (!FileExtMismatchXML.getDefault().save(editableMap)) {
//error
JOptionPane.showMessageDialog(null,
NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"),
NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"),
JOptionPane.ERROR_MESSAGE);
} // else //in the future we might want to update the statusbar to give feedback to the user
} catch (FileExtMismatchXML.FileExtMismatchException ex) {
JOptionPane.showMessageDialog(null, JOptionPane.showMessageDialog(null,
NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.msg"),
NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"), NbBundle.getMessage(this.getClass(), "AddFileExtensionAction.msgDlg.title"),
JOptionPane.ERROR_MESSAGE); JOptionPane.ERROR_MESSAGE);
} // else //in the future we might want to update the statusbar to give feedback to the user }
} }
} }

View File

@ -20,6 +20,7 @@ package org.sleuthkit.autopsy.modules.fileextmismatch;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
@ -31,6 +32,7 @@ import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.ParserConfigurationException;
import org.openide.util.io.NbObjectInputStream; import org.openide.util.io.NbObjectInputStream;
import org.openide.util.io.NbObjectOutputStream;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.coreutils.PlatformUtil; import org.sleuthkit.autopsy.coreutils.PlatformUtil;
import org.sleuthkit.autopsy.coreutils.XMLUtil; import org.sleuthkit.autopsy.coreutils.XMLUtil;
@ -88,7 +90,7 @@ class FileExtMismatchXML {
* *
* @return Loaded hash map or null on error or null if data does not exist * @return Loaded hash map or null on error or null if data does not exist
*/ */
public HashMap<String, String[]> load() { public HashMap<String, String[]> load() throws FileExtMismatchException {
HashMap<String, String[]> sigTypeToExtMap = new HashMap<>(); HashMap<String, String[]> sigTypeToExtMap = new HashMap<>();
File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH); File serializedFile = new File(DEFAULT_SERIALIZED_FILE_PATH);
if (serializedFile.exists()) { if (serializedFile.exists()) {
@ -100,7 +102,6 @@ class FileExtMismatchXML {
} catch (IOException | ClassNotFoundException ex) { } catch (IOException | ClassNotFoundException ex) {
throw new FileExtMismatchException("Couldn't read serialized settings.", ex); throw new FileExtMismatchException("Couldn't read serialized settings.", ex);
} }
return sigTypeToExtMap;
} }
try { try {
@ -157,48 +158,16 @@ class FileExtMismatchXML {
* *
* @return Loaded hash map or null on error or null if data does not exist * @return Loaded hash map or null on error or null if data does not exist
*/ */
public boolean save(HashMap<String, String[]> sigTypeToExtMap) { public boolean save(HashMap<String, String[]> sigTypeToExtMap) throws FileExtMismatchException {
boolean success; try (NbObjectOutputStream out = new NbObjectOutputStream(new FileOutputStream(DEFAULT_SERIALIZED_FILE_PATH))) {
FileExtMismatchSettings settings = new FileExtMismatchSettings(sigTypeToExtMap);
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance(); out.writeObject(settings);
return true;
try { } catch (IOException ex) {
DocumentBuilder docBuilder = dbfac.newDocumentBuilder(); throw new FileExtMismatchException(String.format("Failed to write settings to %s", DEFAULT_SERIALIZED_FILE_PATH), ex);
Document doc = docBuilder.newDocument();
Element rootEl = doc.createElement(ROOT_EL);
doc.appendChild(rootEl);
ArrayList<String> mimeTypeList = new ArrayList<>(sigTypeToExtMap.keySet());
Collections.sort(mimeTypeList);
for (String mimeType : mimeTypeList) {
Element sigEl = doc.createElement(SIG_EL);
sigEl.setAttribute(SIG_MIMETYPE_ATTR, mimeType.toLowerCase());
String[] extArray = sigTypeToExtMap.get(mimeType);
if (extArray != null) {
ArrayList<String> extList = new ArrayList<>(Arrays.asList(extArray));
Collections.sort(extList);
for (String ext : extList) {
Element extEl = doc.createElement(EXT_EL);
extEl.setTextContent(ext.toLowerCase());
sigEl.appendChild(extEl);
}
}
rootEl.appendChild(sigEl);
}
success = XMLUtil.saveDoc(FileExtMismatchXML.class, filePath, ENCODING, doc);
} catch (ParserConfigurationException e) {
logger.log(Level.SEVERE, "Error saving keyword list: can't initialize parser.", e); //NON-NLS
success = false;
} }
return success;
} }
/** /**
* Used to translate more implementation-details-specific exceptions (which * Used to translate more implementation-details-specific exceptions (which
* are logged by this class) into more generic exceptions for propagation to * are logged by this class) into more generic exceptions for propagation to
@ -217,5 +186,4 @@ class FileExtMismatchXML {
} }
} }
} }