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.transform; |
18 | |
19 | import java.util.Map; |
20 | |
21 | import org.springframework.batch.support.PatternMatcher; |
22 | import org.springframework.beans.factory.InitializingBean; |
23 | import org.springframework.util.Assert; |
24 | |
25 | /** |
26 | * A {@link LineTokenizer} implementation that stores a mapping of String |
27 | * patterns to delegate {@link LineTokenizer}s. Each line tokenizied will be |
28 | * checked to see if it matches a pattern. If the line matches a key in the map |
29 | * of delegates, then the corresponding delegate {@link LineTokenizer} will be |
30 | * used. Patterns are sorted starting with the most specific, and the first |
31 | * match succeeds. |
32 | * |
33 | * @author Ben Hale |
34 | * @author Dan Garrette |
35 | * @author Dave Syer |
36 | */ |
37 | public class PatternMatchingCompositeLineTokenizer implements LineTokenizer, InitializingBean { |
38 | |
39 | private PatternMatcher<LineTokenizer> tokenizers = null; |
40 | |
41 | /* |
42 | * (non-Javadoc) |
43 | * |
44 | * @see |
45 | * org.springframework.batch.item.file.transform.LineTokenizer#tokenize( |
46 | * java.lang.String) |
47 | */ |
48 | public FieldSet tokenize(String line) { |
49 | return tokenizers.match(line).tokenize(line); |
50 | } |
51 | |
52 | /* |
53 | * (non-Javadoc) |
54 | * |
55 | * @see |
56 | * org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
57 | */ |
58 | public void afterPropertiesSet() throws Exception { |
59 | Assert.isTrue(this.tokenizers != null, "The 'tokenizers' property must be non-empty"); |
60 | } |
61 | |
62 | public void setTokenizers(Map<String, LineTokenizer> tokenizers) { |
63 | Assert.isTrue(!tokenizers.isEmpty(), "The 'tokenizers' property must be non-empty"); |
64 | this.tokenizers = new PatternMatcher<LineTokenizer>(tokenizers); |
65 | } |
66 | } |