Removed formatting of tables during text extraction, makes Indexed Text viewer easier to read

This commit is contained in:
U-BASIS\dsmyda 2018-09-20 09:58:16 -04:00
parent 6cfd20d58f
commit 8fbae9e5a6

View File

@ -33,7 +33,6 @@ import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.tabulardatareader.AbstractReader; import org.sleuthkit.autopsy.tabulardatareader.AbstractReader;
import org.sleuthkit.autopsy.tabulardatareader.AbstractReader.FileReaderInitException; import org.sleuthkit.autopsy.tabulardatareader.AbstractReader.FileReaderInitException;
import org.sleuthkit.datamodel.Content; import org.sleuthkit.datamodel.Content;
import org.apache.commons.lang3.StringUtils;
import org.sleuthkit.autopsy.tabulardatareader.AbstractReader.FileReaderException; import org.sleuthkit.autopsy.tabulardatareader.AbstractReader.FileReaderException;
import org.sleuthkit.autopsy.tabulardatareader.FileReaderFactory; import org.sleuthkit.autopsy.tabulardatareader.FileReaderFactory;
import org.sleuthkit.datamodel.AbstractFile; import org.sleuthkit.datamodel.AbstractFile;
@ -222,21 +221,21 @@ class SqliteTextExtractor extends ContentTextExtractor {
private class TableBuilder { private class TableBuilder {
private final List<String[]> rows = new LinkedList<>(); private final List<String[]> rows = new LinkedList<>();
private Integer charactersAdded = 0;
//Formatters
private static final String HORIZONTAL_DELIMITER = "-";
private static final String VERTICAL_DELIMITER = "|";
private static final String HEADER_CORNER = "+";
private static final String TAB = "\t"; private static final String TAB = "\t";
private static final String NEW_LINE = "\n"; private static final String NEW_LINE = "\n";
private static final String SPACE = " "; private static final String SPACE = " ";
//Number of escape sequences in the header row private String tableName;
private static final int ESCAPE_SEQUENCES = 4; private Integer charactersAdded;
private String tableName = ""; /**
* Set tableName and charactersAdded to their default values.
*/
public TableBuilder() {
tableName = "";
charactersAdded = 0;
}
/** /**
* Add the section to the top left corner of the table. This is where * Add the section to the top left corner of the table. This is where
@ -248,16 +247,6 @@ class SqliteTextExtractor extends ContentTextExtractor {
this.tableName = tableName + NEW_LINE + NEW_LINE; this.tableName = tableName + NEW_LINE + NEW_LINE;
} }
/**
* Creates a border given the length param.
*
* @return Ex: \t+----------------------+\n
*/
private String createBorder(int length) {
return TAB + HEADER_CORNER + StringUtils.repeat(
HORIZONTAL_DELIMITER, length) + HEADER_CORNER + NEW_LINE;
}
/** /**
* Add header row to underlying list collection, which will be formatted * Add header row to underlying list collection, which will be formatted
* when toString is called. * when toString is called.
@ -275,7 +264,7 @@ class SqliteTextExtractor extends ContentTextExtractor {
* @param vals * @param vals
*/ */
public void addRow(Collection<Object> vals) { public void addRow(Collection<Object> vals) {
List<String> rowValues = new ArrayList<>(); List<String> rowValues = new LinkedList<>();
vals.forEach((val) -> { vals.forEach((val) -> {
rowValues.add(val.toString()); rowValues.add(val.toString());
charactersAdded += val.toString().length(); charactersAdded += val.toString().length();
@ -284,33 +273,6 @@ class SqliteTextExtractor extends ContentTextExtractor {
new String[rowValues.size()])); new String[rowValues.size()]));
} }
/**
* Gets the max width of a cell in each column and the max number of
* columns in any given row. This ensures that there are enough columns
* and enough space for even the longest entry.
*
* @return array of column widths
*/
private int[] getMaxWidthPerColumn() {
int maxNumberOfColumns = 0;
for (String[] row : rows) {
maxNumberOfColumns = Math.max(
maxNumberOfColumns, row.length);
}
int[] widths = new int[maxNumberOfColumns];
for (String[] row : rows) {
for (int colNum = 0; colNum < row.length; colNum++) {
widths[colNum] = Math.max(
widths[colNum],
row[colNum].length()
);
}
}
return widths;
}
/** /**
* Returns a string version of the table, with all of the formatters and * Returns a string version of the table, with all of the formatters and
* escape sequences necessary to print nicely in the console output. * escape sequences necessary to print nicely in the console output.
@ -320,23 +282,14 @@ class SqliteTextExtractor extends ContentTextExtractor {
@Override @Override
public String toString() { public String toString() {
StringBuilder outputTable = new StringBuilder(charactersAdded); StringBuilder outputTable = new StringBuilder(charactersAdded);
int[] colMaxWidths = getMaxWidthPerColumn(); outputTable.append(tableName);
int borderLength = 0;
Iterator<String[]> rowIterator = rows.iterator(); Iterator<String[]> rowIterator = rows.iterator();
if (rowIterator.hasNext()) {
//Length of the header defines the table boundaries
borderLength = appendFormattedHeader(rowIterator.next(),
colMaxWidths, outputTable);
while (rowIterator.hasNext()) { while (rowIterator.hasNext()) {
appendFormattedRow(rowIterator.next(), colMaxWidths, outputTable); appendFormattedRow(rowIterator.next(), outputTable);
} }
outputTable.insert(0, tableName);
outputTable.append(createBorder(borderLength));
outputTable.append(NEW_LINE); outputTable.append(NEW_LINE);
}
return outputTable.toString(); return outputTable.toString();
} }
@ -344,56 +297,20 @@ class SqliteTextExtractor extends ContentTextExtractor {
/** /**
* Outputs a fully formatted row in the table * Outputs a fully formatted row in the table
* *
* Example: \t| John | 12345678 | john@email.com |\n * Example: \t John 12345678 john@email.com\n
* *
* @param row Array containing unformatted row content * @param row Array containing unformatted row content
* @param colMaxWidths An array of column maximum widths, so that * @param colMaxWidths An array of column maximum widths, so that
* everything is pretty printed. * everything is pretty printed.
* @param outputTable Buffer that formatted contents are written to * @param outputTable Buffer that formatted contents are written to
*/ */
private void appendFormattedRow(String[] row, private void appendFormattedRow(String[] row, StringBuilder outputTable) {
int[] colMaxWidths, StringBuilder outputTable) {
outputTable.append(TAB); outputTable.append(TAB);
for (int colNum = 0; colNum < row.length; colNum++) { for (int colNum = 0; colNum < row.length; colNum++) {
outputTable.append(VERTICAL_DELIMITER); outputTable.append(row[colNum]);
outputTable.append(SPACE);
outputTable.append(StringUtils.rightPad(
StringUtils.defaultString(row[colNum]),
colMaxWidths[colNum]));
outputTable.append(SPACE); outputTable.append(SPACE);
} }
outputTable.append(VERTICAL_DELIMITER);
outputTable.append(NEW_LINE); outputTable.append(NEW_LINE);
} }
/**
* Adds a fully formatted header to the table builder and returns the
* length of this header. The length of the header is needed to set the
* table boundaries
*
* Example: \t+----------------------+\n
* \t| Email | Phone | Name |\n
* \t+----------------------+\n
*
* @param row Array of contents in each column
* @param colMaxWidths Widths for each column in the table
* @param outputTable Output stringbuilder
*
* @return length of the formatted header, this length will be needed to
* correctly print the bottom table border.
*/
private int appendFormattedHeader(String[] row, int[] colMaxWidths, StringBuilder outputTable) {
appendFormattedRow(row, colMaxWidths, outputTable);
//Printable table dimensions are equal to the length of the header minus
//the number of escape sequences used to for formatting.
int borderLength = outputTable.length() - ESCAPE_SEQUENCES;
String border = createBorder(borderLength);
//Surround the header with borders above and below.
outputTable.insert(0, border);
outputTable.append(border);
return borderLength;
}
} }
} }