Autopsy  4.18.0
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.datasourcesummary.uiutils;
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 
32 
37  public 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  public 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  public int getRowStart() {
63  return rowStart;
64  }
65 
69  public int getRowEnd() {
70  return rowEnd;
71  }
72 
76  public int getColStart() {
77  return colStart;
78  }
79 
83  public int getColEnd() {
84  return colEnd;
85  }
86  }
87 
91  public interface ExcelItemExportable {
92 
103  ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env) throws ExcelExportException;
104  }
105 
109  public static class SingleCellExportable implements ExcelItemExportable {
110 
111  private final ExcelCellModel item;
112 
118  public SingleCellExportable(String key) {
119  this(new DefaultCellModel<>(key));
120  }
121 
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  public static class KeyValueItemExportable implements ExcelItemExportable {
144 
145  private final ExcelCellModel key;
146  private final ExcelCellModel value;
147 
154  public KeyValueItemExportable(String key, ExcelCellModel value) {
155  this(new DefaultCellModel<>(key), value);
156  }
157 
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  public 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  public 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  public 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 }
ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env)
ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env)
ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env)
ItemDimensions write(Sheet sheet, int rowStart, int colStart, ExcelExport.WorksheetEnv env)
ExcelSpecialFormatExport(String sheetName, List< ExcelItemExportable > exports)

Copyright © 2012-2021 Basis Technology. Generated on: Thu Jul 8 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.