Skip to content

Commit

Permalink
Move getCode to EDIType, allow code on elementType and segment impls
Browse files Browse the repository at this point in the history
- Deprecate EDISimpleType getNumber/number attribute
- Update X12 control schemas to V4
- Use NCName types instead of ID/IDREF in V4 schema
  • Loading branch information
MikeEdgar committed Jun 8, 2020
1 parent ef452c3 commit e40a856
Show file tree
Hide file tree
Showing 22 changed files with 369 additions and 235 deletions.
19 changes: 16 additions & 3 deletions src/main/java/io/xlate/edi/internal/schema/ElementType.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,18 @@
@SuppressWarnings("java:S2160") // Intentionally inherit 'equals' from superclass
class ElementType extends BasicType implements EDISimpleType {

private static final String TOSTRING_FORMAT = "id: %s, type: %s, base: %s, number: %d, minLength: %d, maxLength: %d, values: %s";
private static final String TOSTRING_FORMAT = "id: %s, type: %s, base: %s, code: %s, minLength: %d, maxLength: %d, values: %s";
private Base base;
private String code;
private int number;
private long minLength;
private long maxLength;
private Set<String> values;

ElementType(String id, Base base, int number, long minLength, long maxLength, Set<String> values) {
ElementType(String id, Base base, String code, int number, long minLength, long maxLength, Set<String> values) {
super(id, Type.ELEMENT);
this.base = base;
this.code = code;
this.number = number;
this.minLength = minLength;
this.maxLength = maxLength;
Expand All @@ -42,7 +44,7 @@ class ElementType extends BasicType implements EDISimpleType {

@Override
public String toString() {
return String.format(TOSTRING_FORMAT, getId(), getType(), base, number, minLength, maxLength, values);
return String.format(TOSTRING_FORMAT, getId(), getType(), base, code, minLength, maxLength, values);
}

@Override
Expand All @@ -51,6 +53,17 @@ public Base getBase() {
}

@Override
public String getCode() {
return code;
}

/**
* @see io.xlate.edi.schema.EDISimpleType#getNumber()
* @deprecated
*/
@SuppressWarnings({ "java:S1123", "java:S1133" })
@Override
@Deprecated
public int getNumber() {
return number;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ abstract class SchemaReaderBase implements SchemaReader {

static final EDISimpleType ANY_ELEMENT = new ElementType(StaEDISchema.ANY_ELEMENT_ID,
Base.STRING,
"ANY",
0,
0,
99_999,
Expand Down Expand Up @@ -527,6 +528,7 @@ List<Integer> readSyntaxPositions(XMLStreamReader reader) {

ElementType readSimpleType(XMLStreamReader reader) {
String name = parseAttribute(reader, "name", String::valueOf);
String code = parseAttribute(reader, "code", String::valueOf, name);
String base = parseAttribute(reader, "base", String::valueOf, EDISimpleType.Base.STRING.toString());
EDISimpleType.Base intBase = null;

Expand All @@ -540,7 +542,7 @@ ElementType readSimpleType(XMLStreamReader reader) {
long minLength = parseAttribute(reader, "minLength", Long::parseLong, 1L);
long maxLength = parseAttribute(reader, "maxLength", Long::parseLong, 1L);

return new ElementType(name, intBase, number, minLength, maxLength, readEnumerationValues(reader));
return new ElementType(name, intBase, code, number, minLength, maxLength, readEnumerationValues(reader));
}

Set<String> readEnumerationValues(XMLStreamReader reader) {
Expand Down
77 changes: 32 additions & 45 deletions src/main/java/io/xlate/edi/internal/schema/SchemaReaderV3.java
Original file line number Diff line number Diff line change
Expand Up @@ -297,14 +297,27 @@ void readLoopSequenceEntry(QName entryName, List<EDITypeImplementation> sequence
SegmentImpl readSegmentImplementation() {
List<EDITypeImplementation> sequence = new ArrayList<>();
String typeId = parseAttribute(reader, "type", String::valueOf);
String code = parseAttribute(reader, "code", String::valueOf, typeId);
int minOccurs = parseAttribute(reader, ATTR_MIN_OCCURS, Integer::parseInt, -1);
int maxOccurs = parseAttribute(reader, ATTR_MAX_OCCURS, Integer::parseInt, -1);
BigDecimal discriminatorAttr = parseAttribute(reader, ATTR_DISCRIMINATOR, BigDecimal::new, null);
String title = parseAttribute(reader, ATTR_TITLE, String::valueOf, null);

return readTypeImplementation(reader,
() -> readSequence(reader, e -> readPositionedSequenceEntry(e, sequence, true)),
descr -> newSegmentImpl(minOccurs, maxOccurs, typeId, discriminatorAttr, sequence, title, descr));
descr -> whenExpected(reader, qnSegment, () -> {
Discriminator disc = buildDiscriminator(discriminatorAttr, sequence);
SegmentImpl segment = new SegmentImpl(minOccurs,
maxOccurs,
typeId,
code,
disc,
sequence,
title,
descr);
implementedTypes.add(segment);
return segment;
}));
}

void readPositionedSequenceEntry(QName entryName, List<EDITypeImplementation> sequence, boolean composites) {
Expand Down Expand Up @@ -333,22 +346,6 @@ void readPositionedSequenceEntry(QName entryName, List<EDITypeImplementation> se
}
}

SegmentImpl newSegmentImpl(int minOccurs,
int maxOccurs,
String typeId,
BigDecimal discriminatorAttr,
List<EDITypeImplementation> sequence,
String title,
String descr) {

return whenExpected(reader, qnSegment, () -> {
Discriminator disc = buildDiscriminator(discriminatorAttr, sequence);
SegmentImpl segment = new SegmentImpl(minOccurs, maxOccurs, typeId, disc, sequence, title, descr);
implementedTypes.add(segment);
return segment;
});
}

Discriminator buildDiscriminator(BigDecimal discriminatorAttr,
List<EDITypeImplementation> sequence) {
Discriminator disc = null;
Expand Down Expand Up @@ -400,20 +397,15 @@ CompositeImpl readCompositeImplementation(XMLStreamReader reader) {

return readTypeImplementation(reader,
() -> readSequence(reader, e -> readPositionedSequenceEntry(e, sequence, false)),
descr -> newCompositeImpl(reader, minOccurs, maxOccurs, position, sequence, title, descr));
}

CompositeImpl newCompositeImpl(XMLStreamReader reader,
int minOccurs,
int maxOccurs,
int position,
List<EDITypeImplementation> sequence,
String title,
String descr) {

return whenExpected(reader,
qnComposite,
() -> new CompositeImpl(minOccurs, maxOccurs, null, position, sequence, title, descr));
descr -> whenExpected(reader,
qnComposite,
() -> new CompositeImpl(minOccurs,
maxOccurs,
null,
position,
sequence,
title,
descr)));
}

void readSequence(XMLStreamReader reader, Consumer<QName> startHandler) {
Expand Down Expand Up @@ -445,20 +437,15 @@ ElementImpl readElementImplementation(XMLStreamReader reader) {

return readTypeImplementation(reader,
() -> valueSet.set(super.readEnumerationValues(reader)),
descr -> newElementImpl(reader, minOccurs, maxOccurs, position, valueSet.get(), title, descr));
}

ElementImpl newElementImpl(XMLStreamReader reader,
int minOccurs,
int maxOccurs,
int position,
Set<String> valueSet,
String title,
String descr) {

return whenExpected(reader,
qnElement,
() -> new ElementImpl(minOccurs, maxOccurs, (String) null, position, valueSet, title, descr));
descr -> whenExpected(reader,
qnElement,
() -> new ElementImpl(minOccurs,
maxOccurs,
(String) null,
position,
valueSet.get(),
title,
descr)));
}

String readImplDescription(XMLStreamReader reader) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public String getId() {
return typeId;
}

@Override
public String getCode() {
return typeId;
}

@Override
public EDIType getReferencedType() {
return getStandard();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ public Base getBase() {
return standard.getBase();
}

/**
* @see io.xlate.edi.schema.EDISimpleType#getNumber()
* @deprecated
*/
@SuppressWarnings({ "java:S1123", "java:S1133" })
@Override
@Deprecated
public int getNumber() {
return standard.getNumber();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@
public class LoopImpl extends BaseComplexImpl implements LoopImplementation {

private static final String TOSTRING_FORMAT = "id: %s, minOccurs: %d, maxOccurs: %d, discriminator: { %s }, sequence { %s }, standard: { %s }";
private final String id;
private final String code;
private final Discriminator discriminator;

@SuppressWarnings("java:S107")
public LoopImpl(int minOccurs,
int maxOccurs,
String id,
String code,
String typeId,
Discriminator discriminator,
List<EDITypeImplementation> sequence,
Expand All @@ -25,31 +25,36 @@ public LoopImpl(int minOccurs,
super(sequence, title, description);
this.minOccurs = minOccurs;
this.maxOccurs = maxOccurs;
this.id = id;
this.code = code;
this.typeId = typeId;
this.discriminator = discriminator;
}

@Override
public boolean equals(Object o) {
return super.equals(o) &&
Objects.equals(id, ((LoopImpl) o).id) &&
Objects.equals(code, ((LoopImpl) o).code) &&
Objects.equals(discriminator, ((LoopImpl) o).discriminator);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), id, discriminator);
return Objects.hash(super.hashCode(), code, discriminator);
}

@Override
public String toString() {
return String.format(TOSTRING_FORMAT, id, minOccurs, maxOccurs, discriminator, sequence, standard);
return String.format(TOSTRING_FORMAT, code, minOccurs, maxOccurs, discriminator, sequence, standard);
}

@Override
public String getId() {
return id;
return code;
}

@Override
public String getCode() {
return code;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,47 @@

public class SegmentImpl extends BaseComplexImpl implements SegmentImplementation {

private static final String TOSTRING_FORMAT = "typeId: %s, minOccurs: %d, maxOccurs: %d, discriminator: { %s }, sequence { %s }, standard: { %s }";
private static final String TOSTRING_FORMAT = "typeId: %s, code: %s, minOccurs: %d, maxOccurs: %d, discriminator: { %s }, sequence { %s }, standard: { %s }";
private final String code;
private final Discriminator discriminator;

@SuppressWarnings("java:S107")
public SegmentImpl(int minOccurs,
int maxOccurs,
String typeId,
String code,
Discriminator discriminator,
List<EDITypeImplementation> sequence,
String title,
String description) {
super(sequence, title, description);
this.minOccurs = minOccurs;
this.maxOccurs = maxOccurs;
this.typeId = typeId;
super.minOccurs = minOccurs;
super.maxOccurs = maxOccurs;
super.typeId = typeId;
this.code = code;
this.discriminator = discriminator;
}

@Override
public boolean equals(Object o) {
return super.equals(o) && Objects.equals(discriminator, ((SegmentImpl) o).discriminator);
return super.equals(o) &&
Objects.equals(code, ((SegmentImpl) o).code) &&
Objects.equals(discriminator, ((SegmentImpl) o).discriminator);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), discriminator);
return Objects.hash(super.hashCode(), code, discriminator);
}

@Override
public String toString() {
return String.format(TOSTRING_FORMAT, typeId, minOccurs, maxOccurs, discriminator, sequence, standard);
return String.format(TOSTRING_FORMAT, typeId, code, minOccurs, maxOccurs, discriminator, sequence, standard);
}

@Override
public String getCode() {
return code;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,9 +184,11 @@ public boolean segmentBegin(String segmentTag) {
transactionSchemaAllowed = false;
Validator validator = validator();
boolean eventsReady = true;
String code = null;

if (validator != null && !dialect.isServiceAdviceSegment(segmentTag)) {
validator.validateSegment(this, segmentTag);
code = validator.getSegmentReferenceCode();
eventsReady = !validator.isPendingDiscrimination();
}

Expand All @@ -195,7 +197,7 @@ public boolean segmentBegin(String segmentTag) {
validator().validateSegment(this, segmentTag);
}

enqueueEvent(EDIStreamEvent.START_SEGMENT, EDIStreamValidationError.NONE, segmentTag, null, location);
enqueueEvent(EDIStreamEvent.START_SEGMENT, EDIStreamValidationError.NONE, segmentTag, code, location);
return eventsReady;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,16 +126,10 @@ String getId() {

String getCode() {
if (link instanceof EDITypeImplementation) {
return ((EDITypeImplementation) link).getId();
return ((EDITypeImplementation) link).getCode();
}

EDIType referencedNode = link.getReferencedType();

if (referencedNode instanceof EDIComplexType) {
return ((EDIComplexType) referencedNode).getCode();
}

return referencedNode.getId();
return link.getReferencedType().getCode();
}

void validate(Dialect dialect, CharSequence value, boolean validateCodeValues, List<EDIStreamValidationError> errors) {
Expand Down Expand Up @@ -259,16 +253,27 @@ public String getId() {
}

@Override
public Type getType() {
return Type.ELEMENT;
public Base getBase() {
return target.getBase();
}

@Override
public Base getBase() {
return target.getBase();
public String getCode() {
return target.getCode();
}

@Override
public Type getType() {
return Type.ELEMENT;
}

/**
* @see io.xlate.edi.schema.EDISimpleType#getNumber()
* @deprecated
*/
@SuppressWarnings({ "java:S1123", "java:S1133" })
@Override
@Deprecated
public int getNumber() {
return target.getNumber();
}
Expand Down
Loading

0 comments on commit e40a856

Please sign in to comment.