if(expectedSegments.size() == 0) {
return;
}
while(segmentMappingIndex < expectedSegments.size() && segmentReader.hasCurrentSegment()) {
SegmentGroup expectedSegmentGroup = expectedSegments.get(segmentMappingIndex);
int minOccurs = expectedSegmentGroup.getMinOccurs();
int maxOccurs = expectedSegmentGroup.getMaxOccurs();
// A negative max value indicates an unbound max....
if(maxOccurs < 0) {
maxOccurs = Integer.MAX_VALUE;
}
// Make sure min is not greater than max...
if(minOccurs > maxOccurs) {
maxOccurs = minOccurs;
}
// Only load the next segment if currentSegmentFields == null i.e. we don't have a set of
// preLoadedSegmentFields (see method args) that need to be processed first...
if(currentSegmentFields == null) {
currentSegmentFields = segmentReader.getCurrentSegmentFields();
}
// If the current segment being read from the incoming message doesn't match the expected
// segment code....
if(!currentSegmentFields[0].equals(expectedSegmentGroup.getSegcode())) {
Matcher matcher = expectedSegmentGroup.getSegcodePattern().matcher(segmentReader.getSegmentBuffer());
if (!matcher.matches()) {
if (segmentProcessingCount < minOccurs) {
// check if strict segment matching is inforced
if (!ignoreUnmappedSegment) {
// If we haven't read the minimum number of instances of the current "expected" segment, raise an error...
throw new EDIParseException(edifactModel.getEdimap(), "Must be a minimum of " + minOccurs + " instances of segment [" + expectedSegmentGroup.getSegcode() + "]. Currently at segment number " + segmentReader.getCurrentSegmentNumber() + ".", expectedSegmentGroup, segmentReader.getCurrentSegmentNumber(), segmentReader.getCurrentSegmentFields());
} else {
// skip unmapped current segment
segmentReader.moveToNextSegment();
currentSegmentFields = null;
// check that there still are messages in the EDI message stream for the required segments in the model
if (!segmentReader.hasCurrentSegment()) {
throw new EDIParseException(edifactModel.getEdimap(), "Reached end of EDI message stream but there must be a minimum of " + minOccurs + " instances of segment [" + expectedSegmentGroup.getSegcode() + "]. Currently at segment number " + segmentReader.getCurrentSegmentNumber() + ".", expectedSegmentGroup, segmentReader.getCurrentSegmentNumber(), null);
}
continue;
}
} else {
// Otherwise, move to the next "expected" segment and start the loop again...
segmentMappingIndex++;
segmentProcessingCount = 0;
continue;
}
}
}
if(segmentProcessingCount >= maxOccurs) {
// Move to the next "expected" segment and start the loop again...
segmentMappingIndex++;
segmentProcessingCount = 0;
continue;
}
// The current read message segment appears to match that expected according to the mapping model.
// Proceed to process the segment fields and the segments sub-segments...
if(expectedSegmentGroup instanceof Segment) {
mapSegment(currentSegmentFields, (Segment) expectedSegmentGroup);
} else {
startElement(expectedSegmentGroup, true);
mapSegments(expectedSegmentGroup.getSegments(), currentSegmentFields);
endElement(expectedSegmentGroup, true);
}
// Increment the count on the number of times the current "expected" mapping config has been applied...
segmentProcessingCount++;
currentSegmentFields = null;
if (segmentProcessingCount < minOccurs && !segmentReader.hasCurrentSegment()) {
throw new EDIParseException(edifactModel.getEdimap(), "Reached end of EDI message stream but there must be a minimum of " + minOccurs + " instances of segment [" + expectedSegmentGroup.getSegcode() + "]. Currently at segment number " + segmentReader.getCurrentSegmentNumber() + ".", expectedSegmentGroup, segmentReader.getCurrentSegmentNumber(), null);
}
}
}