Results 1 to 6 of 6

Thread: FlatFileItemReader with optional data

  1. #1
    Join Date
    Aug 2008
    Location
    North Carolina
    Posts
    5

    Default FlatFileItemReader with optional data

    I've implemented a FlatFileItemReader that needs to read a file which contains optional data. For example, my data file contains firstName, middleInitial and lastName. A value for middleInitial is not always present. When I call fieldset.readChar("middleInitial") and a value is , I get this error: java.lang.IllegalArgumentException: Cannot convert field value '' to char.

    What's the best way to handle 'optional data' in a fixed length file?

  2. #2

    Default

    Couldn't you override the default FixedLengthTokenizer.doTokenize(String line).

    Also make sure to setStrict to false on your FixedLengthTokenizer.
    FixedLengthTokenizer.setStrict(false)

    Jeff

  3. #3
    Join Date
    Aug 2008
    Location
    North Carolina
    Posts
    5

    Default

    Quote Originally Posted by visualjeff View Post
    Couldn't you override the default FixedLengthTokenizer.doTokenize(String line).

    Also make sure to setStrict to false on your FixedLengthTokenizer.
    FixedLengthTokenizer.setStrict(false)

    Jeff
    Overriding doTokenize(String line) won't help me. The returned String[] from this method contains the proper values with my optional data element being an empty String. (as is should - no value exists in the file). The problem for me, lies in the FieldSet interface and the method readChar(int idx).

    Here's the implementation I'm using from org.springframework.batch.item.file.transform.Defa ultFieldSet

    public char readChar(int index) {
    String value = readAndTrim(index);
    Assert.isTrue(value.length() == 1, "Cannot convert field value '" + value + "' to char.");
    return value.charAt(0);
    }

    I guess I need to override this method but can't figure out how to inject it in my FieldSetMapper implementation. Thoughts???

  4. #4
    Join Date
    Aug 2008
    Location
    North Carolina
    Posts
    5

    Default

    The solution I came up with was:
    Change my domain object instance variable from Character to String
    and called fieldSet.readRawString(int idx) in my FieldSetMapper implementation.

    readRawString doesn't trim the String value.

  5. #5

    Default

    Very good. That solution should do the trick.

    Jeff

  6. #6
    Join Date
    Jun 2007
    Location
    Minsk, Belarus
    Posts
    217

    Default

    Quote Originally Posted by jpmango View Post
    I guess I need to override this method but can't figure out how to inject it in my FieldSetMapper implementation. Thoughts???
    It is also possible to implement custom FieldSetFactory, which will return extended DefaultFieldSet.

    Code:
    	<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
    		<property name="lineMapper">
    			<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
    				<property name="lineTokenizer">
    					<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
    						<property name="fieldSetFactory">
    							<bean class="org.springframework.batch.item.file.transform.DefaultFieldSetFactory" />
    						</property>
    						...
    					</bean>
    				</property>
    				...
    			</bean>
    		</property>
    		...
    	</bean>

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •