com.ricebridge.csvman.test
Class StreamingTest.MeasureStreamLineListener

java.lang.Object
  extended bycom.ricebridge.csvman.LineListenerSupportImpl
      extended bycom.ricebridge.csvman.CustomLineListener
          extended bycom.ricebridge.csvman.test.StreamingTest.MeasureStreamLineListener
All Implemented Interfaces:
LineListener
Enclosing class:
StreamingTest

public static class StreamingTest.MeasureStreamLineListener
extends CustomLineListener


Constructor Summary
StreamingTest.MeasureStreamLineListener(String pFileName, long pExpectedLines)
           
 
Method Summary
 void endProcessImpl()
          Implement this method to receive notification that the loading of CSV data has ended.
protected  void handleBadLineImpl(BadLine pBadLine)
          Implement this method to be notified when badly formatted data is encountered.
protected  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
 

Constructor Detail

StreamingTest.MeasureStreamLineListener

public StreamingTest.MeasureStreamLineListener(String pFileName,
                                               long pExpectedLines)
Method Detail

startProcessImpl

public void startProcessImpl()
Description copied from class: CustomLineListener
Implement this method to receive notification that the loading of CSV data is about to start.

You 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.

Overrides:
startProcessImpl in class CustomLineListener
See Also:
endProcessImpl

endProcessImpl

public void endProcessImpl()
Description copied from class: CustomLineListener
Implement this method to receive notification that the loading of CSV data has ended.

You 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.

Overrides:
endProcessImpl in class CustomLineListener
See Also:
startProcessImpl

handleBadLineImpl

protected void handleBadLineImpl(BadLine pBadLine)
Description copied from class: CustomLineListener
Implement this method to be notified when badly formatted data is encountered.

You 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.

Overrides:
handleBadLineImpl in class CustomLineListener
Parameters:
pBadLine - BadLine object describing the error
See Also:
LineListener.handleBadLine, handleLineImpl

handleLineImpl

protected BadLine handleLineImpl(String[] pLine,
                                 int pNumFields,
                                 long pLineNumber,
                                 String pOriginalLine)
Description copied from class: CustomLineListener
Implement this method to receive each data line as it is loaded.

This 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.

Specified by:
handleLineImpl in class CustomLineListener
Parameters:
pLine - String values of data fields in line
pNumFields - Number of data fields actually found on the current line
pLineNumber - Count of lines processed so far.
pOriginalLine - Text of original data line from data source
Returns:
null if line is OK, BadLine object if line was bad in some way
See Also:
LineListener.handleLine, BadLine, handleBadLineImpl


Copyright © 2003-2006 Ricebridge