Package de.hybris.platform.util
Class FixedLengthCSVReader
- java.lang.Object
-
- de.hybris.platform.util.CSVReader
-
- de.hybris.platform.util.FixedLengthCSVReader
-
public class FixedLengthCSVReader extends CSVReader
This class extends theCSVReader
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 Summary
Constructors Constructor Description FixedLengthCSVReader(java.io.File file, java.lang.String encoding)
Opens the file and sets the given encoding.FixedLengthCSVReader(java.io.InputStream is, java.lang.String encoding)
Opens the given Inputstream with the given file.FixedLengthCSVReader(java.io.Reader reader)
Opens the given reader.FixedLengthCSVReader(java.lang.String lines)
A convenience constructor for passing csv data as simple string object.FixedLengthCSVReader(java.lang.String fileName, java.lang.String encoding)
Opens the file with the given filename and sets the given encoding.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addField(int startPosition, int endPosition, int columnNumber)
Adds a field information to the reader.protected java.util.Map
parseLine(java.lang.String line)
Tokenises the given line and returns aMap
with following content:
Map{
{ 0:Integer, Field_1:String },
{ 1:Integer, Field_2:String },
...
{ n-1:Integer, Field_n:String }
}
void
setFieldTrimming(boolean trim)
Sets if field content should be trimmed while reading-
Methods inherited from class de.hybris.platform.util.CSVReader
applyDecorators, clearAllCellDecorators, clearCellDecorator, close, closeQuietly, finished, getCellDecorator, getCommentOut, getCurrentLineNumber, getDecoratorMap, getFieldSeparator, getLine, getSourceLine, getTextSeparator, hasCellDecorators, isCommentedOut, isFinished, isMultiLineMode, isReading, isShowComments, markFinished, mustSkip, notifyNextLine, parse, parse, parse, parse, readNextLine, readSrcLineFromStream, setCellDecorator, setCommentOut, setFieldSeparator, setLinesToSkip, setMaxBufferLines, setMultiLineMode, setShowComments, setTextSeparator, trim
-
-
-
-
Constructor Detail
-
FixedLengthCSVReader
public FixedLengthCSVReader(java.lang.String fileName, java.lang.String encoding) throws java.io.UnsupportedEncodingException, java.io.FileNotFoundException
Opens the file with the given filename and sets the given encoding.- Parameters:
fileName
- the filename of the CSV fileencoding
- the given encoding, default is"UTF-8"
- Throws:
java.io.UnsupportedEncodingException
- thrown by false encoding typejava.io.FileNotFoundException
- thrown if file not found
-
FixedLengthCSVReader
public FixedLengthCSVReader(java.io.File file, java.lang.String encoding) throws java.io.UnsupportedEncodingException, java.io.FileNotFoundException
Opens the file and sets the given encoding.- Parameters:
file
- the CSV fileencoding
- the given encoding, default is"UTF-8"
- Throws:
java.io.UnsupportedEncodingException
- thrown by false encoding typejava.io.FileNotFoundException
- thrown if file not found
-
FixedLengthCSVReader
public FixedLengthCSVReader(java.io.InputStream is, java.lang.String encoding) throws java.io.UnsupportedEncodingException
Opens the given Inputstream with the given file.- Parameters:
is
- the InputStreamencoding
- the given encoding- Throws:
java.io.UnsupportedEncodingException
-
FixedLengthCSVReader
public FixedLengthCSVReader(java.io.Reader reader)
Opens the given reader. The default encoding is set to"UTF-8"
- Parameters:
reader
- the reader
-
FixedLengthCSVReader
public FixedLengthCSVReader(java.lang.String lines)
A convenience constructor for passing csv data as simple string object.- Parameters:
lines
- the csv data as string
-
-
Method Detail
-
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 callingparseLine
. 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 (seeparseLine
- Throws:
java.lang.IllegalArgumentException
- field interleaves with an existing one or number of column still exists
-
parseLine
protected java.util.Map parseLine(java.lang.String line)
Description copied from class:CSVReader
Tokenises the given line and returns aMap
with following content:
Map{
{ 0:Integer, Field_1:String },
{ 1:Integer, Field_2:String },
...
{ n-1:Integer, Field_n:String }
}
-
-