Autopsy  4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
ReportExcel.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2013-2014 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.FileOutputStream;
22 import java.io.IOException;
23 import java.text.SimpleDateFormat;
24 import java.util.List;
25 import java.util.logging.Level;
26 import org.apache.poi.ss.usermodel.*;
27 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
28 import org.openide.util.NbBundle;
32 
33 class ReportExcel implements TableReportModule {
34 
35  private static final Logger logger = Logger.getLogger(ReportExcel.class.getName());
36  private static ReportExcel instance;
37 
38  private Workbook wb;
39  private Sheet sheet;
40  private CellStyle titleStyle;
41  private CellStyle setStyle;
42  private CellStyle elementStyle;
43  private int rowIndex = 0;
44  private int sheetColCount = 0;
45  private String reportPath;
46 
47  // Get the default instance of this report
48  public static synchronized ReportExcel getDefault() {
49  if (instance == null) {
50  instance = new ReportExcel();
51  }
52  return instance;
53  }
54 
55  // Hidden constructor
56  private ReportExcel() {
57  }
58 
65  @Override
66  public void startReport(String baseReportDir) {
67  // Set the path and save it for when the report is written to disk.
68  this.reportPath = baseReportDir + getRelativeFilePath();
69 
70  // Make a workbook.
71  wb = new XSSFWorkbook();
72 
73  // Create some cell styles.
74  // TODO: The commented out cell style settings below do not work as desired when
75  // the output file is loaded by MS Excel or OfficeLibre. The font height and weight
76  // settings only work as expected when the output file is loaded by OfficeLibre.
77  // The alignment and text wrap settings appear to have no effect.
78  titleStyle = wb.createCellStyle();
79 // titleStyle.setBorderBottom((short) 1);
80  Font titleFont = wb.createFont();
81  titleFont.setFontHeightInPoints((short) 12);
82  titleStyle.setFont(titleFont);
83  titleStyle.setAlignment(CellStyle.ALIGN_LEFT);
84  titleStyle.setWrapText(true);
85 
86  setStyle = wb.createCellStyle();
87  Font setFont = wb.createFont();
88  setFont.setFontHeightInPoints((short) 14);
89  setFont.setBoldweight((short) 10);
90  setStyle.setFont(setFont);
91  setStyle.setAlignment(CellStyle.ALIGN_LEFT);
92  setStyle.setWrapText(true);
93 
94  elementStyle = wb.createCellStyle();
95 // elementStyle.setF illBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
96  Font elementFont = wb.createFont();
97  elementFont.setFontHeightInPoints((short) 14);
98  elementStyle.setFont(elementFont);
99  elementStyle.setAlignment(CellStyle.ALIGN_LEFT);
100  elementStyle.setWrapText(true);
101 
102  writeSummaryWorksheet();
103  }
104 
108  @Override
109  public void endReport() {
110  FileOutputStream out = null;
111  try {
112  out = new FileOutputStream(reportPath);
113  wb.write(out);
114  Case.getCurrentCase().addReport(reportPath, NbBundle.getMessage(this.getClass(),
115  "ReportExcel.endReport.srcModuleName.text"), "");
116  } catch (IOException ex) {
117  logger.log(Level.SEVERE, "Failed to write Excel report.", ex); //NON-NLS
118  } catch (TskCoreException ex) {
119  String errorMessage = String.format("Error adding %s to case as a report", reportPath); //NON-NLS
120  logger.log(Level.SEVERE, errorMessage, ex);
121  } finally {
122  if (out != null) {
123  try {
124  out.close();
125  } catch (IOException ex) {
126  }
127  }
128  }
129  }
130 
138  @Override
139  public void startDataType(String name, String description) {
140  // Create a worksheet for the data type (assumed to be an artifact type).
141  name = escapeForExcel(name);
142  sheet = wb.createSheet(name);
143  sheet.setAutobreaks(true);
144  rowIndex = 0;
145 
146  // There will be at least two columns, one each for the artifacts count and its label.
147  sheetColCount = 2;
148  }
149 
153  @Override
154  public void endDataType() {
155  // Now that the sheet is complete, size the columns to the content.
156  for (int i = 0; i < sheetColCount; ++i) {
157  sheet.autoSizeColumn(i);
158  }
159  }
160 
166  @Override
167  public void startSet(String setName) {
168  setName = escapeForExcel(setName);
169  Row row = sheet.createRow(rowIndex);
170  row.setRowStyle(setStyle);
171  row.createCell(0).setCellValue(setName);
172  ++rowIndex;
173  }
174 
178  @Override
179  public void endSet() {
180  // Add an empty row as a separator.
181  sheet.createRow(rowIndex);
182  ++rowIndex;
183  }
184 
185  @Override
186  public void addSetIndex(List<String> sets) {
187  // Ignored in Excel Report
188  }
189 
195  @Override
196  public void addSetElement(String elementName) {
197  elementName = escapeForExcel(elementName);
198  Row row = sheet.createRow(rowIndex);
199  row.setRowStyle(elementStyle);
200  row.createCell(0).setCellValue(elementName);
201  ++rowIndex;
202  }
203 
209  @Override
210  public void startTable(List<String> titles) {
211  int tableColCount = 0;
212  Row row = sheet.createRow(rowIndex);
213  row.setRowStyle(titleStyle);
214  for (int i = 0; i < titles.size(); i++) {
215  row.createCell(i).setCellValue(titles.get(i));
216  ++tableColCount;
217  }
218  ++rowIndex;
219 
220  // Keep track of the number of columns with data in them for later column auto-sizing.
221  if (tableColCount > sheetColCount) {
222  sheetColCount = tableColCount;
223  }
224  }
225 
226  @Override
227  public void endTable() {
228  // Add an empty row as a separator.
229  sheet.createRow(rowIndex);
230  ++rowIndex;
231  }
232 
238  @Override
239  public void addRow(List<String> rowData) {
240  Row row = sheet.createRow(rowIndex);
241  for (int i = 0; i < rowData.size(); ++i) {
242  row.createCell(i).setCellValue(rowData.get(i));
243  }
244  ++rowIndex;
245  }
246 
254  @Override
255  public String dateToString(long date) {
256  SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
257  return sdf.format(new java.util.Date(date * 1000));
258  }
259 
260  @Override
261  public String getName() {
262  return NbBundle.getMessage(this.getClass(), "ReportExcel.getName.text");
263  }
264 
265  @Override
266  public String getDescription() {
267  return NbBundle.getMessage(this.getClass(), "ReportExcel.getDesc.text");
268  }
269 
270  @Override
271  public String getRelativeFilePath() {
272  return "Excel.xlsx"; //NON-NLS
273  }
274 
283  private static String escapeForExcel(String text) {
284  return text.replaceAll("[\\/\\:\\?\\*\\\\]", "_");
285  }
286 
287  private void writeSummaryWorksheet() {
288  sheet = wb.createSheet(NbBundle.getMessage(this.getClass(), "ReportExcel.sheetName.text"));
289  rowIndex = 0;
290 
291  Row row = sheet.createRow(rowIndex);
292  row.setRowStyle(setStyle);
293  row.createCell(0).setCellValue(NbBundle.getMessage(this.getClass(), "ReportExcel.cellVal.summary"));
294  ++rowIndex;
295 
296  sheet.createRow(rowIndex);
297  ++rowIndex;
298 
299  Case currentCase = Case.getCurrentCase();
300 
301  row = sheet.createRow(rowIndex);
302  row.setRowStyle(setStyle);
303  row.createCell(0).setCellValue(NbBundle.getMessage(this.getClass(), "ReportExcel.cellVal.caseName"));
304  row.createCell(1).setCellValue(currentCase.getDisplayName());
305  ++rowIndex;
306 
307  row = sheet.createRow(rowIndex);
308  row.setRowStyle(setStyle);
309  row.createCell(0).setCellValue(NbBundle.getMessage(this.getClass(), "ReportExcel.cellVal.caseNum"));
310  row.createCell(1).setCellValue(currentCase.getNumber());
311  ++rowIndex;
312 
313  row = sheet.createRow(rowIndex);
314  row.setRowStyle(setStyle);
315  row.createCell(0).setCellValue(NbBundle.getMessage(this.getClass(), "ReportExcel.cellVal.examiner"));
316  row.createCell(1).setCellValue(currentCase.getExaminer());
317  ++rowIndex;
318 
319  row = sheet.createRow(rowIndex);
320  row.setRowStyle(setStyle);
321  row.createCell(0).setCellValue(NbBundle.getMessage(this.getClass(), "ReportExcel.cellVal.numImages"));
322  int numImages;
323  try {
324  numImages = currentCase.getDataSources().size();
325  } catch (TskCoreException ex) {
326  numImages = 0;
327  }
328  row.createCell(1).setCellValue(numImages);
329  ++rowIndex;
330 
331  sheet.autoSizeColumn(0);
332  sheet.autoSizeColumn(1);
333  }
334 }

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.