Package de.hybris.platform.util
Class CSVReader
java.lang.Object
de.hybris.platform.util.CSVReader
- Direct Known Subclasses:
CSVFromPropertiesReader,FixedLengthCSVReader,ImpExReader.ResultSetCSVReader,SyncScheduleReader,UNSPSCReader
This class parses a CSV InputStream to a list of maps. Each list entry represents a line of the stream and each entry
in the map is a parsed CSV field. By default, the reader ignores comments and empty lines. The format of the
CSV-source is expected as defined in RFC-4180. Separator and
comment chars can differ from the ones in rfc.
Use of this CSVReader:
- Create the Reader
csvreader = new CSVReader(inputStream, null ); //null for default encoding
Use the constructor to set the encoding and the stream. There are also other constructors available. - Configure the Reader
Example:csvreader.setTextSeparator('?');
If you skip this step, following defaults are set:
commentOut = '#'//chars for comment linesfieldseparator = ';'//separate the CSV fieldstextseparator = '\"'//enclose a CSV textshowComments = false//all comment line are ignoredtoSkip = 0//there will be no lines ignored
toSkipis set to 3 the first 3 lines of data are skipped, regardless if they are empty lines, comments, real header or data lines! - Use the Reader
ArrayList i_am_the_parsed_csv_stream = new ArrayList(); while (csvreader.readNextLine()) { i_am_the_parsed_csv_stream.add(csvreader.getLine()); }After this
whileloop the ArrayList contains all parsed CSV lines. Each line is a Map of the parsed CSV fields. SeeparseLine(String)for the setup of the map. - Close the Reader
Do not forget to callcsvreader.close()to close the reader.
-
Constructor Summary
ConstructorsConstructorDescriptionOpens the file and sets the given encoding.CSVReader(InputStream is, String encoding) Opens the given Inputstream with the given file.Opens the given reader.A convenience constructor for passing csv data as simple string object.Opens the file with the given filename and sets the given encoding. -
Method Summary
Modifier and TypeMethodDescriptionstatic MapapplyDecorators(Map<Integer, CSVCellDecorator> decoratorMap, Map line) Applies given decorators to columns in given line.voidRemoves all declared cell decorators.voidclearCellDecorator(int position) Removes one declared cell decorator.voidclose()Close the reader.voidClose the reader quietly.booleanfinished()Is reading from stream is already finished (has stream reached its end)?getCellDecorator(int position) Returns the decorator mapped to the given column position.char[]intprotected Map<Integer,CSVCellDecorator> getDecoratorMap(boolean create) Returns a map containing the current csv decorators mapped to column positions.char[]getLine()Returns the parsed line as map.Gets the last line read from stream.charbooleanAre there declared cell decorators?protected booleanisCommentedOut(String line) Returns true if the passed line is a comment (depends onsetCommentOut(char[])).booleanIs reading of input stream finished (has reached end)?booleanTells whether or not the reader supports csv lines spread across multiple lines by putting a\(backslash) at the end of each unfinished line.protected booleanChecks if reader is already reading from stream.booleanprotected voidSets the finished flag which indicates the reaching of stream end.protected booleanmustSkip()Checks if lines have to be skipped still.protected voidIncrements the line number and decreases the line skip counter.Convenience method which parses csv lines directly from the given reader.Convenience method which parses csv lines directly from the given string.Convenience method which parses csv lines directly from the given string.Convenience method which parses csv lines directly from the given string.Tokenises the given line and returns aMapwith following content:
Map{
{ 0:Integer, Field_1:String },
{ 1:Integer, Field_2:String },
...
{ n-1:Integer, Field_n:String }
}
final booleanReads and parses next line from stream and returns true if the line was read and parsed successfully.protected StringReads next line from stream.voidsetCellDecorator(int position, CSVCellDecorator decorator) Maps a decorator to a column position.voidsetCommentOut(char[] commentOut) Set the characters which indicates a comment line.voidsetFieldSeparator(char[] fieldseparator) Sets the CSV field separator char(s).voidsetLinesToSkip(int i) Set the number of real lines which are skipped whenreadNextLine()is called the first time.voidsetMaxBufferLines(int number) SetmaxBufferLinesto a new value.voidsetMultiLineMode(boolean on) Changes whether or not the reader supports csv lines spread across multiple lines by putting a\(backslash) at the end of each unfinished line.voidsetShowComments(boolean showComments) Set to true if all comment line should also parsed.voidsetTextSeparator(char textseparator) Sets the text separator char which enclose in CSV a text(default is").protected StringTrims the given string like the trim() method of java.lang.String, but allows to disable trimming from start or end of the string.
-
Constructor Details
-
CSVReader
public CSVReader(String fileName, String encoding) throws UnsupportedEncodingException, 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 isCSVConstants.DEFAULT_ENCODING- Throws:
UnsupportedEncodingException- thrown by not supported encoding typeFileNotFoundException- thrown if file not found
-
CSVReader
public CSVReader(File file, String encoding) throws UnsupportedEncodingException, FileNotFoundException Opens the file and sets the given encoding.- Parameters:
file- the CSV fileencoding- the given encoding, default isCSVConstants.DEFAULT_ENCODING- Throws:
UnsupportedEncodingException- thrown by not supported encoding typeFileNotFoundException- thrown if file not found
-
CSVReader
Opens the given Inputstream with the given file.- Parameters:
is- the InputStreamencoding- the given encoding, default isCSVConstants.DEFAULT_ENCODING- Throws:
UnsupportedEncodingException- thrown by not supported encoding type
-
CSVReader
Opens the given reader. The default encoding is set toCSVConstants.DEFAULT_ENCODING- Parameters:
reader- the reader
-
CSVReader
A convenience constructor for passing csv data as simple string object.- Parameters:
lines- the csv data as string
-
-
Method Details
-
getDecoratorMap
Returns a map containing the current csv decorators mapped to column positions. If there are no decorators declared, a new map will be created ifcreateflag is set.- Parameters:
create- If no cell decorators declared and flag is set a new map will be returned- Returns:
- map holding the current declared cell decorators
-
clearAllCellDecorators
public void clearAllCellDecorators()Removes all declared cell decorators. -
clearCellDecorator
public void clearCellDecorator(int position) Removes one declared cell decorator.- Parameters:
position- the column position the decorator is mapped
-
setCellDecorator
Maps a decorator to a column position.- Parameters:
position- position the decorator will be mappeddecorator- the decorator
-
hasCellDecorators
public boolean hasCellDecorators()Are there declared cell decorators?- Returns:
- true if decorators are declared
-
getCellDecorator
Returns the decorator mapped to the given column position.- Parameters:
position- position of column for which the decorator is needed- Returns:
- decorator mapped to given position or null if no one is declared
-
readSrcLineFromStream
Reads next line from stream. If stream has reached end,nullis returned and finished flag will be set.- Returns:
- next line from stream or
null
-
finished
public boolean finished()Is reading from stream is already finished (has stream reached its end)?- Returns:
- true if the stream has reached its end and last call of
readNextLinehas returned null
-
readNextLine
public final boolean readNextLine()Reads and parses next line from stream and returns true if the line was read and parsed successfully.- Returns:
- false if the end of the stream is reached, else true.
-
trim
Trims the given string like the trim() method of java.lang.String, but allows to disable trimming from start or end of the string. Another difference is, that all declared field separators will not be trimmed.- Parameters:
src- the string which will be trimmedfromStart- is trimming from the left side of the string enabled?fromEnd- is trimming from the right side of the string enabled?- Returns:
- if there are no changes, the given string is returned, else a new trimmed copy
- See Also:
-
getLine
Returns the parsed line as map.- Returns:
- the map
-
getSourceLine
Gets the last line read from stream.- Returns:
- the last read source line from the stream.
-
parseLine
Tokenises the given line and returns aMapwith following content:
Map{
{ 0:Integer, Field_1:String },
{ 1:Integer, Field_2:String },
...
{ n-1:Integer, Field_n:String }
}
- Parameters:
line- the line- Returns:
- a map with the parsed CSV fields or
nullif failure
-
close
Close the reader. Should be always called if parsing is finished.- Throws:
IOException- throws if error occurred
-
closeQuietly
public void closeQuietly()Close the reader quietly. The IOException will be catched and if the debug mode is enabled the exeption message is written to the log. -
setTextSeparator
public void setTextSeparator(char textseparator) Sets the text separator char which enclose in CSV a text(default is"). If line was read already a IllegalStateException will be thrown.- Parameters:
textseparator- the text separator char
-
isShowComments
public boolean isShowComments()- Returns:
- true if all comment line are parsed
-
isCommentedOut
Returns true if the passed line is a comment (depends onsetCommentOut(char[])).- Parameters:
line- the passed line- Returns:
- false if line is not a comment line
-
setShowComments
public void setShowComments(boolean showComments) Set to true if all comment line should also parsed.- Parameters:
showComments- default value is false
-
isReading
protected boolean isReading()Checks if reader is already reading from stream.- Returns:
- true if reader is already reading stream
-
setMultiLineMode
public void setMultiLineMode(boolean on) Changes whether or not the reader supports csv lines spread across multiple lines by putting a\(backslash) at the end of each unfinished line.An example:
cell 1 ; cell 2 ; cell 3 starts here \ ...and continues here ... \ ... finally ends here ; cell4
This is read as one single line.
- Parameters:
on- will multi line mode be enabled?
-
isMultiLineMode
public boolean isMultiLineMode()Tells whether or not the reader supports csv lines spread across multiple lines by putting a\(backslash) at the end of each unfinished line.Be default this feature is switched off.
An example:
cell 1 ; cell 2 ; cell 3 starts here \ ...and continues here ... \ ... finally ends here ; cell4
This is read as one single line.
- Returns:
- is multi line mode enabled?
-
setCommentOut
public void setCommentOut(char[] commentOut) Set the characters which indicates a comment line.- Parameters:
commentOut- default characters are '#'
-
setFieldSeparator
public void setFieldSeparator(char[] fieldseparator) Sets the CSV field separator char(s). Default is ';'.- Parameters:
fieldseparator- the char(s)
-
getCommentOut
public char[] getCommentOut()- Returns:
- the comment chars
-
getFieldSeparator
public char[] getFieldSeparator()- Returns:
- the CSV field separator char(s)
-
getTextSeparator
public char getTextSeparator()- Returns:
- the CSV text separator char
-
setLinesToSkip
public void setLinesToSkip(int i) Set the number of real lines which are skipped whenreadNextLine()is called the first time.- Parameters:
i- must be a positive integer value- See Also:
-
getCurrentLineNumber
public int getCurrentLineNumber()- Returns:
- the position of the last line read via
readNextLine()within the input data (file).
-
notifyNextLine
protected void notifyNextLine()Increments the line number and decreases the line skip counter. -
mustSkip
protected boolean mustSkip()Checks if lines have to be skipped still.- Returns:
- true if lines have to be skipped, otherwise false
-
setMaxBufferLines
public void setMaxBufferLines(int number) SetmaxBufferLinesto a new value. Must be larger than 5.- Parameters:
number- the number
-
isFinished
public boolean isFinished()Is reading of input stream finished (has reached end)?- Returns:
- true if input stream has reached end.
-
markFinished
protected void markFinished()Sets the finished flag which indicates the reaching of stream end. -
parse
Convenience method which parses csv lines directly from the given reader.- Parameters:
reader- the reader holding the lines to be read- Returns:
- a array of maps describing one parsed line each
-
parse
public static final Map<Integer,String>[] parse(String lines, char[] fieldSeparator, char textSeparator) Convenience method which parses csv lines directly from the given string.- Parameters:
lines- text which will be readfieldSeparator- e.g. ;textSeparator- e.g. "- Returns:
- a array of maps describing one parsed line each
-
parse
Convenience method which parses csv lines directly from the given string.- Parameters:
lines- text which will be readfieldSeparator- e.g. ;- Returns:
- a array of maps describing one parsed line each
-
parse
Convenience method which parses csv lines directly from the given string.- Parameters:
lines- text which will be read- Returns:
- a array of maps describing one parsed line each
-
applyDecorators
Applies given decorators to columns in given line.- Parameters:
decoratorMap- map containing the decorators mapped to column positionsline- map containing columns of a line- Returns:
- the line after applying given decorators
-