19 package org.sleuthkit.autopsy.casemodule;
21 import java.io.BufferedWriter;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStreamWriter;
25 import java.io.StringWriter;
26 import java.nio.file.Path;
27 import java.nio.file.Paths;
28 import java.text.DateFormat;
29 import java.text.SimpleDateFormat;
30 import java.util.Date;
31 import javax.xml.parsers.DocumentBuilder;
32 import javax.xml.parsers.DocumentBuilderFactory;
33 import javax.xml.parsers.ParserConfigurationException;
34 import javax.xml.transform.OutputKeys;
35 import javax.xml.transform.Result;
36 import javax.xml.transform.Source;
37 import javax.xml.transform.Transformer;
38 import javax.xml.transform.TransformerException;
39 import javax.xml.transform.TransformerFactory;
40 import javax.xml.transform.dom.DOMSource;
41 import javax.xml.transform.stream.StreamResult;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.NodeList;
47 import org.xml.sax.SAXException;
55 private static final DateFormat
DATE_FORMAT =
new SimpleDateFormat(
"yyyy/MM/dd HH:mm:ss (z)");
140 caseDatabaseName =
"";
141 caseDatabasePath =
"";
176 return metadataFilePath.getParent().toString();
217 this.caseDetails = newCaseDetails;
220 }
catch (CaseMetadataException ex) {
221 this.caseDetails = oldCaseDetails;
277 }
catch (CaseMetadataException ex) {
278 this.caseDatabaseName = oldCaseDatabaseName;
298 String getCreatedDate() {
310 void setCreatedDate(String createdDate)
throws CaseMetadataException {
315 }
catch (CaseMetadataException ex) {
316 this.createdDate = oldCreatedDate;
326 String getCreatedByVersion() {
338 void setCreatedByVersion(String buildVersion)
throws CaseMetadataException {
340 this.createdByVersion = buildVersion;
343 }
catch (CaseMetadataException ex) {
344 this.createdByVersion = oldCreatedByVersion;
367 Source source =
new DOMSource(doc);
368 StringWriter stringWriter =
new StringWriter();
369 Result streamResult =
new StreamResult(stringWriter);
370 Transformer transformer = TransformerFactory.newInstance().newTransformer();
371 transformer.setOutputProperty(OutputKeys.INDENT,
"yes");
372 transformer.setOutputProperty(
"{http://xml.apache.org/xslt}indent-amount",
"2");
373 transformer.transform(source, streamResult);
378 try (BufferedWriter fileWriter =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(metadataFilePath.toFile())))) {
379 fileWriter.write(stringWriter.toString());
383 }
catch (ParserConfigurationException | TransformerException | IOException ex) {
384 throw new CaseMetadataException(String.format(
"Error writing to case metadata file %s", metadataFilePath), ex);
395 Element rootElement = doc.createElement(ROOT_ELEMENT_NAME);
396 doc.appendChild(rootElement);
397 createChildElement(doc, rootElement, SCHEMA_VERSION_ELEMENT_NAME, CURRENT_SCHEMA_VERSION);
399 createChildElement(doc, rootElement, MODIFIED_DATE_ELEMENT_NAME, DATE_FORMAT.format(
new Date()));
400 createChildElement(doc, rootElement, AUTOPSY_CREATED_BY_ELEMENT_NAME, createdByVersion);
402 Element caseElement = doc.createElement(CASE_ELEMENT_NAME);
403 rootElement.appendChild(caseElement);
416 createChildElement(doc, caseElement, CASE_DB_ABSOLUTE_PATH_ELEMENT_NAME, caseDatabasePath);
417 createChildElement(doc, caseElement, CASE_DB_NAME_RELATIVE_ELEMENT_NAME, caseDatabaseName);
430 private void createChildElement(Document doc, Element parentElement, String elementName, String elementContent) {
431 Element element = doc.createElement(elementName);
432 element.appendChild(doc.createTextNode(elementContent));
433 parentElement.appendChild(element);
447 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
448 Document doc = builder.parse(this.getFilePath().toFile());
449 doc.getDocumentElement().normalize();
450 Element rootElement = doc.getDocumentElement();
451 if (!rootElement.getNodeName().equals(ROOT_ELEMENT_NAME)) {
452 throw new CaseMetadataException(
"Case metadata file corrupted");
460 if (schemaVersion.equals(SCHEMA_VERSION_ONE)) {
469 NodeList caseElements = doc.getElementsByTagName(CASE_ELEMENT_NAME);
470 if (caseElements.getLength() == 0) {
471 throw new CaseMetadataException(
"Case metadata file corrupted");
473 Element caseElement = (Element) caseElements.item(0);
475 String caseDisplayName;
477 if (schemaVersion.equals(SCHEMA_VERSION_ONE) || schemaVersion.equals(SCHEMA_VERSION_TWO)) {
484 String examinerPhone;
485 String examinerEmail;
487 if (schemaVersion.equals(SCHEMA_VERSION_ONE) || schemaVersion.equals(SCHEMA_VERSION_TWO) || schemaVersion.equals(SCHEMA_VERSION_THREE)) {
496 this.caseDetails =
new CaseDetails(caseDisplayName, caseNumber, examinerName, examinerPhone, examinerEmail, caseNotes);
499 throw new CaseMetadataException(
"Case metadata file corrupted");
501 switch (schemaVersion) {
502 case SCHEMA_VERSION_ONE:
506 case SCHEMA_VERSION_TWO:
507 this.caseDatabaseName =
getElementTextContent(caseElement, CASE_DB_ABSOLUTE_PATH_ELEMENT_NAME,
true);
511 this.caseDatabaseName =
getElementTextContent(caseElement, CASE_DB_NAME_RELATIVE_ELEMENT_NAME,
true);
522 Path possibleAbsoluteCaseDbPath = Paths.get(this.caseDatabaseName);
524 if (possibleAbsoluteCaseDbPath.getNameCount() > 1) {
526 this.caseDatabaseName = caseDirectoryPath.relativize(possibleAbsoluteCaseDbPath).toString();
528 this.caseDatabasePath = caseDirectoryPath.resolve(caseDatabaseName).toAbsolutePath().toString();
531 }
catch (ParserConfigurationException | SAXException | IOException ex) {
532 throw new CaseMetadataException(String.format(
"Error reading from case metadata file %s", metadataFilePath), ex);
549 NodeList elementsList = parentElement.getElementsByTagName(elementName);
550 if (elementsList.getLength() == 0) {
551 throw new CaseMetadataException(String.format(
"Missing %s element from case metadata file %s", elementName, metadataFilePath));
553 String textContent = elementsList.item(0).getTextContent();
554 if (textContent.isEmpty() && contentIsRequired) {
555 throw new CaseMetadataException(String.format(
"Empty %s element in case metadata file %s", elementName, metadataFilePath));
573 super(message, cause);
591 throw new UnsupportedOperationException();
static CaseType fromString(String typeName)
String getExaminerPhone()
static Document createDocument()
String getCaseDisplayName()
static String getVersion()
String getExaminerEmail()