Package fmpp.models

Class CsvSequence

  • All Implemented Interfaces:
    freemarker.template.TemplateHashModel, freemarker.template.TemplateModel, freemarker.template.TemplateSequenceModel

    public class CsvSequence
    extends java.lang.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.

    • Field Summary

      • Fields inherited from interface freemarker.template.TemplateModel

        NOTHING
    • Constructor Summary

      Constructors 
      Constructor Description
      CsvSequence()
      Creates a new instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CsvSequence

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

      • load

        public void load​(java.io.Reader in)
                  throws StringUtil.ParseException,
                         java.io.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
        java.io.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 java.lang.String fixBoolean​(java.lang.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 java.lang.String fixNumber​(java.lang.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​(java.lang.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​(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. Each value in the array corresponds to a header row cell.
      • getExternalHeaderRow

        public java.lang.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​(java.lang.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 java.lang.String[] getEmptyValues()
      • getAltFalse

        public java.lang.String getAltFalse()
      • setAltFalse

        public void setAltFalse​(java.lang.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 java.lang.String getAltTrue()
      • setAltTrue

        public void setAltTrue​(java.lang.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 java.lang.String getDateFormatPattern()
      • getDateTimeFormatPattern

        public java.lang.String getDateTimeFormatPattern()
      • 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 java.lang.String getTimeFormatPattern()
      • getTimeZone

        public java.util.TimeZone getTimeZone()
      • setTimeZone

        public 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. If null, the default time zone of the computer is used.