19 package org.sleuthkit.autopsy.report.modules.file;
24 import java.io.BufferedWriter;
25 import java.io.FileNotFoundException;
26 import java.io.FileOutputStream;
27 import java.io.IOException;
28 import java.io.OutputStreamWriter;
29 import java.io.Writer;
30 import java.nio.charset.StandardCharsets;
31 import java.util.ArrayList;
32 import java.util.Iterator;
33 import java.util.List;
34 import java.util.logging.Level;
35 import javax.swing.JPanel;
37 import org.openide.util.NbBundle;
49 class FileReportText
implements FileReportModule {
51 private static final Logger logger = Logger.getLogger(FileReportText.class.getName());
52 private static final String FILE_NAME =
"file-report.txt";
53 private static FileReportText instance;
54 private String reportPath;
56 private ReportFileTextConfigurationPanel configPanel;
59 public static synchronized FileReportText getDefault() {
60 if (instance == null) {
61 instance =
new FileReportText();
72 public ReportModuleSettings getDefaultConfiguration() {
73 return new FileReportModuleSettings();
82 public ReportModuleSettings getConfiguration() {
84 return configPanel.getConfiguration();
93 public void setConfiguration(ReportModuleSettings settings) {
95 if (settings == null || settings instanceof NoReportModuleSettings) {
96 configPanel.setConfiguration((FileReportModuleSettings) getDefaultConfiguration());
100 if (settings instanceof FileReportModuleSettings) {
101 configPanel.setConfiguration((FileReportModuleSettings) settings);
105 throw new IllegalArgumentException(
"Expected settings argument to be an instance of FileReportModuleSettings");
109 public void startReport(String baseReportDir) {
110 this.reportPath = baseReportDir + FILE_NAME;
112 out =
new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream(this.reportPath), StandardCharsets.UTF_8));
114 }
catch (FileNotFoundException ex) {
115 logger.log(Level.WARNING,
"Failed to create report text file", ex);
116 }
catch (IOException ex) {
117 logger.log(Level.WARNING,
"Failed to write BOM to report text file", ex);
122 public void endReport() {
126 Case.getCurrentCaseThrows().addReport(reportPath, NbBundle.getMessage(
this.getClass(),
127 "FileReportText.getName.text"),
"");
128 }
catch (IOException ex) {
129 logger.log(Level.WARNING,
"Could not close output writer when ending report.", ex);
130 }
catch (TskCoreException ex) {
131 String errorMessage = String.format(
"Error adding %s to case as a report", reportPath);
132 logger.log(Level.SEVERE, errorMessage, ex);
133 }
catch (NoCurrentCaseException ex) {
134 logger.log(Level.SEVERE,
"Exception while getting open case.", ex);
139 private String getDelimitedList(List<String> list, String delimiter) {
140 StringBuilder output;
141 output =
new StringBuilder();
142 Iterator<String> it = list.iterator();
143 while (it.hasNext()) {
144 output.append(
'"').append(it.next()).append(
'"').append((it.hasNext() ? delimiter : System.lineSeparator()));
146 return output.toString();
150 public void startTable(List<FileReportDataTypes> headers) {
151 List<String> titles =
new ArrayList<>();
152 for (FileReportDataTypes col : headers) {
153 titles.add(col.getName());
156 out.write(getDelimitedList(titles, configPanel.getDelimiter()));
157 }
catch (IOException ex) {
158 logger.log(Level.WARNING,
"Error when writing headers to report file: {0}", ex);
163 public void addRow(AbstractFile toAdd, List<FileReportDataTypes> columns) {
164 List<String> cells =
new ArrayList<>();
165 for (FileReportDataTypes type : columns) {
166 cells.add(type.getValue(toAdd));
169 out.write(getDelimitedList(cells, configPanel.getDelimiter()));
170 }
catch (IOException ex) {
171 logger.log(Level.WARNING,
"Error when writing row to report file: {0}", ex);
176 public void endTable() {
178 out.write(System.lineSeparator());
179 }
catch (IOException ex) {
180 logger.log(Level.WARNING,
"Error when closing table: {0}", ex);
185 public String getName() {
186 return NbBundle.getMessage(this.getClass(),
"FileReportText.getName.text");
190 public String getDescription() {
191 return NbBundle.getMessage(this.getClass(),
"FileReportText.getDesc.text");
195 public String getRelativeFilePath() {
200 public JPanel getConfigurationPanel() {
205 private void initializePanel() {
206 if (configPanel == null) {
207 configPanel =
new ReportFileTextConfigurationPanel();