Autopsy  4.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileExtMismatchXML.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2014 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.modules.fileextmismatch;
20 
21 import java.io.File;
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;
38 
43 class FileExtMismatchXML {
44 
45  private static final Logger logger = Logger.getLogger(FileExtMismatchXML.class.getName());
46  private static FileExtMismatchXML defaultInstance = null;
47 
48  private static final String ENCODING = "UTF-8"; //NON-NLS
49  private static final String XSDFILE = "MismatchConfigSchema.xsd"; //NON-NLS
50 
51  private static final String ROOT_EL = "mismatch_config"; //NON-NLS
52  private static final String SIG_EL = "signature"; //NON-NLS
53  private static final String EXT_EL = "ext"; //NON-NLS
54  private static final String SIG_MIMETYPE_ATTR = "mimetype"; //NON-NLS
55 
56  private static final String DEFAULT_CONFIG_FILE_NAME = "mismatch_config.xml"; //NON-NLS
57 
58  protected String filePath;
59 
60  FileExtMismatchXML(String filePath) {
61  this.filePath = filePath;
62 
63  try {
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); //NON-NLS
67  }
68  }
69 
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);
78  }
79  return defaultInstance;
80  }
81 
87  public HashMap<String, String[]> load() {
88  HashMap<String, String[]> sigTypeToExtMap = new HashMap<>();
89 
90  try {
91  final Document doc = XMLUtil.loadDoc(FileExtMismatchXML.class, filePath);
92  if (doc == null) {
93  return null;
94  }
95 
96  Element root = doc.getDocumentElement();
97  if (root == null) {
98  logger.log(Level.SEVERE, "Error loading config file: invalid file format (bad root)."); //NON-NLS
99  return null;
100  }
101 
102  NodeList sigNList = root.getElementsByTagName(SIG_EL);
103  final int numSigs = sigNList.getLength();
104 
105  if (numSigs == 0) {
106  return null;
107  }
108 
109  for (int sigIndex = 0; sigIndex < numSigs; ++sigIndex) {
110  Element sigEl = (Element) sigNList.item(sigIndex);
111  final String mimetype = sigEl.getAttribute(SIG_MIMETYPE_ATTR);
112 
113  NodeList extNList = sigEl.getElementsByTagName(EXT_EL);
114  final int numExts = extNList.getLength();
115 
116  if (numExts != 0) {
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());
121  }
122  String[] sarray = extStrings.toArray(new String[0]);
123  sigTypeToExtMap.put(mimetype, sarray);
124  } else {
125  sigTypeToExtMap.put(mimetype, null); //ok to have an empty type (the ingest module will not use it)
126  }
127  }
128 
129  } catch (Exception e) {
130  logger.log(Level.SEVERE, "Error loading config file.", e); //NON-NLS
131  return null;
132  }
133  return sigTypeToExtMap;
134  }
135 
144  public boolean save(HashMap<String, String[]> sigTypeToExtMap) {
145  boolean success;
146 
147  DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
148 
149  try {
150  DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
151  Document doc = docBuilder.newDocument();
152 
153  Element rootEl = doc.createElement(ROOT_EL);
154  doc.appendChild(rootEl);
155 
156  ArrayList<String> mimeTypeList = new ArrayList<>(sigTypeToExtMap.keySet());
157  Collections.sort(mimeTypeList);
158 
159  for (String mimeType : mimeTypeList) {
160  Element sigEl = doc.createElement(SIG_EL);
161  sigEl.setAttribute(SIG_MIMETYPE_ATTR, mimeType.toLowerCase());
162 
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);
171  }
172  }
173  rootEl.appendChild(sigEl);
174  }
175 
176  success = XMLUtil.saveDoc(FileExtMismatchXML.class, filePath, ENCODING, doc);
177 
178  } catch (ParserConfigurationException e) {
179  logger.log(Level.SEVERE, "Error saving keyword list: can't initialize parser.", e); //NON-NLS
180  success = false;
181  }
182  return success;
183  }
184 
185 }

Copyright © 2012-2015 Basis Technology. Generated on: Wed Apr 6 2016
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.