Changes based on review comments

This commit is contained in:
Kelly Kelly 2020-12-01 11:17:07 -05:00
parent 203caec192
commit bf7b2d78f2
2 changed files with 68 additions and 25 deletions

View File

@ -57,11 +57,10 @@ public final class AutopsyManifestFileParser implements ManifestFileParser {
Path fileName = filePath.getFileName(); Path fileName = filePath.getFileName();
if (fileName.toString().toUpperCase().endsWith(MANIFEST_FILE_NAME_SIGNATURE)) { if (fileName.toString().toUpperCase().endsWith(MANIFEST_FILE_NAME_SIGNATURE)) {
try {
fileIsManifest = isAutopsyManifestFile(filePath); fileIsManifest = (ManifestFileParser.getManifestRootNode(filePath, (str) -> {
} catch (Exception unused) { return (str.compareToIgnoreCase(ROOT_ELEM_TAG_NAME) == 0);
fileIsManifest = false; }) != null);
}
} }
return fileIsManifest; return fileIsManifest;
@ -75,12 +74,12 @@ public final class AutopsyManifestFileParser implements ManifestFileParser {
Date dateFileCreated = new Date(attrs.creationTime().toMillis()); Date dateFileCreated = new Date(attrs.creationTime().toMillis());
Document doc; Document doc;
try { try {
doc = createManifestDOM(filePath); doc = ManifestFileParser.createManifestDOM(filePath);
} catch (Exception ex) { } catch (Exception ex) {
// If the above call to createManifestDOM threw an exception // If the above call to createManifestDOM threw an exception
// try to fix the given XML file. // try to fix the given XML file.
tempPath = ManifestFileParser.makeTidyManifestFile(filePath); tempPath = ManifestFileParser.makeTidyManifestFile(filePath);
doc = createManifestDOM(tempPath); doc = ManifestFileParser.createManifestDOM(tempPath);
} }
XPath xpath = XPathFactory.newInstance().newXPath(); XPath xpath = XPathFactory.newInstance().newXPath();
@ -114,23 +113,6 @@ public final class AutopsyManifestFileParser implements ManifestFileParser {
} }
} }
/**
* Creates the DOM object for the file at the given path.
*
* @param manifestFilePath Path to XML file.
*
* @return DOM object for the given XML file.
*
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
private Document createManifestDOM(Path manifestFilePath) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
return docBuilder.parse(manifestFilePath.toFile());
}
/** /**
* Check to see if the given file is an autopsy auto ingest manifest file by * Check to see if the given file is an autopsy auto ingest manifest file by
* if the root element is ROOT_ELEM_TAG_NAME. * if the root element is ROOT_ELEM_TAG_NAME.
@ -141,7 +123,7 @@ public final class AutopsyManifestFileParser implements ManifestFileParser {
*/ */
private boolean isAutopsyManifestFile(Path filePath) throws IOException { private boolean isAutopsyManifestFile(Path filePath) throws IOException {
try { try {
Document doc = this.createManifestDOM(filePath); Document doc = ManifestFileParser.createManifestDOM(filePath);
Element docElement = doc.getDocumentElement(); Element docElement = doc.getDocumentElement();
return docElement.getTagName().equals(ROOT_ELEM_TAG_NAME); return docElement.getTagName().equals(ROOT_ELEM_TAG_NAME);
} catch (Exception unused) { } catch (Exception unused) {

View File

@ -24,7 +24,14 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.function.Predicate;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.tidy.Tidy; import org.w3c.tidy.Tidy;
import org.xml.sax.SAXException;
/** /**
* Responsible for parsing the manifest files that describe cases, devices, and * Responsible for parsing the manifest files that describe cases, devices, and
@ -77,6 +84,60 @@ public interface ManifestFileParser {
return Paths.get(tempFile.toString()); return Paths.get(tempFile.toString());
} }
/**
* Create a new DOM document object for the given manifest file.
*
* @param manifestFilePath Fully qualified path to manifest file.
*
* @return DOM document object
*
* @throws ParserConfigurationException
* @throws SAXException
* @throws IOException
*/
static Document createManifestDOM(Path manifestFilePath) throws ParserConfigurationException, SAXException, IOException {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
return docBuilder.parse(manifestFilePath.toFile());
}
/**
* Return the root node of the given Manifest XML file.
*
* @param filePath XML filePath
* @param isRootTester Predicate method for testing if a string is the root node.
*
* @return The XML file root node or null if the node was not found or the
* file is not an XML file.
*/
static String getManifestRootNode(Path filePath, Predicate<String> isRootTester) {
Document doc;
Path tempPath = null;
try {
try {
doc = ManifestFileParser.createManifestDOM(filePath);
} catch (Exception unused) {
// If the above call to createManifestDOM threw an exception
// try to fix the given XML file.
tempPath = ManifestFileParser.makeTidyManifestFile(filePath);
doc = ManifestFileParser.createManifestDOM(tempPath);
}
Element docElement = doc.getDocumentElement();
String rootElementTag = docElement.getTagName();
if(isRootTester.test(rootElementTag)) {
return rootElementTag;
}
} catch (Exception unused) {
// Unused exception. If an exception is thrown the given XML file
// cannot be parsed.
} finally {
if (tempPath != null) {
tempPath.toFile().delete();
}
}
return null;
}
public final static class ManifestFileParserException extends Exception { public final static class ManifestFileParserException extends Exception {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;