| 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.Arrays; |
| 20 | |
| 21 | import org.springframework.batch.item.file.mapping.FieldSet; |
| 22 | import org.springframework.util.Assert; |
| 23 | |
| 24 | /** |
| 25 | * LineAggregator implementation which produces line by aggregating provided |
| 26 | * strings into columns with fixed length. Columns are specified by array of |
| 27 | * ranges ({@link #setColumns(Range[])}.</br> |
| 28 | * |
| 29 | * @author tomas.slanina |
| 30 | * @author peter.zozom |
| 31 | * @author Dave Syer |
| 32 | */ |
| 33 | public class FixedLengthLineAggregator implements LineAggregator { |
| 34 | |
| 35 | private static final int ALIGN_CENTER = 1; |
| 36 | private static final int ALIGN_RIGHT = 2; |
| 37 | private static final int ALIGN_LEFT = 3; |
| 38 | |
| 39 | private Range[] ranges; |
| 40 | private int lastColumn; |
| 41 | private int align = ALIGN_LEFT; |
| 42 | private char padding = ' '; |
| 43 | |
| 44 | /** |
| 45 | * Set column ranges. Used in conjunction with the |
| 46 | * {@link RangeArrayPropertyEditor} this property can be set in the form of |
| 47 | * a String describing the range boundaries, e.g. "1,4,7" or "1-3,4-6,7" or |
| 48 | * "1-2,4-5,7-10". |
| 49 | * |
| 50 | * @param columns |
| 51 | * array of Range objects which specify column start and end |
| 52 | * position |
| 53 | */ |
| 54 | public void setColumns(Range[] columns) { |
| 55 | Assert.notNull(columns); |
| 56 | lastColumn = findLastColumn(columns); |
| 57 | this.ranges = columns; |
| 58 | } |
| 59 | |
| 60 | /** |
| 61 | * Aggregate provided strings into single line using specified column |
| 62 | * ranges. |
| 63 | * |
| 64 | * @param fieldSet |
| 65 | * arrays of strings representing data to be aggregated |
| 66 | * @return aggregated strings |
| 67 | */ |
| 68 | public String aggregate(FieldSet fieldSet) { |
| 69 | |
| 70 | Assert.notNull(fieldSet); |
| 71 | Assert.notNull(ranges); |
| 72 | |
| 73 | String[] args = fieldSet.getValues(); |
| 74 | Assert.isTrue(args.length <= ranges.length, |
| 75 | "Number of arguments must match number of fields in a record"); |
| 76 | |
| 77 | // calculate line length |
| 78 | int lineLength = ranges[lastColumn].hasMaxValue() ? ranges[lastColumn] |
| 79 | .getMax() : ranges[lastColumn].getMin() |
| 80 | + args[lastColumn].length() - 1; |
| 81 | |
| 82 | // create stringBuffer with length of line filled with padding |
| 83 | // characters |
| 84 | char[] emptyLine = new char[lineLength]; |
| 85 | Arrays.fill(emptyLine, padding); |
| 86 | |
| 87 | StringBuffer stringBuffer = new StringBuffer(lineLength); |
| 88 | stringBuffer.append(emptyLine); |
| 89 | |
| 90 | // aggregate all strings |
| 91 | for (int i = 0; i < args.length; i++) { |
| 92 | |
| 93 | // offset where text will be inserted |
| 94 | int start = ranges[i].getMin() - 1; |
| 95 | |
| 96 | // calculate column length |
| 97 | int columnLength; |
| 98 | if ((i == lastColumn) && (!ranges[lastColumn].hasMaxValue())) { |
| 99 | columnLength = args[lastColumn].length(); |
| 100 | } else { |
| 101 | columnLength = ranges[i].getMax() - ranges[i].getMin() + 1; |
| 102 | } |
| 103 | |
| 104 | String textToInsert = (args[i] == null) ? "" : args[i]; |
| 105 | |
| 106 | Assert |
| 107 | .isTrue(columnLength >= textToInsert.length(), |
| 108 | "Supplied text: " + textToInsert |
| 109 | + " is longer than defined length: " |
| 110 | + columnLength); |
| 111 | |
| 112 | switch (align) { |
| 113 | case ALIGN_RIGHT: |
| 114 | start += (columnLength - textToInsert.length()); |
| 115 | break; |
| 116 | case ALIGN_CENTER: |
| 117 | start += ((columnLength - textToInsert.length()) / 2); |
| 118 | break; |
| 119 | case ALIGN_LEFT: |
| 120 | // nothing to do |
| 121 | break; |
| 122 | } |
| 123 | |
| 124 | stringBuffer.replace(start, start + textToInsert.length(), |
| 125 | textToInsert); |
| 126 | } |
| 127 | |
| 128 | return stringBuffer.toString(); |
| 129 | } |
| 130 | |
| 131 | /** |
| 132 | * Recognized alignments are <code>CENTER, RIGHT, LEFT</code>. An |
| 133 | * IllegalArgumentException is thrown in case the argument does not match |
| 134 | * any of the recognized values. |
| 135 | * |
| 136 | * @param alignment |
| 137 | * the alignment to be used |
| 138 | */ |
| 139 | public void setAlignment(String alignment) { |
| 140 | if ("CENTER".equalsIgnoreCase(alignment)) { |
| 141 | this.align = ALIGN_CENTER; |
| 142 | } else if ("RIGHT".equalsIgnoreCase(alignment)) { |
| 143 | this.align = ALIGN_RIGHT; |
| 144 | } else if ("LEFT".equalsIgnoreCase(alignment)) { |
| 145 | this.align = ALIGN_LEFT; |
| 146 | } else { |
| 147 | throw new IllegalArgumentException( |
| 148 | "Only 'CENTER', 'RIGHT' or 'LEFT' are allowed alignment values"); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Setter for padding (default is space). |
| 154 | * |
| 155 | * @param padding |
| 156 | * the padding character |
| 157 | */ |
| 158 | public void setPadding(char padding) { |
| 159 | this.padding = padding; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Find last column. Columns are not sorted. Returns index of last column |
| 164 | * (column with highest offset). |
| 165 | */ |
| 166 | private int findLastColumn(Range[] columns) { |
| 167 | |
| 168 | int lastOffset = 1; |
| 169 | int lastIndex = 0; |
| 170 | |
| 171 | for (int i = 0; i < columns.length; i++) { |
| 172 | if (columns[i].getMin() > lastOffset) { |
| 173 | lastOffset = columns[i].getMin(); |
| 174 | lastIndex = i; |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | return lastIndex; |
| 179 | } |
| 180 | } |