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.transform; 018 019import java.util.Map; 020 021import org.springframework.batch.support.PatternMatcher; 022import org.springframework.beans.factory.InitializingBean; 023import org.springframework.util.Assert; 024 025/** 026 * A {@link LineTokenizer} implementation that stores a mapping of String 027 * patterns to delegate {@link LineTokenizer}s. Each line tokenized will be 028 * checked to see if it matches a pattern. If the line matches a key in the map 029 * of delegates, then the corresponding delegate {@link LineTokenizer} will be 030 * used. Patterns are sorted starting with the most specific, and the first 031 * match succeeds. 032 * 033 * @author Ben Hale 034 * @author Dan Garrette 035 * @author Dave Syer 036 */ 037public class PatternMatchingCompositeLineTokenizer implements LineTokenizer, InitializingBean { 038 039 private PatternMatcher<LineTokenizer> tokenizers = null; 040 041 /* 042 * (non-Javadoc) 043 * 044 * @see 045 * org.springframework.batch.item.file.transform.LineTokenizer#tokenize( 046 * java.lang.String) 047 */ 048 @Override 049 public FieldSet tokenize(String line) { 050 return tokenizers.match(line).tokenize(line); 051 } 052 053 /* 054 * (non-Javadoc) 055 * 056 * @see 057 * org.springframework.beans.factory.InitializingBean#afterPropertiesSet() 058 */ 059 @Override 060 public void afterPropertiesSet() throws Exception { 061 Assert.isTrue(this.tokenizers != null, "The 'tokenizers' property must be non-empty"); 062 } 063 064 public void setTokenizers(Map<String, LineTokenizer> tokenizers) { 065 Assert.isTrue(!tokenizers.isEmpty(), "The 'tokenizers' property must be non-empty"); 066 this.tokenizers = new PatternMatcher<LineTokenizer>(tokenizers); 067 } 068}