Class CSVUtils

java.lang.Object
de.hybris.platform.util.CSVUtils

public final class CSVUtils extends Object
Utility class for parsing CSV text.
  • Method Details

    • unescapeString

      public static String unescapeString(String escaped, char[] toUnEscape, boolean doubleCharacter)
      Unescapes the passed toUnEscape char(s) in the passed String. If doubleCharacter is set to true all double occurrence of the char(s) in toUnEscape will be reduced to one. Else all '\' before the chars in toUnEscape 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 string
      toUnEscape - the chars which are to unescaped
      doubleCharacter - 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 String escapeString(String unescaped, char[] toEscape, boolean doubleCharacter)
      Escapes the passed toEscape char(s) in the passed String. If doubleCharacter is set to true all found toEscape 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 string
      toEscape - the char(s) to be escaped
      doubleCharacter - if set to false all found characters are escaped with '\'
      Returns:
      the escaped string
    • escapeString

      public static boolean escapeString(StringBuilder unescaped, char[] toEscape, char[] specialChars, boolean doubleCharacter)
      It escapes the passed toEscape char(s) within the given buffer. If doubleCharacter is set to true all found toEscape 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 content
      toEscape - the char(s) to be escaped
      specialChars - char(s) checked for occurrence in given string
      doubleCharacter - if set to false all found characters are escaped with '\'
      Returns:
      true, if at least one special character occurs in buffer, false otherwise
      See Also:
    • escapeString

      public static boolean escapeString(StringBuilder unescaped, String[] toEscape, String[] specialChars, boolean doubleToEscape)
      It escapes the passed toEscape string(s) within the given buffer. If doubleToEscape is set to true all found toEscape 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 content
      toEscape - the string(s) to be escaped
      specialChars - string(s) checked for occurrence in given string
      doubleToEscape - if set to false all found strings are escaped with '\'
      Returns:
      true, if at least one special string occurs in buffer, false otherwise
      See Also:
    • 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 separators
      actual - the actual char
      Returns:
      true if actual is equal to a char from tokenisers
    • splitAndUnescape

      public static List<String> splitAndUnescape(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 unescape
      delimiters - the chars used for splitting and which have to be unescaped
      escapedByDoubling - 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:
    • joinAndEscape

      public static String joinAndEscape(List<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 merging
      toEscape - char(s) which have to be escaped within fields
      delimiter - delimiter char for merging the fields to one line
      escapeByDoubling - if set to false all found characters are escaped with '\'
      Returns:
      the merged line
    • lineStartsWith

      public static boolean lineStartsWith(String line, char[] prefixes, String appender)
      Checks if given line starts with at least one of the given prefixes followed by given appender text.
      Parameters:
      line - line to check
      prefixes - list of line prefixes where the line has to start with
      appender - 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 String buildCsvLine(Map fields, char fieldSeparator, char textSeparator, String lineBreak)