19 package org.sleuthkit.autopsy.report.modules.excel;
 
   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;
 
   39     private static ExcelReport instance;
 
   40     private static final int EXCEL_CELL_MAXIMUM_SIZE = 36767; 
 
   44     private CellStyle titleStyle;
 
   45     private CellStyle setStyle;
 
   46     private CellStyle elementStyle;
 
   47     private int rowIndex = 0;
 
   48     private int sheetColCount = 0;
 
   49     private String reportPath;
 
   52     public static synchronized ExcelReport getDefault() {
 
   53         if (instance == null) {
 
   54             instance = 
new ExcelReport();
 
   60     private ExcelReport() {
 
   70     public void startReport(String baseReportDir) {
 
   72         this.reportPath = baseReportDir + getRelativeFilePath();
 
   75         wb = 
new XSSFWorkbook();
 
   82         titleStyle = wb.createCellStyle();
 
   84         Font titleFont = wb.createFont();
 
   85         titleFont.setFontHeightInPoints((
short) 12);
 
   86         titleStyle.setFont(titleFont);
 
   87         titleStyle.setAlignment(HorizontalAlignment.LEFT);
 
   88         titleStyle.setWrapText(
true);
 
   90         setStyle = wb.createCellStyle();
 
   91         Font setFont = wb.createFont();
 
   92         setFont.setFontHeightInPoints((
short) 14);
 
   93         setFont.setBold(
true);
 
   94         setStyle.setFont(setFont);
 
   95         setStyle.setAlignment(HorizontalAlignment.LEFT);
 
   96         setStyle.setWrapText(
true);
 
   98         elementStyle = wb.createCellStyle();
 
  100         Font elementFont = wb.createFont();
 
  101         elementFont.setFontHeightInPoints((
short) 14);
 
  102         elementStyle.setFont(elementFont);
 
  103         elementStyle.setAlignment(HorizontalAlignment.LEFT);
 
  104         elementStyle.setWrapText(
true);
 
  106         writeSummaryWorksheet();
 
  113     public void endReport() {
 
  114         FileOutputStream out = null;
 
  116             out = 
new FileOutputStream(reportPath);
 
  119                     "ReportExcel.endReport.srcModuleName.text"), 
"");
 
  120         } 
catch (IOException ex) {
 
  121             logger.log(Level.SEVERE, 
"Failed to write Excel report.", ex); 
 
  123             String errorMessage = String.format(
"Error adding %s to case as a report", reportPath); 
 
  124             logger.log(Level.SEVERE, errorMessage, ex);
 
  126             logger.log(Level.SEVERE, 
"Exception while getting open case.", ex); 
 
  131                 } 
catch (IOException ex) {
 
  145     public void startDataType(String name, String description) {
 
  147         name = escapeForExcel(name);
 
  148         sheet = wb.createSheet(name);
 
  149         sheet.setAutobreaks(
true);
 
  160     public void endDataType() {
 
  162         for (
int i = 0; i < sheetColCount; ++i) {
 
  163             sheet.autoSizeColumn(i);
 
  173     public void startSet(String setName) {
 
  174         setName = escapeForExcel(setName);
 
  175         Row row = sheet.createRow(rowIndex);
 
  176         row.setRowStyle(setStyle);
 
  177         row.createCell(0).setCellValue(setName);
 
  185     public void endSet() {
 
  187         sheet.createRow(rowIndex);
 
  192     public void addSetIndex(List<String> sets) {
 
  202     public void addSetElement(String elementName) {
 
  203         elementName = escapeForExcel(elementName);
 
  204         Row row = sheet.createRow(rowIndex);
 
  205         row.setRowStyle(elementStyle);
 
  206         row.createCell(0).setCellValue(elementName);
 
  216     public void startTable(List<String> titles) {
 
  217         int tableColCount = 0;
 
  218         Row row = sheet.createRow(rowIndex);
 
  219         row.setRowStyle(titleStyle);
 
  220         for (
int i = 0; i < titles.size(); i++) {
 
  221             row.createCell(i).setCellValue(titles.get(i));
 
  227         if (tableColCount > sheetColCount) {
 
  228             sheetColCount = tableColCount;
 
  233     public void endTable() {
 
  235         sheet.createRow(rowIndex);
 
  246         "ReportExcel.exceptionMessage.dataTooLarge=Value is too long to fit into an Excel cell. ",
 
  247         "ReportExcel.exceptionMessage.errorText=Error showing data into an Excel cell." 
  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);
 
  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());
 
  259                     excelCell.setCellValue(Bundle.ReportExcel_exceptionMessage_errorText());
 
  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));
 
  280     public String getName() {
 
  281         return NbBundle.getMessage(this.getClass(), 
"ReportExcel.getName.text");
 
  285     public String getDescription() {
 
  286         return NbBundle.getMessage(this.getClass(), 
"ReportExcel.getDesc.text");
 
  290     public String getRelativeFilePath() {
 
  302     private static String escapeForExcel(String text) {
 
  303         return text.replaceAll(
"[\\/\\:\\?\\*\\\\]", 
"_");
 
  307         "ReportExcel.writeSummary.sheetName=Summary",
 
  308         "ReportExcel.writeSummary.summary=Summary",
 
  309         "ReportExcel.writeSummary.caseName=Case Name:",
 
  310         "ReportExcel.writeSummary.numImages=Number of data sources in case:",
 
  311         "ReportExcel.writeSummary.caseNum=Case Number:",
 
  312         "ReportExcel.writeSummary.caseNotes=Case Notes:",
 
  313         "ReportExcel.writeSummary.examiner=Examiner:" 
  315     private void writeSummaryWorksheet() {
 
  320             logger.log(Level.SEVERE, 
"Exception while getting open case.", ex); 
 
  323         sheet = wb.createSheet(Bundle.ReportExcel_writeSummary_sheetName());
 
  326         Row row = sheet.createRow(rowIndex);
 
  327         row.setRowStyle(setStyle);
 
  328         row.createCell(0).setCellValue(Bundle.ReportExcel_writeSummary_summary());
 
  331         sheet.createRow(rowIndex);
 
  334         row = sheet.createRow(rowIndex);
 
  335         row.setRowStyle(setStyle);
 
  336         row.createCell(0).setCellValue(Bundle.ReportExcel_writeSummary_caseName());
 
  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());
 
  348         row = sheet.createRow(rowIndex);
 
  349         row.setRowStyle(setStyle);
 
  350         row.createCell(0).setCellValue(Bundle.ReportExcel_writeSummary_numImages());
 
  357         row.createCell(1).setCellValue(numImages);
 
  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());
 
  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());
 
  376         sheet.autoSizeColumn(0);
 
  377         sheet.autoSizeColumn(1);
 
List< Content > getDataSources()
void addReport(String localPath, String srcModuleName, String reportName)
synchronized static Logger getLogger(String name)
static Case getCurrentCaseThrows()