1 package emissary.parser;
2
3 import org.apache.commons.collections4.CollectionUtils;
4 import org.slf4j.Logger;
5 import org.slf4j.LoggerFactory;
6
7 import java.util.ArrayList;
8 import java.util.HashMap;
9 import java.util.List;
10 import java.util.Map;
11 import javax.annotation.Nullable;
12
13
14
15
16
17
18
19
20
21
22
23
24
25 public class InputSession {
26
27
28 private static final Logger logger = LoggerFactory.getLogger(InputSession.class);
29
30
31 @Nullable
32 protected PositionRecord overall = null;
33
34
35 protected List<PositionRecord> header = new ArrayList<>();
36
37
38 protected List<PositionRecord> footer = new ArrayList<>();
39
40
41 protected List<PositionRecord> data = new ArrayList<>();
42
43
44 protected Map<String, Object> metaData = new HashMap<>();
45
46
47 protected boolean valid = true;
48
49
50
51
52 public InputSession() {}
53
54
55
56
57
58
59 public InputSession(PositionRecord o) {
60 this.overall = o;
61 }
62
63
64
65
66
67
68
69 public InputSession(PositionRecord o, List<PositionRecord> d) throws ParserException {
70 this(o);
71 addDataRecs(d);
72 }
73
74
75
76
77
78
79
80 public InputSession(PositionRecord o, PositionRecord d) throws ParserException {
81 this(o);
82 addDataRec(d);
83 }
84
85
86
87
88
89
90
91
92
93 public InputSession(PositionRecord o, List<PositionRecord> h, List<PositionRecord> f, List<PositionRecord> d) throws ParserException {
94 this(o, d);
95 addHeaderRecs(h);
96 addFooterRecs(f);
97 }
98
99
100
101
102
103
104
105
106
107
108 public InputSession(PositionRecord o, List<PositionRecord> h, List<PositionRecord> f, List<PositionRecord> d, Map<String, Object> m)
109 throws ParserException {
110 this(o, h, f, d);
111 addMetaData(m);
112 }
113
114
115
116
117
118
119 public void setOverall(PositionRecord rec) throws ParserException {
120 this.overall = rec;
121 validateAll();
122 }
123
124
125
126
127
128
129
130 public void setOverall(int start, int length) throws ParserException {
131 setOverall(new PositionRecord(start, length));
132 }
133
134
135
136
137
138
139 public void addMetaData(Map<String, Object> m) throws ParserException {
140 for (Map.Entry<String, Object> entry : m.entrySet()) {
141 String key = entry.getKey();
142 Object val = entry.getValue();
143 if (val instanceof PositionRecord) {
144 validateRecord((PositionRecord) val);
145 metaData.put(key, val);
146 } else if (val instanceof String) {
147 metaData.put(key, val);
148 } else {
149 logger.warn("Ignoring metadata record named {} with type of {} - it is not a PositionRecord or a String", key,
150 val.getClass().getName());
151 }
152 }
153 }
154
155
156
157
158
159
160 public void setValid(boolean b) {
161 this.valid = b;
162 }
163
164
165
166
167
168
169 public boolean isValid() {
170 return valid;
171 }
172
173
174
175
176
177
178 public long getStart() {
179 if (overall != null) {
180 return overall.getPosition();
181 }
182 return 0;
183 }
184
185
186
187
188
189
190 public long getLength() {
191 if (overall != null) {
192 return overall.getLength();
193 }
194 return 0;
195 }
196
197
198
199
200
201
202 public PositionRecord getOverall() {
203 return overall;
204 }
205
206
207
208
209
210
211
212 public void addHeaderRecs(@Nullable List<PositionRecord> h) throws ParserException {
213 if (CollectionUtils.isNotEmpty(h)) {
214 validateList(h);
215 header.addAll(h);
216 }
217 }
218
219
220
221
222
223
224
225 public void addDataRecs(@Nullable List<PositionRecord> d) throws ParserException {
226 if (CollectionUtils.isNotEmpty(d)) {
227 validateList(d);
228 data.addAll(d);
229 }
230 }
231
232
233
234
235
236
237
238 public void addFooterRecs(@Nullable List<PositionRecord> f) throws ParserException {
239 if (CollectionUtils.isNotEmpty(f)) {
240 validateList(f);
241 footer.addAll(f);
242 }
243 }
244
245
246
247
248
249
250
251 public void addHeaderRec(PositionRecord r) throws ParserException {
252 validateRecord(r);
253 header.add(r);
254 }
255
256
257
258
259
260
261
262
263 public void addHeaderRec(int pos, int len) throws ParserException {
264 addHeaderRec(new PositionRecord(pos, len));
265 }
266
267
268
269
270
271
272
273 public void addFooterRec(PositionRecord r) throws ParserException {
274 validateRecord(r);
275 footer.add(r);
276 }
277
278
279
280
281
282
283
284
285 public void addFooterRec(int pos, int len) throws ParserException {
286 addFooterRec(new PositionRecord(pos, len));
287 }
288
289
290
291
292
293
294
295 public void addDataRec(PositionRecord r) throws ParserException {
296 validateRecord(r);
297 data.add(r);
298 }
299
300
301
302
303
304
305
306
307 public void addDataRec(int pos, int len) throws ParserException {
308 addDataRec(new PositionRecord(pos, len));
309 }
310
311
312
313
314
315
316
317
318 public void addMetaDataRec(String name, PositionRecord r) throws ParserException {
319 validateRecord(r);
320 metaData.put(name, r);
321 }
322
323
324
325
326
327
328
329 public void addMetaDataRec(String name, String rec) {
330 metaData.put(name, rec);
331 }
332
333
334
335
336
337
338 public int getDataCount() {
339 return data.size();
340 }
341
342
343
344
345
346
347 public int getFooterCount() {
348 return footer.size();
349 }
350
351
352
353
354
355
356 public int getHeaderCount() {
357 return header.size();
358 }
359
360
361
362
363
364
365 public int getMetaDataCount() {
366 return metaData.size();
367 }
368
369
370
371
372
373
374 public List<PositionRecord> getFooter() {
375 return footer;
376 }
377
378
379
380
381
382
383 public List<PositionRecord> getHeader() {
384 return header;
385 }
386
387
388
389
390
391
392 public List<PositionRecord> getData() {
393 return data;
394 }
395
396
397
398
399
400
401 public Map<String, Object> getMetaData() {
402 return metaData;
403 }
404
405
406
407
408
409
410 @Override
411 public String toString() {
412 StringBuilder sb = new StringBuilder();
413 sb.append("Session is ").append((this.isValid() ? "" : "not")).append("valid").append("\n");
414 sb.append("Session overall ").append(this.getOverall()).append("\n");
415 sb.append("Header record ").append(this.getHeader()).append("\n");
416 sb.append("Data record ").append(this.getData()).append("\n");
417 sb.append("Footer record ").append(this.getFooter()).append("\n");
418 Map<String, Object> m = this.getMetaData();
419 sb.append("Metadata count ").append((m == null ? 0 : m.size())).append("\n");
420 return sb.toString();
421 }
422
423
424
425
426
427
428
429 protected void validateRecord(PositionRecord r) throws ParserException {
430 if (overall != null && r != null && ((r.getPosition() < overall.getPosition()) || (r.getEnd() > overall.getEnd()))) {
431 throw new ParserException("Position record " + r + " is out of bounds for the data " + overall);
432 }
433 }
434
435
436
437
438
439
440
441 protected void validateList(@Nullable List<PositionRecord> list) throws ParserException {
442
443 if (overall == null || list == null) {
444 return;
445 }
446
447 for (PositionRecord p : list) {
448 validateRecord(p);
449 }
450 }
451
452
453
454
455
456
457 protected void validateAll() throws ParserException {
458 if (overall == null) {
459 return;
460 }
461 validateList(header);
462 validateList(footer);
463 validateList(data);
464 validateMetaData();
465 }
466
467
468
469
470
471
472 protected void validateMetaData() throws ParserException {
473 if (overall == null || metaData.isEmpty()) {
474 return;
475 }
476
477 for (Object obj : metaData.values()) {
478 if (obj instanceof PositionRecord) {
479 validateRecord((PositionRecord) obj);
480 }
481 }
482 }
483
484 }