diff --git a/Experimental/src/org/sleuthkit/autopsy/experimental/autoingest/AutopsyManifestFileParser.java b/Experimental/src/org/sleuthkit/autopsy/experimental/autoingest/AutopsyManifestFileParser.java index c40b20be24..99462f8084 100644 --- a/Experimental/src/org/sleuthkit/autopsy/experimental/autoingest/AutopsyManifestFileParser.java +++ b/Experimental/src/org/sleuthkit/autopsy/experimental/autoingest/AutopsyManifestFileParser.java @@ -57,11 +57,10 @@ public final class AutopsyManifestFileParser implements ManifestFileParser { Path fileName = filePath.getFileName(); if (fileName.toString().toUpperCase().endsWith(MANIFEST_FILE_NAME_SIGNATURE)) { - try { - fileIsManifest = isAutopsyManifestFile(filePath); - } catch (Exception unused) { - fileIsManifest = false; - } + + fileIsManifest = (ManifestFileParser.getManifestRootNode(filePath, (str) -> { + return (str.compareToIgnoreCase(ROOT_ELEM_TAG_NAME) == 0); + }) != null); } return fileIsManifest; @@ -75,12 +74,12 @@ public final class AutopsyManifestFileParser implements ManifestFileParser { Date dateFileCreated = new Date(attrs.creationTime().toMillis()); Document doc; try { - doc = createManifestDOM(filePath); + doc = ManifestFileParser.createManifestDOM(filePath); } catch (Exception ex) { // If the above call to createManifestDOM threw an exception // try to fix the given XML file. tempPath = ManifestFileParser.makeTidyManifestFile(filePath); - doc = createManifestDOM(tempPath); + doc = ManifestFileParser.createManifestDOM(tempPath); } 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 * 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 { try { - Document doc = this.createManifestDOM(filePath); + Document doc = ManifestFileParser.createManifestDOM(filePath); Element docElement = doc.getDocumentElement(); return docElement.getTagName().equals(ROOT_ELEM_TAG_NAME); } catch (Exception unused) { diff --git a/Experimental/src/org/sleuthkit/autopsy/experimental/autoingest/ManifestFileParser.java b/Experimental/src/org/sleuthkit/autopsy/experimental/autoingest/ManifestFileParser.java index a04fe0df0b..7e451c8be1 100644 --- a/Experimental/src/org/sleuthkit/autopsy/experimental/autoingest/ManifestFileParser.java +++ b/Experimental/src/org/sleuthkit/autopsy/experimental/autoingest/ManifestFileParser.java @@ -24,7 +24,14 @@ import java.io.FileOutputStream; import java.io.IOException; import java.nio.file.Path; 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.xml.sax.SAXException; /** * Responsible for parsing the manifest files that describe cases, devices, and @@ -77,6 +84,60 @@ public interface ManifestFileParser { 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 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 { private static final long serialVersionUID = 1L;