public class CsvSequence
extends java.lang.Object
implements freemarker.template.TemplateSequenceModel, freemarker.template.TemplateHashModel
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.
Constructor and Description |
---|
CsvSequence()
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
protected java.lang.String |
fixBoolean(java.lang.String s)
Override this if you want to correct boolean values come from the file.
|
protected java.lang.String |
fixNumber(java.lang.String s)
Override this if you want to correct numerical values come from the
file.
|
freemarker.template.TemplateModel |
get(int index) |
freemarker.template.TemplateModel |
get(java.lang.String key) |
java.lang.String |
getAltFalse() |
java.lang.String |
getAltTrue() |
java.lang.String |
getDateFormatPattern() |
java.lang.String |
getDateTimeFormatPattern() |
char |
getDecimalSeparator() |
java.lang.String[] |
getEmptyValues() |
java.lang.String[] |
getExternalHeaderRow() |
char |
getGroupingSeparator() |
boolean |
getHasHeaderRow() |
boolean |
getNormalizeHeaders() |
char |
getSeparator() |
java.lang.String |
getTimeFormatPattern() |
java.util.TimeZone |
getTimeZone() |
boolean |
getTrimCells() |
boolean |
isEmpty() |
void |
load(java.io.Reader in)
Loads data from text of CSV (or whatever similar) format.
|
void |
setAltFalse(java.lang.String altFalse)
Alternative word used to indicate boolean false in the CSV file.
|
void |
setAltTrue(java.lang.String altTrue)
Alternative word used to indicate boolean true in the CSV file.
|
void |
setDateFormatPattern(java.lang.String dateFormatPattern)
Sets the pattern used to parse date columns.
|
void |
setDateTimeFormatPattern(java.lang.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(java.lang.String[] emptyValues)
Specifies the list of cell values that will be replaced with an empty
(0 length) string.
|
void |
setExternalHeaderRow(java.lang.String[] externalHeaderRow)
If it is not
null , 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(java.lang.String timeFormatPattern)
Sets the pattern used to parse time columns.
|
void |
setTimeZone(java.util.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() |
public void load(java.io.Reader in) throws StringUtil.ParseException, java.io.IOException
separator
) before calling this.in
- reader to read the text (file) to parse.
Will be close()
-d.StringUtil.ParseException
java.io.IOException
public freemarker.template.TemplateModel get(int index) throws freemarker.template.TemplateModelException
get
in interface freemarker.template.TemplateSequenceModel
freemarker.template.TemplateModelException
public int size() throws freemarker.template.TemplateModelException
size
in interface freemarker.template.TemplateSequenceModel
freemarker.template.TemplateModelException
protected java.lang.String fixBoolean(java.lang.String s)
s
- the raw column valueprotected java.lang.String fixNumber(java.lang.String s)
s
- the raw column valuepublic freemarker.template.TemplateModel get(java.lang.String key) throws freemarker.template.TemplateModelException
get
in interface freemarker.template.TemplateHashModel
freemarker.template.TemplateModelException
public boolean isEmpty() throws freemarker.template.TemplateModelException
isEmpty
in interface freemarker.template.TemplateHashModel
freemarker.template.TemplateModelException
public void setExternalHeaderRow(java.lang.String[] externalHeaderRow)
null
, 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.public java.lang.String[] getExternalHeaderRow()
public void setHasHeaderRow(boolean hasHeaderRow)
false
, then the externalHeaderRow
property must not be null
when calling
load(Reader)
. Defaults to true
.public boolean getHasHeaderRow()
public void setNormalizeHeaders(boolean normalizeHeaders)
public boolean getNormalizeHeaders()
public void setTrimCells(boolean trimCells)
false
.public boolean getTrimCells()
public void setEmptyValues(java.lang.String[] emptyValues)
"-"
or
"N/A"
. The comparison is case-sensitice. When
getTrimCells()
is true
, 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 to null
, i.e., no replacement.public java.lang.String[] getEmptyValues()
public java.lang.String getAltFalse()
public void setAltFalse(java.lang.String altFalse)
null
if you don't need alternative value.
Defaults to null
.public java.lang.String getAltTrue()
public void setAltTrue(java.lang.String altTrue)
null
if you don't need alternative value.
Defaults to null
.public java.lang.String getDateFormatPattern()
public void setDateFormatPattern(java.lang.String dateFormatPattern)
null
, then StringUtil.stringToDate(java.lang.String, java.util.TimeZone)
will be used
to parse the values. Defaults to null
.public java.lang.String getDateTimeFormatPattern()
public void setDateTimeFormatPattern(java.lang.String dateTimeFormatPattern)
null
, then StringUtil.stringToDateTime(java.lang.String, java.util.TimeZone)
will be used
to parse the values. Defaults to null
.public char getDecimalSeparator()
public void setDecimalSeparator(char decimalSeparator)
public char getGroupingSeparator()
public void setGroupingSeparator(char groupingSeparator)
'\0'
) if you don't use grouping.
Defaults to 0.public char getSeparator()
public void setSeparator(char separator)
public java.lang.String getTimeFormatPattern()
public void setTimeFormatPattern(java.lang.String timeFormatPattern)
null
, then StringUtil.stringToTime(java.lang.String, java.util.TimeZone)
will be used
to parse the values. Defaults to null
.public java.util.TimeZone getTimeZone()
public void setTimeZone(java.util.TimeZone timeZone)
null
, the default time
zone of the computer is used.