Autopsy  3.1
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 
20 package org.sleuthkit.autopsy.modules.fileextmismatch;
21 
22 import java.io.File;
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;
39 
44 class FileExtMismatchXML {
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  {
92  final Document doc = XMLUtil.loadDoc(FileExtMismatchXML.class, filePath);
93  if (doc == null) {
94  return null;
95  }
96 
97  Element root = doc.getDocumentElement();
98  if (root == null) {
99  logger.log(Level.SEVERE, "Error loading config file: invalid file format (bad root)."); //NON-NLS
100  return null;
101  }
102 
103  NodeList sigNList = root.getElementsByTagName(SIG_EL);
104  final int numSigs = sigNList.getLength();
105 
106  if (numSigs == 0) {
107  return null;
108  }
109 
110  for(int sigIndex = 0; sigIndex < numSigs; ++sigIndex) {
111  Element sigEl = (Element)sigNList.item(sigIndex);
112  final String mimetype = sigEl.getAttribute(SIG_MIMETYPE_ATTR);
113 
114  NodeList extNList = sigEl.getElementsByTagName(EXT_EL);
115  final int numExts = extNList.getLength();
116 
117  if (numExts != 0) {
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());
122  }
123  String[] sarray = extStrings.toArray(new String[0]);
124  sigTypeToExtMap.put(mimetype, sarray);
125  } else {
126  sigTypeToExtMap.put(mimetype, null); //ok to have an empty type (the ingest module will not use it)
127  }
128  }
129 
130  } catch (Exception e) {
131  logger.log(Level.SEVERE, "Error loading config file.", e); //NON-NLS
132  return null;
133  }
134  return sigTypeToExtMap;
135  }
136 
137 
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 }
186 

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.