Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reset Validator state at start of transaction #36

Merged
merged 4 commits into from
May 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ jobs:
run: mvn -B verify javadoc:javadoc --file pom.xml

quality:
if: github.event_name == 'push' && github.repository == 'xlate/staedi'
if: github.repository == 'xlate/staedi'
runs-on: ubuntu-latest
name: Verify Quality

Expand Down
38 changes: 38 additions & 0 deletions src/main/java/io/xlate/edi/internal/stream/LocationView.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,44 @@ protected LocationView() {
elementOccurrence = -1;
}

public String toString() {
StringBuilder display = new StringBuilder();

if (getSegmentPosition() < 0) {
display.append("at offset ");
display.append(getCharacterOffset());
} else {
display.append("in segment ");

if (getSegmentTag() != null) {
display.append(getSegmentTag());
} else {
display.append("???");
}

display.append(" at position ");
display.append(String.valueOf(getSegmentPosition()));

if (getElementPosition() > -1) {
display.append(", element ");
display.append(String.valueOf(getElementPosition()));

if (getElementOccurrence() > 1) {
display.append(" (occurrence ");
display.append(String.valueOf(getElementOccurrence()));
display.append(')');
}
}

if (getComponentPosition() > -1) {
display.append(", component ");
display.append(String.valueOf(getComponentPosition()));
}
}

return display.toString();
}

@Override
public int getLineNumber() {
return lineNumber;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -77,6 +78,7 @@ public class StaEDIStreamWriter implements EDIStreamWriter, ElementDataHandler,
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;
Expand Down Expand Up @@ -199,7 +201,10 @@ public void setControlSchema(Schema controlSchema) {

@Override
public void setTransactionSchema(Schema transactionSchema) {
transactionValidator = transactionSchema != null ? new Validator(transactionSchema, true, controlSchema) : null;
if (!Objects.equals(this.transactionSchema, transactionSchema)) {
this.transactionSchema = transactionSchema;
transactionValidator = transactionSchema != null ? new Validator(transactionSchema, true, controlSchema) : null;
}
}

@Override
Expand Down Expand Up @@ -604,6 +609,9 @@ public void loopBegin(CharSequence id) {
if (EDIType.Type.TRANSACTION.toString().equals(id)) {
transaction = true;
transactionSchemaAllowed = true;
if (transactionValidator != null) {
transactionValidator.reset();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.CharBuffer;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;

import io.xlate.edi.internal.stream.CharArraySequence;
import io.xlate.edi.internal.stream.StaEDIStreamLocation;
Expand Down Expand Up @@ -78,8 +79,10 @@ public Schema getTransactionSchema() {
}

public void setTransactionSchema(Schema transactionSchema) {
this.transactionSchema = transactionSchema;
transactionValidator = transactionSchema != null ? new Validator(transactionSchema, true, controlSchema) : null;
if (!Objects.equals(this.transactionSchema, transactionSchema)) {
this.transactionSchema = transactionSchema;
transactionValidator = transactionSchema != null ? new Validator(transactionSchema, true, controlSchema) : null;
}
}

public void resetEvents() {
Expand Down Expand Up @@ -152,6 +155,9 @@ public void loopBegin(CharSequence id) {
transaction = true;
transactionSchemaAllowed = true;
enqueueEvent(EDIStreamEvent.START_TRANSACTION, EDIStreamValidationError.NONE, id, null);
if (transactionValidator != null) {
transactionValidator.reset();
}
} else if (EDIType.Type.GROUP.toString().equals(id)) {
enqueueEvent(EDIStreamEvent.START_GROUP, EDIStreamValidationError.NONE, id, null);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,23 @@
******************************************************************************/
package io.xlate.edi.internal.stream.validation;

import static io.xlate.edi.stream.EDIStreamValidationError.*;
import static io.xlate.edi.stream.EDIStreamValidationError.IMPLEMENTATION_INVALID_CODE_VALUE;
import static io.xlate.edi.stream.EDIStreamValidationError.IMPLEMENTATION_LOOP_OCCURS_UNDER_MINIMUM_TIMES;
import static io.xlate.edi.stream.EDIStreamValidationError.IMPLEMENTATION_SEGMENT_BELOW_MINIMUM_USE;
import static io.xlate.edi.stream.EDIStreamValidationError.IMPLEMENTATION_TOO_FEW_REPETITIONS;
import static io.xlate.edi.stream.EDIStreamValidationError.IMPLEMENTATION_UNUSED_DATA_ELEMENT_PRESENT;
import static io.xlate.edi.stream.EDIStreamValidationError.IMPLEMENTATION_UNUSED_SEGMENT_PRESENT;
import static io.xlate.edi.stream.EDIStreamValidationError.INVALID_CODE_VALUE;
import static io.xlate.edi.stream.EDIStreamValidationError.LOOP_OCCURS_OVER_MAXIMUM_TIMES;
import static io.xlate.edi.stream.EDIStreamValidationError.MANDATORY_SEGMENT_MISSING;
import static io.xlate.edi.stream.EDIStreamValidationError.REQUIRED_DATA_ELEMENT_MISSING;
import static io.xlate.edi.stream.EDIStreamValidationError.SEGMENT_EXCEEDS_MAXIMUM_USE;
import static io.xlate.edi.stream.EDIStreamValidationError.SEGMENT_NOT_IN_DEFINED_TRANSACTION_SET;
import static io.xlate.edi.stream.EDIStreamValidationError.SEGMENT_NOT_IN_PROPER_SEQUENCE;
import static io.xlate.edi.stream.EDIStreamValidationError.TOO_MANY_COMPONENTS;
import static io.xlate.edi.stream.EDIStreamValidationError.TOO_MANY_DATA_ELEMENTS;
import static io.xlate.edi.stream.EDIStreamValidationError.TOO_MANY_REPETITIONS;
import static io.xlate.edi.stream.EDIStreamValidationError.UNEXPECTED_SEGMENT;

import java.util.ArrayList;
import java.util.Collections;
Expand Down Expand Up @@ -53,6 +69,7 @@ public class Validator {
private Schema containerSchema;
private Schema schema;
private final boolean validateCodeValues;
private boolean initial = true;

private final UsageNode root;
private final UsageNode implRoot;
Expand Down Expand Up @@ -120,6 +137,36 @@ public Validator(Schema schema, boolean validateCodeValues, Schema containerSche
}
}

public void reset() {
if (initial) {
return;
}

root.reset();
correctSegment = segment = root.getFirstChild();

if (implRoot != null) {
implRoot.reset();
implSegment = implRoot.getFirstChild();
}

cursor.reset(root, implRoot);
depth = 1;

segmentExpected = false;
composite = null;
element = null;

implSegmentSelected = false;
implComposite = null;
implElement = null;

implSegmentCandidates.clear();
useErrors.clear();
elementErrors.clear();
initial = true;
}

public boolean isPendingDiscrimination() {
return !implSegmentCandidates.isEmpty();
}
Expand Down Expand Up @@ -284,6 +331,7 @@ UsageNode completeLoop(ValidationEventHandler handler, UsageNode node) {
}

public void validateSegment(ValidationEventHandler handler, CharSequence tag) {
initial = false;
segmentExpected = true;
implSegmentSelected = false;

Expand Down
42 changes: 2 additions & 40 deletions src/main/java/io/xlate/edi/stream/EDIStreamException.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,44 +43,6 @@ public EDIStreamException(Throwable cause) {
location = null;
}

private static String displayLocation(Location location) {
StringBuilder display = new StringBuilder();

if (location.getSegmentPosition() < 0) {
display.append("at offset ");
display.append(location.getCharacterOffset());
} else {
display.append("in segment ");

if (location.getSegmentTag() != null) {
display.append(location.getSegmentTag());
} else {
display.append("???");
}

display.append(" at position ");
display.append(String.valueOf(location.getSegmentPosition()));

if (location.getElementPosition() > -1) {
display.append(", element ");
display.append(String.valueOf(location.getElementPosition()));

if (location.getElementOccurrence() > 1) {
display.append(" (occurrence ");
display.append(String.valueOf(location.getElementOccurrence()));
display.append(')');
}
}

if (location.getComponentPosition() > -1) {
display.append(", component ");
display.append(String.valueOf(location.getComponentPosition()));
}
}

return display.toString();
}

/**
* Construct an exception with the associated message, exception and
* location.
Expand All @@ -93,7 +55,7 @@ private static String displayLocation(Location location) {
* a nested error / exception
*/
public EDIStreamException(String message, Location location, Throwable cause) {
super(message + " " + displayLocation(location), cause);
super(message + " " + location.toString(), cause);
this.location = location;
}

Expand All @@ -107,7 +69,7 @@ public EDIStreamException(String message, Location location, Throwable cause) {
* the location of the error
*/
public EDIStreamException(String message, Location location) {
super(message + " " + displayLocation(location));
super(message + " " + location.toString());
this.location = location;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public EDIValidationException(EDIStreamEvent event,
EDIStreamValidationError error,
Location location,
CharSequence data) {
super();
super("Encountered " + event + " [" + error + "]" + (location != null ? " " + location.toString() : ""));
this.event = event;
this.error = error;
this.location = location;
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/X12/v00200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
<elementType name="E143" number="143" base="string" minLength="3" maxLength="3" />
<elementType name="E329" number="329" base="string" minLength="4" maxLength="9" />
<elementType name="E337" number="337" base="time" minLength="4" maxLength="8" />
<elementType name="E373" number="373" base="date" minLength="8" maxLength="8" />
<elementType name="E373" number="373" base="date" minLength="6" maxLength="8" />
<elementType name="E455" number="455" base="string" maxLength="2" />

<elementType name="E479" number="479" base="identifier" minLength="2" maxLength="2">
Expand Down
Loading