@Test public void testEncode() { final CsvContext context = new CsvContext(1, 1, 1); final String empty = ""; assertEquals("", csvEncoder.encode(empty, context, PREFS)); assertEquals("", csvEncoder.encode(empty, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS)); assertEquals("\"\"", csvEncoder.encode(empty, context, ALWAYS_QUOTE_PREFS)); final String space = " "; assertEquals(" ", csvEncoder.encode(space, context, PREFS)); assertEquals("\" \"", csvEncoder.encode(space, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS)); assertEquals("\" \"", csvEncoder.encode(space, context, ALWAYS_QUOTE_PREFS)); final String leadingSpace = " leading space"; assertEquals(leadingSpace, csvEncoder.encode(leadingSpace, context, PREFS)); assertEquals("\" leading space\"", csvEncoder.encode(leadingSpace, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS)); assertEquals("\" leading space\"", csvEncoder.encode(leadingSpace, context, ALWAYS_QUOTE_PREFS)); final String trailingSpace = "trailing space "; assertEquals(trailingSpace, csvEncoder.encode(trailingSpace, context, PREFS)); assertEquals("\"trailing space \"", csvEncoder.encode(trailingSpace, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS)); assertEquals("\"trailing space \"", csvEncoder.encode(trailingSpace, context, ALWAYS_QUOTE_PREFS)); final String normal = "just a normal phrase"; assertEquals(normal, csvEncoder.encode(normal, context, PREFS)); assertEquals(normal, csvEncoder.encode(normal, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS)); assertEquals("\"just a normal phrase\"", csvEncoder.encode(normal, context, ALWAYS_QUOTE_PREFS)); final String comma = "oh look, a comma"; assertEquals("\"oh look, a comma\"", csvEncoder.encode(comma, context, PREFS)); assertEquals("\"oh look, a comma\"", csvEncoder.encode(comma, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS)); assertEquals("\"oh look, a comma\"", csvEncoder.encode(comma, context, ALWAYS_QUOTE_PREFS)); final String quoted = "\"Watch out for quotes\", he said"; assertEquals("\"\"\"Watch out for quotes\"\", he said\"", csvEncoder.encode(quoted, context, PREFS)); assertEquals("\"\"\"Watch out for quotes\"\", he said\"", csvEncoder.encode(quoted, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS)); assertEquals("\"\"\"Watch out for quotes\"\", he said\"", csvEncoder.encode(quoted, context, ALWAYS_QUOTE_PREFS)); final String twoLines = "text that spans\ntwo lines"; int lineNumber = context.getLineNumber(); assertEquals("\"text that spans\r\ntwo lines\"", csvEncoder.encode(twoLines, context, PREFS)); assertEquals(++lineNumber, context.getLineNumber()); assertEquals("\"text that spans\r\ntwo lines\"", csvEncoder.encode(twoLines, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS)); assertEquals(++lineNumber, context.getLineNumber()); assertEquals("\"text that spans\r\ntwo lines\"", csvEncoder.encode(twoLines, context, ALWAYS_QUOTE_PREFS)); assertEquals(++lineNumber, context.getLineNumber()); final String quotesTwoLines = "text \"with quotes\" that spans\ntwo lines"; assertEquals("\"text \"\"with quotes\"\" that spans\r\ntwo lines\"", csvEncoder.encode(quotesTwoLines, context, PREFS)); assertEquals(++lineNumber, context.getLineNumber()); assertEquals("\"text \"\"with quotes\"\" that spans\r\ntwo lines\"", csvEncoder.encode(quotesTwoLines, context, SURROUNDING_SPACES_REQUIRE_QUOTES_PREFS)); assertEquals(++lineNumber, context.getLineNumber()); assertEquals("\"text \"\"with quotes\"\" that spans\r\ntwo lines\"", csvEncoder.encode(quotesTwoLines, context, ALWAYS_QUOTE_PREFS)); assertEquals(++lineNumber, context.getLineNumber()); }
public String encode(final String input, final CsvContext context, final CsvPreference preference) { final StringBuilder currentColumn = new StringBuilder(); final int delimiter = preference.getDelimiterChar(); final char quote = (char) preference.getQuoteChar(); final char quoteEscapeChar = (char) preference.getQuoteEscapeChar(); final String eolSymbols = preference.getEndOfLineSymbols(); final int lastCharIndex = input.length() - 1; boolean quotesRequiredForSpecialChar = false; boolean skipNewline = false; for( int i = 0; i <= lastCharIndex; i++ ) { final char c = input.charAt(i); if( skipNewline ) { skipNewline = false; if( c == '\n' ) { continue; } } if( c == delimiter ) { quotesRequiredForSpecialChar = true; currentColumn.append(c); } else if( c == quote ) { quotesRequiredForSpecialChar = true; currentColumn.append(quoteEscapeChar); currentColumn.append(quote); } else if( c == '\r' ) { quotesRequiredForSpecialChar = true; currentColumn.append(eolSymbols); context.setLineNumber(context.getLineNumber() + 1); skipNewline = true; } else if( c == '\n' ) { quotesRequiredForSpecialChar = true; currentColumn.append(eolSymbols); context.setLineNumber(context.getLineNumber() + 1); } else { currentColumn.append(c); } } final boolean quotesRequiredForMode = preference.getQuoteMode().quotesRequired(input, context, preference); final boolean quotesRequiredForSurroundingSpaces = preference.isSurroundingSpacesNeedQuotes() && input.length() > 0 && (input.charAt(0) == ' ' || input.charAt(input.length() - 1) == ' '); if( quotesRequiredForSpecialChar || quotesRequiredForMode || quotesRequiredForSurroundingSpaces ) { currentColumn.insert(0, quote).append(quote); } return currentColumn.toString(); }
DefaultCsvEncoder implements CsvEncoder { public String encode(final String input, final CsvContext context, final CsvPreference preference) { final StringBuilder currentColumn = new StringBuilder(); final int delimiter = preference.getDelimiterChar(); final char quote = (char) preference.getQuoteChar(); final char quoteEscapeChar = (char) preference.getQuoteEscapeChar(); final String eolSymbols = preference.getEndOfLineSymbols(); final int lastCharIndex = input.length() - 1; boolean quotesRequiredForSpecialChar = false; boolean skipNewline = false; for( int i = 0; i <= lastCharIndex; i++ ) { final char c = input.charAt(i); if( skipNewline ) { skipNewline = false; if( c == '\n' ) { continue; } } if( c == delimiter ) { quotesRequiredForSpecialChar = true; currentColumn.append(c); } else if( c == quote ) { quotesRequiredForSpecialChar = true; currentColumn.append(quoteEscapeChar); currentColumn.append(quote); } else if( c == '\r' ) { quotesRequiredForSpecialChar = true; currentColumn.append(eolSymbols); context.setLineNumber(context.getLineNumber() + 1); skipNewline = true; } else if( c == '\n' ) { quotesRequiredForSpecialChar = true; currentColumn.append(eolSymbols); context.setLineNumber(context.getLineNumber() + 1); } else { currentColumn.append(c); } } final boolean quotesRequiredForMode = preference.getQuoteMode().quotesRequired(input, context, preference); final boolean quotesRequiredForSurroundingSpaces = preference.isSurroundingSpacesNeedQuotes() && input.length() > 0 && (input.charAt(0) == ' ' || input.charAt(input.length() - 1) == ' '); if( quotesRequiredForSpecialChar || quotesRequiredForMode || quotesRequiredForSurroundingSpaces ) { currentColumn.insert(0, quote).append(quote); } return currentColumn.toString(); } }
DefaultCsvEncoder implements CsvEncoder { public String encode(final String input, final CsvContext context, final CsvPreference preference) { final StringBuilder currentColumn = new StringBuilder(); final int delimiter = preference.getDelimiterChar(); final char quote = (char) preference.getQuoteChar(); final char quoteEscapeChar = (char) preference.getQuoteEscapeChar(); final String eolSymbols = preference.getEndOfLineSymbols(); final int lastCharIndex = input.length() - 1; boolean quotesRequiredForSpecialChar = false; boolean skipNewline = false; for( int i = 0; i <= lastCharIndex; i++ ) { final char c = input.charAt(i); if( skipNewline ) { skipNewline = false; if( c == '\n' ) { continue; } } if( c == delimiter ) { quotesRequiredForSpecialChar = true; currentColumn.append(c); } else if( c == quote ) { quotesRequiredForSpecialChar = true; currentColumn.append(quoteEscapeChar); currentColumn.append(quote); } else if( c == '\r' ) { quotesRequiredForSpecialChar = true; currentColumn.append(eolSymbols); context.setLineNumber(context.getLineNumber() + 1); skipNewline = true; } else if( c == '\n' ) { quotesRequiredForSpecialChar = true; currentColumn.append(eolSymbols); context.setLineNumber(context.getLineNumber() + 1); } else { currentColumn.append(c); } } final boolean quotesRequiredForMode = preference.getQuoteMode().quotesRequired(input, context, preference); final boolean quotesRequiredForSurroundingSpaces = preference.isSurroundingSpacesNeedQuotes() && input.length() > 0 && (input.charAt(0) == ' ' || input.charAt(input.length() - 1) == ' '); if( quotesRequiredForSpecialChar || quotesRequiredForMode || quotesRequiredForSurroundingSpaces ) { currentColumn.insert(0, quote).append(quote); } return currentColumn.toString(); } DefaultCsvEncoder(); }
DefaultCsvEncoder implements CsvEncoder { public String encode(final String input, final CsvContext context, final CsvPreference preference) { final StringBuilder currentColumn = new StringBuilder(); final int delimiter = preference.getDelimiterChar(); final char quote = (char) preference.getQuoteChar(); final char quoteEscapeChar = (char) preference.getQuoteEscapeChar(); final String eolSymbols = preference.getEndOfLineSymbols(); final int lastCharIndex = input.length() - 1; boolean quotesRequiredForSpecialChar = false; boolean skipNewline = false; for( int i = 0; i <= lastCharIndex; i++ ) { final char c = input.charAt(i); if( skipNewline ) { skipNewline = false; if( c == '\n' ) { continue; } } if( c == delimiter ) { quotesRequiredForSpecialChar = true; currentColumn.append(c); } else if( c == quote ) { quotesRequiredForSpecialChar = true; currentColumn.append(quoteEscapeChar); currentColumn.append(quote); } else if( c == '\r' ) { quotesRequiredForSpecialChar = true; currentColumn.append(eolSymbols); context.setLineNumber(context.getLineNumber() + 1); skipNewline = true; } else if( c == '\n' ) { quotesRequiredForSpecialChar = true; currentColumn.append(eolSymbols); context.setLineNumber(context.getLineNumber() + 1); } else { currentColumn.append(c); } } final boolean quotesRequiredForMode = preference.getQuoteMode().quotesRequired(input, context, preference); final boolean quotesRequiredForSurroundingSpaces = preference.isSurroundingSpacesNeedQuotes() && input.length() > 0 && (input.charAt(0) == ' ' || input.charAt(input.length() - 1) == ' '); if( quotesRequiredForSpecialChar || quotesRequiredForMode || quotesRequiredForSurroundingSpaces ) { currentColumn.insert(0, quote).append(quote); } return currentColumn.toString(); } DefaultCsvEncoder(); String encode(final String input, final CsvContext context, final CsvPreference preference); }
DefaultCsvEncoder implements CsvEncoder { public String encode(final String input, final CsvContext context, final CsvPreference preference) { final StringBuilder currentColumn = new StringBuilder(); final int delimiter = preference.getDelimiterChar(); final char quote = (char) preference.getQuoteChar(); final char quoteEscapeChar = (char) preference.getQuoteEscapeChar(); final String eolSymbols = preference.getEndOfLineSymbols(); final int lastCharIndex = input.length() - 1; boolean quotesRequiredForSpecialChar = false; boolean skipNewline = false; for( int i = 0; i <= lastCharIndex; i++ ) { final char c = input.charAt(i); if( skipNewline ) { skipNewline = false; if( c == '\n' ) { continue; } } if( c == delimiter ) { quotesRequiredForSpecialChar = true; currentColumn.append(c); } else if( c == quote ) { quotesRequiredForSpecialChar = true; currentColumn.append(quoteEscapeChar); currentColumn.append(quote); } else if( c == '\r' ) { quotesRequiredForSpecialChar = true; currentColumn.append(eolSymbols); context.setLineNumber(context.getLineNumber() + 1); skipNewline = true; } else if( c == '\n' ) { quotesRequiredForSpecialChar = true; currentColumn.append(eolSymbols); context.setLineNumber(context.getLineNumber() + 1); } else { currentColumn.append(c); } } final boolean quotesRequiredForMode = preference.getQuoteMode().quotesRequired(input, context, preference); final boolean quotesRequiredForSurroundingSpaces = preference.isSurroundingSpacesNeedQuotes() && input.length() > 0 && (input.charAt(0) == ' ' || input.charAt(input.length() - 1) == ' '); if( quotesRequiredForSpecialChar || quotesRequiredForMode || quotesRequiredForSurroundingSpaces ) { currentColumn.insert(0, quote).append(quote); } return currentColumn.toString(); } DefaultCsvEncoder(); String encode(final String input, final CsvContext context, final CsvPreference preference);  }