mirror of
https://github.com/overcuriousity/autopsy-flatpak.git
synced 2025-07-12 16:06:15 +00:00
Added xlsx support (XML model)
This commit is contained in:
parent
3fd1d1cf05
commit
9bb103359b
@ -34,51 +34,46 @@ import org.apache.poi.ss.usermodel.DateUtil;
|
|||||||
import org.apache.poi.ss.usermodel.Row;
|
import org.apache.poi.ss.usermodel.Row;
|
||||||
import org.apache.poi.ss.usermodel.Sheet;
|
import org.apache.poi.ss.usermodel.Sheet;
|
||||||
import org.apache.poi.ss.usermodel.Workbook;
|
import org.apache.poi.ss.usermodel.Workbook;
|
||||||
import org.openide.util.NbBundle;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||||
import org.sleuthkit.autopsy.coreutils.Logger;
|
import org.sleuthkit.autopsy.coreutils.Logger;
|
||||||
import org.sleuthkit.autopsy.ingest.IngestServices;
|
import org.sleuthkit.autopsy.ingest.IngestServices;
|
||||||
import org.sleuthkit.datamodel.AbstractFile;
|
import org.sleuthkit.datamodel.AbstractFile;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reads Excel files and returns results in a list collection.
|
*
|
||||||
|
* @author dsmyda
|
||||||
*/
|
*/
|
||||||
@NbBundle.Messages({
|
public class ExcelReader extends AbstractReader {
|
||||||
"ExcelReader.ReadExcelFiles.moduleName=ExcelReader"
|
|
||||||
})
|
|
||||||
public class ExcelReader extends AbstractReader {
|
|
||||||
|
|
||||||
private Workbook xlsWorkbook;
|
private Workbook workbook;
|
||||||
private final IngestServices services = IngestServices.getInstance();
|
private final IngestServices services = IngestServices.getInstance();
|
||||||
private final Logger logger = services.getLogger(ExcelReader.class.getName());
|
private final Logger logger = services.getLogger(ExcelReader.class.getName());
|
||||||
|
private String XLSXMimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
|
||||||
/**
|
private String XLSMimeType = "application/vnd.ms-excel";
|
||||||
*
|
|
||||||
* @param file
|
public ExcelReader(AbstractFile file, String localDiskPath, String mimeType)
|
||||||
* @param localDiskPath
|
|
||||||
* @throws org.sleuthkit.autopsy.sqlitereader.AbstractReader.FileReaderInitException
|
|
||||||
*/
|
|
||||||
public ExcelReader(AbstractFile file, String localDiskPath)
|
|
||||||
throws FileReaderInitException {
|
throws FileReaderInitException {
|
||||||
super(file, localDiskPath);
|
super(file, localDiskPath);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
getWorkbookFromLocalDisk(localDiskPath);
|
this.workbook = createWorkbook(localDiskPath, mimeType);
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
throw new FileReaderInitException(ex);
|
throw new FileReaderInitException(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
private Workbook createWorkbook(String localDiskPath, String mimeType) throws
|
||||||
* Opens an apache poi workbook instance on the file contents copied from the
|
IOException, FileReaderInitException {
|
||||||
* abstract file.
|
if(mimeType.equals(XLSMimeType)) {
|
||||||
*
|
return new HSSFWorkbook(new FileInputStream(new File(localDiskPath)));
|
||||||
* @param localDiskPath Location of the file contents on local disk
|
} else if(mimeType.equals(XLSXMimeType)) {
|
||||||
* @throws IOException Error opening file at local disk path
|
return new XSSFWorkbook(new FileInputStream(new File(localDiskPath)));
|
||||||
*/
|
} else {
|
||||||
private void getWorkbookFromLocalDisk(String localDiskPath) throws IOException{
|
throw new FileReaderInitException(String.format("Excel reader for mime "
|
||||||
xlsWorkbook = new HSSFWorkbook(new FileInputStream(new File(localDiskPath)));
|
+ "type [%s] is not supported", mimeType));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the number of rows in a given excel table (aka sheet).
|
* Returns the number of rows in a given excel table (aka sheet).
|
||||||
*
|
*
|
||||||
@ -88,7 +83,7 @@ public class ExcelReader extends AbstractReader {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public Integer getRowCountFromTable(String tableName) throws FileReaderException {
|
public Integer getRowCountFromTable(String tableName) throws FileReaderException {
|
||||||
return xlsWorkbook.getSheet(tableName).getLastRowNum();
|
return workbook.getSheet(tableName).getLastRowNum();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -101,7 +96,7 @@ public class ExcelReader extends AbstractReader {
|
|||||||
@Override
|
@Override
|
||||||
public List<Map<String, Object>> getRowsFromTable(String tableName) throws FileReaderException {
|
public List<Map<String, Object>> getRowsFromTable(String tableName) throws FileReaderException {
|
||||||
List<Map<String, Object>> rowContents = new ArrayList<>();
|
List<Map<String, Object>> rowContents = new ArrayList<>();
|
||||||
Iterator<Row> iterator = xlsWorkbook.getSheet(tableName).rowIterator();
|
Iterator<Row> iterator = workbook.getSheet(tableName).rowIterator();
|
||||||
//Consume header
|
//Consume header
|
||||||
if(iterator.hasNext()) {
|
if(iterator.hasNext()) {
|
||||||
//Consume header
|
//Consume header
|
||||||
@ -171,7 +166,7 @@ public class ExcelReader extends AbstractReader {
|
|||||||
@Override
|
@Override
|
||||||
public Map<String, String> getTableSchemas() throws FileReaderException {
|
public Map<String, String> getTableSchemas() throws FileReaderException {
|
||||||
Map<String, String> tableSchemas = new HashMap<>();
|
Map<String, String> tableSchemas = new HashMap<>();
|
||||||
for(Sheet sheet : xlsWorkbook) {
|
for(Sheet sheet : workbook) {
|
||||||
Row header = sheet.getRow(0);
|
Row header = sheet.getRow(0);
|
||||||
if(header != null) {
|
if(header != null) {
|
||||||
String headerStringFormat = StringUtils.join(header.cellIterator(), ", ");
|
String headerStringFormat = StringUtils.join(header.cellIterator(), ", ");
|
||||||
@ -183,17 +178,14 @@ public class ExcelReader extends AbstractReader {
|
|||||||
return tableSchemas;
|
return tableSchemas;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
@Override
|
@Override
|
||||||
public void close() {
|
public void close() {
|
||||||
try {
|
try {
|
||||||
xlsWorkbook.close();
|
workbook.close();
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
//Non-essential exception, user has no need for the connection
|
//Non-essential exception, user has no need for the connection
|
||||||
//object at this stage so closing details are not important
|
//object at this stage so closing details are not important
|
||||||
logger.log(Level.WARNING, "Could not close excel file input stream", ex);
|
logger.log(Level.WARNING, "Could not close excel file input stream", ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -36,11 +36,12 @@ public final class FileReaderFactory {
|
|||||||
*/
|
*/
|
||||||
public static AbstractReader createReader(String mimeType, AbstractFile file,
|
public static AbstractReader createReader(String mimeType, AbstractFile file,
|
||||||
String localDiskPath) throws FileReaderInitException {
|
String localDiskPath) throws FileReaderInitException {
|
||||||
switch(mimeType) {
|
switch (mimeType) {
|
||||||
case "application/x-sqlite3":
|
case "application/x-sqlite3":
|
||||||
return new SQLiteReader(file, localDiskPath);
|
return new SQLiteReader(file, localDiskPath);
|
||||||
case "application/vnd.ms-excel":
|
case "application/vnd.ms-excel":
|
||||||
return new ExcelReader(file, localDiskPath);
|
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
|
||||||
|
return new ExcelReader(file, localDiskPath, mimeType);
|
||||||
default:
|
default:
|
||||||
throw new FileReaderInitException(String.format("Reader for mime "
|
throw new FileReaderInitException(String.format("Reader for mime "
|
||||||
+ "type [%s] is not supported", mimeType));
|
+ "type [%s] is not supported", mimeType));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user