001/*
002 * Copyright 2006-2007 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.batch.item.file.mapping;
018
019import org.springframework.batch.item.file.LineMapper;
020import org.springframework.batch.item.file.transform.FieldSet;
021import org.springframework.batch.item.file.transform.LineTokenizer;
022import org.springframework.beans.factory.InitializingBean;
023import org.springframework.util.Assert;
024
025/**
026 * Two-phase {@link LineMapper} implementation consisting of tokenization of the line into {@link FieldSet} followed by
027 * mapping to item. If finer grained control of exceptions is needed, the {@link LineMapper} interface should be
028 * implemented directly.
029 * 
030 * @author Robert Kasanicky
031 * @author Lucas Ward
032 * 
033 * @param <T> type of the item
034 */
035public class DefaultLineMapper<T> implements LineMapper<T>, InitializingBean {
036
037        private LineTokenizer tokenizer;
038
039        private FieldSetMapper<T> fieldSetMapper;
040
041    @Override
042        public T mapLine(String line, int lineNumber) throws Exception {
043                return fieldSetMapper.mapFieldSet(tokenizer.tokenize(line));
044        }
045
046        public void setLineTokenizer(LineTokenizer tokenizer) {
047                this.tokenizer = tokenizer;
048        }
049
050        public void setFieldSetMapper(FieldSetMapper<T> fieldSetMapper) {
051                this.fieldSetMapper = fieldSetMapper;
052        }
053
054    @Override
055        public void afterPropertiesSet() {
056                Assert.notNull(tokenizer, "The LineTokenizer must be set");
057                Assert.notNull(fieldSetMapper, "The FieldSetMapper must be set");
058        }
059
060}