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