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;
42 class FileReportText
implements FileReportModule {
44 private static final Logger logger = Logger.getLogger(FileReportText.class.getName());
45 private String reportPath;
47 private static final String FILE_NAME =
"file-report.txt";
49 private static FileReportText instance;
52 public static synchronized FileReportText getDefault() {
53 if (instance == null) {
54 instance =
new FileReportText();
60 public void startReport(String baseReportDir) {
61 this.reportPath = baseReportDir + FILE_NAME;
63 out =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(this.reportPath)));
64 }
catch (IOException ex) {
65 logger.log(Level.WARNING,
"Failed to create report text file", ex);
70 public void endReport() {
74 Case.getCurrentCase().addReport(reportPath, NbBundle.getMessage(
this.getClass(),
75 "FileReportText.getName.text"),
"");
76 }
catch (IOException ex) {
77 logger.log(Level.WARNING,
"Could not close output writer when ending report.", ex);
78 }
catch (TskCoreException ex) {
79 String errorMessage = String.format(
"Error adding %s to case as a report", reportPath);
80 logger.log(Level.SEVERE, errorMessage, ex);
85 private String getTabDelimitedList(List<String> list) {
86 StringBuilder output =
new StringBuilder();
87 Iterator<String> it = list.iterator();
88 while (it.hasNext()) {
89 output.append(it.next()).append((it.hasNext() ?
"\t" : System.lineSeparator()));
91 return output.toString();
95 public void startTable(List<FileReportDataTypes> headers) {
96 List<String> titles =
new ArrayList<>();
97 for (FileReportDataTypes col : headers) {
98 titles.add(col.getName());
101 out.write(getTabDelimitedList(titles));
102 }
catch (IOException ex) {
103 logger.log(Level.WARNING,
"Error when writing headers to report file: {0}", ex);
108 public void addRow(AbstractFile toAdd, List<FileReportDataTypes> columns) {
109 List<String> cells =
new ArrayList<>();
110 for (FileReportDataTypes type : columns) {
111 cells.add(type.getValue(toAdd));
114 out.write(getTabDelimitedList(cells));
115 }
catch (IOException ex) {
116 logger.log(Level.WARNING,
"Error when writing row to report file: {0}", ex);
121 public void endTable() {
123 out.write(System.lineSeparator());
124 }
catch (IOException ex) {
125 logger.log(Level.WARNING,
"Error when closing table: {0}", ex);
130 public String getName() {
131 return NbBundle.getMessage(this.getClass(),
"FileReportText.getName.text");
135 public String getDescription() {
136 return NbBundle.getMessage(this.getClass(),
"FileReportText.getDesc.text");
140 public String getRelativeFilePath() {