diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/Alignment.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/Alignment.java new file mode 100644 index 000000000..fb861b841 --- /dev/null +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/Alignment.java @@ -0,0 +1,38 @@ +/* + * Copyright 2006-2007 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.batch.item.file.transform; + +import org.springframework.core.enums.StringCodedLabeledEnum; + +/** + * @author Dave Syer + * + */ +public class Alignment extends StringCodedLabeledEnum { + + public static final Alignment CENTER = new Alignment("CENTER", "center"); + public static final Alignment RIGHT = new Alignment("RIGHT", "right"); + public static final Alignment LEFT = new Alignment("LEFT", "left"); + + /** + * @param code + * @param label + */ + public Alignment(String code, String label) { + super(code, label); + } + +} diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FixedLengthLineAggregator.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FixedLengthLineAggregator.java index 02c9b812a..4a4c56580 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FixedLengthLineAggregator.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/transform/FixedLengthLineAggregator.java @@ -22,7 +22,7 @@ import org.springframework.batch.item.file.mapping.FieldSet; import org.springframework.util.Assert; /** - * LineAggregator implementation which produces line by aggregating provided + * {@link LineAggregator} implementation which produces line by aggregating provided * strings into columns with fixed length. Columns are specified by array of * ranges ({@link #setColumns(Range[])}.
* @@ -32,13 +32,12 @@ import org.springframework.util.Assert; */ public class FixedLengthLineAggregator implements LineAggregator { - private static final int ALIGN_CENTER = 1; - private static final int ALIGN_RIGHT = 2; - private static final int ALIGN_LEFT = 3; - private Range[] ranges; + private int lastColumn; - private int align = ALIGN_LEFT; + + private Alignment align = Alignment.LEFT; + private char padding = ' '; /** @@ -47,9 +46,8 @@ public class FixedLengthLineAggregator implements LineAggregator { * a String describing the range boundaries, e.g. "1,4,7" or "1-3,4-6,7" or * "1-2,4-5,7-10". * - * @param columns - * array of Range objects which specify column start and end - * position + * @param columns array of Range objects which specify column start and end + * position */ public void setColumns(Range[] columns) { Assert.notNull(columns); @@ -61,22 +59,19 @@ public class FixedLengthLineAggregator implements LineAggregator { * Aggregate provided strings into single line using specified column * ranges. * - * @param fieldSet - * arrays of strings representing data to be aggregated + * @param fieldSet arrays of strings representing data to be aggregated * @return aggregated strings */ public String aggregate(FieldSet fieldSet) { Assert.notNull(fieldSet); Assert.notNull(ranges); - + String[] args = fieldSet.getValues(); - Assert.isTrue(args.length <= ranges.length, - "Number of arguments must match number of fields in a record"); + Assert.isTrue(args.length <= ranges.length, "Number of arguments must match number of fields in a record"); // calculate line length - int lineLength = ranges[lastColumn].hasMaxValue() ? ranges[lastColumn] - .getMax() : ranges[lastColumn].getMin() + int lineLength = ranges[lastColumn].hasMaxValue() ? ranges[lastColumn].getMax() : ranges[lastColumn].getMin() + args[lastColumn].length() - 1; // create stringBuffer with length of line filled with padding @@ -97,32 +92,24 @@ public class FixedLengthLineAggregator implements LineAggregator { int columnLength; if ((i == lastColumn) && (!ranges[lastColumn].hasMaxValue())) { columnLength = args[lastColumn].length(); - } else { + } + else { columnLength = ranges[i].getMax() - ranges[i].getMin() + 1; } String textToInsert = (args[i] == null) ? "" : args[i]; - Assert - .isTrue(columnLength >= textToInsert.length(), - "Supplied text: " + textToInsert - + " is longer than defined length: " - + columnLength); + Assert.isTrue(columnLength >= textToInsert.length(), "Supplied text: " + textToInsert + + " is longer than defined length: " + columnLength); - switch (align) { - case ALIGN_RIGHT: + if (align == Alignment.RIGHT) { start += (columnLength - textToInsert.length()); - break; - case ALIGN_CENTER: + } + else if (align == Alignment.CENTER) { start += ((columnLength - textToInsert.length()) / 2); - break; - case ALIGN_LEFT: - // nothing to do - break; } - stringBuffer.replace(start, start + textToInsert.length(), - textToInsert); + stringBuffer.replace(start, start + textToInsert.length(), textToInsert); } return stringBuffer.toString(); @@ -133,27 +120,16 @@ public class FixedLengthLineAggregator implements LineAggregator { * IllegalArgumentException is thrown in case the argument does not match * any of the recognized values. * - * @param alignment - * the alignment to be used + * @param alignment the alignment to be used */ - public void setAlignment(String alignment) { - if ("CENTER".equalsIgnoreCase(alignment)) { - this.align = ALIGN_CENTER; - } else if ("RIGHT".equalsIgnoreCase(alignment)) { - this.align = ALIGN_RIGHT; - } else if ("LEFT".equalsIgnoreCase(alignment)) { - this.align = ALIGN_LEFT; - } else { - throw new IllegalArgumentException( - "Only 'CENTER', 'RIGHT' or 'LEFT' are allowed alignment values"); - } + public void setAlignment(Alignment alignment) { + this.align = alignment; } /** * Setter for padding (default is space). * - * @param padding - * the padding character + * @param padding the padding character */ public void setPadding(char padding) { this.padding = padding; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/FixedLengthLineAggregatorTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/FixedLengthLineAggregatorTests.java index 70aa02be2..5078ade85 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/FixedLengthLineAggregatorTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/transform/FixedLengthLineAggregatorTests.java @@ -104,7 +104,7 @@ public class FixedLengthLineAggregatorTests extends TestCase { */ public void testAggregateFormattedRight() { String[] args = { "Matchsize", "Smallsize" }; - aggregator.setAlignment("right"); + aggregator.setAlignment(Alignment.RIGHT); aggregator.setColumns(new Range[] {new Range(1,13), new Range(14,23)}); String result = aggregator.aggregate(new DefaultFieldSet(args)); assertEquals(23,result.length()); @@ -116,7 +116,7 @@ public class FixedLengthLineAggregatorTests extends TestCase { */ public void testAggregateFormattedCenter() { String[] args = { "Matchsize", "Smallsize" }; - aggregator.setAlignment("center"); + aggregator.setAlignment(Alignment.CENTER); aggregator.setColumns(new Range[] {new Range(1,13), new Range(14,25)}); String result = aggregator.aggregate(new DefaultFieldSet(args)); assertEquals(result, " Matchsize Smallsize "); @@ -128,7 +128,7 @@ public class FixedLengthLineAggregatorTests extends TestCase { public void testAggregateWithCustomPadding() { String[] args = { "Matchsize", "Smallsize" }; aggregator.setPadding('.'); - aggregator.setAlignment("left"); + aggregator.setAlignment(Alignment.LEFT); aggregator.setColumns(new Range[] {new Range(1,13), new Range(14,24)}); String result = aggregator.aggregate(new DefaultFieldSet(args)); assertEquals(result, "Matchsize....Smallsize.."); @@ -139,24 +139,11 @@ public class FixedLengthLineAggregatorTests extends TestCase { */ public void testAggregateFormattedLeft() { String[] args = { "Matchsize", "Smallsize" }; - aggregator.setAlignment("left"); + aggregator.setAlignment(Alignment.LEFT); aggregator.setColumns(new Range[] {new Range(1,13), new Range(14,24)}); String result = aggregator.aggregate(new DefaultFieldSet(args)); assertEquals(result, "Matchsize Smallsize "); } - - - /** - * Try set ivalid alignment - */ - public void testInvalidAlignment() { - try { - aggregator.setAlignment("foo"); - fail("Exception was expected: invalid alignment value"); - } catch (IllegalArgumentException iae) { - // expected - } - } /** * If one of the passed arguments is null, string filled with spaces should