net.sf.jasperreports.engine.data
Class JRCsvDataSource

java.lang.Object
  extended by net.sf.jasperreports.engine.data.JRAbstractTextDataSource
      extended by net.sf.jasperreports.engine.data.JRCsvDataSource
All Implemented Interfaces:
JRDataSource

public class JRCsvDataSource
extends JRAbstractTextDataSource

This datasource implementation reads a CSV stream. Datasource rows are separated by a record delimiter string and fields inside a row are separated by a field delimiter character. Fields containing delimiter characters can be placed inside quotes. If fields contain quotes themselves, these are duplicated (example: "John ""Doe""" will be displayed as John "Doe").

Since CSV does not specify column names, the default naming convention is to name report fields COLUMN_x and map each column with the field found at index x in each row (these indices start with 0). To avoid this situation, users can either specify a collection of column names or set a flag to read the column names from the first row of the CSV file.

Version:
$Id: JRCsvDataSource.java 5346 2012-05-08 12:08:01Z teodord $
Author:
Ionut Nedelcu (ionutned@users.sourceforge.net)

Field Summary
protected static org.apache.commons.logging.Log log
           
 
Constructor Summary
JRCsvDataSource(java.io.File file)
          Creates a datasource instance from a CSV file, using the default encoding.
JRCsvDataSource(java.io.File file, java.lang.String charsetName)
          Creates a datasource instance from a CSV file, using the specified encoding.
JRCsvDataSource(java.io.InputStream stream)
          Creates a datasource instance from a CSV data input stream, using the default encoding.
JRCsvDataSource(java.io.InputStream stream, java.lang.String charsetName)
          Creates a datasource instance from a CSV data input stream, using the specified encoding.
JRCsvDataSource(JasperReportsContext jasperReportsContext, java.lang.String location)
          Creates a datasource instance that reads CSV data from a given location, using the default encoding.
JRCsvDataSource(JasperReportsContext jasperReportsContext, java.lang.String location, java.lang.String charsetName)
          Creates a datasource instance that reads CSV data from a given location, using the specified encoding.
JRCsvDataSource(java.io.Reader reader)
          Creates a datasource instance from a CSV data reader.
JRCsvDataSource(java.lang.String location)
           
JRCsvDataSource(java.lang.String location, java.lang.String charsetName)
           
JRCsvDataSource(java.net.URL url)
          Creates a datasource instance that reads CSV data from a given URL, using the default encoding.
JRCsvDataSource(java.net.URL url, java.lang.String charsetName)
          Creates a datasource instance that reads CSV data from a given URL, using the specified encoding.
 
Method Summary
 void close()
          Closes the reader.
 java.util.Map<java.lang.String,java.lang.Integer> getColumnNames()
           
 java.text.DateFormat getDateFormat()
          Gets the date format that will be used to parse date fields
 char getFieldDelimiter()
          Returns the field delimiter character.
 java.lang.Object getFieldValue(JRField jrField)
          Gets the field value for the current position.
 java.text.NumberFormat getNumberFormat()
           
 java.lang.String getRecordDelimiter()
          Returns the record delimiter string.
 boolean next()
          Tries to position the cursor on the next element in the data source.
 void setColumnNames(java.lang.String[] columnNames)
          Specifies an array of strings representing column names matching field names in the report template
 void setDateFormat(java.text.DateFormat dateFormat)
          Sets the desired date format to be used for parsing date fields
 void setFieldDelimiter(char fieldDelimiter)
          Sets the field delimiter character.
 void setNumberFormat(java.text.NumberFormat numberFormat)
           
 void setRecordDelimiter(java.lang.String recordDelimiter)
          Sets the record delimiter string.
 void setUseFirstRowAsHeader(boolean useFirstRowAsHeader)
          Specifies whether the first line of the CSV file should be considered a table header, containing column names matching field names in the report template
 
Methods inherited from class net.sf.jasperreports.engine.data.JRAbstractTextDataSource
convertNumber, convertStringValue, getConvertBean, getDatePattern, getFormattedDate, getFormattedNumber, getLocale, getNumberPattern, getTimeZone, setDatePattern, setLocale, setLocale, setNumberPattern, setTextAttributes, setTimeZone, setTimeZone
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

log

protected static final org.apache.commons.logging.Log log
Constructor Detail

JRCsvDataSource

public JRCsvDataSource(java.io.InputStream stream)
Creates a datasource instance from a CSV data input stream, using the default encoding.

Parameters:
stream - an input stream containing CSV data

JRCsvDataSource

public JRCsvDataSource(java.io.InputStream stream,
                       java.lang.String charsetName)
                throws java.io.UnsupportedEncodingException
Creates a datasource instance from a CSV data input stream, using the specified encoding.

Parameters:
stream - an input stream containing CSV data
charsetName - the encoding to use
Throws:
java.io.UnsupportedEncodingException

JRCsvDataSource

public JRCsvDataSource(java.net.URL url)
                throws java.io.IOException
Creates a datasource instance that reads CSV data from a given URL, using the default encoding.

Parameters:
url - an URL from where to read CSV data
Throws:
java.io.IOException

JRCsvDataSource

public JRCsvDataSource(java.net.URL url,
                       java.lang.String charsetName)
                throws java.io.IOException
Creates a datasource instance that reads CSV data from a given URL, using the specified encoding.

Parameters:
url - an URL from where to read CSV data
Throws:
java.io.IOException

JRCsvDataSource

public JRCsvDataSource(java.io.File file)
                throws java.io.FileNotFoundException
Creates a datasource instance from a CSV file, using the default encoding.

Parameters:
file - a file containing CSV data
Throws:
java.io.FileNotFoundException

JRCsvDataSource

public JRCsvDataSource(java.io.File file,
                       java.lang.String charsetName)
                throws java.io.FileNotFoundException,
                       java.io.UnsupportedEncodingException
Creates a datasource instance from a CSV file, using the specified encoding.

Parameters:
file - a file containing CSV data
charsetName - the encoding to use
Throws:
java.io.FileNotFoundException
java.io.UnsupportedEncodingException

JRCsvDataSource

public JRCsvDataSource(JasperReportsContext jasperReportsContext,
                       java.lang.String location)
                throws JRException
Creates a datasource instance that reads CSV data from a given location, using the default encoding.

Parameters:
jasperReportsContext - the JasperReportsContext
location - a String representing CSV data source
Throws:
JRException

JRCsvDataSource

public JRCsvDataSource(JasperReportsContext jasperReportsContext,
                       java.lang.String location,
                       java.lang.String charsetName)
                throws JRException,
                       java.io.UnsupportedEncodingException
Creates a datasource instance that reads CSV data from a given location, using the specified encoding.

Parameters:
jasperReportsContext - the JasperReportsContext
location - a String representing CSV data source
charsetName - the encoding to use
Throws:
JRException
java.io.UnsupportedEncodingException

JRCsvDataSource

public JRCsvDataSource(java.lang.String location)
                throws JRException
Throws:
JRException
See Also:
JRCsvDataSource(JasperReportsContext, String)

JRCsvDataSource

public JRCsvDataSource(java.lang.String location,
                       java.lang.String charsetName)
                throws JRException,
                       java.io.UnsupportedEncodingException
Throws:
JRException
java.io.UnsupportedEncodingException
See Also:
JRCsvDataSource(JasperReportsContext, String, String)

JRCsvDataSource

public JRCsvDataSource(java.io.Reader reader)
Creates a datasource instance from a CSV data reader.

Parameters:
reader - a Reader instance, for reading the stream
Method Detail

next

public boolean next()
             throws JRException
Description copied from interface: JRDataSource
Tries to position the cursor on the next element in the data source.

Returns:
true if there is a next record, false otherwise
Throws:
JRException - if any error occurs while trying to move to the next element

getFieldValue

public java.lang.Object getFieldValue(JRField jrField)
                               throws JRException
Description copied from interface: JRDataSource
Gets the field value for the current position.

Returns:
an object containing the field value. The object type must be the field object type.
Throws:
JRException

getDateFormat

public java.text.DateFormat getDateFormat()
Gets the date format that will be used to parse date fields


setDateFormat

public void setDateFormat(java.text.DateFormat dateFormat)
Sets the desired date format to be used for parsing date fields


getFieldDelimiter

public char getFieldDelimiter()
Returns the field delimiter character.


setFieldDelimiter

public void setFieldDelimiter(char fieldDelimiter)
Sets the field delimiter character. The default is comma. If characters such as comma or quotes are specified, the results can be unpredictable.

Parameters:
fieldDelimiter -

getRecordDelimiter

public java.lang.String getRecordDelimiter()
Returns the record delimiter string.


setRecordDelimiter

public void setRecordDelimiter(java.lang.String recordDelimiter)
Sets the record delimiter string. The default is line feed (\n).

Parameters:
recordDelimiter -

setColumnNames

public void setColumnNames(java.lang.String[] columnNames)
Specifies an array of strings representing column names matching field names in the report template


setUseFirstRowAsHeader

public void setUseFirstRowAsHeader(boolean useFirstRowAsHeader)
Specifies whether the first line of the CSV file should be considered a table header, containing column names matching field names in the report template


close

public void close()
Closes the reader. Users of this data source should close it after usage.


getNumberFormat

public java.text.NumberFormat getNumberFormat()

setNumberFormat

public void setNumberFormat(java.text.NumberFormat numberFormat)

getColumnNames

public java.util.Map<java.lang.String,java.lang.Integer> getColumnNames()


© 2001-2010 Jaspersoft Corporation www.jaspersoft.com