19 package org.sleuthkit.autopsy.modules.fileextmismatch;
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.Collections;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.logging.Level;
29 import javax.xml.parsers.DocumentBuilder;
30 import javax.xml.parsers.DocumentBuilderFactory;
31 import javax.xml.parsers.ParserConfigurationException;
35 import org.w3c.dom.Document;
36 import org.w3c.dom.Element;
37 import org.w3c.dom.NodeList;
43 class FileExtMismatchXML {
45 private static final Logger logger = Logger.getLogger(FileExtMismatchXML.class.getName());
46 private static FileExtMismatchXML defaultInstance = null;
48 private static final String ENCODING =
"UTF-8";
49 private static final String XSDFILE =
"MismatchConfigSchema.xsd";
51 private static final String ROOT_EL =
"mismatch_config";
52 private static final String SIG_EL =
"signature";
53 private static final String EXT_EL =
"ext";
54 private static final String SIG_MIMETYPE_ATTR =
"mimetype";
56 private static final String DEFAULT_CONFIG_FILE_NAME =
"mismatch_config.xml";
58 protected String filePath;
60 FileExtMismatchXML(String filePath) {
61 this.filePath = filePath;
64 boolean extracted = PlatformUtil.extractResourceToUserConfigDir(FileExtMismatchXML.class, DEFAULT_CONFIG_FILE_NAME,
false);
65 }
catch (IOException ex) {
66 logger.log(Level.SEVERE,
"Error copying default mismatch configuration to user dir ", ex);
74 public static synchronized FileExtMismatchXML getDefault() {
75 if (defaultInstance == null) {
76 final String FILTER_CONFIG_FILE = PlatformUtil.getUserConfigDirectory() + File.separator + DEFAULT_CONFIG_FILE_NAME;
77 defaultInstance =
new FileExtMismatchXML(FILTER_CONFIG_FILE);
79 return defaultInstance;
87 public HashMap<String, String[]> load() {
88 HashMap<String, String[]> sigTypeToExtMap =
new HashMap<>();
91 final Document doc = XMLUtil.loadDoc(FileExtMismatchXML.class, filePath);
96 Element root = doc.getDocumentElement();
98 logger.log(Level.SEVERE,
"Error loading config file: invalid file format (bad root).");
102 NodeList sigNList = root.getElementsByTagName(SIG_EL);
103 final int numSigs = sigNList.getLength();
109 for (
int sigIndex = 0; sigIndex < numSigs; ++sigIndex) {
110 Element sigEl = (Element) sigNList.item(sigIndex);
111 final String mimetype = sigEl.getAttribute(SIG_MIMETYPE_ATTR);
113 NodeList extNList = sigEl.getElementsByTagName(EXT_EL);
114 final int numExts = extNList.getLength();
117 List<String> extStrings =
new ArrayList<>();
118 for (
int extIndex = 0; extIndex < numExts; ++extIndex) {
119 Element extEl = (Element) extNList.item(extIndex);
120 extStrings.add(extEl.getTextContent());
122 String[] sarray = extStrings.toArray(
new String[0]);
123 sigTypeToExtMap.put(mimetype, sarray);
125 sigTypeToExtMap.put(mimetype, null);
129 }
catch (Exception e) {
130 logger.log(Level.SEVERE,
"Error loading config file.", e);
133 return sigTypeToExtMap;
144 public boolean save(HashMap<String, String[]> sigTypeToExtMap) {
147 DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
150 DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
151 Document doc = docBuilder.newDocument();
153 Element rootEl = doc.createElement(ROOT_EL);
154 doc.appendChild(rootEl);
156 ArrayList<String> mimeTypeList =
new ArrayList<>(sigTypeToExtMap.keySet());
157 Collections.sort(mimeTypeList);
159 for (String mimeType : mimeTypeList) {
160 Element sigEl = doc.createElement(SIG_EL);
161 sigEl.setAttribute(SIG_MIMETYPE_ATTR, mimeType.toLowerCase());
163 String[] extArray = sigTypeToExtMap.get(mimeType);
164 if (extArray != null) {
165 ArrayList<String> extList =
new ArrayList<>(Arrays.asList(extArray));
166 Collections.sort(extList);
167 for (String ext : extList) {
168 Element extEl = doc.createElement(EXT_EL);
169 extEl.setTextContent(ext.toLowerCase());
170 sigEl.appendChild(extEl);
173 rootEl.appendChild(sigEl);
176 success = XMLUtil.saveDoc(FileExtMismatchXML.class, filePath, ENCODING, doc);
178 }
catch (ParserConfigurationException e) {
179 logger.log(Level.SEVERE,
"Error saving keyword list: can't initialize parser.", e);