Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
ExcelSpecialFormatExport.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2021 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.modules.datasourcesummaryexport;
20 
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Optional;
24 import org.apache.poi.ss.usermodel.Row;
25 import org.apache.poi.ss.usermodel.Sheet;
27 
31 class ExcelSpecialFormatExport implements ExcelExport.ExcelSheetExport {
32 
37  static class ItemDimensions {
38 
39  private final int rowStart;
40  private final int rowEnd;
41  private final int colStart;
42  private final int colEnd;
43 
52  ItemDimensions(int rowStart, int colStart, int rowEnd, int colEnd) {
53  this.rowStart = rowStart;
54  this.colStart = colStart;
55  this.rowEnd = rowEnd;
56  this.colEnd = colEnd;
57  }
58 
62  int getRowStart() {
63  return rowStart;
64  }
65 
69  int getRowEnd() {
70  return rowEnd;
71  }
72 
76  int getColStart() {
77  return colStart;
78  }
79 
83  int getColEnd() {
84  return colEnd;
85  }
86  }
87 
91  interface ExcelItemExportable {
92 
103  ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException;
104  }
105 
109  static class SingleCellExportable implements ExcelItemExportable {
110 
111  private final CellModel item;
112 
118  SingleCellExportable(String key) {
119  this(new DefaultCellModel<>(key));
120  }
121 
127  SingleCellExportable(CellModel item) {
128  this.item = item;
129  }
130 
131  @Override
132  public ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException {
133  Row row = sheet.createRow(rowStart);
134  ExcelExport.createCell(env, row, colStart, item, Optional.empty());
135  return new ItemDimensions(rowStart, colStart, rowStart, colStart);
136  }
137  }
138 
143  static class KeyValueItemExportable implements ExcelItemExportable {
144 
145  private final CellModel key;
146  private final CellModel value;
147 
154  KeyValueItemExportable(String key, CellModel value) {
155  this(new DefaultCellModel<>(key), value);
156  }
157 
164  KeyValueItemExportable(CellModel key, CellModel value) {
165  this.key = key;
166  this.value = value;
167  }
168 
169  @Override
170  public ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException {
171  Row row = sheet.createRow(rowStart);
172  ExcelExport.createCell(env, row, colStart, key, Optional.of(env.getHeaderStyle()));
173  ExcelExport.createCell(env, row, colStart + 1, value, Optional.empty());
174  return new ItemDimensions(rowStart, colStart, rowStart, colStart + 1);
175  }
176  }
177 
189  static class TitledExportable implements ExcelItemExportable {
190 
191  private static final int DEFAULT_INDENT = 1;
192 
193  private final String title;
194  private final List<? extends ExcelItemExportable> children;
195 
202  TitledExportable(String title, List<? extends ExcelItemExportable> children) {
203  this.title = title;
204  this.children = children;
205  }
206 
207  @Override
208  public ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException {
209  ExcelExport.createCell(env, sheet.createRow(rowStart), colStart, new DefaultCellModel<>(title), Optional.of(env.getHeaderStyle()));
210  int curRow = rowStart + 1;
211  int maxCol = colStart;
212  for (ExcelItemExportable export : children) {
213  if (export == null) {
214  continue;
215  }
216 
217  ItemDimensions thisItemDim = export.write(sheet, curRow, colStart + DEFAULT_INDENT, env);
218  curRow = thisItemDim.getRowEnd() + 1;
219  maxCol = Math.max(thisItemDim.getColEnd(), maxCol);
220  }
221 
222  return new ItemDimensions(rowStart, colStart, curRow - 1, maxCol);
223  }
224  }
225 
226  private final String sheetName;
227  private final List<ExcelItemExportable> exports;
228 
235  ExcelSpecialFormatExport(String sheetName, List<ExcelItemExportable> exports) {
236  this.sheetName = sheetName;
237  this.exports = exports == null ? Collections.emptyList() : exports;
238  }
239 
240  @Override
241  public String getSheetName() {
242  return sheetName;
243  }
244 
245  @Override
246  public void renderSheet(Sheet sheet, ExcelExport.WorksheetEnv env) throws ExcelExportException {
247  int rowStart = 0;
248  int maxCol = 0;
249  for (ExcelItemExportable export : exports) {
250  if (export == null) {
251  continue;
252  }
253 
254  ItemDimensions dimensions = export.write(sheet, rowStart, 0, env);
255  rowStart = dimensions.getRowEnd() + 1;
256  maxCol = Math.max(maxCol, dimensions.getColEnd());
257  }
258 
259  // Resize all columns to fit the content size
260  for (int i = 0; i <= maxCol; i++) {
261  sheet.autoSizeColumn(i);
262  }
263  }
264 
265 }

Copyright © 2012-2022 Basis Technology. Generated on: Mon Nov 28 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.