Autopsy  4.10.0
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-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.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;
29 import org.openide.util.NbBundle.Messages;
33 import org.sleuthkit.datamodel.TskCoreException;
34 
35 class ReportExcel implements TableReportModule {
36 
37  private static final Logger logger = Logger.getLogger(ReportExcel.class.getName());
38  private static ReportExcel instance;
39  private static final int EXCEL_CELL_MAXIMUM_SIZE = 36767; //Specified at:https://poi.apache.org/apidocs/org/apache/poi/ss/SpreadsheetVersion.html
40 
41  private Workbook wb;
42  private Sheet sheet;
43  private CellStyle titleStyle;
44  private CellStyle setStyle;
45  private CellStyle elementStyle;
46  private int rowIndex = 0;
47  private int sheetColCount = 0;
48  private String reportPath;
49 
50  // Get the default instance of this report
51  public static synchronized ReportExcel getDefault() {
52  if (instance == null) {
53  instance = new ReportExcel();
54  }
55  return instance;
56  }
57 
58  // Hidden constructor
59  private ReportExcel() {
60  }
61 
68  @Override
69  public void startReport(String baseReportDir) {
70  // Set the path and save it for when the report is written to disk.
71  this.reportPath = baseReportDir + getRelativeFilePath();
72 
73  // Make a workbook.
74  wb = new XSSFWorkbook();
75 
76  // Create some cell styles.
77  // TODO: The commented out cell style settings below do not work as desired when
78  // the output file is loaded by MS Excel or OfficeLibre. The font height and weight
79  // settings only work as expected when the output file is loaded by OfficeLibre.
80  // The alignment and text wrap settings appear to have no effect.
81  titleStyle = wb.createCellStyle();
82 // titleStyle.setBorderBottom((short) 1);
83  Font titleFont = wb.createFont();
84  titleFont.setFontHeightInPoints((short) 12);
85  titleStyle.setFont(titleFont);
86  titleStyle.setAlignment(HorizontalAlignment.LEFT);
87  titleStyle.setWrapText(true);
88 
89  setStyle = wb.createCellStyle();
90  Font setFont = wb.createFont();
91  setFont.setFontHeightInPoints((short) 14);
92  setFont.setBold(true);
93  setStyle.setFont(setFont);
94  setStyle.setAlignment(HorizontalAlignment.LEFT);
95  setStyle.setWrapText(true);
96 
97  elementStyle = wb.createCellStyle();
98 // elementStyle.setF illBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
99  Font elementFont = wb.createFont();
100  elementFont.setFontHeightInPoints((short) 14);
101  elementStyle.setFont(elementFont);
102  elementStyle.setAlignment(HorizontalAlignment.LEFT);
103  elementStyle.setWrapText(true);
104 
105  writeSummaryWorksheet();
106  }
107 
111  @Override
112  public void endReport() {
113  FileOutputStream out = null;
114  try {
115  out = new FileOutputStream(reportPath);
116  wb.write(out);
117  Case.getCurrentCaseThrows().addReport(reportPath, NbBundle.getMessage(this.getClass(),
118  "ReportExcel.endReport.srcModuleName.text"), "");
119  } catch (IOException ex) {
120  logger.log(Level.SEVERE, "Failed to write Excel report.", ex); //NON-NLS
121  } catch (TskCoreException ex) {
122  String errorMessage = String.format("Error adding %s to case as a report", reportPath); //NON-NLS
123  logger.log(Level.SEVERE, errorMessage, ex);
124  } catch (NoCurrentCaseException ex) {
125  logger.log(Level.SEVERE, "Exception while getting open case.", ex); //NON-NLS
126  } finally {
127  if (out != null) {
128  try {
129  out.close();
130  } catch (IOException ex) {
131  }
132  }
133  }
134  }
135 
143  @Override
144  public void startDataType(String name, String description) {
145  // Create a worksheet for the data type (assumed to be an artifact type).
146  name = escapeForExcel(name);
147  sheet = wb.createSheet(name);
148  sheet.setAutobreaks(true);
149  rowIndex = 0;
150 
151  // There will be at least two columns, one each for the artifacts count and its label.
152  sheetColCount = 2;
153  }
154 
158  @Override
159  public void endDataType() {
160  // Now that the sheet is complete, size the columns to the content.
161  for (int i = 0; i < sheetColCount; ++i) {
162  sheet.autoSizeColumn(i);
163  }
164  }
165 
171  @Override
172  public void startSet(String setName) {
173  setName = escapeForExcel(setName);
174  Row row = sheet.createRow(rowIndex);
175  row.setRowStyle(setStyle);
176  row.createCell(0).setCellValue(setName);
177  ++rowIndex;
178  }
179 
183  @Override
184  public void endSet() {
185  // Add an empty row as a separator.
186  sheet.createRow(rowIndex);
187  ++rowIndex;
188  }
189 
190  @Override
191  public void addSetIndex(List<String> sets) {
192  // Ignored in Excel Report
193  }
194 
200  @Override
201  public void addSetElement(String elementName) {
202  elementName = escapeForExcel(elementName);
203  Row row = sheet.createRow(rowIndex);
204  row.setRowStyle(elementStyle);
205  row.createCell(0).setCellValue(elementName);
206  ++rowIndex;
207  }
208 
214  @Override
215  public void startTable(List<String> titles) {
216  int tableColCount = 0;
217  Row row = sheet.createRow(rowIndex);
218  row.setRowStyle(titleStyle);
219  for (int i = 0; i < titles.size(); i++) {
220  row.createCell(i).setCellValue(titles.get(i));
221  ++tableColCount;
222  }
223  ++rowIndex;
224 
225  // Keep track of the number of columns with data in them for later column auto-sizing.
226  if (tableColCount > sheetColCount) {
227  sheetColCount = tableColCount;
228  }
229  }
230 
231  @Override
232  public void endTable() {
233  // Add an empty row as a separator.
234  sheet.createRow(rowIndex);
235  ++rowIndex;
236  }
237 
243  @Override
244  @NbBundle.Messages({
245  "ReportExcel.exceptionMessage.dataTooLarge=Value is too long to fit into an Excel cell. ",
246  "ReportExcel.exceptionMessage.errorText=Error showing data into an Excel cell."
247  })
248 
249  public void addRow(List<String> rowData) {
250  Row row = sheet.createRow(rowIndex);
251  for (int i = 0; i < rowData.size(); ++i) {
252  Cell excelCell = row.createCell(i);
253  try {
254  excelCell.setCellValue(rowData.get(i));
255  } catch (Exception e) {
256  if (e instanceof java.lang.IllegalArgumentException && rowData.get(i).length() > EXCEL_CELL_MAXIMUM_SIZE) {
257  excelCell.setCellValue(Bundle.ReportExcel_exceptionMessage_dataTooLarge() + e.getMessage());
258  } else {
259  excelCell.setCellValue(Bundle.ReportExcel_exceptionMessage_errorText());
260  }
261  }
262  }
263  ++rowIndex;
264  }
265 
273  @Override
274  public String dateToString(long date) {
275  SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
276  return sdf.format(new java.util.Date(date * 1000));
277  }
278 
279  @Override
280  public String getName() {
281  return NbBundle.getMessage(this.getClass(), "ReportExcel.getName.text");
282  }
283 
284  @Override
285  public String getDescription() {
286  return NbBundle.getMessage(this.getClass(), "ReportExcel.getDesc.text");
287  }
288 
289  @Override
290  public String getRelativeFilePath() {
291  return "Excel.xlsx"; //NON-NLS
292  }
293 
302  private static String escapeForExcel(String text) {
303  return text.replaceAll("[\\/\\:\\?\\*\\\\]", "_");
304  }
305 
306  @Messages({
307  "ReportExcel.writeSummary.sheetName=Summary",
308  "ReportExcel.writeSummary.summary=Summary",
309  "ReportExcel.writeSummary.caseName=Case Name:",
310  "ReportExcel.writeSummary.numImages=Number of Images:",
311  "ReportExcel.writeSummary.caseNum=Case Number:",
312  "ReportExcel.writeSummary.caseNotes=Case Notes:",
313  "ReportExcel.writeSummary.examiner=Examiner:"
314  })
315  private void writeSummaryWorksheet() {
316  Case currentCase;
317  try {
318  currentCase = Case.getCurrentCaseThrows();
319  } catch (NoCurrentCaseException ex) {
320  logger.log(Level.SEVERE, "Exception while getting open case.", ex); //NON-NLS
321  return;
322  }
323  sheet = wb.createSheet(Bundle.ReportExcel_writeSummary_sheetName());
324  rowIndex = 0;
325 
326  Row row = sheet.createRow(rowIndex);
327  row.setRowStyle(setStyle);
328  row.createCell(0).setCellValue(Bundle.ReportExcel_writeSummary_summary());
329  ++rowIndex;
330 
331  sheet.createRow(rowIndex);
332  ++rowIndex;
333 
334  row = sheet.createRow(rowIndex);
335  row.setRowStyle(setStyle);
336  row.createCell(0).setCellValue(Bundle.ReportExcel_writeSummary_caseName());
337  row.createCell(1).setCellValue(currentCase.getDisplayName());
338  ++rowIndex;
339 
340  if (!currentCase.getNumber().isEmpty()) {
341  row = sheet.createRow(rowIndex);
342  row.setRowStyle(setStyle);
343  row.createCell(0).setCellValue(Bundle.ReportExcel_writeSummary_caseNum());
344  row.createCell(1).setCellValue(currentCase.getNumber());
345  ++rowIndex;
346  }
347 
348  row = sheet.createRow(rowIndex);
349  row.setRowStyle(setStyle);
350  row.createCell(0).setCellValue(Bundle.ReportExcel_writeSummary_numImages());
351  int numImages;
352  try {
353  numImages = currentCase.getDataSources().size();
354  } catch (TskCoreException ex) {
355  numImages = 0;
356  }
357  row.createCell(1).setCellValue(numImages);
358  ++rowIndex;
359 
360  if (!currentCase.getCaseNotes().isEmpty()) {
361  row = sheet.createRow(rowIndex);
362  row.setRowStyle(setStyle);
363  row.createCell(0).setCellValue(Bundle.ReportExcel_writeSummary_caseNotes());
364  row.createCell(1).setCellValue(currentCase.getCaseNotes());
365  ++rowIndex;
366  }
367 
368  if (!currentCase.getExaminer().isEmpty()) {
369  row = sheet.createRow(rowIndex);
370  row.setRowStyle(setStyle);
371  row.createCell(0).setCellValue(Bundle.ReportExcel_writeSummary_examiner());
372  row.createCell(1).setCellValue(currentCase.getExaminer());
373  ++rowIndex;
374  }
375 
376  sheet.autoSizeColumn(0);
377  sheet.autoSizeColumn(1);
378  }
379 }

Copyright © 2012-2018 Basis Technology. Generated on: Fri Mar 22 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.