20 package org.sleuthkit.autopsy.modules.fileextmismatch;
23 import java.io.IOException;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.logging.Level;
30 import javax.xml.parsers.DocumentBuilder;
31 import javax.xml.parsers.DocumentBuilderFactory;
32 import javax.xml.parsers.ParserConfigurationException;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Element;
38 import org.w3c.dom.NodeList;
44 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<>();
92 final Document doc = XMLUtil.loadDoc(FileExtMismatchXML.class, filePath);
97 Element root = doc.getDocumentElement();
99 logger.log(Level.SEVERE,
"Error loading config file: invalid file format (bad root).");
103 NodeList sigNList = root.getElementsByTagName(SIG_EL);
104 final int numSigs = sigNList.getLength();
110 for(
int sigIndex = 0; sigIndex < numSigs; ++sigIndex) {
111 Element sigEl = (Element)sigNList.item(sigIndex);
112 final String mimetype = sigEl.getAttribute(SIG_MIMETYPE_ATTR);
114 NodeList extNList = sigEl.getElementsByTagName(EXT_EL);
115 final int numExts = extNList.getLength();
118 List<String> extStrings =
new ArrayList<>();
119 for(
int extIndex = 0; extIndex < numExts; ++extIndex) {
120 Element extEl = (Element)extNList.item(extIndex);
121 extStrings.add(extEl.getTextContent());
123 String[] sarray = extStrings.toArray(
new String[0]);
124 sigTypeToExtMap.put(mimetype, sarray);
126 sigTypeToExtMap.put(mimetype, null);
130 }
catch (Exception e) {
131 logger.log(Level.SEVERE,
"Error loading config file.", e);
134 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);