|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.io.TextTable
public class TextTable
Class used to parse ASCII tables Many applications (spreadsheets in particular) are capable of exporting data to a ASCII file in a series of rows and columns, where each row corresponds to one record in the table, and the columns correspond to record fields. A common form of this export method is know as tab separated values. This class is helpful in parsing the contents of these types of files and can deal with leading headers.
This class really deserves a overview document - as time permits I will get back to it.
| Field Summary | |
|---|---|
static int |
FROM_FIRST_ROW
Constant used during construction to calculate column count. |
| Constructor Summary | |
|---|---|
TextTable()
Default constructor This method initializes the object such that it expects to parse data from from a TAB separated ASCII text file. |
|
TextTable(int min_cols,
int max_cols,
boolean colHeads,
String sep)
Specify table constraints when constructing the object. |
|
| Method Summary | |
|---|---|
int |
getFieldCount()
The number of columns which were last parsed. |
String |
getFieldString(int pos,
String def)
Retrieve the data from a particular column. |
int |
getMaxColumns()
Get the maximum number of columns each row in the table must have. |
int |
getMinColumns()
Get the minimum number of columns each row in the table must have. |
int |
getTableColumn(String name)
Determine the column index for one of the "columns" of data. |
static int[] |
getTableMap(TextTable tt,
String[][] headers)
Determine the indexes of the columns we are interested in by column heading. |
boolean |
isNextRowColHeads()
Is should the next row parsed be treated as column headings set? |
static void |
main(String[] args)
Main entry point into the application. |
boolean |
parseRow(String raw)
Parse the fields from a single row. |
void |
setNextRowColHeads(boolean val)
Set should the next row parsed be treated as column headings. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static int FROM_FIRST_ROW
| Constructor Detail |
|---|
public TextTable()
TextTable(int,int,boolean,String)
public TextTable(int min_cols,
int max_cols,
boolean colHeads,
String sep)
minCols - The minimum number of columns which must be present in each
valid data row. You can use the constant FROM_FIRST_ROW if you want it to be automatically determined
from the first row parsed.maxCols - The maximum number of columns which must be present in each
valid data row. You can use the constant FROM_FIRST_ROW if you want it to be automatically determined
from the first row parsed.colHeads - Set this to true if the first line of the tab separated file
contains column headings.sep - Pass the string which separates each column in the table. For
a tab separated value file (.tsv), you should pass "\t" -
which is the tab character.parseRow(java.lang.String)| Method Detail |
|---|
public boolean parseRow(String raw)
getFieldCount() to determine the number of fields parsed
and the getFieldString(int, java.lang.String) to retrieve the contents of a
particular field (column).
Note, if this is the first time this method is called and you indicated that the table contained column headings, then this method will return false. Your application needs to be aware of this when it processes the data.
row - A single row (typically read from a tab separated file).
getFieldCount()public int getFieldCount()
parsed.
Each time the parseRow(java.lang.String) method is invoked, it will set
the number of fields which it parsed from the row. It will return
0 if the last invocation of parseRow(java.lang.String) didn't parse out
ANY data.
parseRow(java.lang.String),
getFieldString(int, java.lang.String)
public String getFieldString(int pos,
String def)
static void foo(TextTable tt, String row) {
if (tt.parseRow(row)) {
int len = tt.getFieldCount();
for (int i = 0; i < len; i++) {
String val = tt.getFieldString(i,null);
if (val != null) {
System.out.println("col["+i+"]="+val);
}
}
}
}
column - Which column you want to get the data from.defaultValue - The default value you would like returned if the data wasn't
present in the row.
parseRow(java.lang.String)public static void main(String[] args)
java com.ccg.io.TextTable [-tossHead] [-minCols N] [-maxCols N]
[-inSep STR] [-outSep STR] [-colMap C0[,C1[...CN]]]
The recognized command line arguments are
args - Array of command line arguments.public int getMinColumns()
public int getMaxColumns()
public static int[] getTableMap(TextTable tt,
String[][] headers)
Assume we read the header row of the following table into a TextTable object named 'tt':
Name Description Paul Dad Megan Mom Erik Son Scott Son
We'll now try to figure out which columns contain what fields. The following array of string arrays has three entries. The first entry is the list of names that we will allow to match the last name entry, the second entry is the list of names that we will allow to match the first name entry and the last list is the list of identifiers for the description column we allow.
String[][] mHeaders = {
{ "LastName", "last" },
{ "FirstName", "first", "Name" },
{ "Description", "text" }
};
When we invoke TextTable.getTableMap(tt,mHeaders), it will return a int[]
telling us which column each of the headers in our map was found
in. In this case it will return { -1, 0, 1 }, indicating the the
last name column wasn't found (-1), the first name column was
found at column 0 of our source and the description column was
found at column 1 in our source. We can pass these values to
getFieldString(int, java.lang.String) as we process each line of the file.
tt - The TextTable object which you just parsed the header row from (the first row of a tab separated
file).headers - The headers of the columns you are interested in as described
above.
getFieldString(int, java.lang.String) method when
you are processing the data.public int getTableColumn(String name)
header - The heading at the top of the column (exact match required)
getTableMap(com.ccg.io.TextTable, java.lang.String[][])public void setNextRowColHeads(boolean val)
val - New boolean value to assign.
see #getNextRowColHeadspublic boolean isNextRowColHeads()
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||