|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||
java.lang.Objectcom.ricebridge.csvman.LineListenerSupportImpl
com.ricebridge.csvman.CustomLineListener
com.ricebridge.csvman.test.CsvHandlerTest.TestLineListener
Test LineListener
| Field Summary | |
StringBuffer |
data
|
StringBuffer |
sb
|
| Constructor Summary | |
CsvHandlerTest.TestLineListener()
|
|
| Method Summary | |
void |
endProcessImpl()
Implement this method to receive notification that the loading of CSV data has ended. |
void |
handleBadLineImpl(BadLine pBadLine)
Implement this method to be notified when badly formatted data is encountered. |
BadLine |
handleLineImpl(String[] pLine,
int pNumFields,
long pLineNumber,
String pOriginalLine)
Implement this method to receive each data line as it is loaded. |
void |
startProcessImpl()
Implement this method to receive notification that the loading of CSV data is about to start. |
| Methods inherited from class com.ricebridge.csvman.CustomLineListener |
setCsvSpecImpl, setLineSpecImpl |
| Methods inherited from class com.ricebridge.csvman.LineListenerSupportImpl |
endProcess, handleBadLine, handleLine, setCsvSpec, setLineSpec, startProcess |
| Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
public StringBuffer sb
public StringBuffer data
| Constructor Detail |
public CsvHandlerTest.TestLineListener()
| Method Detail |
public void startProcessImpl()
CustomLineListenerYou can implement this method when you extend CustomLineListener,
but it is not required.
You can use this method to initialise any resource you need to process the CSV data. For example you can open a database connection to store the data as it is loaded.
This method is called after setCsvSpecImpl and
setLineSpecImpl.
startProcessImpl in class CustomLineListenerendProcessImplpublic void endProcessImpl()
CustomLineListenerYou can implement this method when you extend CustomLineListener,
but it is not required.
You can use this method to close any open resources that were used to handle the CSV data. For example you can close any open database connections.
This method is called last, after all handleLineImpl
and handleBadLineImpl calls have been made.
endProcessImpl in class CustomLineListenerstartProcessImpl
public BadLine handleLineImpl(String[] pLine,
int pNumFields,
long pLineNumber,
String pOriginalLine)
CustomLineListenerThis method must be implemented when you extend CustomLineListener.
This method is where you will do the main work of processing the CSV data. As you get each line in, you can decide what to do with the data. The parameters of this method provide you with a lot of information about the CSV data line that you can use in your application.
First, the pLine parameter contains the actual data as a
String[] array. This array is guaranteed not to contain any null Strings.
If empty data fields are found in the CSV line (for example a,,b => ['a','','b'])
then empty strings are placed in the array. This means that you can avoid nasty
NullPointerExceptions.
Equally nasty are ArrayIndexOutOfBoundsExceptions.
CSV Manager helps you avoid them by making sure that the pLine array is always
long enough. By "long enough", we mean either as long as the longest line found so far, or as long
as is specified by the CsvSpec.setNumFields method.
Of course, this means that in the case where there are fewer data fields than normal, you also
need to know exactly how many data fields there actually were,
as pLine.length will not tell you this. This is what the pNumFields
parameter is for. So if you need to check exactly how many data fields a line had, use
pNumFields.
To help with error reporting, CSV Manager also provides the line number of the
current data line, passed in via pLineNumber. This includes any bad lines found.
pLineNumber is a long, just in case you ever have a really,
really big CSV file.
Finally, you also get the text of the original line (pOriginalLine), so you can create
user-friendly error messages for your users. And it makes debugging easier.
Error Handling: What happens when the data in the CSV file is incorrect in some way?
For example, it might not be valid for your database. In this case, even though the syntax
of the CSV is correct, there is a semantic error. To capture this case, we use the following
contract: if all is well, return a null from handleBadLineImpl.
If there is an error with the data, return a BadLine object describing the error.
This provides consistent handling of errors. At any time you can of course
just throw an Exception, but if you use a BadLine instead then you get
proper summary statistics, nice error reporting, and faster performance as you avoid the overhead
of Exception throwing and catching.
But if you have a error that is not data related, (if for example, your database goes down), then it is
better to throw an Exception.
handleLineImpl in class CustomLineListenerpLine - String values of data fields in linepNumFields - Number of data fields actually found on the current linepLineNumber - Count of lines processed so far.pOriginalLine - Text of original data line from data source
null if line is OK, BadLine object if line was bad in some wayLineListener.handleLine,
BadLine,
handleBadLineImpl
public void handleBadLineImpl(BadLine pBadLine)
throws Exception
CustomLineListenerYou can implement this method when you extend CustomLineListener,
but it is not required.
When a syntax error is encountered in the CSV file you are loading, a BadLine object is
created by CSV Manager to describe the problem, and then it is passed to your custom
LineListener for further handling. You can decide how to log the error or what other actions
to take based on your error handling policy.
What happens after handleBadLineImpl is called? It depends on the
CsvSpec.setIgnoreBadLines setting. If this setting is
true, then loading will continue with the rest of the CSV file. If it is
false, then loading will halt and a CsvManagerException will be thrown for
the code that called the CsvManager.load method to catch.
Note: BadLines returned by handleLineImpl are not
passed to this method. Since you already know about them in handeLineImpl, there would not
be much point.
handleBadLineImpl in class CustomLineListenerpBadLine - BadLine object describing the error
ExceptionLineListener.handleBadLine,
handleLineImpl
|
|||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||||