StringUtil { public static CharSequence escapeCsv(CharSequence value) { int length = checkNotNull(value, "value").length(); if (length == 0) { return value; } int last = length - 1; boolean quoted = isDoubleQuote(value.charAt(0)) && isDoubleQuote(value.charAt(last)) && length != 1; boolean foundSpecialCharacter = false; boolean escapedDoubleQuote = false; StringBuilder escaped = new StringBuilder(length + CSV_NUMBER_ESCAPE_CHARACTERS).append(DOUBLE_QUOTE); for (int i = 0; i < length; i++) { char current = value.charAt(i); switch (current) { case DOUBLE_QUOTE: if (i == 0 || i == last) { if (!quoted) { escaped.append(DOUBLE_QUOTE); } else { continue; } } else { boolean isNextCharDoubleQuote = isDoubleQuote(value.charAt(i + 1)); if (!isDoubleQuote(value.charAt(i - 1)) && (!isNextCharDoubleQuote || i + 1 == last)) { escaped.append(DOUBLE_QUOTE); escapedDoubleQuote = true; } break; } case LINE_FEED: case CARRIAGE_RETURN: case COMMA: foundSpecialCharacter = true; } escaped.append(current); } return escapedDoubleQuote || foundSpecialCharacter && !quoted ? escaped.append(DOUBLE_QUOTE) : value; } private  StringUtil(); static String[] split(String value, char delim); static String[] split(String value, char delim, int maxParts); static String substringAfter(String value, char delim); static boolean commonSuffixOfLength(String s, String p, int len); static String byteToHexStringPadded(int value); static T byteToHexStringPadded(T buf, int value); static String toHexStringPadded(byte[] src); static String toHexStringPadded(byte[] src, int offset, int length); static T toHexStringPadded(T dst, byte[] src); static T toHexStringPadded(T dst, byte[] src, int offset, int length); static String byteToHexString(int value); static T byteToHexString(T buf, int value); static String toHexString(byte[] src); static String toHexString(byte[] src, int offset, int length); static T toHexString(T dst, byte[] src); static T toHexString(T dst, byte[] src, int offset, int length); static String simpleClassName(Object o); static String simpleClassName(Class<?> clazz); static CharSequence escapeCsv(CharSequence value); static int length(String s); static boolean isNullOrEmpty(String s); static boolean isSurrogate(char c); static final String EMPTY_STRING; static final String NEWLINE; static final char DOUBLE_QUOTE; static final char COMMA; static final char LINE_FEED; static final char CARRIAGE_RETURN; static final char TAB; }
@Test (expected = NullPointerException.class) public void escapeCsvNull() { StringUtil.escapeCsv(null); }
@Test public void escapeCsvEmpty() { CharSequence value = ""; CharSequence expected = value; escapeCsv(value, expected); }
@Test public void escapeCsvUnquoted() { CharSequence value = "something"; CharSequence expected = value; escapeCsv(value, expected); }
@Test public void escapeCsvAlreadyQuoted() { CharSequence value = "\"something\""; CharSequence expected = "\"something\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuote() { CharSequence value = "s\""; CharSequence expected = "\"s\"\"\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuoteInMiddle() { CharSequence value = "some text\"and more text"; CharSequence expected = "\"some text\"\"and more text\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuoteInMiddleAlreadyQuoted() { CharSequence value = "\"some text\"and more text\""; CharSequence expected = "\"some text\"\"and more text\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuotedWords() { CharSequence value = "\"foo\"\"goo\""; CharSequence expected = "\"foo\"\"goo\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithAlreadyEscapedQuote() { CharSequence value = "foo\"\"goo"; CharSequence expected = "foo\"\"goo"; escapeCsv(value, expected); }
@Test public void escapeCsvEndingWithQuote() { CharSequence value = "some\""; CharSequence expected = "\"some\"\"\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithSingleQuote() { CharSequence value = "\""; CharSequence expected = "\"\"\"\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithSingleQuoteAndCharacter() { CharSequence value = "\"f"; CharSequence expected = "\"\"\"f\""; escapeCsv(value, expected); }
@Test public void escapeCsvAlreadyEscapedQuote() { CharSequence value = "\"some\"\""; CharSequence expected = "\"some\"\"\""; escapeCsv(value, expected); }
@Test public void escapeCsvQuoted() { CharSequence value = "\"foo,goo\""; CharSequence expected = value; escapeCsv(value, expected); }
@Test public void escapeCsvWithLineFeed() { CharSequence value = "some text\n more text"; CharSequence expected = "\"some text\n more text\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithSingleLineFeedCharacter() { CharSequence value = "\n"; CharSequence expected = "\"\n\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithMultipleLineFeedCharacter() { CharSequence value = "\n\n"; CharSequence expected = "\"\n\n\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuotedAndLineFeedCharacter() { CharSequence value = " \" \n "; CharSequence expected = "\" \"\" \n \""; escapeCsv(value, expected); }
@Test public void escapeCsvWithLineFeedAtEnd() { CharSequence value = "testing\n"; CharSequence expected = "\"testing\n\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithComma() { CharSequence value = "test,ing"; CharSequence expected = "\"test,ing\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithSingleComma() { CharSequence value = ","; CharSequence expected = "\",\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithSingleCarriageReturn() { CharSequence value = "\r"; CharSequence expected = "\"\r\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithMultipleCarriageReturn() { CharSequence value = "\r\r"; CharSequence expected = "\"\r\r\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithCarriageReturn() { CharSequence value = "some text\r more text"; CharSequence expected = "\"some text\r more text\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithQuotedAndCarriageReturnCharacter() { CharSequence value = "\"\r"; CharSequence expected = "\"\"\"\r\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithCarriageReturnAtEnd() { CharSequence value = "testing\r"; CharSequence expected = "\"testing\r\""; escapeCsv(value, expected); }
@Test public void escapeCsvWithCRLFCharacter() { CharSequence value = "\r\n"; CharSequence expected = "\"\r\n\""; escapeCsv(value, expected); }