com.ricebridge.csvman.test
Class ReallyBadLines.NullLineListener

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

public static class ReallyBadLines.NullLineListener
extends CustomLineListener


Constructor Summary
ReallyBadLines.NullLineListener()
           
 
Method Summary
 BadLine handleLineImpl(String[] pLine, int pNumFields, long pLineNumber, String pOriginalLine)
          Implement this method to receive each data line as it is loaded.
 
Methods inherited from class com.ricebridge.csvman.CustomLineListener
endProcessImpl, handleBadLineImpl, setCsvSpecImpl, setLineSpecImpl, startProcessImpl
 
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

ReallyBadLines.NullLineListener

public ReallyBadLines.NullLineListener()
Method Detail

handleLineImpl

public 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