Class CsvSequence
- All Implemented Interfaces:
freemarker.template.TemplateHashModel
,freemarker.template.TemplateModel
,freemarker.template.TemplateSequenceModel
The sequence is the list of table rows, and each row is hash where you can access the cells with the column name. The column names (headers) are the values in the first row of cells in the CSV file.
The values in the table will be always exposed as string variables, unless
you specify an other type in the header cell directly. This can be done
by using colon + a type identifier at the end of the header cell. The type
indetifier can be: n
or number
, b
or
boolean
, d
or date
,
t
or time
, dt
or
dateTime
, s
or string
. For example, if
the value of a header cell in the CSV file is "price:n", then the values of
the cell will be exposed as numberical variables, not string.
CsvSequence
is also a hash that contains one key:
headers
. This is a sequence that stores the header names.
-
Field Summary
Fields inherited from interface freemarker.template.TemplateModel
NOTHING
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected String
fixBoolean
(String s) Override this if you want to correct boolean values come from the file.protected String
Override this if you want to correct numerical values come from the file.freemarker.template.TemplateModel
get
(int index) freemarker.template.TemplateModel
char
String[]
String[]
char
boolean
boolean
char
boolean
boolean
isEmpty()
void
Loads data from text of CSV (or whatever similar) format.void
setAltFalse
(String altFalse) Alternative word used to indicate boolean false in the CSV file.void
setAltTrue
(String altTrue) Alternative word used to indicate boolean true in the CSV file.void
setDateFormatPattern
(String dateFormatPattern) Sets the pattern used to parse date columns.void
setDateTimeFormatPattern
(String dateTimeFormatPattern) Sets the pattern used to parse date-time columns.void
setDecimalSeparator
(char decimalSeparator) Sets the alternative symbol used for the decimal dot in the file for numbers.void
setEmptyValues
(String[] emptyValues) Specifies the list of cell values that will be replaced with an empty (0 length) string.void
setExternalHeaderRow
(String[] externalHeaderRow) If it is notnull
, then it is used as the header row instead of the first row of the CSV file.void
setGroupingSeparator
(char groupingSeparator) Set the grouping separator symbol used for grouping in the file for numbers.void
setHasHeaderRow
(boolean hasHeaderRow) Specifies if the file contains header row or not.void
setNormalizeHeaders
(boolean normalizeHeaders) Specifies if the header names coming from the file will be normalized or should be left as is.void
setSeparator
(char separator) Separator char between cloumns.void
setTimeFormatPattern
(String timeFormatPattern) Sets the pattern used to parse time columns.void
setTimeZone
(TimeZone timeZone) Sets the time zone used for parsing date/time/date-time that does not specify a time zone explicitly.void
setTrimCells
(boolean trimCells) Specifies if all cells will be trimmed.int
size()
-
Constructor Details
-
CsvSequence
public CsvSequence()Creates a new instance.
-
-
Method Details
-
load
Loads data from text of CSV (or whatever similar) format. This method can be called once per instance. Set all options (asseparator
) before calling this.- Parameters:
in
- reader to read the text (file) to parse. Will beclose()
-d.- Throws:
StringUtil.ParseException
IOException
-
get
public freemarker.template.TemplateModel get(int index) throws freemarker.template.TemplateModelException - Specified by:
get
in interfacefreemarker.template.TemplateSequenceModel
- Throws:
freemarker.template.TemplateModelException
-
size
public int size() throws freemarker.template.TemplateModelException- Specified by:
size
in interfacefreemarker.template.TemplateSequenceModel
- Throws:
freemarker.template.TemplateModelException
-
fixBoolean
Override this if you want to correct boolean values come from the file. The default implementation removes leading and trailing white-space, converts to lowe case, and reaplaces the alternative boolean values (if set) with "true" and "false".- Parameters:
s
- the raw column value- Returns:
- String fixed value
-
fixNumber
Override this if you want to correct numerical values come from the file. The default implementation removes leading and trailing white-space, removes grouping symbols (if set) and replaces decimal separator (if set) with dot.- Parameters:
s
- the raw column value- Returns:
- String fixed value
-
get
public freemarker.template.TemplateModel get(String key) throws freemarker.template.TemplateModelException - Specified by:
get
in interfacefreemarker.template.TemplateHashModel
- Throws:
freemarker.template.TemplateModelException
-
isEmpty
public boolean isEmpty() throws freemarker.template.TemplateModelException- Specified by:
isEmpty
in interfacefreemarker.template.TemplateHashModel
- Throws:
freemarker.template.TemplateModelException
-
setExternalHeaderRow
If it is notnull
, then it is used as the header row instead of the first row of the CSV file. Each value in the array corresponds to a header row cell. -
getExternalHeaderRow
-
setHasHeaderRow
public void setHasHeaderRow(boolean hasHeaderRow) Specifies if the file contains header row or not. If it isfalse
, then theexternalHeaderRow
property must not benull
when callingload(Reader)
. Defaults totrue
. -
getHasHeaderRow
public boolean getHasHeaderRow() -
setNormalizeHeaders
public void setNormalizeHeaders(boolean normalizeHeaders) Specifies if the header names coming from the file will be normalized or should be left as is. Normalization means:- Remove the part between the first "(" and last ")", before the header is parsed for column type identifier (like ":n").
- After the type identifier was extracted and removed (if there was any), the cell value is trimmed. (Trimming happens even if header normalization is off.)
- Then it's converted to lower case.
- Then the following characters are replaced with "_": space, comma, semicolon, colon.
- Then all "__" and "___" and so on is replaced with a single "_".
-
getNormalizeHeaders
public boolean getNormalizeHeaders() -
setTrimCells
public void setTrimCells(boolean trimCells) Specifies if all cells will be trimmed. Trimming means the removal of all leading and trailing white-space. Defaults tofalse
. -
getTrimCells
public boolean getTrimCells() -
setEmptyValues
Specifies the list of cell values that will be replaced with an empty (0 length) string. Typical such values are"-"
or"N/A"
. The comparison is case-sensitice. WhengetTrimCells()
istrue
, the comparison occurs after the trimming. The replacement occurs before type conversions (for typed columns). Header values will not be affected by this property. Defaults tonull
, i.e., no replacement. -
getEmptyValues
-
getAltFalse
-
setAltFalse
Alternative word used to indicate boolean false in the CSV file. Usenull
if you don't need alternative value. Defaults tonull
. -
getAltTrue
-
setAltTrue
Alternative word used to indicate boolean true in the CSV file. Usenull
if you don't need alternative value. Defaults tonull
. -
getDateFormatPattern
-
setDateFormatPattern
Sets the pattern used to parse date columns. If this isnull
, thenStringUtil.stringToDate(java.lang.String, java.util.TimeZone)
will be used to parse the values. Defaults tonull
. -
getDateTimeFormatPattern
-
setDateTimeFormatPattern
Sets the pattern used to parse date-time columns. If this isnull
, thenStringUtil.stringToDateTime(java.lang.String, java.util.TimeZone)
will be used to parse the values. Defaults tonull
. -
getDecimalSeparator
public char getDecimalSeparator() -
setDecimalSeparator
public void setDecimalSeparator(char decimalSeparator) Sets the alternative symbol used for the decimal dot in the file for numbers. Note that dot will be always assumed as decimal separator, except if groupingSeparator is set to dot. -
getGroupingSeparator
public char getGroupingSeparator() -
setGroupingSeparator
public void setGroupingSeparator(char groupingSeparator) Set the grouping separator symbol used for grouping in the file for numbers. Use 0 ('\0'
) if you don't use grouping. Defaults to 0. -
getSeparator
public char getSeparator() -
setSeparator
public void setSeparator(char separator) Separator char between cloumns. Defaults to semi-colon (;). -
getTimeFormatPattern
-
setTimeFormatPattern
Sets the pattern used to parse time columns. If this isnull
, thenStringUtil.stringToTime(java.lang.String, java.util.TimeZone)
will be used to parse the values. Defaults tonull
. -
getTimeZone
-
setTimeZone
Sets the time zone used for parsing date/time/date-time that does not specify a time zone explicitly. Ifnull
, the default time zone of the computer is used.
-