| 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 | @Override |
| 49 | public FieldSet tokenize(String line) { |
| 50 | return tokenizers.match(line).tokenize(line); |
| 51 | } |
| 52 | |
| 53 | /* |
| 54 | * (non-Javadoc) |
| 55 | * |
| 56 | * @see |
| 57 | * org.springframework.beans.factory.InitializingBean#afterPropertiesSet() |
| 58 | */ |
| 59 | @Override |
| 60 | public void afterPropertiesSet() throws Exception { |
| 61 | Assert.isTrue(this.tokenizers != null, "The 'tokenizers' property must be non-empty"); |
| 62 | } |
| 63 | |
| 64 | public void setTokenizers(Map<String, LineTokenizer> tokenizers) { |
| 65 | Assert.isTrue(!tokenizers.isEmpty(), "The 'tokenizers' property must be non-empty"); |
| 66 | this.tokenizers = new PatternMatcher<LineTokenizer>(tokenizers); |
| 67 | } |
| 68 | } |