Class CSVUtils
- java.lang.Object
-
- de.hybris.platform.util.CSVUtils
-
public final class CSVUtils extends java.lang.Object
Utility class for parsing CSV text.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
buildCsvLine(java.util.Map fields, char fieldSeparator, char textSeparator, java.lang.String lineBreak)
static boolean
escapeString(java.lang.StringBuilder unescaped, char[] toEscape, char[] specialChars, boolean doubleCharacter)
It escapes the passedtoEscape
char(s) within the given buffer.static boolean
escapeString(java.lang.StringBuilder unescaped, java.lang.String[] toEscape, java.lang.String[] specialChars, boolean doubleToEscape)
It escapes the passedtoEscape
string(s) within the given buffer.static java.lang.String
escapeString(java.lang.String unescaped, char[] toEscape, boolean doubleCharacter)
Escapes the passedtoEscape
char(s) in the passed String.static boolean
isSeparator(char[] tokenisers, char actual)
Returns false is the actual char is not a separator.static java.lang.String
joinAndEscape(java.util.List<java.lang.String> cells, char[] toEscape, char delimiter, boolean escapeByDoubling)
Escapes the given list of text fields and merges them to one single line using given delimiter.static boolean
lineStartsWith(java.lang.String line, char[] prefixes, java.lang.String appender)
Checks if given line starts with at least one of the given prefixes followed by given appender text.static java.util.List<java.lang.String>
splitAndUnescape(java.lang.String line, char[] delimiters, boolean escapedByDoubling)
Splits a given line in fields separated by given delimiters and unescapes the fields with usage of {unescapeString(String, char[], boolean)
.static java.lang.String
unescapeString(java.lang.String escaped, char[] toUnEscape, boolean doubleCharacter)
Unescapes the passedtoUnEscape
char(s) in the passed String.
-
-
-
Method Detail
-
unescapeString
public static java.lang.String unescapeString(java.lang.String escaped, char[] toUnEscape, boolean doubleCharacter)
Unescapes the passedtoUnEscape
char(s) in the passed String. IfdoubleCharacter
is set to true all double occurrence of the char(s) intoUnEscape
will be reduced to one. Else all'\'
before the chars intoUnEscape
are removed.This method undo all changes to a String which are done by
escapeString(String, char[], boolean)
escapeString( unEscapeString( String, char[], boolean ), char[], boolean )
== unEscapeString( escapeString( String, char[], boolean ), char[], boolean )
== String
for the same parameters (== means here equal strings)- Parameters:
escaped
- the escaped stringtoUnEscape
- the chars which are to unescapeddoubleCharacter
- false: '\' is removed if char occurs in char[]; true: double occurrence of char from char[] is reduced to one- Returns:
- the unescaped string
-
escapeString
public static java.lang.String escapeString(java.lang.String unescaped, char[] toEscape, boolean doubleCharacter)
Escapes the passedtoEscape
char(s) in the passed String. IfdoubleCharacter
is set totrue
all foundtoEscape
char(s) are doubled, else the found characters will be escaped with'\'
.Example:
textseparator chars are: '"', ';'
a,";"bc -> a,"";;""bc doubleCharacter=true
a,";"bc -> a,\"\;\"bc doubleCharacter=false
If you furthermore want to wrap the string only on demand dependent of occurrence of special characters see
escapeString(StringBuilder, char[], char[], boolean)
.- Parameters:
unescaped
- the unescaped stringtoEscape
- the char(s) to be escapeddoubleCharacter
- if set tofalse
all found characters are escaped with'\'
- Returns:
- the escaped string
-
escapeString
public static boolean escapeString(java.lang.StringBuilder unescaped, char[] toEscape, char[] specialChars, boolean doubleCharacter)
It escapes the passedtoEscape
char(s) within the given buffer. IfdoubleCharacter
is set totrue
all foundtoEscape
char(s) are doubled, else the found characters will be escaped with '\'.Difference to
escapeString(String, char[], boolean)
is, that it returns an boolean to indicate whether special characters occur in buffer. So you can decide dependent on the return value whether you wrap the buffer with text separators or not.- Parameters:
unescaped
- the field contenttoEscape
- the char(s) to be escapedspecialChars
- char(s) checked for occurrence in given stringdoubleCharacter
- if set tofalse
all found characters are escaped with'\'
- Returns:
- true, if at least one special character occurs in buffer, false otherwise
- See Also:
escapeString(String, char[], boolean)
-
escapeString
public static boolean escapeString(java.lang.StringBuilder unescaped, java.lang.String[] toEscape, java.lang.String[] specialChars, boolean doubleToEscape)
It escapes the passedtoEscape
string(s) within the given buffer. IfdoubleToEscape
is set totrue
all foundtoEscape
string(s) are doubled, else the found strings will be escaped with '\'.Difference to
escapeString(String, char[], boolean)
is, that it returns an boolean to indicate whether special strings occur in buffer. So you can decide dependent on the return value whether you wrap the buffer with text separators or not.- Parameters:
unescaped
- the field contenttoEscape
- the string(s) to be escapedspecialChars
- string(s) checked for occurrence in given stringdoubleToEscape
- if set tofalse
all found strings are escaped with'\'
- Returns:
- true, if at least one special string occurs in buffer, false otherwise
- See Also:
escapeString(String, char[], boolean)
-
isSeparator
public static boolean isSeparator(char[] tokenisers, char actual)
Returns false is the actual char is not a separator.- Parameters:
tokenisers
- an array of chars which are separatorsactual
- the actual char- Returns:
- true if actual is equal to a char from
tokenisers
-
splitAndUnescape
public static java.util.List<java.lang.String> splitAndUnescape(java.lang.String line, char[] delimiters, boolean escapedByDoubling)
Splits a given line in fields separated by given delimiters and unescapes the fields with usage of {unescapeString(String, char[], boolean)
.- Parameters:
line
- text to split and unescapedelimiters
- the chars used for splitting and which have to be unescapedescapedByDoubling
- if false '\' is removed if char occurs in given delimiters ,if true double occurrence of char from delimiters is reduced to one- Returns:
- list containing splittet and unescaped fields
- See Also:
unescapeString(String, char[], boolean)
-
joinAndEscape
public static java.lang.String joinAndEscape(java.util.List<java.lang.String> cells, char[] toEscape, char delimiter, boolean escapeByDoubling)
Escapes the given list of text fields and merges them to one single line using given delimiter.- Parameters:
cells
- list of text fields for escaping and mergingtoEscape
- char(s) which have to be escaped within fieldsdelimiter
- delimiter char for merging the fields to one lineescapeByDoubling
- if set tofalse
all found characters are escaped with'\'
- Returns:
- the merged line
-
lineStartsWith
public static boolean lineStartsWith(java.lang.String line, char[] prefixes, java.lang.String appender)
Checks if given line starts with at least one of the given prefixes followed by given appender text.- Parameters:
line
- line to checkprefixes
- list of line prefixes where the line has to start withappender
- text which has to follow a found line prefix (can be null)- Returns:
- line starts with one of the given prefixes followed by given appender text
-
buildCsvLine
public static java.lang.String buildCsvLine(java.util.Map fields, char fieldSeparator, char textSeparator, java.lang.String lineBreak)
-
-