-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFormatter.cs
800 lines (675 loc) · 26 KB
/
Formatter.cs
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
using System;
using System.IO;
using System.Data;
using System.Text;
using System.Collections;
using Want.RecordEditor;
using Want.Logging;
using Want.Patterns;
namespace Want.RecordEditor.Model
{
/// <summary>
/// Summary description for Formatter.
/// </summary>
public class Formatter
{
private static Logger logger = Logger.GetSingleton();
private static Encoding encoder = System.Text.Encoding.UTF8;
private string _filename;
private FormattedDataDataset ds;
private Datafile data;
private Layoutfile layout;
private string _error;
private Boolean hasErrors = false;
private string _leftover;
private string _eolDelimiter = "\r\n";
private char[] buffer;
private int idx;
private bool _isUpdating;
/// <summary>
///
/// </summary>
class RecordIdentifier
{
public int[] PositionFromStart = null;
public int[] Length = null;
public string[] IdentifyingValue = null;
public string RecordName = "";
public bool HideInFormattedView = false;
public RecordIdentifier(int[] PositionFromStart, int[] Length, string RecordName, string[] Values, bool Hide)
{
this.PositionFromStart = PositionFromStart;
this.Length = Length;
this.RecordName = RecordName;
this.IdentifyingValue = Values;
this.HideInFormattedView = Hide;
}
}
/// <summary>
///
/// </summary>
public Formatter()
{
data = new Datafile();
layout = new Layoutfile();
_isUpdating = false;
ds = new FormattedDataDataset ();
ds.Record.RowChanged += new DataRowChangeEventHandler(DataChanged);
ds.Field.RowChanged += new DataRowChangeEventHandler(DataChanged);
}
/// <summary>
///
/// </summary>
/// <param name="filename"></param>
public void Load(string filename)
{
_filename = filename;
_isUpdating = true;
ds.ReadXml(_filename);
_isUpdating = false;
}
/// <summary>
/// Use the current layout.DataSet to format data and populate the FormattedDataDataset (ds).
/// As a single record can span multiple lines, the read approach is based on a memory
/// stream (instead of simple readlines)
/// </summary>
public void FormatDatafile()
{
if (logger.TextBox != null)
{
logger.TextBox.Clear();
}
ValidateLayout();
if ((data.Source != null) && (data.Source.Length > 0) &&
(layout.DataSet.Record.Count > 0))
{
_isUpdating = true;
RecordIdentifier[] recordTable = GetRecordIdentifiers();
hasErrors = false;
_error = "";
_leftover = "";
ds.Clear();
// 2. put entire data source into a memory stream
this.buffer = data.Source.ToCharArray();
this.idx = 0;
int recordIndex = 0;
try
{
while (this.idx < this.buffer.Length)
{
RecordIdentifier theRecId = GetCurrentRecord(recordTable);
if (theRecId != null)
{
FormatRecord(recordIndex, theRecId);
recordIndex++;
}
else
{
// no matching record identifier found, add to error message and
// jump to next line
string unparsedSection = "";
while (this.idx < this.buffer.Length)
{
unparsedSection += this.buffer[idx];
idx++;
if (unparsedSection.EndsWith(_eolDelimiter))
{
break;
}
}
String errorMessage = "Parse error: failed to find a matching record around [";
if (unparsedSection.Length > 10)
{
errorMessage += unparsedSection.Substring(0, 10);
errorMessage += "...";
}
else
{
errorMessage += unparsedSection;
}
errorMessage += "], proceeding to next line ...";
_error += Environment.NewLine + errorMessage;
hasErrors = true;
}
} // while position is < StringReader buffer
}
catch (Exception ex)
{
logger.Log(Logger.MessageType.DEBUG, ex);
_leftover = GetString(0, this.buffer.Length - idx);
_error += "\n" + ex.Message;
_error += "\nUnparsed: ";
if (_leftover.Length > 50)
{
_error += _leftover.Substring(0, 50);
_error += "...";
}
else
{
_error += _leftover;
}
hasErrors = true;
return;
}
finally
{
_isUpdating = false;
}
} // if data and layout present
}
private void FormatRecord(int recordIndex, RecordIdentifier theRecId)
{
LayoutDataset.FieldRow[] fields;
FormattedDataDataset.RecordRow fmtRec = ds.Record.NewRecordRow();
fmtRec.Name = theRecId.RecordName;
fmtRec.Index = recordIndex;
fmtRec.Hidden = theRecId.HideInFormattedView;
ds.Record.AddRecordRow(fmtRec);
// b. process this record and move position accordingly
fields = (LayoutDataset.FieldRow[])layout.DataSet.Field.Select("RecordName = '" + theRecId.RecordName + "'", "Index");
logger.Log(Logger.MessageType.DEBUG, "Parsing " + fields.Length + " fields ...");
int fieldIndex = 0;
for (; fieldIndex < fields.Length; fieldIndex++)
{
bool indexMoved = false;
LayoutDataset.FieldRow fieldRow = fields[fieldIndex];
LayoutDataset.FieldRow eor = GetNextEorField(theRecId, fieldRow, recordIndex);
string dataValue = null;
if (fieldRow.Length < 0)
{
dataValue = GetVariableLengthValue(fields, fieldIndex, fieldRow);
indexMoved = true;
}
else
{
dataValue = GetString(0, fieldRow.Length);
}
if (dataValue != null)
{
bool processField = CheckValueForProcessing(recordIndex, theRecId, fmtRec, fieldRow, eor, dataValue);
if (processField)
{
FormattedDataDataset.FieldRow fmtField = ds.Field.NewFieldRow();
fmtField.Name = fieldRow.Name;
fmtField.RecordName = fmtRec.Name;
fmtField.RecordIndex = recordIndex;
fmtField.Description = fieldRow.IsDescriptionNull() ? "" : fieldRow.Description;
fmtField.Value = dataValue;
fmtField.Index = fieldRow.Index;
fmtField.Length = fieldRow.Length;
ds.Field.AddFieldRow(fmtField);
if (!indexMoved)
{
this.idx += dataValue.Length;
}
}
}
else if (!indexMoved && !fieldRow.IsLengthNull())
{
// if data field was not set, still move index
this.idx += fieldRow.Length;
}
}
}
/// <summary>
/// Check if current data value should be processed (mainly for conditional fields). For values
/// which do not match a condition, the StringReader is set back to the previous position. If
/// an optional fields does not define a condition or predefined value and the datavalue matches
/// next EOR, that EOR is written.
/// </summary>
/// <param name="recordIndex"></param>
/// <param name="theRecId"></param>
/// <param name="fmtRec"></param>
/// <param name="fieldRow"></param>
/// <param name="eor"></param>
/// <param name="dataValue"></param>
/// <returns></returns>
private bool CheckValueForProcessing(int recordIndex, RecordIdentifier theRecId, FormattedDataDataset.RecordRow fmtRec, LayoutDataset.FieldRow fieldRow, LayoutDataset.FieldRow eor, string dataValue)
{
if ((!fieldRow.IsIsOptionalNull()) && (fieldRow.IsOptional))
{
// optional values are identified either by predefined values
// or by conditions
if ((!fieldRow.IsConditionNull()) && (fieldRow.Condition.Length > 0))
{
string condExpression = "((RecordName = '" + theRecId.RecordName + "') and (RecordIndex = '" + recordIndex + "'))";
condExpression = condExpression + " and (" + fieldRow.Condition + ")";
Condition cond = new Condition(condExpression, ds.Field);
if (!cond.IsConditionTrue())
{
// go back
return false;
}
return true;
}
if ((!fieldRow.IsPredefinedValueNull()) &&
(fieldRow.PredefinedValue.Length > 0))
{
// field has predefined value, check and roll back if no match
if (!layout.Escape(fieldRow.PredefinedValue).Equals(dataValue))
{
return false;
}
return true;
}
if (eor != null)
{
// optional field has no condition and no predefined value,
// compare current data against next EOR, maybe record is
// already finished
string seor = layout.Escape(eor.PredefinedValue);
if (dataValue.Substring(0, seor.Length).Equals(seor))
{
FormattedDataDataset.FieldRow fmtField = ds.Field.NewFieldRow();
fmtField.Name = eor.Name;
fmtField.RecordName = fmtRec.Name;
fmtField.RecordIndex = recordIndex;
fmtField.Description = eor.IsDescriptionNull() ? "" : fieldRow.Description;
fmtField.Value = seor;
fmtField.Index = eor.Index;
fmtField.Length = eor.Length;
ds.Field.AddFieldRow(fmtField);
this.idx += seor.Length;
// datavalue already written, do not process again
return false;
}
return true;
}
throw new Exception("Parse error: optional field " + fieldRow.Name + " must have condition or predefined value.");
}
return true;
}
/// <summary>
/// Return a list of all defined record identifiers
/// </summary>
/// <returns></returns>
private RecordIdentifier[] GetRecordIdentifiers()
{
int recIdPos = 0;
int index = 0;
RecordIdentifier[] recordTable = new RecordIdentifier[layout.DataSet.Record.Count];
LayoutDataset.FieldRow[] fields;
foreach (LayoutDataset.RecordRow recRow in layout.DataSet.Record.Rows)
{
recIdPos = 0;
// get fields for record name, sorted by 'Index'
fields = (LayoutDataset.FieldRow[])layout.DataSet.Field.Select("RecordName = '" + recRow.Name + "'", "Index");
ArrayList posList = new ArrayList();
ArrayList valueList = new ArrayList();
ArrayList lengthList = new ArrayList();
foreach (LayoutDataset.FieldRow fieldRow in fields)
{
if (fieldRow.IsRecordIdentifier == true)
{
if ((fieldRow.IsPredefinedValueNull()) || (fieldRow.IsLengthNull()))
{
throw new Exception("Parse error: Predefined value and length must be set for " + fieldRow.Name + " in " + recRow.Name);
}
else
{
string escPredefinedValue = layout.Escape(fieldRow.PredefinedValue);
posList.Add(recIdPos);
valueList.Add(escPredefinedValue);
lengthList.Add(fieldRow.Length);
}
}
recIdPos += fieldRow.Length;
}
if (posList.Count > 0)
{
recordTable[index] = new RecordIdentifier(
(int[])posList.ToArray(typeof(int)),
(int[])lengthList.ToArray(typeof(int)),
recRow.Name,
(string[])valueList.ToArray(typeof(string)),
recRow.IsHideInFormatViewNull() ? false : recRow.HideInFormatView);
index++;
logger.Log(Logger.MessageType.DEBUG, "Loaded record identifier for " + recRow.Name);
posList.Clear();
valueList.Clear();
lengthList.Clear();
}
else
{
logger.Log(Logger.MessageType.WARN, "Failed to locate an identifier field for record " + recRow.Name);
}
}
return recordTable;
}
/// <summary>
/// Find matching record based on (1-n) Field.IsRecordIdentifier
/// </summary>
/// <returns></returns>
private RecordIdentifier GetCurrentRecord(RecordIdentifier[] recordTable)
{
foreach (RecordIdentifier recId in recordTable)
{
if (recId != null)
{
bool match = true;
for (int i = 0; i < recId.PositionFromStart.Length; i++)
{
int pos = recId.PositionFromStart[i];
int len = recId.Length[i];
string[] expectedValue = recId.IdentifyingValue[i].Split(new char[] {'|'});
// if a record has been defined but without fields, recId will be null
string sourceData = GetString(pos, len);
if (sourceData != null) {
// expectedValue could be an array of options, but one has to match
bool oneOptionMatches = false;
foreach (string option in expectedValue)
{
if (option.Equals(sourceData))
{
oneOptionMatches = true;
break;
}
}
if (oneOptionMatches)
{
logger.Log(Logger.MessageType.DEBUG, "Found match for " + sourceData + " at position " + pos);
}
else
{
match = false;
}
}
}
if (match)
{
logger.Log(Logger.MessageType.DEBUG, "Found record identifier " + recId.RecordName);
return recId;
}
}
}
int preview = buffer.Length > 20 ? 20 : buffer.Length;
logger.Log(Logger.MessageType.WARN, "Failed to identify record type, buffer was " + GetString(0, preview));
return null;
}
/// <summary>
/// Get next assigned EOR. This is needed if the record ends with a series
/// of optional fields, and the next field could be the EOR.
/// </summary>
/// <param name="theRecId"></param>
/// <param name="fieldRow"></param>
/// <param name="index">Record index</param>
/// <returns></returns>
private LayoutDataset.FieldRow GetNextEorField(RecordIdentifier theRecId, LayoutDataset.FieldRow fieldRow, int index)
{
LayoutDataset.FieldRow[] eors = (LayoutDataset.FieldRow[])layout.DataSet.Field.Select("(RecordName = '" + theRecId.RecordName +
"') and (IsEOR = true) and (Index > " + fieldRow.Index + ")", "Index");
LayoutDataset.FieldRow eor = null;
foreach (LayoutDataset.FieldRow r in eors)
{
if ((!r.IsConditionNull()) && (r.Condition.Length > 0))
{
string condExpression = "((RecordName = '" + theRecId.RecordName + "') and (RecordIndex = '" + index + "'))";
condExpression = condExpression + " and (" + r.Condition + ")";
if (ds.Field.Select(condExpression).Length > 0)
{
eor = r;
break;
}
}
else
{
eor = r;
}
}
return eor;
}
/// <summary>
/// Read from memory stream until predefined value of NEXT field definition is reached
/// </summary>
/// <param name="mem"></param>
/// <param name="pos"></param>
/// <param name="fields"></param>
/// <param name="fieldIndex"></param>
/// <param name="fieldRow"></param>
/// <returns></returns>
private string GetVariableLengthValue(LayoutDataset.FieldRow[] fields, int fieldIndex, LayoutDataset.FieldRow fieldRow)
{
// field has variable length, next field MUST have a predefined value
LayoutDataset.FieldRow nextRow = fields[fieldIndex + 1];
if (nextRow.IsPredefinedValueNull() || (nextRow.PredefinedValue.Length == 0))
{
throw new Exception("Parse error: variable length field " + fieldRow.Name + " MUST be followed by a field with predefined value.");
}
string dataValue = "";
string terminator = layout.Escape(nextRow.PredefinedValue);
while (idx < buffer.Length)
{
dataValue += buffer[idx];
if (dataValue.EndsWith(terminator))
{
break;
}
idx++;
}
// move file pointer back by length of predefined value,
idx -= terminator.Length;
// +1 to position at beginning of next field
idx++;
dataValue = dataValue.Remove(dataValue.Length - terminator.Length);
if (dataValue.Length == 0)
{
dataValue = null;
}
return dataValue;
}
public void UpdateSourceFromDataset()
{
if (ds.Record.Count > 0)
{
string s = "";
foreach(FormattedDataDataset.RecordRow recRow in ds.Record.Rows)
{
foreach(FormattedDataDataset.FieldRow fieldRow in ds.Field.Select("(RecordName = '" +
recRow.Name + "') and (RecordIndex = " + recRow.Index + ")"))
{
if (fieldRow.Length >= 0)
{
s += fieldRow.Value.PadLeft(fieldRow.Length).Substring(0, fieldRow.Length);
}
else
{
// variable length!
s += fieldRow.Value;
}
}
}
data.Source = s + _leftover;
}
}
/// <summary>
/// Check each record for an identifier and EOR field. Check lengths against pre-defined values.
/// </summary>
/// <returns>True if validation is OK, false if there are errors</returns>
protected Boolean ValidateLayout()
{
int autofix = -1;
Boolean hasErrors = false;
foreach (LayoutDataset.RecordRow recRow in layout.DataSet.Record.Rows)
{
Boolean hasEor = false;
Boolean hasIdentity = false;
// these fields are not sorted!
LayoutDataset.FieldRow[] fields = (LayoutDataset.FieldRow[])recRow.GetChildRows("RecordField");
foreach (LayoutDataset.FieldRow fieldRow in fields)
{
if (fieldRow.IsEOR)
{
hasEor = true;
}
if (fieldRow.IsRecordIdentifier)
{
hasIdentity = true;
}
if ((!fieldRow.IsPredefinedValueNull()) && (fieldRow.PredefinedValue.Length > 0))
{
string value = layout.Escape(fieldRow.PredefinedValue);
if ((value.Length != fieldRow.Length) && (value.IndexOf('|') < 0))
{
if (autofix < 0)
{
System.Windows.Forms.DialogResult answer = System.Windows.Forms.MessageBox.Show(
"Some fields have a mismatch between field-length and length of the predefined value." +
Environment.NewLine + "Should the field-length automatically be updated to the of the "+
"predefined value?",
"Record Editor",
System.Windows.Forms.MessageBoxButtons.YesNo,
System.Windows.Forms.MessageBoxIcon.Question);
autofix = (answer == System.Windows.Forms.DialogResult.Yes ? 1 : 0);
}
if (autofix > 0)
{
fieldRow.Length = value.Length;
}
else
{
logger.Log(Logger.MessageType.WARN, "[VALIDATION] - Field " + recRow.Name + "." + fieldRow.Name + " has mismatch between field length and length of predefined value.");
hasErrors = true;
}
}
}
}
if (!hasEor)
{
logger.Log(Logger.MessageType.WARN, "[VALIDATION] - Record '" + recRow.Name + "' has no end-of-record field.");
hasErrors = true;
}
if (!hasIdentity)
{
logger.Log(Logger.MessageType.WARN, "[VALIDATION] - Record '" + recRow.Name + "' has no identity field.");
hasErrors = true;
}
}
if (hasErrors)
{
logger.Log(Logger.MessageType.ERROR, "The current layout has errors. See \nthe 'formatting log' tab for details.");
return false;
}
return true;
}
/// <summary>
/// Read a string from buffer
/// </summary>
/// <param name="offset">Offset from current idx</param>
/// <param name="length"></param>
/// <returns></returns>
private String GetString(int offset, int length)
{
if (buffer.Length >= (idx + offset + length))
{
char[] result = new char[length];
Array.Copy(buffer, idx + offset, result, 0, length);
return new String(result);
}
return null;
}
public void SaveData()
{
data.Save();
}
public void SaveLayout()
{
layout.Save();
}
#region Properties
public Subject LayoutSubject
{
get { return layout; }
}
public Subject DataSubject
{
get { return data; }
}
/// <summary>
///
/// </summary>
public string DataFile
{
get { return data.FileName; }
set
{
data.Load(value);
FormatDatafile();
}
}
/// <summary>
///
/// </summary>
public string LayoutFile
{
get { return layout.FileName; }
set
{
layout.BeginUpdate();
layout.Load(value);
layout.EndUpdate();
FormatDatafile();
}
}
public string EolDelimiter
{
get { return _eolDelimiter; }
set { _eolDelimiter = value; }
}
public string OverrideLayoutFile
{
set {layout.FileName = value;}
}
public string OverrideDataFile
{
set {data.FileName = value;}
}
/// <summary>
///
/// </summary>
public string Source
{
get { return data.Source; }
set {
data.Source = value;
FormatDatafile();
}
}
/// <summary>
///
/// </summary>
public FormattedDataDataset DataSet
{
get { return ds; }
}
/// <summary>
///
/// </summary>
public LayoutDataset LayoutDataSet
{
get { return layout.DataSet; }
}
public string Error
{
get { return _error;}
}
public Boolean HasErrors
{
get { return hasErrors; }
}
#endregion
private void DataChanged(object sender, DataRowChangeEventArgs e)
{
if (!_isUpdating)
{
data.SetState(State.DIRTY);
}
}
/// <summary>
/// Get encoding
/// </summary>
public static Encoding DefaultEncoding {
get { return encoder; }
set { encoder = value; }
}
}
}