Class FixedLengthCSVReader

java.lang.Object
de.hybris.platform.util.CSVReader
de.hybris.platform.util.FixedLengthCSVReader

public class FixedLengthCSVReader extends CSVReader
This class extends the CSVReader and parses a CSV InputStream to a list of maps using fixed lengths for the columns. That means the CSV stream is not separated by field separators like ';', instead each field/column has a fixed position in line. Only full line comments (if activated) and empty lines are considered.

After instantiating the parser you need to set the positions of the columns within a line.
String testString = "wert1wert2foowert3\nwert4wert5foowert6";
FixedLengthCSVReader r = new FixedLengthCSVReader( testString );
//adding fixed field from position 0 to 4 and assigning it to column 0 (entry number in resulting map)
r.addField( 0, 4, 0 );
//adding fixed field from position 5 to 9 and assigning it to column 1
r.addField( 5, 9, 1 );
//adding fixed field from position 13 to 17 and assigning it to column 3
r.addField( 13, 17, 3 );
//Pasing first line
Map line = r.getLine();

  • Constructor Details

  • Method Details

    • setFieldTrimming

      public void setFieldTrimming(boolean trim)
      Sets if field content should be trimmed while reading
      Parameters:
      trim - will be trimming enabled?
    • addField

      public void addField(int startPosition, int endPosition, int columnNumber)
      Adds a field information to the reader. This information will be considered while parsing. An information consists of the fixed start and end position of the field within the records, as well as the position of the column at the resulting line object when calling parseLine. Interleaving of fields with respect to their start and end position is not allowed and will be checked. Furthermore adding an information for an already existing column number is not allowed.
      Parameters:
      startPosition - absolute start position of the field (starting at 0)
      endPosition - absolute end position of the field (position of the last character)
      columnNumber - number of the parsed field in line map (see parseLine
      Throws:
      IllegalArgumentException - field interleaves with an existing one or number of column still exists
    • parseLine

      protected Map parseLine(String line)
      Description copied from class: CSVReader
      Tokenises the given line and returns a Map with following content:

      Map{
      { 0:Integer, Field_1:String },
      { 1:Integer, Field_2:String },
      ...
      { n-1:Integer, Field_n:String }
      }

      Overrides:
      parseLine in class CSVReader
      Parameters:
      line - the line
      Returns:
      a map with the parsed CSV fields or null if failure