View Javadoc

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.ArrayList;
20  import java.util.Arrays;
21  import java.util.List;
22  
23  /**
24   * Abstract class handling common concerns of various {@link LineTokenizer}
25   * implementations such as dealing with names and actual construction of
26   * {@link FieldSet}
27   * 
28   * @author Dave Syer
29   * @author Robert Kasanicky
30   * @author Lucas Ward
31   */
32  public abstract class AbstractLineTokenizer implements LineTokenizer {
33  
34  	protected String[] names = new String[0];
35  
36  	private boolean strict = true;
37  	
38  	private String emptyToken = "";
39  
40  	private FieldSetFactory fieldSetFactory = new DefaultFieldSetFactory();
41  
42  	/**
43  	 * Public setter for the strict flag. If true (the default) then number of 
44  	 * tokens in line must match the number of tokens defined 
45  	 * (by {@link Range}, columns, etc.) in {@link LineTokenizer}. 
46  	 * If false then lines with less tokens will be tolerated and padded with 
47  	 * empty columns, and lines with more tokens will 
48  	 * simply be truncated.
49  	 * 
50  	 * @param strict the strict flag to set
51  	 */
52  	public void setStrict(boolean strict) {
53  		this.strict = strict;
54  	}
55  	
56  	/**
57  	 * Provides access to the strict flag for subclasses if needed.
58  	 * 
59  	 * @return the strict flag value
60  	 */
61  	protected boolean isStrict() {
62  		return strict;
63  	}
64  	
65  	/**
66  	 * Factory for {@link FieldSet} instances. Can be injected by clients to
67  	 * customize the default number and date formats.
68  	 * 
69  	 * @param fieldSetFactory the {@link FieldSetFactory} to set
70  	 */
71  	public void setFieldSetFactory(FieldSetFactory fieldSetFactory) {
72  		this.fieldSetFactory = fieldSetFactory;
73  	}
74  
75  	/**
76  	 * Setter for column names. Optional, but if set, then all lines must have
77  	 * as many or fewer tokens.
78  	 * 
79  	 * @param names
80  	 */
81  	public void setNames(String[] names) {
82  		this.names = names==null ? null : Arrays.asList(names).toArray(new String[names.length]);
83  	}
84  
85  	/**
86  	 * @return <code>true</code> if column names have been specified
87  	 * @see #setNames(String[])
88  	 */
89  	public boolean hasNames() {
90  		if (names != null && names.length > 0) {
91  			return true;
92  		}
93  		return false;
94  	}
95  
96  	/**
97  	 * Yields the tokens resulting from the splitting of the supplied
98  	 * <code>line</code>.
99  	 * 
100 	 * @param line the line to be tokenised (can be <code>null</code>)
101 	 * 
102 	 * @return the resulting tokens
103 	 */
104     @Override
105 	public FieldSet tokenize(String line) {
106 
107 		if (line == null) {
108 			line = "";
109 		}
110 
111 		List<String> tokens = new ArrayList<String>(doTokenize(line));
112 		
113 		// if names are set and strict flag is false
114 		if ( ( names.length != 0 ) && ( ! strict ) ) {
115 			adjustTokenCountIfNecessary( tokens );
116 		}
117 		
118 		String[] values = (String[]) tokens.toArray(new String[tokens.size()]);
119 
120 		if (names.length == 0) {
121 			return fieldSetFactory.create(values);
122 		}
123 		else if (values.length != names.length) {
124 			throw new IncorrectTokenCountException(names.length, values.length);
125 		}
126 		return fieldSetFactory.create(values, names);
127 	}
128 
129 	protected abstract List<String> doTokenize(String line);
130 	
131 	/**
132 	 * Adds empty tokens or truncates existing token list to match expected 
133 	 * (configured) number of tokens in {@link LineTokenizer}.
134 	 * 
135 	 * @param tokens - list of tokens
136 	 */
137 	private void adjustTokenCountIfNecessary( List<String> tokens ) {
138 		
139 		int nameLength = names.length;
140 		int tokensSize = tokens.size();
141 		
142 		// if the number of tokens is not what expected
143 		if ( nameLength != tokensSize ) {
144 			
145 			if ( nameLength > tokensSize ) {
146 
147 				// add empty tokens until the token list size matches
148 				// the expected number of tokens
149 				for ( int i = 0; i < ( nameLength - tokensSize ); i++ ) {
150 					tokens.add( emptyToken );
151 				}
152 
153 			} else {
154 				// truncate token list to match the number of expected tokens
155 				for ( int i = tokensSize - 1; i >= nameLength; i-- ) {
156 					tokens.remove(i);
157 				}
158 			}
159 				
160 		}
161 	}
162 }