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;

Abstract class handling common concerns of various LineTokenizer implementations such as dealing with names and actual construction of FieldSet

Author(s):
Dave Syer
Robert Kasanicky
Lucas Ward
 
 public abstract class AbstractLineTokenizer implements LineTokenizer {
 
 	protected String[] names = new String[0];
 
 	private boolean strict = true;
 	
 	private String emptyToken = "";
 
Public setter for the strict flag. If true (the default) then number of tokens in line must match the number of tokens defined (by Range, columns, etc.) in LineTokenizer. If false then lines with less tokens will be tolerated and padded with empty columns, and lines with more tokens will simply be truncated.

Parameters:
strict the strict flag to set
 
 	public void setStrict(boolean strict) {
 		this. = strict;
 	}

Provides access to the strict flag for subclasses if needed.

Returns:
the strict flag value
 
 	protected boolean isStrict() {
 		return ;
 	}

Factory for FieldSet instances. Can be injected by clients to customize the default number and date formats.

Parameters:
fieldSetFactory the FieldSetFactory to set
 
 	public void setFieldSetFactory(FieldSetFactory fieldSetFactory) {
 		this. = fieldSetFactory;
 	}

Setter for column names. Optional, but if set, then all lines must have as many or fewer tokens.

Parameters:
names
 
 	public void setNames(String[] names) {
 		this. = names==null ? null : Arrays.asList(names).toArray(new String[names.length]);
 	}

Returns:
true if column names have been specified
See also:
setNames(java.lang.String[])
 
 	public boolean hasNames() {
 		if ( != null && . > 0) {
 			return true;
 		}
 		return false;
 	}

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

Parameters:
line the line to be tokenised (can be null)
Returns:
the resulting tokens
	public FieldSet tokenize(String line) {
		if (line == null) {
			line = "";
		}
		List<Stringtokens = new ArrayList<String>(doTokenize(line));
		// if names are set and strict flag is false
		if ( ( . != 0 ) && ( !  ) ) {
		}
		String[] values = (String[]) tokens.toArray(new String[tokens.size()]);
		if (. == 0) {
			return .create(values);
		}
		else if (values.length != .) {
			throw new IncorrectTokenCountException(.values.length);
		}
		return .create(values);
	}
	protected abstract List<StringdoTokenize(String line);

Adds empty tokens or truncates existing token list to match expected (configured) number of tokens in LineTokenizer.

Parameters:
tokens - list of tokens
	private void adjustTokenCountIfNecessaryList<Stringtokens ) {
		int nameLength = .;
		int tokensSize = tokens.size();
		// if the number of tokens is not what expected
		if ( nameLength != tokensSize ) {
			if ( nameLength > tokensSize ) {
				// add empty tokens until the token list size matches
				// the expected number of tokens
				for ( int i = 0; i < ( nameLength - tokensSize ); i++ ) {
					tokens.add );
				}
else {
				// truncate token list to match the number of expected tokens
				for ( int i = tokensSize - 1; i >= nameLengthi-- ) {
					tokens.remove(i);
				}
			}
		}
	}
New to GrepCode? Check out our FAQ X