Autopsy  4.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 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;
36 
42 class FileReportText implements FileReportModule {
43 
44  private static final Logger logger = Logger.getLogger(FileReportText.class.getName());
45  private String reportPath;
46  private Writer out;
47  private static final String FILE_NAME = "file-report.txt"; //NON-NLS
48 
49  private static FileReportText instance;
50 
51  // Get the default implementation of this report
52  public static synchronized FileReportText getDefault() {
53  if (instance == null) {
54  instance = new FileReportText();
55  }
56  return instance;
57  }
58 
59  @Override
60  public void startReport(String baseReportDir) {
61  this.reportPath = baseReportDir + FILE_NAME;
62  try {
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); //NON-NLS
66  }
67  }
68 
69  @Override
70  public void endReport() {
71  if (out != null) {
72  try {
73  out.close();
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); //NON-NLS
78  } catch (TskCoreException ex) {
79  String errorMessage = String.format("Error adding %s to case as a report", reportPath); //NON-NLS
80  logger.log(Level.SEVERE, errorMessage, ex);
81  }
82  }
83  }
84 
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()));
90  }
91  return output.toString();
92  }
93 
94  @Override
95  public void startTable(List<FileReportDataTypes> headers) {
96  List<String> titles = new ArrayList<>();
97  for (FileReportDataTypes col : headers) {
98  titles.add(col.getName());
99  }
100  try {
101  out.write(getTabDelimitedList(titles));
102  } catch (IOException ex) {
103  logger.log(Level.WARNING, "Error when writing headers to report file: {0}", ex); //NON-NLS
104  }
105  }
106 
107  @Override
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));
112  }
113  try {
114  out.write(getTabDelimitedList(cells));
115  } catch (IOException ex) {
116  logger.log(Level.WARNING, "Error when writing row to report file: {0}", ex); //NON-NLS
117  }
118  }
119 
120  @Override
121  public void endTable() {
122  try {
123  out.write(System.lineSeparator());
124  } catch (IOException ex) {
125  logger.log(Level.WARNING, "Error when closing table: {0}", ex); //NON-NLS
126  }
127  }
128 
129  @Override
130  public String getName() {
131  return NbBundle.getMessage(this.getClass(), "FileReportText.getName.text");
132  }
133 
134  @Override
135  public String getDescription() {
136  return NbBundle.getMessage(this.getClass(), "FileReportText.getDesc.text");
137  }
138 
139  @Override
140  public String getRelativeFilePath() {
141  return FILE_NAME;
142  }
143 }

Copyright © 2012-2016 Basis Technology. Generated on: Mon Apr 24 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.