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

    Author:
    Ionut Nedelcu (ionutned@users.sourceforge.net)
    • Field Detail

      • log

        protected static final org.apache.commons.logging.Log log
      • EXCEPTION_MESSAGE_KEY_CSV_FIELD_VALUE_NOT_RETRIEVED

        public static final String EXCEPTION_MESSAGE_KEY_CSV_FIELD_VALUE_NOT_RETRIEVED
        See Also:
        Constant Field Values
      • EXCEPTION_MESSAGE_KEY_MALFORMED_QUOTED_FIELD

        public static final String EXCEPTION_MESSAGE_KEY_MALFORMED_QUOTED_FIELD
        See Also:
        Constant Field Values
      • EXCEPTION_MESSAGE_KEY_MISPLACED_QUOTE

        public static final String EXCEPTION_MESSAGE_KEY_MISPLACED_QUOTE
        See Also:
        Constant Field Values
      • PROPERTY_FIELD_COLUMN_NAME

        public static final String PROPERTY_FIELD_COLUMN_NAME
        Property specifying the CSV column name for the dataset field.
        See Also:
        Constant Field Values
      • PROPERTY_FIELD_COLUMN_INDEX

        public static final String PROPERTY_FIELD_COLUMN_INDEX
        Property specifying the CSV column index for the dataset field.
        See Also:
        Constant Field Values
    • 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
      • assignColumnNames

        protected void assignColumnNames()
      • getFieldValue

        public 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 DateFormat getDateFormat()
        Gets the date format that will be used to parse date fields
      • setDateFormat

        public void setDateFormat​(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 String getRecordDelimiter()
        Returns the record delimiter string.
      • setRecordDelimiter

        public void setRecordDelimiter​(String recordDelimiter)
        Sets the record delimiter string. The default is line feed (\n).
        Parameters:
        recordDelimiter -
      • setColumnNames

        public void setColumnNames​(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.
      • setNumberFormat

        public void setNumberFormat​(NumberFormat numberFormat)