Autopsy  3.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  private static final Logger logger = Logger.getLogger(ReportExcel.class.getName());
35  private static ReportExcel instance;
36 
37  private Workbook wb;
38  private Sheet sheet;
39  private CellStyle titleStyle;
40  private CellStyle setStyle;
41  private CellStyle elementStyle;
42  private int rowIndex = 0;
43  private int sheetColCount = 0;
44  private String reportPath;
45 
46  // Get the default instance of this report
47  public static synchronized ReportExcel getDefault() {
48  if (instance == null) {
49  instance = new ReportExcel();
50  }
51  return instance;
52  }
53 
54  // Hidden constructor
55  private ReportExcel() {
56  }
57 
63  @Override
64  public void startReport(String baseReportDir) {
65  // Set the path and save it for when the report is written to disk.
66  this.reportPath = baseReportDir + getRelativeFilePath();
67 
68  // Make a workbook.
69  wb = new XSSFWorkbook();
70 
71  // Create some cell styles.
72  // TODO: The commented out cell style settings below do not work as desired when
73  // the output file is loaded by MS Excel or OfficeLibre. The font height and weight
74  // settings only work as expected when the output file is loaded by OfficeLibre.
75  // The alignment and text wrap settings appear to have no effect.
76  titleStyle = wb.createCellStyle();
77 // titleStyle.setBorderBottom((short) 1);
78  Font titleFont = wb.createFont();
79  titleFont.setFontHeightInPoints((short) 12);
80  titleStyle.setFont(titleFont);
81  titleStyle.setAlignment(CellStyle.ALIGN_LEFT);
82  titleStyle.setWrapText(true);
83 
84  setStyle = wb.createCellStyle();
85  Font setFont = wb.createFont();
86  setFont.setFontHeightInPoints((short) 14);
87  setFont.setBoldweight((short) 10);
88  setStyle.setFont(setFont);
89  setStyle.setAlignment(CellStyle.ALIGN_LEFT);
90  setStyle.setWrapText(true);
91 
92  elementStyle = wb.createCellStyle();
93 // elementStyle.setF illBackgroundColor(HSSFColor.LIGHT_YELLOW.index);
94  Font elementFont = wb.createFont();
95  elementFont.setFontHeightInPoints((short) 14);
96  elementStyle.setFont(elementFont);
97  elementStyle.setAlignment(CellStyle.ALIGN_LEFT);
98  elementStyle.setWrapText(true);
99 
100  writeSummaryWorksheet();
101  }
102 
106  @Override
107  public void endReport() {
108  FileOutputStream out = null;
109  try {
110  out = new FileOutputStream(reportPath);
111  wb.write(out);
112  Case.getCurrentCase().addReport(reportPath, NbBundle.getMessage(this.getClass(),
113  "ReportExcel.endReport.srcModuleName.text"), "");
114  } catch (IOException ex) {
115  logger.log(Level.SEVERE, "Failed to write Excel report.", ex); //NON-NLS
116  } catch (TskCoreException ex) {
117  String errorMessage = String.format("Error adding %s to case as a report", reportPath); //NON-NLS
118  logger.log(Level.SEVERE, errorMessage, ex);
119  } finally {
120  if (out != null) {
121  try {
122  out.close();
123  } catch (IOException ex) {
124  }
125  }
126  }
127  }
128 
136  @Override
137  public void startDataType(String name, String description) {
138  // Create a worksheet for the data type (assumed to be an artifact type).
139  name = escapeForExcel(name);
140  sheet = wb.createSheet(name);
141  sheet.setAutobreaks(true);
142  rowIndex = 0;
143 
144  // There will be at least two columns, one each for the artifacts count and its label.
145  sheetColCount = 2;
146  }
147 
151  @Override
152  public void endDataType() {
153  // Now that the sheet is complete, size the columns to the content.
154  for (int i = 0; i < sheetColCount; ++i) {
155  sheet.autoSizeColumn(i);
156  }
157  }
158 
163  @Override
164  public void startSet(String setName) {
165  setName = escapeForExcel(setName);
166  Row row = sheet.createRow(rowIndex);
167  row.setRowStyle(setStyle);
168  row.createCell(0).setCellValue(setName);
169  ++rowIndex;
170  }
171 
175  @Override
176  public void endSet() {
177  // Add an empty row as a separator.
178  sheet.createRow(rowIndex);
179  ++rowIndex;
180  }
181 
182  @Override
183  public void addSetIndex(List<String> sets) {
184  // Ignored in Excel Report
185  }
186 
191  @Override
192  public void addSetElement(String elementName) {
193  elementName = escapeForExcel(elementName);
194  Row row = sheet.createRow(rowIndex);
195  row.setRowStyle(elementStyle);
196  row.createCell(0).setCellValue(elementName);
197  ++rowIndex;
198  }
199 
204  @Override
205  public void startTable(List<String> titles) {
206  int tableColCount = 0;
207  Row row = sheet.createRow(rowIndex);
208  row.setRowStyle(titleStyle);
209  for (int i=0; i<titles.size(); i++) {
210  row.createCell(i).setCellValue(titles.get(i));
211  ++tableColCount;
212  }
213  ++rowIndex;
214 
215  // Keep track of the number of columns with data in them for later column auto-sizing.
216  if (tableColCount > sheetColCount) {
217  sheetColCount = tableColCount;
218  }
219  }
220 
221  @Override
222  public void endTable() {
223  // Add an empty row as a separator.
224  sheet.createRow(rowIndex);
225  ++rowIndex;
226  }
227 
232  @Override
233  public void addRow(List<String> rowData) {
234  Row row = sheet.createRow(rowIndex);
235  for (int i = 0; i < rowData.size(); ++i) {
236  row.createCell(i).setCellValue(rowData.get(i));
237  }
238  ++rowIndex;
239  }
240 
246  @Override
247  public String dateToString(long date) {
248  SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
249  return sdf.format(new java.util.Date(date * 1000));
250  }
251 
252  @Override
253  public String getName() {
254  return NbBundle.getMessage(this.getClass(), "ReportExcel.getName.text");
255  }
256 
257  @Override
258  public String getDescription() {
259  return NbBundle.getMessage(this.getClass(), "ReportExcel.getDesc.text");
260  }
261 
262  @Override
263  public String getRelativeFilePath() {
264  return "Excel.xlsx"; //NON-NLS
265  }
266 
273  private static String escapeForExcel(String text) {
274  return text.replaceAll("[\\/\\:\\?\\*\\\\]", "_");
275  }
276 
277  private void writeSummaryWorksheet() {
278  sheet = wb.createSheet(NbBundle.getMessage(this.getClass(), "ReportExcel.sheetName.text"));
279  rowIndex = 0;
280 
281  Row row = sheet.createRow(rowIndex);
282  row.setRowStyle(setStyle);
283  row.createCell(0).setCellValue(NbBundle.getMessage(this.getClass(), "ReportExcel.cellVal.summary"));
284  ++rowIndex;
285 
286  sheet.createRow(rowIndex);
287  ++rowIndex;
288 
289  Case currentCase = Case.getCurrentCase();
290 
291  row = sheet.createRow(rowIndex);
292  row.setRowStyle(setStyle);
293  row.createCell(0).setCellValue(NbBundle.getMessage(this.getClass(), "ReportExcel.cellVal.caseName"));
294  row.createCell(1).setCellValue(currentCase.getName());
295  ++rowIndex;
296 
297  row = sheet.createRow(rowIndex);
298  row.setRowStyle(setStyle);
299  row.createCell(0).setCellValue(NbBundle.getMessage(this.getClass(), "ReportExcel.cellVal.caseNum"));
300  row.createCell(1).setCellValue(currentCase.getNumber());
301  ++rowIndex;
302 
303  row = sheet.createRow(rowIndex);
304  row.setRowStyle(setStyle);
305  row.createCell(0).setCellValue(NbBundle.getMessage(this.getClass(), "ReportExcel.cellVal.examiner"));
306  row.createCell(1).setCellValue(currentCase.getExaminer());
307  ++rowIndex;
308 
309  row = sheet.createRow(rowIndex);
310  row.setRowStyle(setStyle);
311  row.createCell(0).setCellValue(NbBundle.getMessage(this.getClass(), "ReportExcel.cellVal.numImages"));
312  int numImages;
313  try {
314  numImages = currentCase.getDataSources().size();
315  } catch (TskCoreException ex) {
316  numImages = 0;
317  }
318  row.createCell(1).setCellValue(numImages);
319  ++rowIndex;
320 
321  sheet.autoSizeColumn(0);
322  sheet.autoSizeColumn(1);
323  }
324 }

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.