-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #301 from MikeEdgar/issue256
Validate control references and counters
- Loading branch information
Showing
36 changed files
with
541 additions
and
96 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
src/main/java/io/xlate/edi/internal/schema/ControlType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.xlate.edi.internal.schema; | ||
|
||
import java.util.List; | ||
|
||
import io.xlate.edi.schema.EDIControlType; | ||
import io.xlate.edi.schema.EDIElementPosition; | ||
import io.xlate.edi.schema.EDIReference; | ||
import io.xlate.edi.schema.EDISyntaxRule; | ||
import io.xlate.edi.schema.EDIType; | ||
|
||
@SuppressWarnings("java:S2160") // Intentionally inherit 'equals' from superclass | ||
class ControlType extends StructureType implements EDIControlType { | ||
|
||
private final EDIElementPosition headerRefPosition; | ||
private final EDIElementPosition trailerRefPosition; | ||
private final EDIElementPosition trailerCountPosition; | ||
private final EDIControlType.Type countType; | ||
|
||
@SuppressWarnings("java:S107") | ||
ControlType(String id, | ||
EDIType.Type type, | ||
String code, | ||
List<EDIReference> references, | ||
List<EDISyntaxRule> syntaxRules, | ||
EDIElementPosition headerRefPosition, | ||
EDIElementPosition trailerRefPosition, | ||
EDIElementPosition trailerCountPosition, | ||
EDIControlType.Type countType, | ||
String title, | ||
String description) { | ||
|
||
super(id, type, code, references, syntaxRules, title, description); | ||
this.headerRefPosition = headerRefPosition; | ||
this.trailerRefPosition = trailerRefPosition; | ||
this.trailerCountPosition = trailerCountPosition; | ||
this.countType = countType; | ||
} | ||
|
||
@Override | ||
public EDIElementPosition getHeaderRefPosition() { | ||
return headerRefPosition; | ||
} | ||
|
||
@Override | ||
public EDIElementPosition getTrailerRefPosition() { | ||
return trailerRefPosition; | ||
} | ||
|
||
@Override | ||
public EDIElementPosition getTrailerCountPosition() { | ||
return trailerCountPosition; | ||
} | ||
|
||
@Override | ||
public EDIControlType.Type getCountType() { | ||
return countType; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/main/java/io/xlate/edi/internal/stream/validation/ControlUsageNode.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.xlate.edi.internal.stream.validation; | ||
|
||
import java.util.List; | ||
import java.util.logging.Logger; | ||
|
||
import io.xlate.edi.schema.EDIControlType; | ||
import io.xlate.edi.schema.EDIElementPosition; | ||
import io.xlate.edi.schema.EDIReference; | ||
import io.xlate.edi.stream.EDIStreamValidationError; | ||
import io.xlate.edi.stream.Location; | ||
|
||
class ControlUsageNode extends UsageNode { | ||
|
||
static final Logger LOGGER = Logger.getLogger(ControlUsageNode.class.getName()); | ||
|
||
String referenceValue; | ||
EDIControlType type; | ||
int count; | ||
|
||
ControlUsageNode(UsageNode parent, int depth, EDIReference link, int siblingIndex) { | ||
super(parent, depth, link, siblingIndex); | ||
type = (EDIControlType) link.getReferencedType(); | ||
} | ||
|
||
@Override | ||
void reset() { | ||
super.reset(); | ||
this.referenceValue = null; | ||
this.count = 0; | ||
} | ||
|
||
@Override | ||
void incrementUsage() { | ||
super.incrementUsage(); | ||
this.referenceValue = null; | ||
this.count = 0; | ||
} | ||
|
||
boolean matchesLocation(int segmentRef, EDIElementPosition position, Location location) { | ||
return position != null | ||
&& position.matchesLocation(location) | ||
&& type.getReferences().get(segmentRef).getReferencedType().getId().equals(location.getSegmentTag()); | ||
} | ||
|
||
void validateReference(Location location, CharSequence value, List<EDIStreamValidationError> errors) { | ||
if (referenceValue == null) { | ||
if (matchesLocation(0, type.getHeaderRefPosition(), location)) { | ||
this.referenceValue = value.toString(); | ||
} | ||
return; | ||
} | ||
|
||
if (matchesLocation(type.getReferences().size() - 1, type.getTrailerRefPosition(), location) | ||
&& !referenceValue.contentEquals(value)) { | ||
errors.add(EDIStreamValidationError.CONTROL_REFERENCE_MISMATCH); | ||
} | ||
} | ||
|
||
void validateCount(Location location, CharSequence value, List<EDIStreamValidationError> errors) { | ||
if (matchesLocation(type.getReferences().size() - 1, type.getTrailerCountPosition(), location) | ||
// Don't bother comparing the actual value if it's not formatted correctly | ||
&& !errors.contains(EDIStreamValidationError.INVALID_CHARACTER_DATA) | ||
&& !String.valueOf(count).contentEquals(value)) { | ||
errors.add(EDIStreamValidationError.CONTROL_COUNT_DOES_NOT_MATCH_ACTUAL_COUNT); | ||
} | ||
} | ||
|
||
int incrementCount(EDIControlType.Type countType) { | ||
if (this.type.getCountType() == countType) { | ||
count++; | ||
return count; | ||
} | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.