19 package org.sleuthkit.autopsy.report;
21 import java.io.BufferedWriter;
22 import java.io.FileOutputStream;
23 import java.io.IOException;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26 import java.util.ArrayList;
27 import java.util.Iterator;
28 import java.util.List;
29 import java.util.logging.Level;
32 import org.openide.util.NbBundle;
43 class FileReportText
implements FileReportModule {
45 private static final Logger logger = Logger.getLogger(FileReportText.class.getName());
46 private String reportPath;
48 private static final String FILE_NAME =
"file-report.txt";
50 private static FileReportText instance;
53 public static synchronized FileReportText getDefault() {
54 if (instance == null) {
55 instance =
new FileReportText();
61 public void startReport(String baseReportDir) {
62 this.reportPath = baseReportDir + FILE_NAME;
64 out =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(this.reportPath)));
65 }
catch (IOException ex) {
66 logger.log(Level.WARNING,
"Failed to create report text file", ex);
71 public void endReport() {
75 Case.getCurrentCaseThrows().addReport(reportPath, NbBundle.getMessage(
this.getClass(),
76 "FileReportText.getName.text"),
"");
77 }
catch (IOException ex) {
78 logger.log(Level.WARNING,
"Could not close output writer when ending report.", ex);
79 }
catch (TskCoreException ex) {
80 String errorMessage = String.format(
"Error adding %s to case as a report", reportPath);
81 logger.log(Level.SEVERE, errorMessage, ex);
82 }
catch (NoCurrentCaseException ex) {
83 logger.log(Level.SEVERE,
"Exception while getting open case.", ex);
88 private String getTabDelimitedList(List<String> list) {
89 StringBuilder output =
new StringBuilder();
90 Iterator<String> it = list.iterator();
91 while (it.hasNext()) {
92 output.append(it.next()).append((it.hasNext() ?
"\t" : System.lineSeparator()));
94 return output.toString();
98 public void startTable(List<FileReportDataTypes> headers) {
99 List<String> titles =
new ArrayList<>();
100 for (FileReportDataTypes col : headers) {
101 titles.add(col.getName());
104 out.write(getTabDelimitedList(titles));
105 }
catch (IOException ex) {
106 logger.log(Level.WARNING,
"Error when writing headers to report file: {0}", ex);
111 public void addRow(AbstractFile toAdd, List<FileReportDataTypes> columns) {
112 List<String> cells =
new ArrayList<>();
113 for (FileReportDataTypes type : columns) {
114 cells.add(type.getValue(toAdd));
117 out.write(getTabDelimitedList(cells));
118 }
catch (IOException ex) {
119 logger.log(Level.WARNING,
"Error when writing row to report file: {0}", ex);
124 public void endTable() {
126 out.write(System.lineSeparator());
127 }
catch (IOException ex) {
128 logger.log(Level.WARNING,
"Error when closing table: {0}", ex);
133 public String getName() {
134 return NbBundle.getMessage(this.getClass(),
"FileReportText.getName.text");
138 public String getDescription() {
139 return NbBundle.getMessage(this.getClass(),
"FileReportText.getDesc.text");
143 public String getRelativeFilePath() {