Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 2006-2007 the original author or authors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.springframework.batch.item.file.transform;
 
 import java.util.List;
 
A LineTokenizer implementation that splits the input String on a configurable delimiter. This implementation also supports the use of an escape character to escape delimiters and line endings.

Author(s):
Rob Harrop
Dave Syer
 
 public class DelimitedLineTokenizer extends AbstractLineTokenizer {
Convenient constant for the common case of a tab delimiter.
 
 	public static final char DELIMITER_TAB = '\t';

Convenient constant for the common case of a comma delimiter.
 
 	public static final char DELIMITER_COMMA = ',';

Convenient constant for the common case of a " character used to escape delimiters or line endings.
 
 	public static final char DEFAULT_QUOTE_CHARACTER = '"';
 
 	// the delimiter character used when reading input.
 	private char delimiter;
 
 
 	private String quoteString;

Create a new instance of the DelimitedLineTokenizer class for the common case where the delimiter is a comma.

 
 	public DelimitedLineTokenizer() {
 	}

Create a new instance of the DelimitedLineTokenizer class.

Parameters:
delimiter the desired delimiter
 
 	public DelimitedLineTokenizer(char delimiter) {
 		Assert.state(delimiter != "[" + 
 		        + "] is not allowed as delimiter for tokenizers.");
 
 		this. = delimiter;
 	}

Setter for the delimiter character.

Parameters:
delimiter
 
 	public void setDelimiter(char delimiter) {
 		this. = delimiter;
 	}

Public setter for the quoteCharacter. The quote character can be used to extend a field across line endings or to enclose a String which contains the delimiter. Inside a quoted token the quote character can be used to escape itself, thus "a""b""c" is tokenized to a"b"c.

Parameters:
quoteCharacter the quoteCharacter to set
See also:
DEFAULT_QUOTE_CHARACTER
 
 	public final void setQuoteCharacter(char quoteCharacter) {
		this. = quoteCharacter;
		this. = "" + quoteCharacter;
	}

Yields the tokens resulting from the splitting of the supplied line.

Parameters:
line the line to be tokenized
Returns:
the resulting tokens
	protected List<StringdoTokenize(String line) {
		List<Stringtokens = new ArrayList<String>();
		// line is never null in current implementation
		// line is checked in parent: AbstractLineTokenizer.tokenize()
		char[] chars = line.toCharArray();
		boolean inQuoted = false;
		int lastCut = 0;
		int length = chars.length;
		for (int i = 0; i < lengthi++) {
			char currentChar = chars[i];
			boolean isEnd = (i == (length - 1));
			if ((isDelimiterCharacter(currentChar) && !inQuoted) || isEnd) {
				int endPosition = (isEnd ? (length - lastCut) : (i - lastCut));
				if (isEnd && isDelimiterCharacter(currentChar)) {
					endPosition--;
				}
				String value = null;
				value = maybeStripQuotes(new String(charslastCutendPosition));
				tokens.add(value);
				if (isEnd && (isDelimiterCharacter(currentChar))) {
					tokens.add("");
				}
				lastCut = i + 1;
else if (isQuoteCharacter(currentChar)) {
				inQuoted = !inQuoted;
			}
		}
		return tokens;
	}

If the string is quoted strip (possibly with whitespace outside the quotes (which will be stripped), replace escaped quotes inside the string. Quotes are escaped with double instances of the quote character.

Parameters:
string
Returns:
the same string but stripped and unescaped if necessary
	private String maybeStripQuotes(String string) {
		String value = string.trim();
		if (isQuoted(value)) {
			value = StringUtils.replace(value"" +  + "" + );
			int endLength = value.length() - 1;
			// used to deal with empty quoted values
			if (endLength == 0) {
				endLength = 1;
			}
			value = value.substring(1, endLength);
		}
		return value;
	}

Is this string surrounded by quote characters?

Parameters:
value
Returns:
true if the value starts and ends with the quoteCharacter
	private boolean isQuoted(String value) {
		if (value.startsWith() && value.endsWith()) {
			return true;
		}
		return false;
	}

Is the supplied character the delimiter character?

Parameters:
c the character to be checked
Returns:
true if the supplied character is the delimiter character
See also:
DelimitedLineTokenizer.DelimitedLineTokenizer(char)
	private boolean isDelimiterCharacter(char c) {
		return c == this.;
	}

Is the supplied character a quote character?

Parameters:
c the character to be checked
Returns:
true if the supplied character is an quote character
See also:
setQuoteCharacter(char)
	protected boolean isQuoteCharacter(char c) {
		return c == ;
	}
New to GrepCode? Check out our FAQ X