Package fmpp.models

Class CsvSequence

java.lang.Object
fmpp.models.CsvSequence
All Implemented Interfaces:
freemarker.template.TemplateHashModel, freemarker.template.TemplateModel, freemarker.template.TemplateSequenceModel

public class CsvSequence extends Object implements freemarker.template.TemplateSequenceModel, freemarker.template.TemplateHashModel
Sequence variable implementation that wraps text of CSV or tab separated values format, or any other format that is the same as CSV except that it uses different column separator char.

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 Details

    • CsvSequence

      public CsvSequence()
      Creates a new instance.
  • Method Details

    • load

      public void load(Reader in) throws StringUtil.ParseException, IOException
      Loads data from text of CSV (or whatever similar) format. This method can be called once per instance. Set all options (as separator) before calling this.
      Parameters:
      in - reader to read the text (file) to parse. Will be close()-d.
      Throws:
      StringUtil.ParseException
      IOException
    • get

      public freemarker.template.TemplateModel get(int index) throws freemarker.template.TemplateModelException
      Specified by:
      get in interface freemarker.template.TemplateSequenceModel
      Throws:
      freemarker.template.TemplateModelException
    • size

      public int size() throws freemarker.template.TemplateModelException
      Specified by:
      size in interface freemarker.template.TemplateSequenceModel
      Throws:
      freemarker.template.TemplateModelException
    • fixBoolean

      protected String fixBoolean(String s)
      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

      protected String fixNumber(String s)
      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 interface freemarker.template.TemplateHashModel
      Throws:
      freemarker.template.TemplateModelException
    • isEmpty

      public boolean isEmpty() throws freemarker.template.TemplateModelException
      Specified by:
      isEmpty in interface freemarker.template.TemplateHashModel
      Throws:
      freemarker.template.TemplateModelException
    • setExternalHeaderRow

      public void setExternalHeaderRow(String[] externalHeaderRow)
      If it is not 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.
    • getExternalHeaderRow

      public String[] getExternalHeaderRow()
    • setHasHeaderRow

      public void setHasHeaderRow(boolean hasHeaderRow)
      Specifies if the file contains header row or not. If it is false, then the externalHeaderRow property must not be null when calling load(Reader). Defaults to true.
    • 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:
      1. Remove the part between the first "(" and last ")", before the header is parsed for column type identifier (like ":n").
      2. 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.)
      3. Then it's converted to lower case.
      4. Then the following characters are replaced with "_": space, comma, semicolon, colon.
      5. Then all "__" and "___" and so on is replaced with a single "_".
      For example, "Price, old (category: A, B, F): n" will be normailzed to "price_old", and the type identifier will be n.
    • 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 to false.
    • getTrimCells

      public boolean getTrimCells()
    • setEmptyValues

      public void setEmptyValues(String[] emptyValues)
      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. 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.
    • getEmptyValues

      public String[] getEmptyValues()
    • getAltFalse

      public String getAltFalse()
    • setAltFalse

      public void setAltFalse(String altFalse)
      Alternative word used to indicate boolean false in the CSV file. Use null if you don't need alternative value. Defaults to null.
    • getAltTrue

      public String getAltTrue()
    • setAltTrue

      public void setAltTrue(String altTrue)
      Alternative word used to indicate boolean true in the CSV file. Use null if you don't need alternative value. Defaults to null.
    • getDateFormatPattern

      public String getDateFormatPattern()
    • setDateFormatPattern

      public void setDateFormatPattern(String dateFormatPattern)
      Sets the pattern used to parse date columns. If this is null, then StringUtil.stringToDate(java.lang.String, java.util.TimeZone) will be used to parse the values. Defaults to null.
    • getDateTimeFormatPattern

      public String getDateTimeFormatPattern()
    • setDateTimeFormatPattern

      public void setDateTimeFormatPattern(String dateTimeFormatPattern)
      Sets the pattern used to parse date-time columns. If this is null, then StringUtil.stringToDateTime(java.lang.String, java.util.TimeZone) will be used to parse the values. Defaults to null.
    • 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

      public String getTimeFormatPattern()
    • setTimeFormatPattern

      public void setTimeFormatPattern(String timeFormatPattern)
      Sets the pattern used to parse time columns. If this is null, then StringUtil.stringToTime(java.lang.String, java.util.TimeZone) will be used to parse the values. Defaults to null.
    • getTimeZone

      public TimeZone getTimeZone()
    • setTimeZone

      public void setTimeZone(TimeZone timeZone)
      Sets the time zone used for parsing date/time/date-time that does not specify a time zone explicitly. If null, the default time zone of the computer is used.