IN PROGRESS - BATCH-863: introduce LineMapper interface to encapsulate string-to-item mapping

ResourceLineReader now passes the line to LineMapper
This commit is contained in:
robokaso
2008-10-06 12:18:57 +00:00
parent 97a80463ed
commit 3ab9807775
8 changed files with 173 additions and 156 deletions

View File

@@ -9,9 +9,11 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ReaderNotOpenException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.file.mapping.LineMapper;
import org.springframework.batch.item.file.separator.RecordSeparatorPolicy;
import org.springframework.batch.item.file.separator.SimpleRecordSeparatorPolicy;
import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
@@ -19,12 +21,13 @@ import org.springframework.util.ClassUtils;
/**
* Restartable {@link ItemReader} that reads lines from input
* {@link #setResource(Resource)}. Line is defined by the
* {@link #setRecordSeparatorPolicy(RecordSeparatorPolicy)}.
* {@link #setRecordSeparatorPolicy(RecordSeparatorPolicy)} and mapped to item
* using {@link #setLineMapper(LineMapper)}.
*
* @author Robert Kasanicky
*/
public class ResourceLineReader extends AbstractItemCountingItemStreamItemReader<String> implements
ResourceAwareItemReaderItemStream<String> {
public class ResourceLineReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements
ResourceAwareItemReaderItemStream<T>, InitializingBean {
private static final Log logger = LogFactory.getLog(ResourceLineReader.class);
@@ -44,11 +47,21 @@ public class ResourceLineReader extends AbstractItemCountingItemStreamItemReader
private boolean noInput = false;
private String encoding = DEFAULT_CHARSET;
private LineMapper<T> lineMapper;
public ResourceLineReader() {
setName(ClassUtils.getShortName(FlatFileItemReader.class));
}
/**
* Setter for line mapper. This property is required to be set.
* @param lineMapper maps line to item
*/
public void setLineMapper(LineMapper<T> lineMapper) {
this.lineMapper = lineMapper;
}
/**
* Setter for the encoding for this input source. Default value is
* {@link #DEFAULT_CHARSET}.
@@ -95,7 +108,7 @@ public class ResourceLineReader extends AbstractItemCountingItemStreamItemReader
* multiple lines in file).
*/
@Override
protected String doRead() {
protected T doRead() throws Exception {
if (noInput) {
return null;
}
@@ -106,7 +119,13 @@ public class ResourceLineReader extends AbstractItemCountingItemStreamItemReader
record = recordSeparatorPolicy.preProcess(record) + (line = readLine());
}
}
return recordSeparatorPolicy.postProcess(record);
String logicalLine = recordSeparatorPolicy.postProcess(record);
if (logicalLine == null) {
return null;
}
else {
return lineMapper.mapLine(logicalLine, lineCount);
}
}
/**
@@ -173,4 +192,8 @@ public class ResourceLineReader extends AbstractItemCountingItemStreamItemReader
}
public void afterPropertiesSet() throws Exception {
Assert.notNull(lineMapper, "LineMapper is required");
}
}

View File

@@ -0,0 +1,31 @@
package org.springframework.batch.item.file.mapping;
import org.springframework.batch.item.file.transform.LineTokenizer;
/**
* Two-phase {@link LineMapper} implementation consisting of tokenization of the
* line into {@link FieldSet} followed by mapping to item.
*
* @author Robert Kasanicky
*
* @param <T> type of the item
*/
public class DefaultLineMapper<T> implements LineMapper<T> {
private LineTokenizer tokenizer;
private FieldSetMapper<T> fieldSetMapper;
public T mapLine(String line, int lineNumber) throws Exception {
return fieldSetMapper.process(tokenizer.process(line));
}
public void setTokenizer(LineTokenizer tokenizer) {
this.tokenizer = tokenizer;
}
public void setFieldSetMapper(FieldSetMapper<T> fieldSetMapper) {
this.fieldSetMapper = fieldSetMapper;
}
}

View File

@@ -0,0 +1,13 @@
package org.springframework.batch.item.file.mapping;
/**
* Interface for mapping lines (strings) to domain objects.
*
* @author Robert Kasanicky
*
* @param <T> type of the domain object
*/
public interface LineMapper<T> {
T mapLine(String line, int lineNumber) throws Exception;
}

View File

@@ -0,0 +1,9 @@
package org.springframework.batch.item.file.mapping;
public class PassThroughLineMapper implements LineMapper<String>{
public String mapLine(String line, int lineNumber) throws Exception {
return line;
}
}