Skip to content

Commit

Permalink
Merge ed3c2a4 into 9a6c3f7
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeEdgar committed Sep 25, 2020
2 parents 9a6c3f7 + ed3c2a4 commit 2345878
Show file tree
Hide file tree
Showing 5 changed files with 2,505 additions and 23 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>io.xlate</groupId>
<artifactId>staedi</artifactId>
<version>1.11.1-SNAPSHOT</version>
<version>1.12.0-SNAPSHOT</version>

<name>StAEDI : Streaming API for EDI for Java</name>
<description>Streaming API for EDI for Java</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,13 @@ void reset(UsageNode root, UsageNode implRoot) {
standard = UsageNode.getFirstChild(root);
impl = UsageNode.getFirstChild(implRoot);
}

UsageCursor copy() {
UsageCursor copy = new UsageCursor();
copy.standard = this.standard;
copy.impl = this.impl;
return copy;
}
}

static class RevalidationNode {
Expand Down Expand Up @@ -405,6 +412,7 @@ public void validateSegment(ValidationEventHandler handler, CharSequence tag) {
clearElements();

final int startDepth = this.depth;
UsageCursor startLoop = null;

cursor.standard = correctSegment;
cursor.impl = implNode;
Expand All @@ -430,13 +438,15 @@ public void validateSegment(ValidationEventHandler handler, CharSequence tag) {
// Advance to the next segment in the loop
cursor.next(nextImpl); // Impl node may be unchanged
} else {
// End of the loop - check if the segment appears earlier in the loop
handled = checkPeerSegments(tag, cursor.standard, startDepth, handler);

if (!handled) {
// Determine if the segment is in a loop higher in the tree or in the transaction whatsoever
handled = checkParents(cursor, tag, startDepth, handler);
if (startLoop == null) {
/*
* Remember the position of the last known loop's segment in case
* the segment being validated is an earlier sibling that is out of
* proper sequence.
*/
startLoop = cursor.copy();
}
handled = handleLoopEnd(cursor, startLoop, tag, startDepth, handler);
}
}
}
Expand Down Expand Up @@ -620,10 +630,35 @@ boolean handleLoop(CharSequence tag, UsageNode current, UsageNode currentImpl, i
return true;
}

boolean checkPeerSegments(CharSequence tag, UsageNode current, int startDepth, ValidationEventHandler handler) {
boolean handleLoopEnd(UsageCursor cursor, UsageCursor startLoop, CharSequence tag, int startDepth, ValidationEventHandler handler) {
boolean handled;

if (depth > 1) {
// Determine if the segment is in a loop higher in the tree
cursor.nagivateUp(this.depth);
this.depth--;
handled = false;
} else {
// End of the loop - check if the segment appears earlier in the loop
handled = checkPeerSegments(tag, startLoop.standard, handler);

if (handled) {
// Found the segment among the last known segment's peers so reset the depth
this.depth = startDepth;
} else {
// Determine if the segment is in the transaction whatsoever
cursor.reset(this.root, this.implRoot);
handled = checkUnexpectedSegment(tag, cursor.standard, startDepth, handler);
}
}

return handled;
}

boolean checkPeerSegments(CharSequence tag, UsageNode current, ValidationEventHandler handler) {
boolean handled = false;

if (this.depth == startDepth && current != correctSegment) {
if (current != correctSegment) {
/*
* End of the loop; try to see if we can find the segment among
* the siblings of the last good segment. If the last good
Expand Down Expand Up @@ -652,20 +687,6 @@ boolean checkPeerSegments(CharSequence tag, UsageNode current, int startDepth, V
return handled;
}

boolean checkParents(UsageCursor cursor, CharSequence tag, int startDepth, ValidationEventHandler handler) {
boolean handled = false;

if (this.depth > 1) {
cursor.nagivateUp(this.depth);
this.depth--;
} else {
cursor.reset(this.root, this.implRoot);
handled = checkUnexpectedSegment(tag, cursor.standard, startDepth, handler);
}

return handled;
}

boolean checkUnexpectedSegment(CharSequence tag, UsageNode current, int startDepth, ValidationEventHandler handler) {
boolean handled = false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1757,4 +1757,41 @@ void testValidatorResetElement_Issue106() throws Exception {

assertEquals(0, unexpected.size());
}

/**
* Original issue: https://github.com/xlate/staedi/issues/122
*
* @throws Exception
*/
@Test
void testValidatorLookAhead_Issue122() throws Exception {
EDIInputFactory factory = EDIInputFactory.newFactory();
Schema transSchema = SchemaFactory.newFactory().createSchema(getClass().getResourceAsStream("/EDIFACT/issue122/BAPLIE-d95b.xml"));
EDIStreamReader reader = factory.createEDIStreamReader(getClass().getResourceAsStream("/EDIFACT/issue122/baplie-test.edi"));
List<Object> unexpected = new ArrayList<>();

try {
while (reader.hasNext()) {
switch (reader.next()) {
case START_TRANSACTION:
reader.setTransactionSchema(transSchema);
break;
case SEGMENT_ERROR:
case ELEMENT_OCCURRENCE_ERROR:
case ELEMENT_DATA_ERROR:
unexpected.add(reader.getErrorType());
break;
default:
break;
}
}
} catch (Exception e) {
unexpected.add(e);
} finally {
reader.close();
}

assertEquals(0, unexpected.size());
}

}
Loading

0 comments on commit 2345878

Please sign in to comment.