1 /*
2 * Copyright 2006-2007 the original author or authors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.springframework.batch.item.file.mapping;
18
19 import java.util.Map;
20
21 import org.springframework.batch.item.file.LineMapper;
22 import org.springframework.batch.item.file.transform.LineTokenizer;
23 import org.springframework.batch.item.file.transform.PatternMatchingCompositeLineTokenizer;
24 import org.springframework.batch.support.PatternMatcher;
25 import org.springframework.beans.factory.InitializingBean;
26 import org.springframework.util.Assert;
27
28 /**
29 * <p>
30 * A {@link LineMapper} implementation that stores a mapping of String patterns
31 * to delegate {@link LineTokenizer}s as well as a mapping of String patterns to
32 * delegate {@link FieldSetMapper}s. Each line received will be tokenized and
33 * then mapped to a field set.
34 *
35 * <p>
36 * Both the tokenizing and the mapping work in a similar way. The line will be
37 * checked for its matching pattern. If the key matches a pattern in the map of
38 * delegates, then the corresponding delegate will be used. Patterns are sorted
39 * starting with the most specific, and the first match succeeds.
40 *
41 * @see PatternMatchingCompositeLineTokenizer
42 *
43 * @author Dan Garrette
44 * @author Dave Syer
45 * @since 2.0
46 */
47 public class PatternMatchingCompositeLineMapper<T> implements LineMapper<T>, InitializingBean {
48
49 private PatternMatchingCompositeLineTokenizer tokenizer = new PatternMatchingCompositeLineTokenizer();
50
51 private PatternMatcher<FieldSetMapper<T>> patternMatcher;
52
53 /*
54 * (non-Javadoc)
55 *
56 * @see
57 * org.springframework.batch.item.file.mapping.LineMapper#mapLine(java.lang
58 * .String, int)
59 */
60 @Override
61 public T mapLine(String line, int lineNumber) throws Exception {
62 return patternMatcher.match(line).mapFieldSet(this.tokenizer.tokenize(line));
63 }
64
65 /*
66 * (non-Javadoc)
67 *
68 * @see
69 * org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
70 */
71 @Override
72 public void afterPropertiesSet() throws Exception {
73 this.tokenizer.afterPropertiesSet();
74 Assert.isTrue(this.patternMatcher != null, "The 'fieldSetMappers' property must be non-empty");
75 }
76
77 public void setTokenizers(Map<String, LineTokenizer> tokenizers) {
78 this.tokenizer.setTokenizers(tokenizers);
79 }
80
81 public void setFieldSetMappers(Map<String, FieldSetMapper<T>> fieldSetMappers) {
82 Assert.isTrue(!fieldSetMappers.isEmpty(), "The 'fieldSetMappers' property must be non-empty");
83 this.patternMatcher = new PatternMatcher<FieldSetMapper<T>>(fieldSetMappers);
84 }
85 }