Autopsy  3.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;
34 
40  class FileReportText implements FileReportModule {
41  private static final Logger logger = Logger.getLogger(FileReportText.class.getName());
42  private String reportPath;
43  private Writer out;
44  private static final String FILE_NAME = "file-report.txt"; //NON-NLS
45 
46  private static FileReportText instance;
47 
48  // Get the default implementation of this report
49  public static synchronized FileReportText getDefault() {
50  if (instance == null) {
51  instance = new FileReportText();
52  }
53  return instance;
54  }
55 
56  @Override
57  public void startReport(String baseReportDir) {
58  this.reportPath = baseReportDir + FILE_NAME;
59  try {
60  out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(this.reportPath)));
61  } catch (IOException ex) {
62  logger.log(Level.WARNING, "Failed to create report text file", ex); //NON-NLS
63  }
64  }
65 
66  @Override
67  public void endReport() {
68  if (out != null) {
69  try {
70  out.close();
71  } catch (IOException ex) {
72  logger.log(Level.WARNING, "Could not close output writer when ending report.", ex); //NON-NLS
73  }
74  }
75  }
76 
77  private String getTabDelimitedList(List<String> list) {
78  StringBuilder output = new StringBuilder();
79  Iterator<String> it = list.iterator();
80  while(it.hasNext()) {
81  output.append(it.next()).append((it.hasNext() ? "\t" : System.lineSeparator()));
82  }
83  return output.toString();
84  }
85 
86  @Override
87  public void startTable(List<FileReportDataTypes> headers) {
88  List<String> titles = new ArrayList<>();
89  for(FileReportDataTypes col : headers) {
90  titles.add(col.getName());
91  }
92  try {
93  out.write(getTabDelimitedList(titles));
94  } catch (IOException ex) {
95  logger.log(Level.WARNING, "Error when writing headers to report file: {0}", ex); //NON-NLS
96  }
97  }
98 
99  @Override
100  public void addRow(AbstractFile toAdd, List<FileReportDataTypes> columns) {
101  List<String> cells = new ArrayList<>();
102  for(FileReportDataTypes type : columns) {
103  cells.add(type.getValue(toAdd));
104  }
105  try {
106  out.write(getTabDelimitedList(cells));
107  } catch (IOException ex) {
108  logger.log(Level.WARNING, "Error when writing row to report file: {0}", ex); //NON-NLS
109  }
110  }
111 
112  @Override
113  public void endTable() {
114  try {
115  out.write(System.lineSeparator());
116  } catch (IOException ex) {
117  logger.log(Level.WARNING, "Error when closing table: {0}", ex); //NON-NLS
118  }
119  }
120 
121  @Override
122  public String getName() {
123  return NbBundle.getMessage(this.getClass(), "FileReportText.getName.text");
124  }
125 
126  @Override
127  public String getDescription() {
128  return NbBundle.getMessage(this.getClass(), "FileReportText.getDesc.text");
129  }
130 
131  @Override
132  public String getRelativeFilePath() {
133  return FILE_NAME;
134  }
135 }

Copyright © 2012-2015 Basis Technology. Generated on: Mon Oct 19 2015
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.