01: /*
02: * FixedLengthLineParser.java
03: *
04: * This file is part of SQL Workbench/J, http://www.sql-workbench.net
05: *
06: * Copyright 2002-2008, Thomas Kellerer
07: * No part of this code maybe reused without the permission of the author
08: *
09: * To contact the author please send an email to: support@sql-workbench.net
10: *
11: */
12: package workbench.util;
13:
14: /**
15: *
16: * @author support@sql-workbench.net
17: */
18: public class FixedLengthLineParser implements LineParser {
19: private int currentColIndex;
20: private int[] widths;
21: private String line;
22: private int currentLineIndex;
23: private boolean trimValues = false;
24:
25: public FixedLengthLineParser(int[] colWidths) {
26: if (colWidths == null) {
27: throw new IllegalArgumentException(
28: "Column widths may not be null");
29: }
30: this .widths = colWidths;
31: }
32:
33: public void setTrimValues(boolean trimValues) {
34: this .trimValues = trimValues;
35: }
36:
37: public void setLine(String newLine) {
38: this .line = newLine;
39: this .currentColIndex = 0;
40: this .currentLineIndex = 0;
41: }
42:
43: public boolean hasNext() {
44: return currentColIndex < widths.length;
45: }
46:
47: public String getNext() {
48: if (!hasNext()) {
49: return null;
50: }
51: int end = currentLineIndex + widths[currentColIndex];
52: if (end > line.length())
53: return null;
54: String result = line.substring(currentLineIndex, end);
55: currentLineIndex += widths[currentColIndex];
56: currentColIndex++;
57: if (trimValues)
58: return result.trim();
59: return result;
60: }
61: }
|