1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 package net.sf.flatpack.brparse;
34
35 import java.io.BufferedReader;
36 import java.io.File;
37 import java.io.IOException;
38 import java.io.InputStream;
39 import java.io.Reader;
40 import java.util.List;
41 import java.util.Map;
42
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import net.sf.flatpack.DataSet;
47 import net.sf.flatpack.DefaultDataSet;
48 import net.sf.flatpack.FixedLengthParser;
49 import net.sf.flatpack.structure.ColumnMetaData;
50 import net.sf.flatpack.structure.Row;
51 import net.sf.flatpack.util.FPConstants;
52 import net.sf.flatpack.util.FPException;
53 import net.sf.flatpack.util.FixedWidthParserUtils;
54 import net.sf.flatpack.util.ParserUtils;
55
56
57
58
59
60
61 public class BuffReaderFixedParser extends FixedLengthParser implements InterfaceBuffReaderParse {
62 private static final Logger LOGGER = LoggerFactory.getLogger(BuffReaderFixedParser.class);
63 private BufferedReader br = null;
64 private int lineCount = 0;
65 private Map recordLengths = null;
66
67
68
69
70
71
72 public BuffReaderFixedParser(final InputStream pzmapXMLStream, final InputStream dataSourceStream) {
73 super(pzmapXMLStream, dataSourceStream);
74 }
75
76
77
78
79
80
81 public BuffReaderFixedParser(final File pzmapXML, final File dataSource) {
82 super(pzmapXML, dataSource);
83 }
84
85
86
87
88
89
90
91 public BuffReaderFixedParser(final Reader pzmapXML, final Reader dataSource) {
92 super(pzmapXML, dataSource);
93 }
94
95 protected BuffReaderFixedParser(final Reader dataSourceReader, final String dataDefinition) {
96 super(dataSourceReader, dataDefinition);
97 }
98
99 @Override
100 protected DataSet doParse() {
101 final DataSet ds = new BuffReaderDataSet(getPzMetaData(), this);
102 lineCount = 0;
103 recordLengths = ParserUtils.calculateRecordLengths(getPzMetaData());
104 try {
105
106 ds.setPZConvertProps(ParserUtils.loadConvertProperties());
107
108 br = new BufferedReader(getDataSourceReader());
109
110 return ds;
111
112 } catch (final IOException ex) {
113 LOGGER.error("error accessing/creating inputstream", ex);
114 }
115
116 return null;
117 }
118
119
120
121
122
123
124
125 @Override
126 public Row buildRow(final DefaultDataSet ds) {
127 String line = null;
128 try {
129 while ((line = br.readLine()) != null) {
130 lineCount++;
131
132 if (line.trim().length() == 0) {
133 continue;
134 }
135
136 final String mdkey = FixedWidthParserUtils.getCMDKey(getPzMetaData(), line);
137
138 final Row row = new Row();
139 row.setRowNumber(lineCount);
140 row.setMdkey(mdkey.equals(FPConstants.DETAIL_ID) ? null : mdkey);
141
142 final List<ColumnMetaData> cmds = ParserUtils.getColumnMetaData(mdkey, getPzMetaData());
143
144 final int recordLength = ((Integer) recordLengths.get(mdkey)).intValue();
145
146 if (line.length() > recordLength) {
147
148
149
150 if (isIgnoreExtraColumns()) {
151 addError(ds, "TRUNCATED LINE TO CORRECT LENGTH", lineCount, 1, isStoreRawDataToDataError() ? line : null);
152
153
154 row.addColumn(FixedWidthParserUtils.splitFixedText(cmds, line.substring(0, recordLength), isPreserveLeadingWhitespace(),
155 isPreserveTrailingWhitespace()));
156 } else {
157 addError(ds, "LINE TOO LONG. LINE IS " + line.length() + " LONG. SHOULD BE " + recordLength, lineCount, 2,
158 isStoreRawDataToDataError() ? line : null);
159 continue;
160 }
161 } else if (line.length() < recordLength) {
162 if (isHandlingShortLines()) {
163
164 addError(ds, "PADDED LINE TO CORRECT RECORD LENGTH", lineCount, 1, isStoreRawDataToDataError() ? line : null);
165
166 row.addColumn(FixedWidthParserUtils.splitFixedText(cmds, line + ParserUtils.padding(recordLength - line.length(), ' '),
167 isPreserveLeadingWhitespace(), isPreserveTrailingWhitespace()));
168
169 } else {
170 addError(ds, "LINE TOO SHORT. LINE IS " + line.length() + " LONG. SHOULD BE " + recordLength, lineCount, 2,
171 isStoreRawDataToDataError() ? line : null);
172 continue;
173 }
174 } else {
175 row.addColumn(FixedWidthParserUtils.splitFixedText(cmds, line, isPreserveLeadingWhitespace(), isPreserveTrailingWhitespace()));
176 }
177
178 if (isFlagEmptyRows()) {
179
180 row.setEmpty(ParserUtils.isListElementsEmpty(row.getCols()));
181 }
182 if (isStoreRawDataToDataSet()) {
183
184
185 row.setRawData(line);
186 }
187
188 return row;
189 }
190
191 } catch (final IOException e) {
192 throw new FPException("Error Fetching Record From File...", e);
193 }
194
195 return null;
196 }
197
198
199
200
201
202
203 @Override
204 public void close() throws IOException {
205 if (br != null) {
206 br.close();
207 }
208 }
209
210
211
212 @Override
213 protected void finalize() throws Throwable {
214 try {
215 close();
216 } catch (final IOException ex) {
217 LOGGER.warn("Problem trying to auto close file handles...", ex);
218 } finally {
219 super.finalize();
220 }
221 }
222 }