Java Code Examples for org.supercsv.prefs.CsvPreference#getDelimiterChar()

The following examples show how to use org.supercsv.prefs.CsvPreference#getDelimiterChar() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Tokenizer.java    From super-csv with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a new <tt>Tokenizer</tt>, which reads the CSV file, line by line.
 * 
 * @param reader
 *            the reader
 * @param preferences
 *            the CSV preferences
 * @throws NullPointerException
 *             if reader or preferences are null
 */
public Tokenizer(final Reader reader, final CsvPreference preferences) {
	super(reader, preferences);
	this.quoteChar = preferences.getQuoteChar();
	this.delimiterChar = preferences.getDelimiterChar();
	this.surroundingSpacesNeedQuotes = preferences.isSurroundingSpacesNeedQuotes();
	this.ignoreEmptyLines = preferences.isIgnoreEmptyLines();
	this.commentMatcher = preferences.getCommentMatcher();
	this.maxLinesPerRow = preferences.getMaxLinesPerRow();
	this.emptyColumnParsing = preferences.getEmptyColumnParsing();
	this.quoteEscapeChar = preferences.getQuoteEscapeChar();
}
 
Example 2
Source File: QuoteTrackingTokenizer.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Constructs a new <tt>Tokenizer</tt>, which reads the CSV file, line by line.
 *
 * @param reader      the reader
 * @param preferences the CSV preferences
 * @throws NullPointerException if reader or preferences is null
 */
public QuoteTrackingTokenizer(final Reader reader,final CsvPreference preferences){
    super(reader,preferences);
    this.quoteChar=preferences.getQuoteChar();
    this.delimeterChar=preferences.getDelimiterChar();
    this.surroundingSpacesNeedQuotes=preferences.isSurroundingSpacesNeedQuotes();
    this.ignoreEmptyLines=preferences.isIgnoreEmptyLines();
    this.commentMatcher=preferences.getCommentMatcher();
    this.maxLinesPerRow=preferences.getMaxLinesPerRow();
}
 
Example 3
Source File: DefaultCsvEncoder.java    From super-csv with Apache License 2.0 4 votes vote down vote up
/**
 * {@inheritDoc}
 */
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; // newline following a carriage return is skipped
			}
		}
		
		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();
}