Autopsy  4.9.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
FileReportText.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013 - 2018 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.report;
20 
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;
31 
32 import org.openide.util.NbBundle;
35 import org.sleuthkit.datamodel.AbstractFile;
36 import org.sleuthkit.datamodel.TskCoreException;
37 
43 class FileReportText implements FileReportModule {
44 
45  private static final Logger logger = Logger.getLogger(FileReportText.class.getName());
46  private String reportPath;
47  private Writer out;
48  private static final String FILE_NAME = "file-report.txt"; //NON-NLS
49 
50  private static FileReportText instance;
51 
52  // Get the default implementation of this report
53  public static synchronized FileReportText getDefault() {
54  if (instance == null) {
55  instance = new FileReportText();
56  }
57  return instance;
58  }
59 
60  @Override
61  public void startReport(String baseReportDir) {
62  this.reportPath = baseReportDir + FILE_NAME;
63  try {
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); //NON-NLS
67  }
68  }
69 
70  @Override
71  public void endReport() {
72  if (out != null) {
73  try {
74  out.close();
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); //NON-NLS
79  } catch (TskCoreException ex) {
80  String errorMessage = String.format("Error adding %s to case as a report", reportPath); //NON-NLS
81  logger.log(Level.SEVERE, errorMessage, ex);
82  } catch (NoCurrentCaseException ex) {
83  logger.log(Level.SEVERE, "Exception while getting open case.", ex);
84  }
85  }
86  }
87 
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()));
93  }
94  return output.toString();
95  }
96 
97  @Override
98  public void startTable(List<FileReportDataTypes> headers) {
99  List<String> titles = new ArrayList<>();
100  for (FileReportDataTypes col : headers) {
101  titles.add(col.getName());
102  }
103  try {
104  out.write(getTabDelimitedList(titles));
105  } catch (IOException ex) {
106  logger.log(Level.WARNING, "Error when writing headers to report file: {0}", ex); //NON-NLS
107  }
108  }
109 
110  @Override
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));
115  }
116  try {
117  out.write(getTabDelimitedList(cells));
118  } catch (IOException ex) {
119  logger.log(Level.WARNING, "Error when writing row to report file: {0}", ex); //NON-NLS
120  }
121  }
122 
123  @Override
124  public void endTable() {
125  try {
126  out.write(System.lineSeparator());
127  } catch (IOException ex) {
128  logger.log(Level.WARNING, "Error when closing table: {0}", ex); //NON-NLS
129  }
130  }
131 
132  @Override
133  public String getName() {
134  return NbBundle.getMessage(this.getClass(), "FileReportText.getName.text");
135  }
136 
137  @Override
138  public String getDescription() {
139  return NbBundle.getMessage(this.getClass(), "FileReportText.getDesc.text");
140  }
141 
142  @Override
143  public String getRelativeFilePath() {
144  return FILE_NAME;
145  }
146 }

Copyright © 2012-2018 Basis Technology. Generated on: Tue Dec 18 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.