|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.io.TableProcessor
public class TableProcessor
Base class to facilitate the processing of text tables.
Often it seems like one comes across tables of data that reside in ASCII files. These tables may resemble the form of a "tab separated value" file as shown below:
Last First Phone Blankenbaker Erik 555-555-7893 Blankenbaker Paul 555-555-7839 Blankenbaker Scott 555-555-8393 Brown Megan 555-555-3923
This class greatly simplifies the processing of tables of this form. Basically it involves the following steps:
processData(), processHeader(), exceptionData(java.lang.Exception, java.io.LineNumberReader), exceptionHeader(java.lang.Exception, java.io.LineNumberReader).
TextTable object works for tables that
are of the tab separated value form).
process(java.io.LineNumberReader) method, which does the
rest and makes each row of data parsed available to your processData() handler.
A example program PhoneTable.java has been provided which demonstrates how one can use this to process text tables of different forms and layouts by appropriately mapping the locations of column headers. Developers are encouraged to examine this sample code to get a good idea of how this object is used to process table data.
TextTable,
PhoneTable.java| Constructor Summary | |
|---|---|
TableProcessor(TextTable t)
Initializes the object with a table parser object. |
|
| Method Summary | |
|---|---|
boolean |
exceptionData(Exception e,
LineNumberReader lnr)
Method which is invoked when a problem occurs processing a row of data. |
boolean |
exceptionHeader(Exception e,
LineNumberReader lnr)
Method which is invoked when a problem occurs processing column headers. |
Exception |
getDataException()
Get the last exception which occurred while processing data. |
Exception |
getHeaderException()
Get the last exception which occurred while processing header. |
TextTable |
getTable()
Get the table definition object. |
int |
process(LineNumberReader in)
Reads all rows from the text table (including column headers). |
boolean |
processData()
Invoked for each row of data that has been parsed from the source table. |
boolean |
processHeader()
Invoked when the initial header line of a table is being processed. |
String |
readLine(LineNumberReader lnr,
StringBuffer joiner)
Helper method to "smartly" join split lines together. |
void |
setDataException(Exception val)
Set the last exception which occurred while processing data. |
void |
setHeaderException(Exception val)
Set the last exception which occurred while processing header. |
void |
setTable(TextTable val)
Set the table definition object. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public TableProcessor(TextTable t)
table parser object.
You create a TableProcessor object (which is capable
of processing the entire contents of a table) by providing it
with a table parser (which is capable of
parsing the columns from a single data row). After construction,
you then use the process(java.io.LineNumberReader) method to process the contents
of the table.
t - The table object used for parsing the column
fields from each row read from the file. If you pass null, you
should use the setTable(com.ccg.io.TextTable) method to set to a non-null
value prior to invoking process(java.io.LineNumberReader).TextTable,
process(java.io.LineNumberReader)| Method Detail |
|---|
public int process(LineNumberReader in)
This method will read the contents of a input source and
process each row read. If the table has column headings, then the
first row will be assumed to contain the table column headers and
the processHeader() method will be invoked after it has
been read. All subsequent rows will be assumed to be data rows
and the processData() method will be invoked each time a
data row is successfully parsed from the source.
See the class overview for more details
on how a developer would extend this class. Also, don't forget to
set the table prior to invoking this method
(this is required by the constructor).
It should be noted that this method doesn't throw an
exception. Instead it logs any exceptions encountered. You can
use the getDataException() and/or getHeaderException() to determine if there were any problems
during the processing of your data.
in - The input source to read the table data from (you can pass
null in which case 0 is returned).
readLine(java.io.LineNumberReader, java.lang.StringBuffer)
public String readLine(LineNumberReader lnr,
StringBuffer joiner)
throws IOException
This is the default implementation of the code which reads the next row of table data from the input source as a single line. It was discovered that tab separated value tables will sometimes split their lines if a field contains new line characters. This implementation tries to be "smart" about this situation and counts double quote characters (") on each line read. If there is a "open" quote after reading a line, it is assumed that more lines need to be read from the source and appended until all "quoted" fields are complete.
If you find yourself processing ASCII tables that don't follow this convention, you may need to override this method. However, this method seems to work for tables exported in the tab-separated-value form from Excel, Quattro Pro, Xess, and StarOffice.
lnr - Source to read the next line from.joiner - Scratch buffer to build the line read from source (expect this
buffer to be trashed after each invocation).
IOException - If there was a problem reading line(s) from the sourceprocess(java.io.LineNumberReader)public boolean processHeader()
This method is typically only invoked one time (at most) if the table being processed contains column headings at the start of the table. You can override this method to verify that the column headings are what you expect (return true if they are OK, or false if not).
It can be very handy/efficient to determine your column heading mappings at this point. By doing so here one time, you can greatly improve the efficiency at which your code parses the table data (I need to provide an example of this).
exceptionHeader(java.lang.Exception, java.io.LineNumberReader) or setHeaderException(java.lang.Exception) to allow others to know why). This default
implementation always returns true.processData()
public boolean exceptionHeader(Exception e,
LineNumberReader lnr)
This method is invoked when there is a problem processing the column headers at the start of the table. If this method returns true, then processing will continue. If this method returns false, then processing of the table contents will stop.
You can override this method if you are interested in knowing when problems occur with the processing of the column headers.
e - An exception describing what went wronglnr - The source we were reading the data from.
processHeader()public boolean processData()
Each time a row has been parsed from the table, you can use
this method to examine/process the data that was parsed. First
use the getTable() method to get access to the object
where the fields are stored, you can then access the data in
specific columns using the standard TextTable methods.
exceptionData(java.lang.Exception, java.io.LineNumberReader) or setDataException(java.lang.Exception) to allow
others to know why). This default implementation always
returns true.processHeader()
public boolean exceptionData(Exception e,
LineNumberReader lnr)
This method is invoked when there is a problem processing a row of data in the table. If this method returns true, then processing will continue. If this method returns false, then processing of the table contents will stop.
You can override this method if you are interested in knowing when problems occur with the processing of data (or if you want to change the default behavior).
e - An exception describing what went wronglnr - The source we were reading the data from.
processData()public void setTable(TextTable val)
table definition object.
val - New TextTable value to assign.getTable()public TextTable getTable()
table definition object.
setTable(com.ccg.io.TextTable)public void setDataException(Exception val)
val - New Exception value to assign.getDataException()public Exception getDataException()
setDataException(java.lang.Exception)public void setHeaderException(Exception val)
val - New Exception value to assign.getHeaderException()public Exception getHeaderException()
setHeaderException(java.lang.Exception)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||