Package net.sf.jasperreports.engine.data
Class JRCsvDataSource
java.lang.Object
net.sf.jasperreports.engine.data.JRAbstractTextDataSource
net.sf.jasperreports.engine.data.JRCsvDataSource
- All Implemented Interfaces:
JRDataSource
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.
- Author:
- Ionut Nedelcu (ionutned@users.sourceforge.net)
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringstatic final Stringstatic final Stringstatic final Stringstatic final Stringprotected static final org.apache.commons.logging.Logstatic final StringProperty specifying the CSV column index for the dataset field.static final StringProperty specifying the CSV column name for the dataset field.Fields inherited from class net.sf.jasperreports.engine.data.JRAbstractTextDataSource
EXCEPTION_MESSAGE_KEY_CANNOT_CONVERT_FIELD_TYPE, EXCEPTION_MESSAGE_KEY_CANNOT_MODIFY_PROPERTIES_AFTER_START, EXCEPTION_MESSAGE_KEY_NODE_NOT_AVAILABLE, EXCEPTION_MESSAGE_KEY_NULL_DOCUMENT, EXCEPTION_MESSAGE_KEY_NULL_SELECT_EXPRESSION, EXCEPTION_MESSAGE_KEY_UNKNOWN_COLUMN_NAME, EXCEPTION_MESSAGE_KEY_UNKNOWN_NUMBER_TYPE -
Constructor Summary
ConstructorsConstructorDescriptionJRCsvDataSource(File file) Creates a datasource instance from a CSV file, using the default encoding.JRCsvDataSource(File file, String charsetName) Creates a datasource instance from a CSV file, using the specified encoding.JRCsvDataSource(InputStream stream) Creates a datasource instance from a CSV data input stream, using the default encoding.JRCsvDataSource(InputStream stream, String charsetName) Creates a datasource instance from a CSV data input stream, using the specified encoding.JRCsvDataSource(Reader reader) Creates a datasource instance from a CSV data reader.JRCsvDataSource(String location) JRCsvDataSource(String location, String charsetName) JRCsvDataSource(URL url) Creates a datasource instance that reads CSV data from a given URL, using the default encoding.JRCsvDataSource(URL url, String charsetName) Creates a datasource instance that reads CSV data from a given URL, using the specified encoding.JRCsvDataSource(JasperReportsContext jasperReportsContext, String location) Creates a datasource instance that reads CSV data from a given location, using the default encoding.JRCsvDataSource(JasperReportsContext jasperReportsContext, String location, String charsetName) Creates a datasource instance that reads CSV data from a given location, using the specified encoding.JRCsvDataSource(RepositoryContext context, String location) JRCsvDataSource(RepositoryContext context, String location, String charsetName) -
Method Summary
Modifier and TypeMethodDescriptionprotected voidvoidclose()Closes the reader.Gets the date format that will be used to parse date fieldscharReturns the field delimiter character.getFieldValue(JRField jrField) Gets the field value for the current position.Returns the record delimiter string.booleannext()Tries to position the cursor on the next element in the data source.voidsetColumnNames(String[] columnNames) Specifies an array of strings representing column names matching field names in the report templatevoidsetDateFormat(DateFormat dateFormat) Sets the desired date format to be used for parsing date fieldsvoidsetFieldDelimiter(char fieldDelimiter) Sets the field delimiter character.voidsetNumberFormat(NumberFormat numberFormat) voidsetRecordDelimiter(String recordDelimiter) Sets the record delimiter string.voidsetUseFirstRowAsHeader(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 templateMethods inherited from class net.sf.jasperreports.engine.data.JRAbstractTextDataSource
convertNumber, convertStringValue, getConvertBean, getDatePattern, getLocale, getNumberPattern, getTextAttributes, getTimeZone, setDatePattern, setLocale, setLocale, setNumberPattern, setTextAttributes, setTextAttributes, setTimeZone, setTimeZone
-
Field Details
-
log
protected static final org.apache.commons.logging.Log log -
EXCEPTION_MESSAGE_KEY_CSV_FIELD_VALUE_NOT_RETRIEVED
- See Also:
-
EXCEPTION_MESSAGE_KEY_MALFORMED_QUOTED_FIELD
- See Also:
-
EXCEPTION_MESSAGE_KEY_MISPLACED_QUOTE
- See Also:
-
EXCEPTION_MESSAGE_KEY_NO_MORE_CHARS
- See Also:
-
PROPERTY_FIELD_COLUMN_NAME
Property specifying the CSV column name for the dataset field.- See Also:
-
PROPERTY_FIELD_COLUMN_INDEX
Property specifying the CSV column index for the dataset field.- See Also:
-
INDEXED_COLUMN_PREFIX
- See Also:
-
-
Constructor Details
-
JRCsvDataSource
Creates a datasource instance from a CSV data input stream, using the default encoding.- Parameters:
stream- an input stream containing CSV data
-
JRCsvDataSource
Creates a datasource instance from a CSV data input stream, using the specified encoding.- Parameters:
stream- an input stream containing CSV datacharsetName- the encoding to use- Throws:
UnsupportedEncodingException
-
JRCsvDataSource
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:
IOException
-
JRCsvDataSource
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:
IOException
-
JRCsvDataSource
Creates a datasource instance from a CSV file, using the default encoding.- Parameters:
file- a file containing CSV data- Throws:
FileNotFoundException
-
JRCsvDataSource
public JRCsvDataSource(File file, String charsetName) throws FileNotFoundException, UnsupportedEncodingException Creates a datasource instance from a CSV file, using the specified encoding.- Parameters:
file- a file containing CSV datacharsetName- the encoding to use- Throws:
FileNotFoundExceptionUnsupportedEncodingException
-
JRCsvDataSource
public JRCsvDataSource(JasperReportsContext jasperReportsContext, String location) throws JRException Creates a datasource instance that reads CSV data from a given location, using the default encoding.- Parameters:
jasperReportsContext- the JasperReportsContextlocation- a String representing CSV data source- Throws:
JRException
-
JRCsvDataSource
- Throws:
JRException
-
JRCsvDataSource
public JRCsvDataSource(JasperReportsContext jasperReportsContext, String location, String charsetName) throws JRException, UnsupportedEncodingException Creates a datasource instance that reads CSV data from a given location, using the specified encoding.- Parameters:
jasperReportsContext- the JasperReportsContextlocation- a String representing CSV data sourcecharsetName- the encoding to use- Throws:
JRExceptionUnsupportedEncodingException
-
JRCsvDataSource
public JRCsvDataSource(RepositoryContext context, String location, String charsetName) throws JRException, UnsupportedEncodingException -
JRCsvDataSource
- Throws:
JRException- See Also:
-
JRCsvDataSource
public JRCsvDataSource(String location, String charsetName) throws JRException, UnsupportedEncodingException - Throws:
JRExceptionUnsupportedEncodingException- See Also:
-
JRCsvDataSource
Creates a datasource instance from a CSV data reader.- Parameters:
reader- a Reader instance, for reading the stream
-
-
Method Details
-
next
Description copied from interface:JRDataSourceTries 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
-
assignColumnNames
protected void assignColumnNames() -
getFieldValue
Description copied from interface:JRDataSourceGets 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
Gets the date format that will be used to parse date fields -
setDateFormat
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
Returns the record delimiter string. -
setRecordDelimiter
Sets the record delimiter string. The default is line feed (\n).- Parameters:
recordDelimiter-
-
setColumnNames
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
-
setNumberFormat
-
getColumnNames
-