19 package org.sleuthkit.autopsy.report;
 
   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;
 
   33 class ReportExcel 
implements TableReportModule {
 
   35     private static final Logger logger = Logger.getLogger(ReportExcel.class.getName());
 
   36     private static ReportExcel instance;
 
   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;
 
   48     public static synchronized ReportExcel getDefault() {
 
   49         if (instance == null) {
 
   50             instance = 
new ReportExcel();
 
   56     private ReportExcel() {
 
   66     public void startReport(String baseReportDir) {
 
   68         this.reportPath = baseReportDir + getRelativeFilePath();
 
   71         wb = 
new XSSFWorkbook();
 
   78         titleStyle = wb.createCellStyle();
 
   80         Font titleFont = wb.createFont();
 
   81         titleFont.setFontHeightInPoints((
short) 12);
 
   82         titleStyle.setFont(titleFont);
 
   83         titleStyle.setAlignment(CellStyle.ALIGN_LEFT);
 
   84         titleStyle.setWrapText(
true);
 
   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);
 
   94         elementStyle = wb.createCellStyle();
 
   96         Font elementFont = wb.createFont();
 
   97         elementFont.setFontHeightInPoints((
short) 14);
 
   98         elementStyle.setFont(elementFont);
 
   99         elementStyle.setAlignment(CellStyle.ALIGN_LEFT);
 
  100         elementStyle.setWrapText(
true);
 
  102         writeSummaryWorksheet();
 
  109     public void endReport() {
 
  110         FileOutputStream out = null;
 
  112             out = 
new FileOutputStream(reportPath);
 
  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); 
 
  118         } 
catch (TskCoreException ex) {
 
  119             String errorMessage = String.format(
"Error adding %s to case as a report", reportPath); 
 
  120             logger.log(Level.SEVERE, errorMessage, ex);
 
  125                 } 
catch (IOException ex) {
 
  139     public void startDataType(String name, String description) {
 
  141         name = escapeForExcel(name);
 
  142         sheet = wb.createSheet(name);
 
  143         sheet.setAutobreaks(
true);
 
  154     public void endDataType() {
 
  156         for (
int i = 0; i < sheetColCount; ++i) {
 
  157             sheet.autoSizeColumn(i);
 
  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);
 
  179     public void endSet() {
 
  181         sheet.createRow(rowIndex);
 
  186     public void addSetIndex(List<String> sets) {
 
  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);
 
  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));
 
  221         if (tableColCount > sheetColCount) {
 
  222             sheetColCount = tableColCount;
 
  227     public void endTable() {
 
  229         sheet.createRow(rowIndex);
 
  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));
 
  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));
 
  261     public String getName() {
 
  262         return NbBundle.getMessage(this.getClass(), 
"ReportExcel.getName.text");
 
  266     public String getDescription() {
 
  267         return NbBundle.getMessage(this.getClass(), 
"ReportExcel.getDesc.text");
 
  271     public String getRelativeFilePath() {
 
  283     private static String escapeForExcel(String text) {
 
  284         return text.replaceAll(
"[\\/\\:\\?\\*\\\\]", 
"_");
 
  287     private void writeSummaryWorksheet() {
 
  288         sheet = wb.createSheet(NbBundle.getMessage(
this.getClass(), 
"ReportExcel.sheetName.text"));
 
  291         Row row = sheet.createRow(rowIndex);
 
  292         row.setRowStyle(setStyle);
 
  293         row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(), 
"ReportExcel.cellVal.summary"));
 
  296         sheet.createRow(rowIndex);
 
  299         Case currentCase = Case.getCurrentCase();
 
  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());
 
  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());
 
  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());
 
  319         row = sheet.createRow(rowIndex);
 
  320         row.setRowStyle(setStyle);
 
  321         row.createCell(0).setCellValue(NbBundle.getMessage(
this.getClass(), 
"ReportExcel.cellVal.numImages"));
 
  324             numImages = currentCase.getDataSources().size();
 
  325         } 
catch (TskCoreException ex) {
 
  328         row.createCell(1).setCellValue(numImages);
 
  331         sheet.autoSizeColumn(0);
 
  332         sheet.autoSizeColumn(1);