/
StaEDIStreamWriter.java
1011 lines (854 loc) · 33.6 KB
/
StaEDIStreamWriter.java
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
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
* Copyright 2017 xlate.io LLC, http://www.xlate.io
*
* 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 io.xlate.edi.internal.stream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;
import java.util.logging.Logger;
import io.xlate.edi.internal.stream.tokenization.CharacterClass;
import io.xlate.edi.internal.stream.tokenization.CharacterSet;
import io.xlate.edi.internal.stream.tokenization.Dialect;
import io.xlate.edi.internal.stream.tokenization.DialectFactory;
import io.xlate.edi.internal.stream.tokenization.EDIException;
import io.xlate.edi.internal.stream.tokenization.EDIFACTDialect;
import io.xlate.edi.internal.stream.tokenization.ElementDataHandler;
import io.xlate.edi.internal.stream.tokenization.State;
import io.xlate.edi.internal.stream.tokenization.ValidationEventHandler;
import io.xlate.edi.internal.stream.validation.UsageError;
import io.xlate.edi.internal.stream.validation.Validator;
import io.xlate.edi.schema.EDIReference;
import io.xlate.edi.schema.EDIType;
import io.xlate.edi.schema.Schema;
import io.xlate.edi.stream.EDIOutputErrorReporter;
import io.xlate.edi.stream.EDIOutputFactory;
import io.xlate.edi.stream.EDIStreamConstants.Delimiters;
import io.xlate.edi.stream.EDIStreamEvent;
import io.xlate.edi.stream.EDIStreamException;
import io.xlate.edi.stream.EDIStreamValidationError;
import io.xlate.edi.stream.EDIStreamWriter;
import io.xlate.edi.stream.EDIValidationException;
import io.xlate.edi.stream.Location;
public class StaEDIStreamWriter implements EDIStreamWriter, ElementDataHandler, ValidationEventHandler {
static final Logger LOGGER = Logger.getLogger(StaEDIStreamWriter.class.getName());
private static final int LEVEL_INITIAL = 0;
private static final int LEVEL_INTERCHANGE = 1;
private static final int LEVEL_SEGMENT = 2;
private static final int LEVEL_ELEMENT = 3;
private static final int LEVEL_COMPOSITE = 4;
private static final int LEVEL_COMPONENT = 5;
private int level;
private State state = State.INITIAL;
private CharacterSet characters = new CharacterSet();
private final OutputStream stream;
private final OutputStreamWriter writer;
private final Map<String, Object> properties;
private final EDIOutputErrorReporter reporter;
private Dialect dialect;
CharBuffer unconfirmedBuffer = CharBuffer.allocate(500);
private final StaEDIStreamLocation location;
private Schema controlSchema;
private Validator controlValidator;
private boolean transactionSchemaAllowed = false;
private boolean transaction = false;
private Schema transactionSchema;
private Validator transactionValidator;
private CharArraySequence dataHolder = new CharArraySequence();
private boolean atomicElementWrite = false;
private CharBuffer elementBuffer = CharBuffer.allocate(500);
private final StringBuilder formattedElement = new StringBuilder();
private List<EDIValidationException> errors = new ArrayList<>();
private CharArraySequence elementHolder = new CharArraySequence();
private char segmentTerminator;
private char segmentTagTerminator;
private char dataElementSeparator;
private char componentElementSeparator;
private char repetitionSeparator;
private char decimalMark;
private char releaseIndicator;
final boolean emptyElementTruncation;
final boolean formatElements;
private final boolean prettyPrint;
private String prettyPrintString;
private long elementLength = 0;
private int emptyElements = 0;
private boolean unterminatedElement = false;
private int emptyComponents = 0;
private boolean unterminatedComponent = false;
public StaEDIStreamWriter(OutputStream stream, Charset charset, Map<String, Object> properties, EDIOutputErrorReporter reporter) {
this.stream = stream;
this.writer = new OutputStreamWriter(stream, charset);
this.properties = new HashMap<>(properties);
this.reporter = reporter;
this.emptyElementTruncation = booleanValue(properties.get(EDIOutputFactory.TRUNCATE_EMPTY_ELEMENTS));
this.prettyPrint = booleanValue(properties.get(EDIOutputFactory.PRETTY_PRINT));
this.formatElements = booleanValue(properties.get(EDIOutputFactory.FORMAT_ELEMENTS));
this.location = new StaEDIStreamLocation();
}
boolean booleanValue(Object value) {
if (value instanceof Boolean) {
return (Boolean) value;
}
if (value instanceof String) {
return Boolean.valueOf(value.toString());
}
if (value == null) {
return false;
}
LOGGER.warning(() -> "Value [" + value + "] could not be converted to boolean");
return false;
}
private void setupDelimiters() {
segmentTerminator = getDelimiter(properties, Delimiters.SEGMENT, dialect::getSegmentTerminator);
segmentTagTerminator = dialect.getSegmentTagTerminator(); // Not configurable - TRADACOMS
dataElementSeparator = getDelimiter(properties, Delimiters.DATA_ELEMENT, dialect::getDataElementSeparator);
componentElementSeparator = getDelimiter(properties, Delimiters.COMPONENT_ELEMENT, dialect::getComponentElementSeparator);
decimalMark = getDelimiter(properties, Delimiters.DECIMAL, dialect::getDecimalMark);
releaseIndicator = getDelimiter(properties, Delimiters.RELEASE, dialect::getReleaseIndicator);
repetitionSeparator = getDelimiter(properties, Delimiters.REPETITION, dialect::getRepetitionSeparator);
String lineSeparator = System.getProperty("line.separator");
if (prettyPrint && lineSeparator.indexOf(segmentTerminator) < 0) {
// Do not add the line separator after the segment terminator if they conflict. I.e., the separater contains the terminator
prettyPrintString = lineSeparator;
} else {
prettyPrintString = "";
}
}
private boolean areDelimitersSpecified() {
return Arrays.asList(Delimiters.SEGMENT,
Delimiters.DATA_ELEMENT,
Delimiters.COMPONENT_ELEMENT,
Delimiters.REPETITION,
Delimiters.DECIMAL,
Delimiters.RELEASE)
.stream()
.anyMatch(properties::containsKey);
}
char getDelimiter(Map<String, Object> properties, String key, Supplier<Character> dialectSupplier) {
if (properties.containsKey(key) && !dialect.isConfirmed()) {
return (char) properties.get(key);
}
return dialectSupplier.get();
}
static void putDelimiter(String key, char value, Map<String, Character> delimiters) {
if (value != '\0') {
delimiters.put(key, value);
}
}
private static void ensureArgs(int arrayLength, int start, int end) {
if (start < 0 || start >= arrayLength || end > arrayLength) {
throw new IndexOutOfBoundsException();
}
if (end < start) {
throw new IllegalArgumentException();
}
}
private void ensureState(State s) {
if (this.state != s) {
throw new IllegalStateException();
}
}
private void ensureLevel(int l) {
if (this.level != l) {
throw new IllegalStateException();
}
}
private void ensureLevelAtLeast(int lvl) {
if (this.level < lvl) {
throw new IllegalStateException();
}
}
private void ensureLevelBetween(int min, int max) {
if (this.level < min || this.level > max) {
throw new IllegalStateException();
}
}
@Override
public Object getProperty(String name) {
if (name == null) {
throw new IllegalArgumentException("Name must not be null");
}
return properties.get(name);
}
@Override
public void close() throws EDIStreamException {
flush();
// Do not close the stream
}
@Override
public void flush() throws EDIStreamException {
try {
writer.flush();
stream.flush();
} catch (IOException e) {
throw new EDIStreamException("Exception flushing output stream", location, e);
}
}
@Override
public Schema getControlSchema() {
return this.controlSchema;
}
@Override
public void setControlSchema(Schema controlSchema) {
ensureLevel(LEVEL_INITIAL);
this.controlSchema = controlSchema;
controlValidator = Validator.forSchema(controlSchema, null, true, formatElements);
}
@Override
public void setTransactionSchema(Schema transactionSchema) {
if (!Objects.equals(this.transactionSchema, transactionSchema)) {
this.transactionSchema = transactionSchema;
transactionValidator = Validator.forSchema(transactionSchema, controlSchema, true, formatElements);
}
}
@Override
public Location getLocation() {
return location;
}
@Override
public String getStandard() {
if (dialect == null) {
throw new IllegalStateException("standard not accessible");
}
return dialect.getStandard();
}
@Override
public Map<String, Character> getDelimiters() {
if (dialect == null) {
throw new IllegalStateException("standard not accessible");
}
Map<String, Character> delimiters = new HashMap<>(6);
putDelimiter(Delimiters.SEGMENT, segmentTerminator, delimiters);
putDelimiter(Delimiters.DATA_ELEMENT, dataElementSeparator, delimiters);
putDelimiter(Delimiters.COMPONENT_ELEMENT, componentElementSeparator, delimiters);
putDelimiter(Delimiters.REPETITION, repetitionSeparator, delimiters);
putDelimiter(Delimiters.DECIMAL, decimalMark, delimiters);
putDelimiter(Delimiters.RELEASE, releaseIndicator, delimiters);
return delimiters;
}
private Optional<Validator> validator() {
Validator validator;
// Do not use the transactionValidator in the period where it may be set/mutated by the user
if (transaction && !transactionSchemaAllowed) {
validator = transactionValidator;
} else {
validator = controlValidator;
}
return Optional.ofNullable(validator);
}
private void write(int output) throws EDIStreamException {
write(output, false);
}
private void write(int output, boolean isPrettyPrint) throws EDIStreamException {
CharacterClass clazz;
clazz = characters.getClass(output);
if (clazz == CharacterClass.INVALID) {
throw new EDIStreamException(String.format("Invalid character: 0x%04X", output), location);
}
state = State.transition(state, dialect, clazz);
switch (state) {
case HEADER_X12_I: // I(SA)
case HEADER_EDIFACT_U: // U(NA) or U(NB)
case HEADER_TRADACOMS_S: // S(TX)
unconfirmedBuffer.clear();
writeHeader((char) output, isPrettyPrint);
break;
case HEADER_X12_S:
case HEADER_EDIFACT_N:
case HEADER_TRADACOMS_T:
case INTERCHANGE_CANDIDATE:
case HEADER_DATA:
case HEADER_ELEMENT_END:
case HEADER_COMPONENT_END:
case HEADER_SEGMENT_END:
writeHeader((char) output, isPrettyPrint);
break;
case INVALID:
throw new EDIException(String.format("Invalid state: %s; output 0x%04X", state, output));
default:
writeOutput(output);
break;
}
}
void writeHeader(char output, boolean isPrettyPrint) throws EDIStreamException {
if (!isPrettyPrint && !dialect.appendHeader(characters, output)) {
throw new EDIStreamException(String.format("Unexpected header character: 0x%04X [%s]", (int) output, output), location);
}
unconfirmedBuffer.append(output);
if (dialect.isConfirmed()) {
// Set up the delimiters again once the dialect has confirmed them
setupDelimiters();
// Switching to non-header states to proceed after dialect is confirmed
switch (state) {
case HEADER_DATA:
state = State.TAG_SEARCH;
break;
case HEADER_ELEMENT_END:
state = State.ELEMENT_END;
break;
case HEADER_COMPONENT_END:
state = State.COMPONENT_END;
break;
case HEADER_SEGMENT_END:
state = State.SEGMENT_END;
break;
default:
throw new IllegalStateException("Confirmed at state " + state);
}
unconfirmedBuffer.flip();
if (EDIFACTDialect.UNA.equals(dialect.getHeaderTag())) {
// Overlay the UNA segment repetition separator now that it has be confirmed
unconfirmedBuffer.put(7, this.repetitionSeparator > 0 ? this.repetitionSeparator : ' ');
}
while (unconfirmedBuffer.hasRemaining()) {
writeOutput(unconfirmedBuffer.get());
}
} else if (dialect.isRejected()) {
state = State.INITIAL;
throw new EDIStreamException(dialect.getRejectionMessage(), location);
}
}
void writeOutput(int output) throws EDIStreamException {
try {
location.incrementOffset(output);
writer.write(output);
} catch (IOException e) {
throw new EDIStreamException("Exception to output stream", location, e);
}
}
@Override
public EDIStreamWriter startInterchange() throws EDIStreamException {
ensureLevel(LEVEL_INITIAL);
ensureState(State.INITIAL);
level = LEVEL_INTERCHANGE;
return this;
}
@Override
public EDIStreamWriter endInterchange() throws EDIStreamException {
ensureLevel(LEVEL_INTERCHANGE);
level = LEVEL_INITIAL;
flush();
return this;
}
@Override
public EDIStreamWriter writeStartSegment(String name) throws EDIStreamException {
ensureLevel(LEVEL_INTERCHANGE);
location.incrementSegmentPosition(name);
if (state == State.INITIAL) {
dialect = DialectFactory.getDialect(name);
setupDelimiters();
if (dialect instanceof EDIFACTDialect) {
if (EDIFACTDialect.UNB.equals(name) && areDelimitersSpecified()) {
/*
* Writing the EDIFACT header when delimiters were given via properties requires that
* a UNA is written first.
*/
dialect = DialectFactory.getDialect(EDIFACTDialect.UNA);
writeServiceAdviceString();
segmentValidation(name);
// Now write the UNB
writeString(name);
} else {
if (EDIFACTDialect.UNA.equals(name)) {
writeString(name);
writeServiceAdviceCharacters();
} else {
segmentValidation(name);
writeString(name);
}
}
} else {
segmentValidation(name);
writeString(name);
}
} else {
segmentValidation(name);
writeString(name);
}
level = LEVEL_SEGMENT;
emptyElements = 0;
terminateSegmentTag();
return this;
}
void segmentValidation(String name) {
validate(validator -> validator.validateSegment(this, name));
if (exitTransaction(name)) {
transaction = false;
validate(validator -> validator.validateSegment(this, name));
}
}
void terminateSegmentTag() throws EDIStreamException {
if (this.segmentTagTerminator != '\0') {
write(this.segmentTagTerminator);
// TRADACOMS - Empty segments not supported due to fixed segment tag terminator requirement
unterminatedElement = false;
} else {
// Treat the segment tag as an unterminated element that must be closed when element data is encountered
unterminatedElement = true;
}
}
void writeServiceAdviceString() throws EDIStreamException {
writeString(EDIFACTDialect.UNA);
writeServiceAdviceCharacters();
writeSegmentTerminator();
}
void writeServiceAdviceCharacters() throws EDIStreamException {
write(this.componentElementSeparator);
write(this.dataElementSeparator);
write(this.decimalMark);
write(this.releaseIndicator);
// This will be re-written once the dialect version is detected
write(this.repetitionSeparator);
}
private void writeString(String value) throws EDIStreamException {
for (int i = 0, m = value.length(); i < m; i++) {
write(value.charAt(i));
}
}
void writeSegmentTerminator() throws EDIStreamException {
write(this.segmentTerminator);
if (prettyPrint) {
for (int i = 0, m = prettyPrintString.length(); i < m; i++) {
write(prettyPrintString.charAt(i), true);
}
}
}
boolean exitTransaction(CharSequence tag) {
return transaction && !transactionSchemaAllowed && controlSchema != null
&& controlSchema.containsSegment(tag.toString());
}
@Override
public EDIStreamWriter writeEndSegment() throws EDIStreamException {
ensureLevelAtLeast(LEVEL_SEGMENT);
if (level > LEVEL_SEGMENT) {
validateElement(this.elementBuffer::flip, this.elementBuffer);
}
validate(validator -> validator.validateSyntax(dialect, this, this, location, false));
if (state == State.ELEMENT_DATA_BINARY) {
state = State.ELEMENT_END_BINARY;
}
writeSegmentTerminator();
switch (state) {
case SEGMENT_END:
case HEADER_SEGMENT_END:
case INITIAL: // Ending final segment of the interchange
break;
default:
if (state.isHeaderState()) {
Optional<String> invalidHeaderReason = dialect.assertValidHeaderEnd();
if (invalidHeaderReason.isPresent()) {
throw new EDIStreamException(invalidHeaderReason.get());
}
}
break;
}
level = LEVEL_INTERCHANGE;
location.clearSegmentLocations();
transactionSchemaAllowed = false;
return this;
}
@Override
public EDIStreamWriter writeStartElement() throws EDIStreamException {
ensureLevel(LEVEL_SEGMENT);
level = LEVEL_ELEMENT;
location.incrementElementPosition();
elementBuffer.clear();
elementLength = 0;
emptyComponents = 0;
unterminatedComponent = false;
if (!emptyElementTruncation && unterminatedElement) {
write(this.dataElementSeparator);
}
return this;
}
@Override
public EDIStreamWriter writeStartElementBinary() throws EDIStreamException {
writeStartElement();
state = State.ELEMENT_DATA_BINARY;
return this;
}
@Override
public EDIStreamWriter writeRepeatElement() throws EDIStreamException {
ensureLevelAtLeast(LEVEL_SEGMENT);
write(this.repetitionSeparator);
// The repetition separator was used instead of the data element separator
unterminatedElement = false;
level = LEVEL_ELEMENT;
location.incrementElementOccurrence();
elementLength = 0;
emptyComponents = 0;
unterminatedComponent = false;
return this;
}
@Override
public EDIStreamWriter endElement() throws EDIStreamException {
ensureLevelAtLeast(LEVEL_ELEMENT);
if (!atomicElementWrite) {
if (level > LEVEL_ELEMENT) {
validate(validator -> validator.validateSyntax(dialect, this, this, location, true));
} else {
validateElement(this.elementBuffer::flip, this.elementBuffer);
}
}
location.clearComponentPosition();
level = LEVEL_SEGMENT;
if (elementLength > 0) {
unterminatedElement = true;
} else {
emptyElements++;
}
if (state == State.ELEMENT_DATA_BINARY) {
state = State.ELEMENT_END_BINARY;
}
return this;
}
@Override
public EDIStreamWriter startComponent() throws EDIStreamException {
ensureLevelBetween(LEVEL_ELEMENT, LEVEL_COMPOSITE);
if (state == State.ELEMENT_DATA_BINARY) {
throw new IllegalStateException();
}
if (LEVEL_ELEMENT == level) {
// Level is LEVEL_ELEMENT only for the first component
validateCompositeOccurrence();
}
if (LEVEL_COMPOSITE == level && !emptyElementTruncation) {
write(this.componentElementSeparator);
}
level = LEVEL_COMPONENT;
location.incrementComponentPosition();
elementBuffer.clear();
elementLength = 0;
return this;
}
@Override
public EDIStreamWriter endComponent() throws EDIStreamException {
ensureLevel(LEVEL_COMPONENT);
if (!atomicElementWrite) {
validateElement(this.elementBuffer::flip, this.elementBuffer);
}
if (elementLength > 0) {
unterminatedComponent = true;
} else {
emptyComponents++;
}
level = LEVEL_COMPOSITE;
return this;
}
@Override
public EDIStreamWriter writeElement(CharSequence text) throws EDIStreamException {
atomicElementWrite = true;
writeStartElement();
CharSequence value = validateElement(() -> {}, text);
writeElementData(value);
endElement();
atomicElementWrite = false;
return this;
}
@Override
public EDIStreamWriter writeElement(char[] text, int start, int end) throws EDIStreamException {
atomicElementWrite = true;
writeStartElement();
CharSequence value = validateElement(() -> dataHolder.set(text, start, start + end), dataHolder);
writeElementData(value);
endElement();
atomicElementWrite = false;
return this;
}
@Override
public EDIStreamWriter writeEmptyElement() throws EDIStreamException {
atomicElementWrite = true;
writeStartElement();
// Ignore possibly-formatted value
validateElement(dataHolder::clear, dataHolder);
endElement();
atomicElementWrite = false;
return this;
}
@Override
public EDIStreamWriter writeComponent(CharSequence text) throws EDIStreamException {
atomicElementWrite = true;
startComponent();
CharSequence value = validateElement(() -> {}, text);
writeElementData(value);
endComponent();
atomicElementWrite = false;
return this;
}
@Override
public EDIStreamWriter writeComponent(char[] text, int start, int end) throws EDIStreamException {
atomicElementWrite = true;
startComponent();
CharSequence value = validateElement(() -> dataHolder.set(text, start, start + end), dataHolder);
writeElementData(value);
endComponent();
atomicElementWrite = false;
return this;
}
@Override
public EDIStreamWriter writeEmptyComponent() throws EDIStreamException {
atomicElementWrite = true;
startComponent();
// Ignore possibly-formatted value
validateElement(dataHolder::clear, dataHolder);
endComponent();
atomicElementWrite = false;
return this;
}
void writeRequiredSeparators(int dataLength) throws EDIStreamException {
if (dataLength < 1 || !emptyElementTruncation) {
return;
}
writeRequiredSeparator(emptyElements, unterminatedElement, this.dataElementSeparator);
emptyElements = 0;
unterminatedElement = false;
if (level == LEVEL_COMPONENT) {
writeRequiredSeparator(emptyComponents, unterminatedComponent, this.componentElementSeparator);
emptyComponents = 0;
unterminatedComponent = false;
}
}
void writeRequiredSeparator(int emptyCount, boolean unterminated, char separator) throws EDIStreamException {
for (int i = 0; i < emptyCount; i++) {
write(separator);
}
if (unterminated) {
write(separator);
}
}
@Override
public EDIStreamWriter writeElementData(CharSequence text) throws EDIStreamException {
ensureLevelAtLeast(LEVEL_ELEMENT);
writeRequiredSeparators(text.length());
for (int i = 0, m = text.length(); i < m; i++) {
char curr = text.charAt(i);
if (characters.isDelimiter(curr)) {
if (releaseIndicator > 0) {
write(releaseIndicator);
} else {
throw new IllegalArgumentException("Value contains separator: " + curr);
}
}
write(curr);
elementBuffer.put(curr);
elementLength++;
}
return this;
}
@Override
public EDIStreamWriter writeElementData(char[] text, int start, int end) throws EDIStreamException {
ensureLevelAtLeast(LEVEL_ELEMENT);
ensureArgs(text.length, start, end);
writeRequiredSeparators(end - start);
for (int i = start, m = end; i < m; i++) {
char curr = text[i];
if (characters.isDelimiter(curr)) {
throw new IllegalArgumentException("Value contains separator");
}
write(curr);
elementBuffer.put(curr);
elementLength++;
}
return this;
}
@Override
public EDIStreamWriter writeBinaryData(InputStream binaryStream) throws EDIStreamException {
ensureLevel(LEVEL_ELEMENT);
ensureState(State.ELEMENT_DATA_BINARY);
int output;
try {
writeRequiredSeparators(binaryStream.available());
flush(); // Write `Writer` buffers to stream before writing binary
while ((output = binaryStream.read()) != -1) {
location.incrementOffset(output);
stream.write(output);
elementLength++;
}
} catch (IOException e) {
throw new EDIStreamException("Exception writing binary element data", location, e);
}
return this;
}
@Override
public EDIStreamWriter writeBinaryData(byte[] binary, int start, int end) throws EDIStreamException {
ensureLevel(LEVEL_ELEMENT);
ensureState(State.ELEMENT_DATA_BINARY);
ensureArgs(binary.length, start, end);
writeRequiredSeparators(end - start);
try {
flush(); // Write `Writer` buffers to stream before writing binary
for (int i = start; i < end; i++) {
location.incrementOffset(binary[i]);
stream.write(binary[i]);
elementLength++;
}
} catch (IOException e) {
throw new EDIStreamException("Exception writing binary element data", location, e);
}
return this;
}
@Override
public EDIStreamWriter writeBinaryData(ByteBuffer binary) throws EDIStreamException {
ensureLevel(LEVEL_ELEMENT);
ensureState(State.ELEMENT_DATA_BINARY);
writeRequiredSeparators(binary.remaining());
while (binary.hasRemaining()) {
write(binary.get());
elementLength++;
}
return this;
}
@Override
public boolean binaryData(InputStream binary) {
// No operation
return true;
}
@Override
public boolean elementData(char[] text, int start, int length) {
elementHolder.set(text, start, length);
dialect.elementData(elementHolder, location);
validator().ifPresent(validator -> {
if (!validator.validateElement(dialect, location, elementHolder, null)) {
reportElementErrors(validator, elementHolder);
}
});
return true;
}
@Override
public void loopBegin(EDIReference typeReference) {
final String loopCode = typeReference.getReferencedType().getCode();
if (EDIType.Type.TRANSACTION.toString().equals(loopCode)) {
transaction = true;
transactionSchemaAllowed = true;
if (transactionValidator != null) {
transactionValidator.reset();
}
}
}
@Override
public void loopEnd(EDIReference typeReference) {
final String loopCode = typeReference.getReferencedType().getCode();
if (EDIType.Type.TRANSACTION.toString().equals(loopCode)) {
transaction = false;
dialect.transactionEnd();
} else if (EDIType.Type.GROUP.toString().equals(loopCode)) {
dialect.groupEnd();
}
}
@Override
public void elementError(EDIStreamEvent event,
EDIStreamValidationError error,
EDIReference typeReference,
CharSequence data,
int element,
int component,
int repetition) {
StaEDIStreamLocation copy = location.copy();
copy.setElementPosition(element);
copy.setElementOccurrence(repetition);
copy.setComponentPosition(component);
if (this.reporter != null) {
this.reporter.report(error, this, copy, data, typeReference);
} else {
errors.add(new EDIValidationException(event, error, copy, data));
}
}
@Override
public void segmentError(CharSequence token, EDIReference typeReference, EDIStreamValidationError error) {
if (this.reporter != null) {
this.reporter.report(error, this, this.getLocation(), token, typeReference);
} else {
errors.add(new EDIValidationException(EDIStreamEvent.SEGMENT_ERROR, error, location, token));
}
}
private void validate(Consumer<Validator> command) {
validator().ifPresent(validator -> {
errors.clear();
command.accept(validator);
if (!errors.isEmpty()) {
throw validationExceptionChain(errors);
}
});
}
private void validateCompositeOccurrence() {
validator().ifPresent(validator -> {
errors.clear();
if (!validator.validCompositeOccurrences(dialect, location)) {
reportElementErrors(validator, "");
}
if (!errors.isEmpty()) {
throw validationExceptionChain(errors);
}
});
}
private CharSequence validateElement(Runnable setupCommand, CharSequence data) {
return validator().map(validator -> {
CharSequence elementData;
if (this.formatElements) {
elementData = this.formattedElement;
this.formattedElement.setLength(0);
this.formattedElement.append(data); // Validator will clear and re-format if configured
} else {
elementData = data;
}
errors.clear();
setupCommand.run();
if (!validator.validateElement(dialect, location, data, this.formattedElement)) {
reportElementErrors(validator, elementData);
}
if (!errors.isEmpty()) {
throw validationExceptionChain(errors);
}
dialect.elementData(elementData, location);
return elementData;
}).orElse(data);
}
void reportElementErrors(Validator validator, CharSequence data) {
for (UsageError error : validator.getElementErrors()) {
elementError(error.getError().getCategory(),
error.getError(),
error.getTypeReference(),
data,
location.getElementPosition(),
location.getComponentPosition(),
location.getElementOccurrence());
}
}
EDIValidationException validationExceptionChain(List<EDIValidationException> errors) {
Iterator<EDIValidationException> iter = errors.iterator();
EDIValidationException first = iter.next();