com.ricebridge.csvman
Class BeanLineListener

java.lang.Object
  extended bycom.ricebridge.csvman.LineListenerSupportImpl
      extended bycom.ricebridge.csvman.CustomLineListener
          extended bycom.ricebridge.csvman.BeanLineListener
All Implemented Interfaces:
LineListener

public class BeanLineListener
extends CustomLineListener

Implementation of LineListener that stores data records as a List of Java Beans.

This class is used to implement the loadBeans(Object,LineSpec,BeanSpec) method in CsvManager.

Data records are stored in new instances of Java Bean objects by calling the set property methods of the objects in accordance with the Java Beans specification. Ordinary, boolean and indexed properties are supported. See the BeanSpec documentation for more information.

BeanLineListener is designed to be subclassed. You can change the default implementation by calling the CsvManagerStore.setBeanLineListenerClass method, and providing a subclass of BeanLineListener. CsvManagerStore can be accessed using CsvManager.getCsvManagerStore().

The Source Code of this Java class is available under a BSD-style license.

See Also:
LineListener, CustomLineListener, ResultSetLineListener, AsListsLineListener, TableModelLineListener, ResultSetLineListener

Field Summary
static boolean DEFAULT_Bean_firstLineFieldsAsNeeded
          Default value of Bean.firstLineFieldsAsNeeded).
static boolean DEFAULT_Bean_useDefault
          Default value of Bean.useDefault (false).
protected  ArrayList iBeans
          List of beans loaded.
protected  BeanSpec iBeanSpec
          BeanSpec specification object defining the properties of the beans to load.
protected  String[] iFieldNames
          The names of the data fields, corresponding to the names of the Java Bean property methods.
protected  boolean iFirstLineFieldsAsNeeded
          Specify that the first line of the CSV file contains the Java Bean field names.
protected  LineSpec iLineSpec
          Description of the data fields.
protected  boolean iUseDefault
          If a data field is empty, use the default value of the property for that field data type.
static String PROP_Bean_firstLineFieldsAsNeeded
          Specify that the first line of the CSV file contains the Java Bean field names.
static String PROP_Bean_useDefault
          Specify that empty data fields have the default value for their data type (name for CsvSpec.setProperty: Bean.useDefault).
 
Constructor Summary
BeanLineListener()
           
 
Method Summary
 List getBeans()
          Get the data records as List of Java Bean objects.
protected  BadLine handleBean(Object pBean)
          Store a bean object in the list of beans.
 BadLine handleLineImpl(String[] pLine, int pNumFields, long pLineNumber, String pOriginalLine)
          Convert the data line String[] in a Java Bean instance and store it in a list.
protected  Object makeBean(String[] pLine)
          Create a new instance of the bean specified by the BeanSpec and set the bean properties data from the data line String[] array.
protected  String[] makeFieldNames(String[] pLine, int pNumFields)
          Interpret the first line of data as the property field names.
protected  Object newBean(Class pBeanClass, String[] pLine)
          Create a new instance of the Java Bean object.
 void setBeanSpec(BeanSpec pBeanSpec)
          Set the BeanSpec object that defines the beans to load.
protected  void setCsvSpecImpl(CsvSpec pCsvSpec)
          Handle property settings for loading Java Beans.
protected  void setLineSpecImpl(LineSpec pLineSpec)
          Use the LineSpec to map data fields to Java Bean methods.
protected  void startProcessImpl()
          Start the load process.
 String toString()
          Return a textual description suitable for debugging.
 
Methods inherited from class com.ricebridge.csvman.CustomLineListener
endProcessImpl, handleBadLineImpl
 
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, wait, wait, wait
 

Field Detail

PROP_Bean_useDefault

public static final String PROP_Bean_useDefault
Specify that empty data fields have the default value for their data type (name for CsvSpec.setProperty: Bean.useDefault).

See Also:
Constant Field Values

DEFAULT_Bean_useDefault

public static final boolean DEFAULT_Bean_useDefault
Default value of Bean.useDefault (false).

See Also:
Constant Field Values

PROP_Bean_firstLineFieldsAsNeeded

public static final String PROP_Bean_firstLineFieldsAsNeeded
Specify that the first line of the CSV file contains the Java Bean field names.

This is only used when no LineSpec has been provided. If a LineSpec has been provided, this setting is ignored and the first line is assumed to be normal data.

(name for CsvSpec.setProperty: Bean.firstLineFieldsAsNeeded).

See Also:
Constant Field Values

DEFAULT_Bean_firstLineFieldsAsNeeded

public static final boolean DEFAULT_Bean_firstLineFieldsAsNeeded
Default value of Bean.firstLineFieldsAsNeeded).

See Also:
Constant Field Values

iBeans

protected ArrayList iBeans
List of beans loaded.


iBeanSpec

protected BeanSpec iBeanSpec
BeanSpec specification object defining the properties of the beans to load.


iUseDefault

protected boolean iUseDefault
If a data field is empty, use the default value of the property for that field data type.


iFirstLineFieldsAsNeeded

protected boolean iFirstLineFieldsAsNeeded
Specify that the first line of the CSV file contains the Java Bean field names.


iLineSpec

protected LineSpec iLineSpec
Description of the data fields.


iFieldNames

protected String[] iFieldNames
The names of the data fields, corresponding to the names of the Java Bean property methods.

Constructor Detail

BeanLineListener

public BeanLineListener()
Method Detail

setBeanSpec

public void setBeanSpec(BeanSpec pBeanSpec)
Set the BeanSpec object that defines the beans to load.

The BeanSpec object contains the definitions of the bean properties and performs the conversion from String values to internal Java data types.

Parameters:
pBeanSpec - BeanSpec definition of beans to load

getBeans

public List getBeans()
Get the data records as List of Java Bean objects.

Returns:
List of beans

toString

public String toString()
Return a textual description suitable for debugging.


setCsvSpecImpl

protected void setCsvSpecImpl(CsvSpec pCsvSpec)
                       throws Exception
Handle property settings for loading Java Beans.

You can change these using CsvSpec.

The properties you can use here are: PROP_Bean_useDefault and PROP_Bean_firstLineFieldsAsNeeded.

Overrides:
setCsvSpecImpl in class CustomLineListener
Parameters:
pCsvSpec - CsvSpec object
Throws:
Exception
See Also:
LineListener.setCsvSpec(com.ricebridge.csvman.CsvSpec), PROP_Bean_useDefault

setLineSpecImpl

protected void setLineSpecImpl(LineSpec pLineSpec)
                        throws Exception
Use the LineSpec to map data fields to Java Bean methods.

Overrides:
setLineSpecImpl in class CustomLineListener
Parameters:
pLineSpec - LineSpec object
Throws:
Exception
See Also:
LineListener.setLineSpec(com.ricebridge.csvman.LineSpec)

startProcessImpl

protected void startProcessImpl()
                         throws Exception
Start the load process.

Performs some sanity checks on the loading setup.

Overrides:
startProcessImpl in class CustomLineListener
Throws:
Exception
See Also:
endProcessImpl

handleLineImpl

public BadLine handleLineImpl(String[] pLine,
                              int pNumFields,
                              long pLineNumber,
                              String pOriginalLine)
                       throws Exception
Convert the data line String[] in a Java Bean instance and store it in a list.

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
Throws:
Exception
See Also:
LineListener.handleLine(java.lang.String[], int, long, java.lang.String)

handleBean

protected BadLine handleBean(Object pBean)
Store a bean object in the list of beans.

Parameters:
pBean - bean object

makeBean

protected Object makeBean(String[] pLine)
Create a new instance of the bean specified by the BeanSpec and set the bean properties data from the data line String[] array.

Parameters:
pLine - record data

newBean

protected Object newBean(Class pBeanClass,
                         String[] pLine)
Create a new instance of the Java Bean object.

Parameters:
pBeanClass - class of bean to create
pLine - current data line

makeFieldNames

protected String[] makeFieldNames(String[] pLine,
                                  int pNumFields)
Interpret the first line of data as the property field names.

These names will be used to identify the get and set method names for each data field. The names will be capitalised if necessary.

Parameters:
pLine - header line of data, containing field names
pNumFields - number of actual fields, may be less than pLine.length
Returns:
array of Java Bean field names


Copyright © 2003-2006 Ricebridge