Skip to content

Commit

Permalink
Complete migration of location mutation from Lexer to ProxyEventHandler
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Edgar <[email protected]>
  • Loading branch information
MikeEdgar committed Apr 26, 2023
1 parent 8928b6a commit 84c9c5b
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 78 deletions.
112 changes: 54 additions & 58 deletions src/main/java/io/xlate/edi/internal/stream/StaEDIStreamLocation.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@
******************************************************************************/
package io.xlate.edi.internal.stream;

import io.xlate.edi.internal.stream.tokenization.State;
import io.xlate.edi.stream.Location;

public class StaEDIStreamLocation extends LocationView implements Location {

private boolean repeated = false;
private boolean composite = false;
private boolean repeating = false;
private int repeatCount = -1;

public StaEDIStreamLocation() {
super();
Expand All @@ -33,7 +34,9 @@ public StaEDIStreamLocation(Location source) {
@Override
public StaEDIStreamLocation copy() {
StaEDIStreamLocation copy = new StaEDIStreamLocation(this);
copy.repeated = this.repeated;
copy.composite = this.composite;
copy.repeating = this.repeating;
copy.repeatCount = this.repeatCount;
return copy;
}

Expand Down Expand Up @@ -69,99 +72,92 @@ public void incrementOffset(int value) {
this.columnNumber++;
}

public void incrementSegmentPosition(String segmentTag) {
if (this.segmentPosition < 0) {
this.segmentPosition = 1;
} else {
this.segmentPosition++;
static int initOrIncrement(int position) {
if (position < 0) {
return 1;
}
return position + 1;
}

public void incrementSegmentPosition(String segmentTag) {
this.segmentPosition = initOrIncrement(segmentPosition);
this.segmentTag = segmentTag;

clearSegmentLocations();
}

public void clearSegmentLocations() {
this.elementPosition = -1;
this.elementOccurrence = -1;
this.repeating = false;
this.repeatCount = -1;
clearComponentPosition();
}

public void incrementElementPosition() {
if (this.elementPosition < 0) {
this.elementPosition = 1;
} else {
this.elementPosition++;
}

this.elementPosition = initOrIncrement(elementPosition);
this.elementOccurrence = 1;
clearComponentPosition();
}

public void incrementElementOccurrence() {
this.elementOccurrence++;
this.elementPosition = Math.max(elementPosition, 1);
this.elementOccurrence = initOrIncrement(elementOccurrence);
clearComponentPosition();
}

public void incrementComponentPosition() {
if (this.componentPosition < 0) {
this.componentPosition = 1;
} else {
this.componentPosition++;
}
this.componentPosition = initOrIncrement(componentPosition);
}

public void clearComponentPosition() {
this.composite = false;
this.componentPosition = -1;
}

public void setRepeated(boolean repeated) {
this.repeated = repeated;
public void setComposite(boolean composite) {
this.composite = composite;
}

public boolean isRepeated() {
return repeated;
}

public void updateLocation(State state) {
if (state == State.ELEMENT_REPEAT) {
if (isRepeated()) {
updateElementOccurrence();
public void setRepeating(boolean repeating) {
if (repeating) {
// Encountered a repeat delimiter
if (this.repeating) {
// Previous delimiter was repeat, increment
repeatCount++;
} else {
setElementOccurrence(1);
}
setRepeated(true);
} else if (isRepeated()) {
if (state != State.COMPONENT_END) {
updateElementOccurrence();
setRepeated(false);
// First repeat delimiter for this element
repeatCount = 0;
}
} else if (this.repeating) {
// Previous delimiter was repeat, this one is not. The element just completed is a repeat
repeatCount++;
} else {
setElementOccurrence(1);
// Repeat does not apply
repeatCount = -1;
}

switch (state) {
case COMPONENT_END:
case HEADER_COMPONENT_END:
incrementComponentPosition();
break;
this.repeating = repeating;
}

default:
if (getComponentPosition() > 0) {
incrementComponentPosition();
} else if (getElementOccurrence() == 1) {
public void incrementElement(boolean compositeBegin) {
if (composite) {
incrementComponentPosition();
} else if (elementPosition < 0 || repeatCount == -1) {
// First element of the segment or not a repeating element
incrementElementPosition();
} else if (repeating) {
if (compositeBegin) {
// Previous element delimiter was a repeater and the first component was encountered
incrementElementOccurrence();
} else if (repeatCount == 0) {
// First element of the repeating series is in a new element position
incrementElementPosition();
} else {
incrementElementOccurrence();
}
break;
}
}

void updateElementOccurrence() {
/*
* Only increment the position if we have not yet started
* the composite - i.e, only a single component is present.
*/
if (getComponentPosition() < 1) {
} else if (compositeBegin) {
incrementElementPosition();
} else {
incrementElementOccurrence();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ public EDIStreamWriter writeEndSegment() throws EDIStreamException {
if (level > LEVEL_SEGMENT) {
validateElement(this.elementBuffer::flip, this.elementBuffer);
}
level = LEVEL_SEGMENT;
validate(validator -> validator.validateSyntax(dialect, this, this, location, false));

if (state == State.ELEMENT_DATA_BINARY) {
Expand Down Expand Up @@ -859,6 +860,12 @@ public boolean binaryData(InputStream binary) {

@Override
public boolean elementData(CharSequence text, boolean fromStream) {
if (level > LEVEL_ELEMENT) {
location.incrementComponentPosition();
} else {
location.incrementElementPosition();
}

dialect.elementData(elementHolder, location);

validator().ifPresent(validator -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,13 @@ public Lexer(InputStream stream, Charset charset, EventHandler handler, StaEDISt
sen = (notifyState, start, length) -> handler.segmentEnd();
csn = (notifyState, start, length) -> handler.compositeBegin(false, false);
cen = (notifyState, start, length) -> handler.compositeEnd(false);
bn = (notifyState, start, length) -> handler.binaryData(binaryStream);

en = (notifyState, start, length) -> {
this.location.updateLocation(notifyState);
elementHolder.set(buffer.array(), start, length);
return handler.elementData(elementHolder, true);
};

bn = (notifyState, start, length) -> {
this.location.updateLocation(notifyState);
return handler.binaryData(binaryStream);
};
}

public Dialect getDialect() {
Expand Down Expand Up @@ -168,10 +164,6 @@ public void parse() throws IOException, EDIException {
}
}

public void parse(CharBuffer buffer) throws EDIException {
parse(() -> buffer.hasRemaining() ? buffer.get() : -1);
}

void parse(IntSupplier inputSource) throws EDIException {
if (nextEvent()) {
return;
Expand Down Expand Up @@ -534,6 +526,8 @@ private void emptySegment() throws EDIException {
}

private void handleElement() throws EDIException {
location.setRepeating(State.ELEMENT_REPEAT.equals(state));

if (previous != State.ELEMENT_END_BINARY) {
addElementEvent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,9 @@ public boolean segmentEnd() {
@Override
public boolean compositeBegin(boolean isNil, boolean derived) {
if (!derived) {
if (location.isRepeated()) {
location.incrementElementOccurrence();
} else {
location.incrementElementPosition();
}
location.incrementElement(true);
}
location.setComposite(true);

EDIReference typeReference = null;
boolean eventsReady = true;
Expand Down Expand Up @@ -365,6 +362,8 @@ public boolean compositeEnd(boolean isNil) {

@Override
public boolean elementData(CharSequence text, boolean fromStream) {
location.incrementElement(false);

boolean derivedComposite;
EDIReference typeReference;
final boolean compositeFromStream = location.getComponentPosition() > -1;
Expand Down Expand Up @@ -549,6 +548,7 @@ public boolean isBinaryElementLength() {

@Override
public boolean binaryData(InputStream binaryStream) {
location.incrementElement(false);
Validator validator = validator();
EDIReference typeReference = validator != null ? validator.getElementReference() : null;
enqueueEvent(EDIStreamEvent.ELEMENT_DATA_BINARY, EDIStreamValidationError.NONE, "", typeReference, location);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1285,12 +1285,6 @@ public void validateSyntax(Dialect dialect, ElementDataHandler handler, Validati

// Ensure the start index is at least zero. Index may be -1 for empty segments
for (int i = Math.max(index, 0), max = children.size(); i < max; i++) {
if (isComposite) {
location.incrementComponentPosition();
} else {
location.incrementElementPosition();
}

handler.elementData("", false);
}

Expand Down

0 comments on commit 84c9c5b

Please sign in to comment.